Skip to content

Commit

Permalink
PEP8 name cleanup in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ptmcg committed May 8, 2024
1 parent d4c8206 commit 6794328
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 144 deletions.
7 changes: 5 additions & 2 deletions CHANGES
Expand Up @@ -22,8 +22,11 @@ Version 3.1.3 - in development
- Some type annotations added for parse action related methods, thanks August
Karlstedt (#551).

- delta_time.py example cleaned up to use latest PEP8 names and add minor
enhancements.
- Added `set_name` calls to internal expressions generated in `infix_notation`, for
improved railroad diagramming.

- `delta_time`, `lua_parser`, and `roman_numerals` examples cleaned up to use latest PEP8
names and add minor enhancements.

- Added early testing support for Python 3.13 with JIT enabled.

Expand Down
2 changes: 1 addition & 1 deletion examples/0README.html
Expand Up @@ -141,7 +141,7 @@ <h1>pyparsing Examples</h1>
</li>
<p>

<li><a href="romanNumerals.py">romanNumerals.py</a><br>
<li><a href="roman_numerals.py">romanNumerals.py</a><br>
A Roman numeral generator and parser example, showing the power of parse actions
to compile Roman numerals into their integer values.
</li>
Expand Down
2 changes: 1 addition & 1 deletion examples/delta_time.py
Expand Up @@ -105,7 +105,7 @@ def _singular_or_plural(s: str) -> pp.ParserElement:
a_qty = (a_ | an_).set_parse_action(pp.replace_with(1))
the_qty = the_.set_parse_action(pp.replace_with(1))
qty = pp.ungroup(
pp.Opt(adverb_) + (integer | couple | a_qty | the_qty).set_name("qty_expression")
(pp.Opt(adverb_) + (integer | couple | a_qty | the_qty)).set_name("qty_expression")
).set_name("qty")
time_ref_present = pp.Empty().add_parse_action(pp.replace_with(True))(
"time_ref_present"
Expand Down
70 changes: 34 additions & 36 deletions examples/lua_parser.py
Expand Up @@ -85,33 +85,35 @@
import pyparsing as pp

ppc = pp.pyparsing_common
pp.ParserElement.enablePackrat()
pp.ParserElement.enable_packrat()

LBRACK, RBRACK, LBRACE, RBRACE, LPAR, RPAR, EQ, COMMA, SEMI, COLON = map(
pp.Suppress, "[]{}()=,;:"
)
LBRACK, RBRACK, LBRACE, RBRACE, LPAR, RPAR = pp.Suppress.using_each("[]{}()")
COMMA, SEMI, COLON = pp.Suppress.using_each(",;:")
OPT_SEMI = pp.Optional(SEMI).suppress()
ELLIPSIS = pp.Literal("...")
EQ = pp.Literal("=")

keywords = {
k.upper(): pp.Keyword(k)
for k in """\
return break do end while if then elseif else for in function local repeat until nil false true and or not
return break do end while if then elseif else for in function
local repeat until nil false true and or not
""".split()
}
vars().update(keywords)
any_keyword = pp.MatchFirst(keywords.values()).setName("<keyword>")
any_keyword = pp.MatchFirst(keywords.values()).set_name("keyword")

comment_intro = pp.Literal("--")
short_comment = comment_intro + pp.restOfLine
short_comment = comment_intro + pp.rest_of_line
long_comment = comment_intro + LBRACK + ... + RBRACK
lua_comment = long_comment | short_comment

# 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)
name = pp.DelimitedList(ident, delim=".", combine=True)

namelist = pp.delimitedList(name)
namelist = pp.DelimitedList(name)
number = ppc.number

# does not parse levels
Expand All @@ -121,15 +123,17 @@
exp = pp.Forward()

# explist1 ::= {exp ','} exp
explist1 = pp.delimitedList(exp)
explist1 = pp.DelimitedList(exp)

stat = pp.Forward()
# set up for recursive definition of 'stmt' (since some statements are
# composed of nested statements)
stat = pp.Forward().set_name("stat")

# laststat ::= return [explist1] | break
laststat = pp.Group(RETURN + explist1) | BREAK

# block ::= {stat [';']} [laststat[';']]
block = pp.Group(stat + OPT_SEMI)[1, ...] + pp.Optional(laststat + OPT_SEMI)
block = pp.Group((stat + OPT_SEMI)[1, ...] + pp.Optional(laststat + OPT_SEMI))

# field ::= '[' exp ']' '=' exp | Name '=' exp | exp
field = pp.Group(
Expand All @@ -140,7 +144,7 @@
fieldsep = COMMA | SEMI

# fieldlist ::= field {fieldsep field} [fieldsep]
field_list = pp.delimitedList(field, delim=fieldsep) + pp.Optional(fieldsep)
field_list = pp.DelimitedList(field, delim=fieldsep, allow_trailing_delim=True)

# tableconstructor ::= '{' [fieldlist] '}'
tableconstructor = pp.Group(LBRACE + pp.Optional(field_list) + RBRACE)
Expand All @@ -165,14 +169,16 @@
# prefixexp ::= var | functioncall | '(' exp ')'
# functioncall ::= prefixexp args | prefixexp ':' Name args

prefixexp = name | LPAR + exp + RPAR
functioncall = prefixexp + args | prefixexp + COLON + name + args
exp_group = pp.Group(LPAR + exp + RPAR)
prefixexp = name | exp_group
functioncall = pp.Group(prefixexp + pp.Optional(COLON + name) + pp.Group(args))
var = pp.Forward()
var_atom = functioncall | name | LPAR + exp + RPAR
var_atom = functioncall | name | exp_group
index_ref = pp.Group(LBRACK + exp + RBRACK)
var <<= pp.delimitedList(pp.Group(var_atom + index_ref) | var_atom, delim=".")
var_part = pp.Group(var_atom + index_ref) | var_atom
var <<= pp.DelimitedList(var_part, delim=".")

varlist1 = pp.delimitedList(var)
varlist1 = pp.DelimitedList(var)

# exp ::= nil | false | true | Number | String | '...' |
# function | prefixexp | tableconstructor
Expand All @@ -186,14 +192,14 @@
| functioncall
| var # prefixexp
| tableconstructor
)
).set_name("exp_atom")

# precedence of operations from https://www.lua.org/manual/5.3/manual.html#3.4.8
exp <<= pp.infixNotation(
exp <<= pp.infix_notation(
exp_atom,
[
("^", 2, pp.opAssoc.LEFT),
(NOT | pp.oneOf("# - ~"), 1, pp.opAssoc.RIGHT),
((NOT | pp.oneOf("# - ~")).set_name("not op"), 1, pp.opAssoc.RIGHT),
(pp.oneOf("* / // %"), 2, pp.opAssoc.LEFT),
(pp.oneOf("+ -"), 2, pp.opAssoc.LEFT),
("..", 2, pp.opAssoc.LEFT),
Expand All @@ -205,7 +211,7 @@
(AND, 2, pp.opAssoc.LEFT),
(OR, 2, pp.opAssoc.LEFT),
],
)
).set_name("exp")

assignment_stat = pp.Optional(LOCAL) + varlist1 + EQ + explist1
func_call_stat = pp.Optional(LOCAL) + functioncall
Expand All @@ -227,18 +233,7 @@
)
function_def = pp.Optional(LOCAL) + FUNCTION + funcname + funcbody

