So far I have tried using ‘setParseAction’ to get the location of matched tokens, but it doesn’t work as I expect sometimes. Take this code as an example:
>>> from pyparsing import *
>>>
>>> Z = Literal('0')
>>> POINT = Literal('.')
>>> BIN_DIGITS = Word('01')
>>> OCT_DIGITS = Word('01234567')
>>> DEC_DIGITS = Word('0123456789')
>>> HEX_DIGITS = Word('0123456789abcdefABCDEF')
>>> DEC_INT = DEC_DIGITS.setParseAction(lambda t: int(t[0]))
>>> BIN_INT = Combine(Z + ((Literal('b') | 'B')) + BIN_DIGITS).\
... setParseAction(lambda t: int(t[0], 2))
>>> OCT_INT = Combine(Z + ((Literal('o') | 'O')) + OCT_DIGITS).\
... setParseAction(lambda t: int(t[0], 8))
>>> HEX_INT = Combine(Z + ((Literal('x') | 'X')) + HEX_DIGITS).\
... setParseAction(lambda t: int(t[0], 16))
>>> INTEGER = HEX_INT | OCT_INT | BIN_INT | DEC_INT
>>> EXP = Combine(CaselessLiteral('E') + Optional(Literal('+') | '-') + DEC_INT)
>>> POINT_FLOAT = Combine(Optional(DEC_INT) + POINT + DEC_INT) | \
... Combine(DEC_INT + POINT)
>>> EXP_FLOAT = Combine(DEC_INT + EXP) | Combine(POINT_FLOAT + EXP)
>>> FLOAT = (EXP_FLOAT | POINT_FLOAT).setParseAction(lambda t: float(t[0]))
>>>
>>>
>>> def p(s, l, t):
... print 'Location of %s: %s' % (t[0], l,)
...
>>>
>>> NUMBER = (FLOAT | INTEGER).setParseAction(p)
>>> NUMBER.parseString(' 12345')
Location of 12345: 0
([12345], {})
>>> NUMBER.parseString(' 12345')
Location of 12345: 0
([12345], {})
>>> NUMBER.parseString('12345')
Location of 12345: 0
([12345], {})
Location is always 0 no matter where I position the number ‘12345’ in the string. However if I try:
>>> LITERAL = Literal('someword').setParseAction(p)
>>> LITERAL.parseString(' someword')
Location of someword: 4
(['someword'], {})
>>> LITERAL.parseString(' someword')
Location of someword: 1
(['someword'], {})
>>> LITERAL.parseString('someword')
Location of someword: 0
(['someword'], {})
It works as expected. What am I doing wrong in the first example?
Ok, thanks to Paul’s tip, I managed work around this problem by combining the ‘Or’ expression with a supressed whitespace expression, and using it to parse the string. Heres the end result: