summaryrefslogtreecommitdiff
path: root/examples/parsePythonValue.py
diff options
context:
space:
mode:
authorPaul McGuire <ptmcg@austin.rr.com>2019-04-06 13:29:44 -0500
committerPaul McGuire <ptmcg@austin.rr.com>2019-04-06 13:29:44 -0500
commit832986ffccac943b363da43795c335eafc31b5da (patch)
tree20c2c5123580e9afa7145bf9d05f3c4730f40d63 /examples/parsePythonValue.py
parent9bb045a6a019bd058c56da35568ee6334eb34d8c (diff)
downloadpyparsing-git-832986ffccac943b363da43795c335eafc31b5da.tar.gz
Updated examples to current pyparsing styles, and to use runTests
Diffstat (limited to 'examples/parsePythonValue.py')
-rw-r--r--examples/parsePythonValue.py137
1 files changed, 67 insertions, 70 deletions
diff --git a/examples/parsePythonValue.py b/examples/parsePythonValue.py
index 351dad2..cdfac70 100644
--- a/examples/parsePythonValue.py
+++ b/examples/parsePythonValue.py
@@ -1,70 +1,67 @@
-# parsePythonValue.py
-#
-# Copyright, 2006, by Paul McGuire
-#
-from __future__ import print_function
-from pyparsing import *
-
-
-cvtBool = lambda t:t[0]=='True'
-cvtInt = lambda toks: int(toks[0])
-cvtReal = lambda toks: float(toks[0])
-cvtTuple = lambda toks : tuple(toks.asList())
-cvtDict = lambda toks: dict(toks.asList())
-cvtList = lambda toks: [toks.asList()]
-
-# define punctuation as suppressed literals
-lparen,rparen,lbrack,rbrack,lbrace,rbrace,colon = \
- map(Suppress,"()[]{}:")
-
-integer = Regex(r"[+-]?\d+")\
- .setName("integer")\
- .setParseAction( cvtInt )
-real = Regex(r"[+-]?\d+\.\d*([Ee][+-]?\d+)?")\
- .setName("real")\
- .setParseAction( cvtReal )
-tupleStr = Forward()
-listStr = Forward()
-dictStr = Forward()
-
-unicodeString.setParseAction(lambda t:t[0][2:-1].decode('unicode-escape'))
-quotedString.setParseAction(lambda t:t[0][1:-1].decode('string-escape'))
-boolLiteral = oneOf("True False").setParseAction(cvtBool)
-noneLiteral = Literal("None").setParseAction(replaceWith(None))
-
-listItem = real|integer|quotedString|unicodeString|boolLiteral|noneLiteral| \
- Group(listStr) | tupleStr | dictStr
-
-tupleStr << ( Suppress("(") + Optional(delimitedList(listItem)) +
- Optional(Suppress(",")) + Suppress(")") )
-tupleStr.setParseAction( cvtTuple )
-
-listStr << (lbrack + Optional(delimitedList(listItem) +
- Optional(Suppress(","))) + rbrack)
-listStr.setParseAction(cvtList, lambda t: t[0])
-
-dictEntry = Group( listItem + colon + listItem )
-dictStr << (lbrace + Optional(delimitedList(dictEntry) + \
- Optional(Suppress(","))) + rbrace)
-dictStr.setParseAction( cvtDict )
-
-tests = """['a', 100, ('A', [101,102]), 3.14, [ +2.718, 'xyzzy', -1.414] ]
- [{0: [2], 1: []}, {0: [], 1: [], 2: []}, {0: [1, 2]}]
- { 'A':1, 'B':2, 'C': {'a': 1.2, 'b': 3.4} }
- 3.14159
- 42
- 6.02E23
- 6.02e+023
- 1.0e-7
- 'a quoted string'""".split("\n")
-
-for test in tests:
- print("Test:", test.strip())
- result = listItem.parseString(test)[0]
- print("Result:", result)
- try:
- for dd in result:
- if isinstance(dd,dict): print(list(dd.items()))
- except TypeError as te:
- pass
- print()
+# parsePythonValue.py
+#
+# Copyright, 2006, by Paul McGuire
+#
+from __future__ import print_function
+import pyparsing as pp
+
+
+cvtBool = lambda t:t[0]=='True'
+cvtInt = lambda toks: int(toks[0])
+cvtReal = lambda toks: float(toks[0])
+cvtTuple = lambda toks : tuple(toks.asList())
+cvtDict = lambda toks: dict(toks.asList())
+cvtList = lambda toks: [toks.asList()]
+
+# define punctuation as suppressed literals
+lparen, rparen, lbrack, rbrack, lbrace, rbrace, colon, comma = map(pp.Suppress,"()[]{}:,")
+
+integer = pp.Regex(r"[+-]?\d+").setName("integer").setParseAction(cvtInt )
+real = pp.Regex(r"[+-]?\d+\.\d*([Ee][+-]?\d+)?").setName("real").setParseAction(cvtReal)
+tupleStr = pp.Forward()
+listStr = pp.Forward()
+dictStr = pp.Forward()
+
+pp.unicodeString.setParseAction(lambda t:t[0][2:-1])
+pp.quotedString.setParseAction(lambda t:t[0][1:-1])
+boolLiteral = pp.oneOf("True False").setParseAction(cvtBool)
+noneLiteral = pp.Literal("None").setParseAction(pp.replaceWith(None))
+
+listItem = (real
+ | integer
+ | pp.quotedString
+ | pp.unicodeString
+ | boolLiteral
+ | noneLiteral
+ | pp.Group(listStr)
+ | tupleStr
+ | dictStr)
+
+tupleStr << (lparen
+ + pp.Optional(pp.delimitedList(listItem))
+ + pp.Optional(comma)
+ + rparen)
+tupleStr.setParseAction(cvtTuple)
+
+listStr << (lbrack
+ + pp.Optional(pp.delimitedList(listItem) + pp.Optional(comma))
+ + rbrack)
+listStr.setParseAction(cvtList, lambda t: t[0])
+
+dictEntry = pp.Group(listItem + colon + listItem)
+dictStr << (lbrace
+ + pp.Optional(pp.delimitedList(dictEntry) + pp.Optional(comma))
+ + rbrace)
+dictStr.setParseAction(cvtDict)
+
+tests = """['a', 100, ('A', [101,102]), 3.14, [ +2.718, 'xyzzy', -1.414] ]
+ [{0: [2], 1: []}, {0: [], 1: [], 2: []}, {0: [1, 2]}]
+ { 'A':1, 'B':2, 'C': {'a': 1.2, 'b': 3.4} }
+ 3.14159
+ 42
+ 6.02E23
+ 6.02e+023
+ 1.0e-7
+ 'a quoted string'"""
+
+listItem.runTests(tests)