for var_name in """
assignment_stat
func_call_stat
do_stat
while_stat
repeat_stat
for_loop_stat
for_seq_stat
if_stat
function_def
""".split():
vars()[var_name].setName(var_name)
pp.autoname_elements()

# stat ::= varlist1 '=' explist1 |
# functioncall |
Expand All @@ -263,13 +258,12 @@
| function_def
)

lua_script = stat[...]
lua_script = stat[...].set_name("script")

# ignore comments
lua_script.ignore(lua_comment)

if __name__ == "__main__":

sample = r"""
function test(x)
local t = {foo=1, bar=2, arg=x}
Expand All @@ -283,6 +277,9 @@
if (10 > 8) then
n = n + 2
end
for var in vars do
print(var, '=', var)
end
end
"""

Expand All @@ -291,3 +288,4 @@
result.pprint()
except pp.ParseException as pe:
print(pe.explain())
raise
6 changes: 4 additions & 2 deletions examples/make_diagram.py
Expand Up @@ -18,14 +18,16 @@
# from examples.mozillaCalendarParser import calendars as imported_expr
# from examples.pgn import pgnGrammar as imported_expr
# from examples.idlParse import CORBA_IDL_BNF; imported_expr = CORBA_IDL_BNF()
# from examples.chemicalFormulas import formula as imported_expr
# from examples.romanNumerals import romanNumeral as imported_expr
# from examples.chemical_formulas import formula as imported_expr
# from examples.roman_numerals import roman_numeral as imported_expr
# from examples.protobuf_parser import parser as imported_expr
# from examples.parsePythonValue import listItem as imported_expr
# from examples.one_to_ninety_nine import one_to_99 as imported_expr
# from examples.simpleSQL import simpleSQL as imported_expr
# from examples.simpleBool import boolExpr as imported_expr
# from examples.adventureEngine import Parser; imported_expr = Parser().bnf
# from examples.lua_parser import lua_script as imported_expr


grammar = imported_expr

Expand Down
102 changes: 0 additions & 102 deletions examples/romanNumerals.py

This file was deleted.

0 comments on commit 6794328

Please sign in to comment.