diff options
author | ptmcg <ptmcg@austin.rr.com> | 2020-11-02 18:14:56 -0600 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2020-11-02 18:14:56 -0600 |
commit | 2dd2e2bb70407eea91f18de2caea5ba4527eb7dc (patch) | |
tree | c0a8824908983d6b4b81a6081af629e6ba8064b0 /examples | |
parent | 96e0fab07788fca87e1473b0ae755335d6988895 (diff) | |
download | pyparsing-git-2dd2e2bb70407eea91f18de2caea5ba4527eb7dc.tar.gz |
Add IndentedBlock class; made vertical keyword arg more visible when creating railroad diags; changed create_diagram from monkeypatch to included method on ParserElement; better debug exception if Dict is constructed with non-Group expressionpyparsing_3.0.0b1
Diffstat (limited to 'examples')
-rw-r--r-- | examples/indentedGrammarExample.py | 4 | ||||
-rw-r--r-- | examples/indented_block_example.py | 54 | ||||
-rw-r--r-- | examples/number_words.py | 31 | ||||
-rw-r--r-- | examples/verilogParse.py | 13 |
4 files changed, 83 insertions, 19 deletions
diff --git a/examples/indentedGrammarExample.py b/examples/indentedGrammarExample.py index c761393..706a0d7 100644 --- a/examples/indentedGrammarExample.py +++ b/examples/indentedGrammarExample.py @@ -10,6 +10,7 @@ from pyparsing import *
+
data = """\
def A(z):
A1
@@ -32,9 +33,8 @@ def spam(x,y): """
-indentStack = [1]
stmt = Forward()
-suite = indentedBlock(stmt, indentStack)
+suite = IndentedBlock(stmt)
identifier = Word(alphas, alphanums)
funcDecl = (
diff --git a/examples/indented_block_example.py b/examples/indented_block_example.py new file mode 100644 index 0000000..4f5feb1 --- /dev/null +++ b/examples/indented_block_example.py @@ -0,0 +1,54 @@ +# +# indented_block_example.py +# + +import pyparsing as pp + +ppc = pp.pyparsing_common + +data = """\ + + A + 100 + 101 + + 102 + B + 200 + 201 + + C + 300 + +""" + +integer = ppc.integer +group = pp.Group(pp.Char(pp.alphas) + pp.Group(pp.IndentedBlock(integer))) + +print(group[...].parseString(data).dump()) + +# example of a recursive IndentedBlock + +data = """\ + + A + 100 + 101 + + 102 + B + 200 + b + 210 + 211 + 202 + C + 300 + +""" + +group = pp.Forward() +group <<= pp.Group(pp.Char(pp.alphas) + pp.Group(pp.IndentedBlock(integer | group))) + +print("using searchString") +print(sum(group.searchString(data)).dump()) diff --git a/examples/number_words.py b/examples/number_words.py index 724059f..b1217cf 100644 --- a/examples/number_words.py +++ b/examples/number_words.py @@ -29,13 +29,18 @@ import pyparsing as pp from operator import mul import pyparsing.diagram + def define_numeric_word(s, value): return pp.CaselessKeyword(s).addParseAction(lambda: value) + def define_numeric_word_range(s, vals): if isinstance(s, str): s = s.split() - return pp.MatchFirst(define_numeric_word(nm, nm_value) for nm, nm_value in zip(s, vals)) + return pp.MatchFirst( + define_numeric_word(nm, nm_value) for nm, nm_value in zip(s, vals) + ) + opt_dash = pp.Optional(pp.Suppress("-")).setName("optional '-'") opt_and = pp.Optional((pp.CaselessKeyword("and") | "-").suppress()).setName("optional 'and'") @@ -54,23 +59,24 @@ thousands = one_to_999 + define_numeric_word("thousand", 1000) hundreds.setName("100s") thousands.setName("1000s") + def multiply(t): return mul(*t) + hundreds.addParseAction(multiply) thousands.addParseAction(multiply) -numeric_expression = (pp.Optional(thousands + opt_and) - + pp.Optional(hundreds + opt_and) - + one_to_99 - | pp.Optional(thousands + opt_and) - + hundreds - | thousands - ).setName("numeric_words") +numeric_expression = ( + pp.Optional(thousands + opt_and) + pp.Optional(hundreds + opt_and) + one_to_99 + | pp.Optional(thousands + opt_and) + hundreds + | thousands +).setName("numeric_words") numeric_expression.addParseAction(sum) -if __name__ == '__main__': - numeric_expression.runTests(""" +if __name__ == "__main__": + numeric_expression.runTests( + """ one seven twelve @@ -83,7 +89,8 @@ if __name__ == '__main__': nine hundred thousand nine hundred and ninety nine nine hundred and ninety nine thousand nine hundred and ninety nine nineteen hundred thousand nineteen hundred and ninety nine - """) + """ + ) # create railroad diagram - numeric_expression.create_diagram("numeric_words_diagram.html") + numeric_expression.create_diagram("numeric_words_diagram.html", vertical=5) diff --git a/examples/verilogParse.py b/examples/verilogParse.py index 8a809d1..cba0ac0 100644 --- a/examples/verilogParse.py +++ b/examples/verilogParse.py @@ -914,6 +914,9 @@ if 0: else: def main(): + import sys + + sys.setrecursionlimit(5000) print("Verilog parser test (V %s)" % __version__) print(" - using pyparsing version", pyparsing.__version__) print(" - using Python version", sys.version) @@ -927,8 +930,8 @@ else: failCount = 0 Verilog_BNF() numlines = 0 - startTime = time.clock() - fileDir = "verilog" + startTime = time.time() + fileDir = "../scratch/verilog" # ~ fileDir = "verilog/new" # ~ fileDir = "verilog/new2" # ~ fileDir = "verilog/new3" @@ -950,9 +953,9 @@ else: print(fnam, len(filelines), end=" ") numlines += len(filelines) teststr = "".join(filelines) - time1 = time.clock() + time1 = time.time() tokens = test(teststr) - time2 = time.clock() + time2 = time.time() elapsed = time2 - time1 totalTime += elapsed if len(tokens): @@ -974,7 +977,7 @@ else: failCount += 1 for i, line in enumerate(filelines, 1): print("%4d: %s" % (i, line.rstrip())) - endTime = time.clock() + endTime = time.time() print("Total parse time:", totalTime) print("Total source lines:", numlines) print("Average lines/sec:", ("%.1f" % (float(numlines) / (totalTime + 0.05)))) |