-
Notifications
You must be signed in to change notification settings - Fork 4
/
regen.py
executable file
·257 lines (239 loc) · 8.84 KB
/
regen.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python3
# =================================================================================================
#
# Regenerate ANTLR parser from grammar.
# Also generates the lexical token constants (required by both ANTLR and the scala lexer).
#
# For the purpose of running this script from a Gradle task the following can be assumed:
#
# Input dependencies:
# - regen.py (this file, obviously)
# - SVParser.g4
#
# Outputs (see below for paths):
# - javaDir/*
# - scalaDir/*
#
# Iow the only input is the grammar and the outputs are all written to java/scala source
# directories that only contains code generated by this script.
#
# TODO: do this from build.gradle instead.
#
# =================================================================================================
import os
import subprocess
# Package and dir for generated java/scala code.
package = "com.github.svstuff.systemverilog.generated"
javaDir = os.path.join("src/main/java", package.replace('.', '/'))
scalaDir = os.path.join("src/main/scala", package.replace('.', '/'))
if not os.path.isdir(javaDir):
raise Exception("Directory for generated java sources does not exist")
if not os.path.isdir(scalaDir):
raise Exception("Directory for generated scala sources does not exist")
# NOTE: running this script will overwrite the following files:
antlrLexerTokens = os.path.join(javaDir, "SVLexer.tokens")
scalaLexerTokens = os.path.join(scalaDir, "LexerTokens.scala")
# List of lexical token types. ANTLR needs a tokens file with such
# a list (including numberical value), and this must correspond to what
# the actual lexer produces as well.
# Special-cased tokens
tokens_special = [
# error token, produced when the lexer finds an invalid token
("ERROR", ""),
("LIT_STRING", "string-literal"),
("LIT_STRING_DPI", "DPI"),
("LIT_STRING_DPI_C", "DPI-C"),
("LIT_NUM", "number-literal"),
("LIT_UNBASED_UNSIZED", "unbased-unsized-literal"),
("LIT_TIME", "time-literal"),
("ID", "identifier"),
("SYSTEM_ID", "system-identifier"),
("TIMESCALE", "`timescale"),
("DOLLAR_UNIT", "$unit"),
("DOLLAR_ROOT", "$root"),
("DOLLAR_FATAL", "$fatal"),
("DOLLAR_ERROR", "$error"),
("DOLLAR_WARNING", "$warning"),
("DOLLAR_INFO", "$info"),
("DOLLAR_SETUP", "$setup"),
("DOLLAR_HOLD", "$hold"),
("DOLLAR_SETUPHOLD", "$setuphold"),
("DOLLAR_RECOVERY", "$recovery"),
("DOLLAR_REMOVAL", "$removal"),
("DOLLAR_RECREM", "$recrem"),
("DOLLAR_SKEW", "$skew"),
("DOLLAR_TIMESKEW", "$timeskew"),
("DOLLAR_FULLSKEW", "$fullskew"),
("DOLLAR_PERIOD", "$period"),
("DOLLAR_WIDTH", "$width"),
("DOLLAR_NOCHANGE", "$nochange"),
("KW_1STEP", "1step"),
]
# Operators and other similar terminals, which are lexed greedily.
# Haven't spent much energy in naming these.
tokens_operators = [
("DOLLAR", "$"),
("HASH", "#"),
("HASH2", "##"),
("HASH_SUB_HASH", "#-#"),
("HASH_EQ_HASH", "#=#"),
("AT_SIGN", "@"),
("APOSTROPHE", "'"),
("DOT", "."),
("COLON", ":"),
("COLON_EQ", ":="),
("COLON_DIV", ":/"),
("COLON2", "::"),
("SEMI", ";"),
("COMMA", ","),
("LPAREN", "("),
("RPAREN", ")"),
("LSQUARE", "["),
("RSQUARE", "]"),
("LCURLY", "{"),
("RCURLY", "}"),
("QUE", "?"),
("NOT", "!"),
("NOT_EQ", "!="),
("NOT_EQ2", "!=="),
("NOT_EQ_Q", "!=?"),
("MOD", "%"),
("MOD_EQ", "%="),
("AND", "&"),
("AND2", "&&"),
("AND3", "&&&"),
("AND_EQ", "&="),
("MUL", "*"),
("MUL2", "**"),
("MUL_EQ", "*="),
("MUL_GT", "*>"),
("ADD", "+"),
("ADD2", "++"),
("ADD_EQ", "+="),
("ADD_COLON", "+:"),
("SUB", "-"),
("SUB2", "--"),
("SUB_EQ", "-="),
("SUB_GT", "->"),
("SUB_GT2", "->>"),
("SUB_COLON", "-:"),
("DIV", "/"),
("DIV_EQ", "/="),
("LT", "<"),
("LT_SUB_GT", "<->"),
("LT2", "<<"),
("LT3", "<<<"),
("LT3_EQ", "<<<="),
("LT2_EQ", "<<="),
("LT_EQ", "<="),
("EQ", "="),
("EQ2", "=="),
("EQ_GT", "=>"),
("EQ3", "==="),
("EQ2_Q", "==?"),
("GT", ">"),
("GT_EQ", ">="),
("GT2", ">>"),
("GT2_EQ", ">>="),
("GT3", ">>>"),
("GT3_EQ", ">>>="),
("XOR", "^"),
("XOR_EQ", "^="),
("XOR_INV", "^~"),
("OR", "|"),
("OR_EQ", "|="),
("OR2", "||"),
("OR_SUB_GT", "|->"),
("OR_EQ_GT", "|=>"),
("INV", "~"),
("INV_AND", "~&"),
("INV_XOR", "~^"),
("INV_OR", "~|"),
]
def get_keyword_tokens():
import sv_keywords
tokens = []
for kw in sv_keywords.keywords:
tokname = "KW_{}".format(kw.upper())
tokens.append((tokname, kw))
return tokens
def regen():
print("Running ANTLR and generating parser java source...")
tokens_keywords = get_keyword_tokens()
tokens = tokens_special + tokens_operators + tokens_keywords
# start from 1, since this appears to be what ANTLR generates from its own
# lexers.
startIndex = 1
# generate ANTLR tokens file
try:
os.makedirs(os.path.dirname(antlrLexerTokens))
except:
pass
with open(antlrLexerTokens, "w") as f:
i = startIndex
for ttype, _ in tokens:
f.write("{} = {}\n".format(ttype, i))
i += 1
with open(scalaLexerTokens, "w") as f:
# generate scala lexer constants
f.write("// NOTE: this file is automatically generated.\n")
f.write("package {}\n".format(package))
f.write("\n")
# generate scala singleton object with mapping from token type to
# string
f.write("object LexerTokens {\n")
f.write("\n")
i = startIndex
for ttype, _ in tokens:
f.write(" final val {} = {}\n".format(ttype, i))
i += 1
f.write("\n")
f.write(" val tokenNames = Array(\n")
for i in range(startIndex):
f.write(' "",\n')
for toktype, _ in tokens:
f.write(' "{}",\n'.format(toktype))
f.write(' "DUMMY")\n')
f.write("\n")
f.write(" val tokenConstText = Array(\n")
for i in range(startIndex):
f.write(' "",\n')
for _, text in tokens:
f.write(' "{}",\n'.format(text))
f.write(' "DUMMY")\n')
f.write("\n")
f.write(" val keywords = Map(\n")
for kw_index, (toktype, kw) in enumerate(tokens_keywords):
f.write(' "{}" -> {}'.format(kw, toktype))
if kw_index == len(tokens_keywords) - 1:
f.write(')\n')
else:
f.write(',\n')
f.write("\n")
f.write(" val operators = Map(\n")
for index, (toktype, op) in enumerate(tokens_operators):
f.write(' "{}" -> {}'.format(op, toktype))
if index == len(tokens_operators) - 1:
f.write(')\n')
else:
f.write(',\n')
f.write("\n")
f.write(' val operatorPattern = """(?s)(')
ops_by_length = sorted(
[op for (_, op) in tokens_operators], key=lambda x: len(x), reverse=True)
max_length = 0
for index, op in enumerate(ops_by_length):
max_length = max(max_length, len(op))
f.write('\Q{}\E'.format(op))
if index == len(tokens_operators) - 1:
f.write(').*""".r\n')
else:
f.write('|')
f.write(' val operatorMaxLength = {}\n'.format(max_length))
f.write("\n")
f.write("}\n")
# run ANTLR on the grammar
subprocess.check_call(
"java -Xmx2g -jar lib/antlr-4.4-complete.jar -atn -visitor -o {} SVParser.g4".format(javaDir), shell=True)
if __name__ == "__main__":
regen()