-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
87 lines (73 loc) · 2.6 KB
/
main.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
#!/usr/bin/env python3.8
import argparse
import os
import sys
from tokenize import generate_tokens
from search_parser.tokenizer import Tokenizer
from search_parser.generator3 import check, generate
from search_parser.visualizer import Visualizer
argparser = argparse.ArgumentParser()
argparser.add_argument("grammar", nargs="?", help="Grammar file (search.gram)")
argparser.add_argument("-r", "--regen", action="store_true", help="Regenerate grammar")
argparser.add_argument("-o", "--output", help="Output file (search.py)")
argparser.add_argument("-c", "--classname", help="Output class name (SearchParser)")
argparser.add_argument("-v", "--visualize", action="store_true", help="Use visualizer")
argparser.add_argument("-b", "--backup", action="store_true", help="Use old grammar parser")
def main():
args = argparser.parse_args()
file = args.grammar
if not file:
if args.regen:
file = "search_parser/grammar.gram"
else:
file = "search_parser/search.gram"
outfile = args.output
if not outfile:
head, tail = os.path.split(file)
base, ext = os.path.splitext(tail)
if base == "grammar":
base += "parser"
outfile = os.path.join(head, base + ".py")
classname = args.classname
if args.backup:
from search_parser.grammar import GrammarParser
else:
from search_parser.grammarparser import GrammarParser
print("Reading", file, file=sys.stderr)
with open(file) as f:
tokengen = generate_tokens(f.readline)
vis = None
if args.visualize:
vis = Visualizer()
try:
tok = Tokenizer(tokengen, vis)
p = GrammarParser(tok)
grammar = p.start()
if vis:
vis.done()
finally:
if vis:
vis.close()
if not grammar:
if tok.tokens:
last = tok.tokens[-1]
print(f"Line {last.start[0]}:")
print(last.line)
print(" "*last.start[1] + "^")
sys.exit("SyntaxError")
print(repr(grammar))
print(str(grammar))
if not classname:
classname = grammar.metas_dict.get("class")
if not classname:
tail = os.path.basename(file)
base, ext = os.path.splitext(tail)
classname = base.title() + "Parser"
errors = check(grammar)
if errors:
sys.exit(f"Detected {errors} errors")
print("Writing class", classname, "to", outfile, file=sys.stderr)
with open(outfile, "w") as stream:
generate(grammar, classname, stream)
if __name__ == '__main__':
main()