-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from parser_base import ParserBase | ||
from sexpressions import parse_sexpression | ||
|
||
|
||
class NetlistParser(ParserBase): | ||
def get_extra_field_data(self): | ||
with open(self.file_name, 'r') as f: | ||
sexpression = parse_sexpression(f.read()) | ||
components = None | ||
for s in sexpression: | ||
if s[0] == 'components': | ||
components = s[1:] | ||
if components is None: | ||
return None | ||
field_set = set() | ||
comp_dict = {} | ||
for c in components: | ||
ref = None | ||
fields = None | ||
for f in c[1:]: | ||
if f[0] == 'ref': | ||
ref = f[1] | ||
if f[0] == 'fields': | ||
fields = f[1:] | ||
if ref is None: | ||
return None | ||
if fields is None: | ||
continue | ||
for f in fields: | ||
if len(f) > 1: | ||
field_set.add(f[1][1]) | ||
if len(f) > 2: | ||
ref_fields = comp_dict.setdefault(ref, {}) | ||
ref_fields[f[1][1]] = f[2] | ||
|
||
return list(field_set), comp_dict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import re | ||
|
||
term_regex = r'''(?mx) | ||
\s*(?: | ||
(?P<open>\()| | ||
(?P<close>\))| | ||
(?P<sq>"[^"]*")| | ||
(?P<s>[^(^)\s]+) | ||
)''' | ||
pattern = re.compile(term_regex) | ||
|
||
|
||
def parse_sexpression(sexpression): | ||
stack = [] | ||
out = [] | ||
for terms in pattern.finditer(sexpression): | ||
term, value = [(t, v) for t, v in terms.groupdict().items() if v][0] | ||
if term == 'open': | ||
stack.append(out) | ||
out = [] | ||
elif term == 'close': | ||
assert stack, "Trouble with nesting of brackets" | ||
tmp, out = out, stack.pop(-1) | ||
out.append(tmp) | ||
elif term == 'sq': | ||
out.append(value[1:-1]) | ||
elif term == 's': | ||
out.append(value) | ||
else: | ||
raise NotImplementedError("Error: %s, %s" % (term, value)) | ||
assert not stack, "Trouble with nesting of brackets" | ||
return out[0] |