diff options
author | ptmcg <ptmcg@austin.rr.com> | 2020-08-17 17:42:14 -0500 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2020-08-17 17:42:14 -0500 |
commit | c1c9c8dcf5bee8bdf885767751eebfff2ed49f7c (patch) | |
tree | b0ce71a4e68fe144fdd96a75969a7aaf6af51b89 | |
parent | 1a2920dc1ac5b9d90401e53471838d6892c27c59 (diff) | |
download | pyparsing-git-c1c9c8dcf5bee8bdf885767751eebfff2ed49f7c.tar.gz |
Add lookahead on matching identifiers to ensure we aren't matching a keyword
-rw-r--r-- | examples/lua_parser.py | 11 |
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 """ |