|
1 | 1 | #------------------------------------------------------------------------------- |
2 | 2 | # lexer.py |
3 | 3 | # |
4 | | -# Lexical analyzer |
| 4 | +# Verilog Lexical Analyzer |
5 | 5 | # |
6 | 6 | # Copyright (C) 2013, Shinya Takamaeda-Yamazaki |
7 | | -# |
8 | | -# edited by ryosuke fukatani |
9 | | -# |
| 7 | +# Edited by ryosuke fukatani |
10 | 8 | # License: Apache 2.0 |
11 | 9 | #------------------------------------------------------------------------------- |
12 | 10 |
|
@@ -265,20 +263,66 @@ def _find_tok_column(self, token): |
265 | 263 | def _make_tok_location(self, token): |
266 | 264 | return (token.lineno, self._find_tok_column(token)) |
267 | 265 |
|
268 | | -if __name__ == '__main__': |
| 266 | +#------------------------------------------------------------------------------- |
| 267 | +def dump_tokens(text): |
269 | 268 | def my_error_func(msg, a, b): |
270 | 269 | sys.write(msg + "\n") |
271 | 270 | sys.exit() |
272 | | - |
273 | | - filename = '../testcode/test.v' |
274 | | - text = open(filename, 'r').read() |
275 | | - |
276 | | - lexer = VerilogLexer(error_func = my_error_func) |
| 271 | + |
| 272 | + lexer = VerilogLexer(error_func=my_error_func) |
277 | 273 | lexer.build() |
278 | 274 | lexer.input(text) |
279 | 275 |
|
| 276 | + ret = [] |
| 277 | + |
280 | 278 | # Tokenize |
281 | | - while 1: |
| 279 | + while True: |
282 | 280 | tok = lexer.token() |
283 | | - if not tok: break # No more input |
284 | | - print (tok.value, tok.type, tok.lineno, lexer.filename, tok.lexpos) |
| 281 | + if not tok: break # No more input |
| 282 | + ret.append("%s %s %d %s %d\n" % |
| 283 | + (tok.value, tok.type, tok.lineno, lexer.filename, tok.lexpos)) |
| 284 | + |
| 285 | + return ''.join(ret) |
| 286 | + |
| 287 | +#------------------------------------------------------------------------------- |
| 288 | +if __name__ == '__main__': |
| 289 | + import pyverilog.utils.version |
| 290 | + from pyverilog.vparser.preprocessor import preprocess |
| 291 | + from optparse import OptionParser |
| 292 | + |
| 293 | + INFO = "Verilog Preprocessor" |
| 294 | + VERSION = pyverilog.utils.version.VERSION |
| 295 | + USAGE = "Usage: python preprocessor.py file ..." |
| 296 | + |
| 297 | + def showVersion(): |
| 298 | + print(INFO) |
| 299 | + print(VERSION) |
| 300 | + print(USAGE) |
| 301 | + sys.exit() |
| 302 | + |
| 303 | + optparser = OptionParser() |
| 304 | + optparser.add_option("-v","--version",action="store_true",dest="showversion", |
| 305 | + default=False,help="Show the version") |
| 306 | + optparser.add_option("-I","--include",dest="include",action="append", |
| 307 | + default=[],help="Include path") |
| 308 | + optparser.add_option("-D",dest="define",action="append", |
| 309 | + default=[],help="Macro Definition") |
| 310 | + (options, args) = optparser.parse_args() |
| 311 | + |
| 312 | + filelist = args |
| 313 | + if options.showversion: |
| 314 | + showVersion() |
| 315 | + |
| 316 | + for f in filelist: |
| 317 | + if not os.path.exists(f): raise IOError("file not found: " + f) |
| 318 | + |
| 319 | + if len(filelist) == 0: |
| 320 | + showVersion() |
| 321 | + |
| 322 | + text = preprocess(filelist, |
| 323 | + preprocess_include=options.include, |
| 324 | + preprocess_define=options.define) |
| 325 | + |
| 326 | + dump = dump_tokens(text) |
| 327 | + |
| 328 | + print(dump) |
0 commit comments