summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-06-08 21:52:35 +0000
committerptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-06-08 21:52:35 +0000
commit0e48421b611da979e10222f7e3024d436905c8c7 (patch)
tree841fd66945123803592dc7b21c355535c94e06f1
parentd66cd522c20810bf24f73af0adece6c86d307699 (diff)
downloadpyparsing-0e48421b611da979e10222f7e3024d436905c8c7.tar.gz
Add support in pyparsing_common for fractions and mixed integer-fraction values
git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/trunk@361 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b
-rw-r--r--src/CHANGES2
-rw-r--r--src/pyparsing.py18
-rw-r--r--src/unitTests.py37
3 files changed, 44 insertions, 13 deletions
diff --git a/src/CHANGES b/src/CHANGES
index 3b33ed9..33f1266 100644
--- a/src/CHANGES
+++ b/src/CHANGES
@@ -36,6 +36,8 @@ Verison 2.1.5 - June, 2016
. ISO8601 date and date time strings
. UUID (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
. hex integer (returned as int)
+ . fraction (integer '/' integer, returned as float)
+ . mixed integer (integer '-' fraction, or just fraction, returned as float)
. stripHTMLTags (parse action to remove tags from HTML source)
- runTests now returns a two-tuple: success if all tests succeed,
diff --git a/src/pyparsing.py b/src/pyparsing.py
index e68b78c..4c1eca7 100644
--- a/src/pyparsing.py
+++ b/src/pyparsing.py
@@ -3962,19 +3962,27 @@ class pyparsing_common:
"""
integer = Word(nums).setName("integer").setParseAction(convertToInteger)
- """expression that parses an unsigned integer and returns an int"""
+ """expression that parses an unsigned integer, returns an int"""
hex_integer = Word(hexnums).setName("hex integer").setParseAction(tokenMap(int,16))
- """expression that parses a hexadecimal integer and returns an int"""
+ """expression that parses a hexadecimal integer, returns an int"""
signedInteger = Regex(r'[+-]?\d+').setName("signed integer").setParseAction(convertToInteger)
- """expression that parses an integer with optional leading sign and returns an int"""
+ """expression that parses an integer with optional leading sign, returns an int"""
+
+ fraction = signedInteger.addParseAction(convertToFloat) + '/' + signedInteger.addParseAction(convertToFloat)
+ """fractional expression of an integer divided by an integer, returns a float"""
+ fraction.addParseAction(lambda t: t[0]/t[-1])
+
+ mixed_integer = fraction | integer + Optional(Optional('-').suppress() + fraction)
+ """mixed integer of the form 'integer - fraction', with optional leading integer, returns float"""
+ mixed_integer.addParseAction(lambda t: sum(t))
real = Regex(r'[+-]?\d+\.\d*').setName("real number").setParseAction(convertToFloat)
"""expression that parses a floating point number and returns a float"""
- sciReal = Regex(r'[+-]?\d+([eE][+-]?\d+|\.\d*([eE][+-]?\d+)?)').setName("real number with scientfic notation").setParseAction(convertToFloat)
- """expression that parses a floating point number with optional scientfic notation and returns a float"""
+ sciReal = Regex(r'[+-]?\d+([eE][+-]?\d+|\.\d*([eE][+-]?\d+)?)').setName("real number with scientific notation").setParseAction(convertToFloat)
+ """expression that parses a floating point number with optional scientific notation and returns a float"""
# streamlining this expression makes the docs nicer-looking
numeric = (sciReal | real | signedInteger).streamline()
diff --git a/src/unitTests.py b/src/unitTests.py
index d43912c..d9eece9 100644
--- a/src/unitTests.py
+++ b/src/unitTests.py
@@ -70,7 +70,7 @@ class PyparsingTestInit(ParseTestCase):
print_("Python version", sys.version)
def tearDown(self):
pass
-
+
class ParseASMLTest(ParseTestCase):
def runTest(self):
import parseASML
@@ -86,7 +86,7 @@ class ParseASMLTest(ParseTestCase):
#~ pprint.pprint( results.asList() )
#~ pprint.pprint( results.batchData.asList() )
#~ print results.batchData.keys()
-
+
allToks = flatten( results.asList() )
assert len(allToks) == numToks, \
"wrong number of tokens parsed (%s), got %d, expected %d" % (testFile, len(allToks),numToks)
@@ -1006,7 +1006,7 @@ class RepeaterTest(ParseTestCase):
# retest using matchPreviousExpr instead of matchPreviousLiteral
second = matchPreviousExpr(first).setName("repeat(word1expr)")
seq = first + bridge + second
-
+
tests = [
( "abc12abc", True ),
( "abc12cba", False ),
@@ -1021,7 +1021,7 @@ class RepeaterTest(ParseTestCase):
if not found:
print_("No expression match in", tst)
assert found == result, "Failed repeater for test: %s, matching %s" % (tst, str(seq))
-
+
print_()
first = Word("abcdef").setName("word1")
@@ -1033,7 +1033,7 @@ class RepeaterTest(ParseTestCase):
compoundSeq = csFirst + ":" + csSecond
compoundSeq.streamline()
print_(compoundSeq)
-
+
tests = [
( "abc12abc:abc12abc", True ),
( "abc12cba:abc12abc", False ),
@@ -1068,17 +1068,17 @@ class RepeaterTest(ParseTestCase):
if not found:
print_("No expression match in", tst)
assert found == result, "Failed repeater for test: %s, matching %s" % (tst, str(seq))
-
+
print_()
eFirst = Word(nums)
eSecond = matchPreviousExpr(eFirst)
eSeq = eFirst + ":" + eSecond
-
+
tests = [
( "1:1A", True ),
( "1:10", False ),
]
-
+
for tst,result in tests:
found = False
for tokens,start,end in eSeq.scanString(tst):
@@ -2725,6 +2725,27 @@ class CommonExpressionsTest(ParseTestCase):
""")[0]
assert success, "failed to parse valid uuid"
+
+ success = pyparsing_common.fraction.runTests("""
+ 1/2
+ -15/16
+ -3/-4
+ """)[0]
+ assert success, "failed to parse valid fraction"
+
+
+ success = pyparsing_common.mixed_integer.runTests("""
+ 1/2
+ -15/16
+ -3/-4
+ 1 1/2
+ 2 -15/16
+ 0 -3/-4
+ 12
+ """)[0]
+ assert success, "failed to parse valid mixed integer"
+
+
class TokenMapTest(ParseTestCase):
def runTest(self):
from pyparsing import tokenMap, Word, hexnums, OneOrMore