diff options
Diffstat (limited to 'examples/include_preprocessor.py')
-rw-r--r-- | examples/include_preprocessor.py | 55 |
1 files changed, 33 insertions, 22 deletions
diff --git a/examples/include_preprocessor.py b/examples/include_preprocessor.py index dbb333b..0b0d742 100644 --- a/examples/include_preprocessor.py +++ b/examples/include_preprocessor.py @@ -8,31 +8,36 @@ import pyparsing as pp from pathlib import Path +# parser elements to be used to assemble into #include parser SEMI = pp.Suppress(';') INCLUDE = pp.Keyword("#include") quoted_string = pp.quotedString.addParseAction(pp.removeQuotes) +file_ref = (quoted_string + | pp.Word(pp.printables, excludeChars=';')) -include_directive = (INCLUDE - + (quoted_string - | pp.Word(pp.printables, excludeChars=';'))("include_file") - + SEMI) +# parser for parsing "#include xyz.dat;" directives +include_directive = (INCLUDE + file_ref("include_file_name") + SEMI) -# add parse action that will recursively pull in included files +# add parse action that will recursively pull in included files - when +# using transformString, the value returned from the parse action will replace +# the text matched by the attached expression seen = set() def read_include_contents(s, l, t): - include_file_ref = t.include_file + include_file_ref = t.include_file_name include_echo = "/* {} */".format(pp.line(l, s).strip()) # guard against recursive includes if include_file_ref not in seen: seen.add(include_file_ref) + included_file_contents = Path(include_file_ref).read_text() return (include_echo + '\n' - + include_directive.transformString(Path(include_file_ref).read_text())) + + include_directive.transformString(included_file_contents)) 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 +# attach include processing method as parse action (parse-time callback) +# to include_directive expression include_directive.addParseAction(read_include_contents) @@ -40,24 +45,29 @@ if __name__ == '__main__': # demo - # create test files + # create test files: + # - a.txt includes b.txt + # - b.txt includes c.txt + # - c.txt includes b.txt (must catch infinite recursion) Path('a.txt').write_text("""\ - /* a.txt */ - int i; + /* a.txt */ + int i; - #include b.txt; - """) + /* sometimes included files aren't in quotes */ + #include b.txt; + """) Path('b.txt').write_text("""\ - i = 100; - #include 'c.txt'; - """) + i = 100; + #include 'c.txt'; + """) Path('c.txt').write_text("""\ - i += 1; - - #include b.txt; - """) + i += 1; + + /* watch out! this might be recursive if this file included by b.txt */ + #include b.txt; + """) # use include_directive.transformString to perform includes @@ -69,8 +79,9 @@ if __name__ == '__main__': print(initial_file) print('-----------------') - # print expanded file - print(include_directive.transformString(initial_file)) + # expand includes in source file (and any included files) and print the result + expanded_source = include_directive.transformString(initial_file) + print(expanded_source) # clean up for fname in "a.txt b.txt c.txt".split(): |