summaryrefslogtreecommitdiff
path: root/examples/ebnf.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/ebnf.py
parent41752aa52cc97c710474bb2972cceab057b52ad4 (diff)
downloadpyparsing-git-53d1b4a6f48a53c4c4ec4ac7031362b691c0366d.tar.gz
Blacken the project (#141)
Diffstat (limited to 'examples/ebnf.py')
-rw-r--r--examples/ebnf.py80
1 files changed, 51 insertions, 29 deletions
diff --git a/examples/ebnf.py b/examples/ebnf.py
index bb19155..4843d40 100644
--- a/examples/ebnf.py
+++ b/examples/ebnf.py
@@ -11,7 +11,7 @@
from pyparsing import *
-all_names = '''
+all_names = """
integer
meta_identifier
terminal_string
@@ -25,29 +25,36 @@ single_definition
definitions_list
syntax_rule
syntax
-'''.split()
+""".split()
integer = Word(nums)
-meta_identifier = Word(alphas, alphanums + '_')
-terminal_string = Suppress("'") + CharsNotIn("'") + Suppress("'") ^ \
- Suppress('"') + CharsNotIn('"') + Suppress('"')
+meta_identifier = Word(alphas, alphanums + "_")
+terminal_string = Suppress("'") + CharsNotIn("'") + Suppress("'") ^ Suppress(
+ '"'
+) + CharsNotIn('"') + Suppress('"')
definitions_list = Forward()
-optional_sequence = Suppress('[') + definitions_list + Suppress(']')
-repeated_sequence = Suppress('{') + definitions_list + Suppress('}')
-grouped_sequence = Suppress('(') + definitions_list + Suppress(')')
-syntactic_primary = optional_sequence ^ repeated_sequence ^ \
- grouped_sequence ^ meta_identifier ^ terminal_string
-syntactic_factor = Optional(integer + Suppress('*')) + syntactic_primary
-syntactic_term = syntactic_factor + Optional(Suppress('-') + syntactic_factor)
-single_definition = delimitedList(syntactic_term, ',')
-definitions_list << delimitedList(single_definition, '|')
-syntax_rule = meta_identifier + Suppress('=') + definitions_list + \
- Suppress(';')
-
-ebnfComment = ( "(*" +
- ZeroOrMore( CharsNotIn("*") | ( "*" + ~Literal(")") ) ) +
- "*)" ).streamline().setName("ebnfComment")
+optional_sequence = Suppress("[") + definitions_list + Suppress("]")
+repeated_sequence = Suppress("{") + definitions_list + Suppress("}")
+grouped_sequence = Suppress("(") + definitions_list + Suppress(")")
+syntactic_primary = (
+ optional_sequence
+ ^ repeated_sequence
+ ^ grouped_sequence
+ ^ meta_identifier
+ ^ terminal_string
+)
+syntactic_factor = Optional(integer + Suppress("*")) + syntactic_primary
+syntactic_term = syntactic_factor + Optional(Suppress("-") + syntactic_factor)
+single_definition = delimitedList(syntactic_term, ",")
+definitions_list << delimitedList(single_definition, "|")
+syntax_rule = meta_identifier + Suppress("=") + definitions_list + Suppress(";")
+
+ebnfComment = (
+ ("(*" + ZeroOrMore(CharsNotIn("*") | ("*" + ~Literal(")"))) + "*)")
+ .streamline()
+ .setName("ebnfComment")
+)
syntax = OneOrMore(syntax_rule)
syntax.ignore(ebnfComment)
@@ -56,6 +63,7 @@ syntax.ignore(ebnfComment)
def do_integer(str, loc, toks):
return int(toks[0])
+
def do_meta_identifier(str, loc, toks):
if toks[0] in symbol_table:
return symbol_table[toks[0]]
@@ -64,28 +72,35 @@ def do_meta_identifier(str, loc, toks):
symbol_table[toks[0]] = Forward()
return symbol_table[toks[0]]
+
def do_terminal_string(str, loc, toks):
return Literal(toks[0])
+
def do_optional_sequence(str, loc, toks):
return Optional(toks[0])
+
def do_repeated_sequence(str, loc, toks):
return ZeroOrMore(toks[0])
+
def do_grouped_sequence(str, loc, toks):
return Group(toks[0])
+
def do_syntactic_primary(str, loc, toks):
return toks[0]
+
def do_syntactic_factor(str, loc, toks):
if len(toks) == 2:
# integer * syntactic_primary
return And([toks[1]] * toks[0])
else:
# syntactic_primary
- return [ toks[0] ]
+ return [toks[0]]
+
def do_syntactic_term(str, loc, toks):
if len(toks) == 2:
@@ -93,7 +108,8 @@ def do_syntactic_term(str, loc, toks):
return NotAny(toks[1]) + toks[0]
else:
# syntactic_factor
- return [ toks[0] ]
+ return [toks[0]]
+
def do_single_definition(str, loc, toks):
toks = toks.asList()
@@ -102,7 +118,8 @@ def do_single_definition(str, loc, toks):
return And(toks)
else:
# syntactic_term
- return [ toks[0] ]
+ return [toks[0]]
+
def do_definitions_list(str, loc, toks):
toks = toks.asList()
@@ -111,31 +128,36 @@ def do_definitions_list(str, loc, toks):
return Or(toks)
else:
# single_definition
- return [ toks[0] ]
+ return [toks[0]]
+
def do_syntax_rule(str, loc, toks):
# meta_identifier = definitions_list ;
assert toks[0].expr is None, "Duplicate definition"
forward_count.value -= 1
toks[0] << toks[1]
- return [ toks[0] ]
+ return [toks[0]]
+
def do_syntax(str, loc, toks):
# syntax_rule syntax_rule ...
return symbol_table
-
symbol_table = {}
+
+
class forward_count:
pass
+
+
forward_count.value = 0
for name in all_names:
expr = vars()[name]
- action = vars()['do_' + name]
+ action = vars()["do_" + name]
expr.setName(name)
expr.setParseAction(action)
- #~ expr.setDebug()
+ # ~ expr.setDebug()
def parse(ebnf, given_table={}):
@@ -147,5 +169,5 @@ def parse(ebnf, given_table={}):
for name in table:
expr = table[name]
expr.setName(name)
- #~ expr.setDebug()
+ # ~ expr.setDebug()
return table