summaryrefslogtreecommitdiff
path: root/examples/pythonGrammarParser.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/pythonGrammarParser.py
parent41752aa52cc97c710474bb2972cceab057b52ad4 (diff)
downloadpyparsing-git-53d1b4a6f48a53c4c4ec4ac7031362b691c0366d.tar.gz
Blacken the project (#141)
Diffstat (limited to 'examples/pythonGrammarParser.py')
-rw-r--r--examples/pythonGrammarParser.py54
1 files changed, 36 insertions, 18 deletions
diff --git a/examples/pythonGrammarParser.py b/examples/pythonGrammarParser.py
index e3685a1..e9d7d94 100644
--- a/examples/pythonGrammarParser.py
+++ b/examples/pythonGrammarParser.py
@@ -130,35 +130,42 @@ testlist1: test (',' test)*
encoding_decl: NAME
"""
+
class SemanticGroup:
- def __init__(self,contents):
+ def __init__(self, contents):
self.contents = contents
while self.contents[-1].__class__ == self.__class__:
self.contents = self.contents[:-1] + self.contents[-1].contents
def __str__(self):
- return "{}({})".format(self.label,
- " ".join([isinstance(c,str) and c or str(c) for c in self.contents]) )
+ return "{}({})".format(
+ self.label,
+ " ".join([isinstance(c, str) and c or str(c) for c in self.contents]),
+ )
+
class OrList(SemanticGroup):
label = "OR"
pass
+
class AndList(SemanticGroup):
label = "AND"
pass
+
class OptionalGroup(SemanticGroup):
label = "OPT"
pass
+
class Atom(SemanticGroup):
- def __init__(self,contents):
+ def __init__(self, contents):
if len(contents) > 1:
self.rep = contents[1]
else:
self.rep = ""
- if isinstance(contents,str):
+ if isinstance(contents, str):
self.contents = contents
else:
self.contents = contents[0]
@@ -166,12 +173,14 @@ class Atom(SemanticGroup):
def __str__(self):
return "{}{}".format(self.rep, self.contents)
+
def makeGroupObject(cls):
- def groupAction(s,l,t):
+ def groupAction(s, l, t):
try:
return cls(t[0].asList())
except Exception:
return cls(t)
+
return groupAction
@@ -180,20 +189,27 @@ LPAREN = Suppress("(")
RPAREN = Suppress(")")
LBRACK = Suppress("[")
RBRACK = Suppress("]")
-COLON = Suppress(":")
+COLON = Suppress(":")
ALT_OP = Suppress("|")
# bnf grammar
-ident = Word(alphanums+"_")
-bnfToken = Word(alphanums+"_") + ~FollowedBy(":")
+ident = Word(alphanums + "_")
+bnfToken = Word(alphanums + "_") + ~FollowedBy(":")
repSymbol = oneOf("* +")
bnfExpr = Forward()
-optionalTerm = Group(LBRACK + bnfExpr + RBRACK).setParseAction(makeGroupObject(OptionalGroup))
-bnfTerm = ( (bnfToken | quotedString | optionalTerm | ( LPAREN + bnfExpr + RPAREN )) + Optional(repSymbol) ).setParseAction(makeGroupObject(Atom))
+optionalTerm = Group(LBRACK + bnfExpr + RBRACK).setParseAction(
+ makeGroupObject(OptionalGroup)
+)
+bnfTerm = (
+ (bnfToken | quotedString | optionalTerm | (LPAREN + bnfExpr + RPAREN))
+ + Optional(repSymbol)
+).setParseAction(makeGroupObject(Atom))
andList = Group(bnfTerm + OneOrMore(bnfTerm)).setParseAction(makeGroupObject(AndList))
bnfFactor = andList | bnfTerm
-orList = Group( bnfFactor + OneOrMore( ALT_OP + bnfFactor ) ).setParseAction(makeGroupObject(OrList))
-bnfExpr << ( orList | bnfFactor )
+orList = Group(bnfFactor + OneOrMore(ALT_OP + bnfFactor)).setParseAction(
+ makeGroupObject(OrList)
+)
+bnfExpr << (orList | bnfFactor)
bnfLine = ident + COLON + bnfExpr
bnfComment = "#" + restOfLine
@@ -207,14 +223,16 @@ bnfDefs = bnf.parseString(grammar)
# correct answer is 78
expected = 78
-assert len(bnfDefs) == expected, \
- "Error, found %d BNF defns, expected %d" % (len(bnfDefs), expected)
+assert len(bnfDefs) == expected, "Error, found %d BNF defns, expected %d" % (
+ len(bnfDefs),
+ expected,
+)
# list out defns in order they were parsed (to verify accuracy of parsing)
-for k,v in bnfDefs:
- print(k,"=",v)
+for k, v in bnfDefs:
+ print(k, "=", v)
print()
# list out parsed grammar defns (demonstrates dictionary access to parsed tokens)
for k in list(bnfDefs.keys()):
- print(k,"=",bnfDefs[k])
+ print(k, "=", bnfDefs[k])