-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
106 lines (89 loc) · 3.28 KB
/
example.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
104
105
106
from sys import argv
from os import getenv
from dataclasses import asdict
from yaml import dump
from json import dumps, loads
from rich import print as richprint
from re import sub
from pathlib import Path
from argparse import ArgumentParser
from colorama import Fore
from lark.exceptions import UnexpectedCharacters
from hdltree.hdltree import HdlParser, is_excluded, vhdl_fileext
from hdltree.symbol import to_symbol
if __name__ == "__main__":
parser = ArgumentParser(description="Pure Python HDL parser")
parser.add_argument("-i", "--input", action="append", help="HDL source file or directory")
parser.add_argument(
"-p", "--print-tree", action="store_true", help="Print the parsed tree to stdout"
)
parser.add_argument(
"-a",
"--ambig",
action="store_true",
help="Instruct the parser to return an ambiguous tree and resolve them according to semantic rules",
)
parser.add_argument(
"-e",
"--exclude",
action="append",
default=[],
help="Files and directories to ignore",
)
args, unparsed = parser.parse_known_args()
# Allow file to be passed in without -i
if not args.input:
if len(unparsed) > 0:
args.input = unparsed
else:
args.input = ["."]
ve = HdlParser(ambig=args.ambig)
args.input = [Path(f) for f in args.input]
args.exclude = [Path(f) for f in args.exclude]
files = []
for inpath in args.input:
if inpath.is_file() and not is_excluded(inpath, args.exclude):
files.append(inpath)
elif inpath.is_dir() and not is_excluded(inpath, args.exclude):
for ext in vhdl_fileext:
for infile in inpath.rglob("**/*." + ext):
if infile.is_file() and not is_excluded(infile, args.exclude):
files.append(infile)
for f in files:
print(f"analyzing {f}")
try:
cst = ve.parseFile(f)
except UnexpectedCharacters as e:
print()
print(f"error at line {e.line}, column {e.column}")
print("expected:")
print(e.allowed)
print("from rules:")
print(e.considered_rules)
print()
except Exception as e:
print(e)
##richprint(cst.rich_tree())
##print(cst)
# cst.print()
# richprint(cst)
# print(parse_tree.pretty())
# print(cst.pretty())
# print(dumps(asdict(cst), indent=2))
# print(dump(asdict(cst), default_flow_style=False))
if getenv("RICHPRINT_CST"):
richprint(cst.rich_tree())
if getenv("PRINT_CST"):
print(cst)
txt = f.read_text("latin-1").lower()
csttxt = str(cst).lower()
txt = sub(r"--.*", r"", txt)
csttxt = sub(r"--.*", r"", csttxt)
txt = sub(r"\s+", "", txt).strip()
csttxt = sub(r"\s+", "", csttxt).strip()
if txt != csttxt:
print(f"{Fore.RED}inexact recreation: {f.name}{Fore.RESET}")
else:
print(f"{Fore.GREEN}more or less exact recreation: {f.name}{Fore.RESET}")
for ent in cst.entities:
to_symbol(ent)