summaryrefslogtreecommitdiff
path: root/examples/lua_parser.py
diff options
context:
space:
mode:
authorptmcg <ptmcg@austin.rr.com>2020-08-17 17:42:14 -0500
committerptmcg <ptmcg@austin.rr.com>2020-08-17 17:42:14 -0500
commitc1c9c8dcf5bee8bdf885767751eebfff2ed49f7c (patch)
treeb0ce71a4e68fe144fdd96a75969a7aaf6af51b89 /examples/lua_parser.py
parent1a2920dc1ac5b9d90401e53471838d6892c27c59 (diff)
downloadpyparsing-git-c1c9c8dcf5bee8bdf885767751eebfff2ed49f7c.tar.gz
Add lookahead on matching identifiers to ensure we aren't matching a keyword
Diffstat (limited to 'examples/lua_parser.py')
-rw-r--r--examples/lua_parser.py11
1 files changed, 10 insertions, 1 deletions
diff --git a/examples/lua_parser.py b/examples/lua_parser.py
index 34651dd..9ee730a 100644
--- a/examples/lua_parser.py
+++ b/examples/lua_parser.py
@@ -99,13 +99,16 @@ keywords = {
""".split()
}
vars().update(keywords)
+any_keyword = pp.MatchFirst(keywords.values()).setName("<keyword>")
comment_intro = pp.Literal("--")
short_comment = comment_intro + pp.restOfLine
long_comment = comment_intro + LBRACK + ... + RBRACK
lua_comment = long_comment | short_comment
-ident = ppc.identifier
+# must use negative lookahead to ensure we don't parse a keyword as an identifier
+ident = ~any_keyword + ppc.identifier
+
name = pp.delimitedList(ident, delim=".", combine=True)
namelist = pp.delimitedList(name)
@@ -274,6 +277,12 @@ if __name__ == "__main__":
if t['foo'] then
n = n + 1
end
+ if 10 > 8 then
+ n = n + 2
+ end
+ if (10 > 8) then
+ n = n + 2
+ end
end
"""