summaryrefslogtreecommitdiff
path: root/examples/include_preprocessor.py
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2019-10-31 21:10:28 -0700
committerPaul McGuire <ptmcg@users.noreply.github.com>2019-10-31 23:10:28 -0500
commit53d1b4a6f48a53c4c4ec4ac7031362b691c0366d (patch)
tree088ad3cf3561b78a00af4fb2fd474f4a2b8ca70c /examples/include_preprocessor.py
parent41752aa52cc97c710474bb2972cceab057b52ad4 (diff)
downloadpyparsing-git-53d1b4a6f48a53c4c4ec4ac7031362b691c0366d.tar.gz
Blacken the project (#141)
Diffstat (limited to 'examples/include_preprocessor.py')
-rw-r--r--examples/include_preprocessor.py50
1 files changed, 30 insertions, 20 deletions
diff --git a/examples/include_preprocessor.py b/examples/include_preprocessor.py
index 0b0d742..294d658 100644
--- a/examples/include_preprocessor.py
+++ b/examples/include_preprocessor.py
@@ -9,19 +9,20 @@ import pyparsing as pp
from pathlib import Path
# parser elements to be used to assemble into #include parser
-SEMI = pp.Suppress(';')
+SEMI = pp.Suppress(";")
INCLUDE = pp.Keyword("#include")
quoted_string = pp.quotedString.addParseAction(pp.removeQuotes)
-file_ref = (quoted_string
- | pp.Word(pp.printables, excludeChars=';'))
+file_ref = quoted_string | pp.Word(pp.printables, excludeChars=";")
# parser for parsing "#include xyz.dat;" directives
-include_directive = (INCLUDE + file_ref("include_file_name") + SEMI)
+include_directive = INCLUDE + file_ref("include_file_name") + SEMI
# 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_name
include_echo = "/* {} */".format(pp.line(l, s).strip())
@@ -30,18 +31,22 @@ def read_include_contents(s, l, t):
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(included_file_contents))
+ return (
+ include_echo
+ + "\n"
+ + include_directive.transformString(included_file_contents)
+ )
else:
- lead = ' '*(pp.col(l, s) - 1)
+ lead = " " * (pp.col(l, s) - 1)
return "/* recursive include! */\n{}{}".format(lead, include_echo)
+
# attach include processing method as parse action (parse-time callback)
# to include_directive expression
include_directive.addParseAction(read_include_contents)
-if __name__ == '__main__':
+if __name__ == "__main__":
# demo
@@ -49,35 +54,40 @@ if __name__ == '__main__':
# - a.txt includes b.txt
# - b.txt includes c.txt
# - c.txt includes b.txt (must catch infinite recursion)
- Path('a.txt').write_text("""\
+ Path("a.txt").write_text(
+ """\
/* a.txt */
int i;
-
- /* sometimes included files aren't in quotes */
+
+ /* sometimes included files aren't in quotes */
#include b.txt;
- """)
+ """
+ )
- Path('b.txt').write_text("""\
+ Path("b.txt").write_text(
+ """\
i = 100;
#include 'c.txt';
- """)
+ """
+ )
- Path('c.txt').write_text("""\
+ Path("c.txt").write_text(
+ """\
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
# read contents of original file
- initial_file = Path('a.txt').read_text()
+ initial_file = Path("a.txt").read_text()
# print original file
print(initial_file)
- print('-----------------')
+ print("-----------------")
# expand includes in source file (and any included files) and print the result
expanded_source = include_directive.transformString(initial_file)