diff options
author | Paul McGuire <ptmcg@austin.rr.com> | 2019-04-05 01:06:53 -0500 |
---|---|---|
committer | Paul McGuire <ptmcg@austin.rr.com> | 2019-04-05 01:08:05 -0500 |
commit | 37e25526351930f8360fb7ec3393195086d8491c (patch) | |
tree | d9694f37a8a8c871f360de49cdf8a902acbb2e54 /examples/include_preprocessor.py | |
parent | 0d88a303a7f7e574bfc0c06ad6f84ca8c9d4d248 (diff) | |
download | pyparsing-git-37e25526351930f8360fb7ec3393195086d8491c.tar.gz |
Add example include_preprocessor.py
Diffstat (limited to 'examples/include_preprocessor.py')
-rw-r--r-- | examples/include_preprocessor.py | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/examples/include_preprocessor.py b/examples/include_preprocessor.py new file mode 100644 index 0000000..dbb333b --- /dev/null +++ b/examples/include_preprocessor.py @@ -0,0 +1,77 @@ +# +# include_preprocessor.py +# +# Short pyparsing script to perform #include inclusions similar to the C preprocessor +# +# Copyright 2019, Paul McGuire +# +import pyparsing as pp +from pathlib import Path + +SEMI = pp.Suppress(';') +INCLUDE = pp.Keyword("#include") +quoted_string = pp.quotedString.addParseAction(pp.removeQuotes) + +include_directive = (INCLUDE + + (quoted_string + | pp.Word(pp.printables, excludeChars=';'))("include_file") + + SEMI) + +# add parse action that will recursively pull in included files +seen = set() +def read_include_contents(s, l, t): + include_file_ref = t.include_file + include_echo = "/* {} */".format(pp.line(l, s).strip()) + + # guard against recursive includes + if include_file_ref not in seen: + seen.add(include_file_ref) + return (include_echo + '\n' + + include_directive.transformString(Path(include_file_ref).read_text())) + else: + lead = ' '*(pp.col(l, s) - 1) + return "/* recursive include! */\n{}{}".format(lead, include_echo) + +# attach include processing method as parse action to include_directive expression +include_directive.addParseAction(read_include_contents) + + +if __name__ == '__main__': + + # demo + + # create test files + Path('a.txt').write_text("""\ + /* a.txt */ + int i; + + #include b.txt; + """) + + Path('b.txt').write_text("""\ + i = 100; + #include 'c.txt'; + """) + + Path('c.txt').write_text("""\ + i += 1; + + #include b.txt; + """) + + + # use include_directive.transformString to perform includes + + # read contents of original file + initial_file = Path('a.txt').read_text() + + # print original file + print(initial_file) + print('-----------------') + + # print expanded file + print(include_directive.transformString(initial_file)) + + # clean up + for fname in "a.txt b.txt c.txt".split(): + Path(fname).unlink() |