forked from nesaro/pydsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.py
executable file
·103 lines (92 loc) · 3.9 KB
/
validate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#This file is part of pydsl.
#
#pydsl is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#pydsl is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with pydsl. If not, see <http://www.gnu.org/licenses/>.
"""
validates input against the Grammar
"""
__author__ = "Néstor Arocha Rodríguez"
__copyright__ = "Copyright 2008-2012, Néstor Arocha Rodríguez"
__email__ = "[email protected]"
import logging
from pydsl.Interaction.Program import UnixProgram
def print_errors(postnode):
result = ""
if not postnode.valid:
result += "Error at production: " + str(postnode.production) + "(" + str(postnode.leftpos) + "," + str(postnode.rightpos) + ")"
for child in postnode.childlist:
result += print_errors(child) + "\n"
return result
def errors_to_list(postnode):
result = []
if not postnode.valid:
result.append((postnode.leftpos, postnode.rightpos, str(postnode.production)))
for child in postnode.childlist:
result += errors_to_list(child)
return result
class Validate(UnixProgram):
"""Read input file contents, creates grammar and transform objects, create connections,
and afterwards reads required input/launch main loop"""
def __init__(self, optionsdict):
from pydsl.Memory.External.Loader import load_grammar
UnixProgram.__init__(self, optionsdict)
self.__sgrammar = load_grammar(optionsdict.sgrammar)
def execute(self):
resulttrees = None
from pydsl.Interaction.Validate import validate
if self._opt["expression"]:
resulttrees = validate(self.__sgrammar, self._opt["expression"])
elif self._opt["inputfile"]:
with open(self._opt["inputfile"], "rb") as f:
resulttrees = validate(self.__sgrammar, f.read())
else:
raise Exception #No input method
jsonlist = []
for index, posttree in enumerate(resulttrees):
if self._opt["outputformat"] == "str":
print("Tree: " + str(index) + "\n")
if self._opt["outputformat"] == "str":
if posttree.valid:
print("Result OK")
else:
print("Errors:")
print(print_errors(posttree))
elif self._opt["outputformat"] == "json":
jsonlist.append(errors_to_list(posttree))
if self._opt["outputformat"] == "json":
import json
print(json.dumps(jsonlist))
return True
if __name__ == "__main__":
import argparse
TUSAGE = "usage: %(prog)s [options] [filename]"
PARSER = argparse.ArgumentParser(usage = TUSAGE)
PARSER.add_argument("-d", "--debuglevel", action="store", type=int, dest="debuglevel", help="Sets debug level")
PARSER.add_argument("-i", "--inputfile", action="store", dest="inputfile", help="input filename ")
PARSER.add_argument("-e", "--expression", action="store", dest="expression", help="input expression")
PARSER.add_argument('-o', dest='outputformat',nargs='?', choices=["str","json"], default="str", help="output formats")
PARSER.add_argument("sgrammar", metavar="sgrammar" , help="Grammar")
ARGS = PARSER.parse_args()
import sys
DEBUGLEVEL = ARGS.debuglevel or logging.WARNING
logging.basicConfig(level = DEBUGLEVEL)
manager = Validate(ARGS)
try:
result = manager.execute()
except EOFError:
sys.exit(0)
if not result:
sys.exit(-1)
sys.exit(0)