Skip to content

Commit 8840422

Browse files
author
Mohamed Gaber
committed
Added a number of configuration options
+ iverilog executable now configurable via PYVERILOG_IVERILOG configuration option + filenames with spaces now supported + parse() command now has an option for "outputdir" and "debug" that are passed to PLY
1 parent 3fbde9b commit 8840422

File tree

2 files changed

+47
-30
lines changed

2 files changed

+47
-30
lines changed

pyverilog/vparser/parser.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from __future__ import print_function
1919
import sys
2020
import os
21+
import pathlib
2122

2223
from pyverilog.vparser.ply.yacc import yacc
2324
from pyverilog.vparser.plyparser import PLYParser, Coord, ParseError
@@ -48,14 +49,18 @@ class VerilogParser(PLYParser):
4849
# -> Strong
4950
)
5051

51-
def __init__(self):
52+
def __init__(self, outputdir=".", debug=True):
5253
self.lexer = VerilogLexer(error_func=self._lexer_error_func)
5354
self.lexer.build()
5455

5556
self.tokens = self.lexer.tokens
56-
#self.parser = yacc(module=self)
57-
# Use this if you want to build the parser using LALR(1) instead of SLR
58-
self.parser = yacc(module=self, method="LALR")
57+
pathlib.Path(outputdir).mkdir(parents=True, exist_ok=True)
58+
self.parser = yacc(
59+
module=self,
60+
method="LALR",
61+
outputdir=outputdir,
62+
debug=debug
63+
)
5964

6065
def _lexer_error_func(self, msg, line, column):
6166
self._parse_error(msg, self._coord(line, column))
@@ -2233,13 +2238,16 @@ class VerilogCodeParser(object):
22332238

22342239
def __init__(self, filelist, preprocess_output='preprocess.output',
22352240
preprocess_include=None,
2236-
preprocess_define=None):
2241+
preprocess_define=None,
2242+
outputdir=".",
2243+
debug=True
2244+
):
22372245
self.preprocess_output = preprocess_output
22382246
self.directives = ()
22392247
self.preprocessor = VerilogPreprocessor(filelist, preprocess_output,
22402248
preprocess_include,
22412249
preprocess_define)
2242-
self.parser = VerilogParser()
2250+
self.parser = VerilogParser(outputdir=outputdir, debug=debug)
22432251

22442252
def preprocess(self):
22452253
self.preprocessor.preprocess()
@@ -2257,10 +2265,20 @@ def get_directives(self):
22572265
return self.directives
22582266

22592267

2260-
def parse(filelist, preprocess_include=None, preprocess_define=None):
2261-
codeparser = VerilogCodeParser(filelist,
2262-
preprocess_include=preprocess_include,
2263-
preprocess_define=preprocess_define)
2268+
def parse(
2269+
filelist,
2270+
preprocess_include=None,
2271+
preprocess_define=None,
2272+
outputdir=".",
2273+
debug=True
2274+
):
2275+
codeparser = VerilogCodeParser(
2276+
filelist,
2277+
preprocess_include=preprocess_include,
2278+
preprocess_define=preprocess_define,
2279+
outputdir=outputdir,
2280+
debug=debug
2281+
)
22642282
ast = codeparser.parse()
22652283
directives = codeparser.get_directives()
22662284
return ast, directives

pyverilog/vparser/preprocessor.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,30 @@
3030
class VerilogPreprocessor(object):
3131
def __init__(self, filelist, outputfile='pp.out', include=None, define=None):
3232
self.filelist = filelist
33-
cmd = []
34-
cmd.append('iverilog ')
35-
if include:
36-
for inc in include:
37-
cmd.append('-I ')
38-
cmd.append(inc)
39-
cmd.append(' ')
40-
if define:
41-
for d in define:
42-
cmd.append('-D')
43-
cmd.append(d)
44-
cmd.append(' ')
45-
cmd.append('-E -o ')
46-
cmd.append(outputfile)
47-
self.iv = ''.join(cmd)
33+
iverilog_invocable = os.environ.get("PYVERILOG_IVERILOG") or "iverilog"
34+
include = include or []
35+
define = define or []
36+
includes = map(
37+
lambda includable: "-I '{0}'".format(includable), include
38+
)
39+
defines = map(lambda definable: "-D {0}".format(definable), define)
40+
self.iv = "'{0}' {1} {2} -E -o '{3}'".format(
41+
iverilog_invocable, ' '.join(includes), ' '.join(defines),
42+
outputfile
43+
)
4844

4945
def preprocess(self):
50-
cmd = self.iv + ' '
51-
for f in self.filelist:
52-
cmd += ' ' + f
46+
files = map(lambda file: "'{0}'".format(file), self.filelist)
47+
cmd = "{0} {1}".format(self.iv, ' '.join(files))
5348
subprocess.call(cmd, shell=True)
5449

5550

56-
def preprocess(filelist,
57-
output='preprocess.output', include=None, define=None):
51+
def preprocess(
52+
filelist,
53+
output='preprocess.output',
54+
include=None,
55+
define=None
56+
):
5857
pre = VerilogPreprocessor(filelist, output, include, define)
5958
pre.preprocess()
6059
text = open(output).read()

0 commit comments

Comments
 (0)