Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting variable type in pyparsing

I have the sequence of tokens like this:

TokenName(alphanum) value(printable) value(printable, optional) value(printable, optional) Literal(';')

by example:

Token1 "stringValue with escaped data eg: \" in it";
Token2 12;
Token3 0xaaaa; // and hex string indicated by 0x
Token4 "value1" 2 0xbbcc;

Result I except is:

[
    ['Token1', 'stringValue with escaped data eg: " in it'],
    ['Token2', 12],
    ['Token3', '0xaaaa'],
    ['Token4', ['value1', 2, '0xbbcc']],
]

Now I have something like this:

import pyparsing as p
non_semi = ''.join(c for c in p.printables if c != ';')
semi = p.Literal(';').suppress()

single_value = p.Word(non_semi) + semi
multi_value = p.Group(
    p.Word(non_semi) +
    p.Word(non_semi) +
    p.Optional(p.Word(non_semi)) +
    semi
)

value = single_value | multi_value
assignment = p.Group(p.Word(p.alphanum) + value)

bnf = p.Group(p.OneOrMore(assignment))
bnf.ignore(p.cStyleComment)

But this code, does not change 12 into int(12) and also gets " chars in strings, can pyparsing handle this value transformations?

EDIT

Ok, I know how to deal with string: QuotedString helps a lot

like image 240
canni Avatar asked Mar 01 '26 00:03

canni


1 Answers

OK, I have found my answers:

For handling of strings: QuotedString

For handling of int and other conversions, there is a setParseAction method of a token def, witch can take eg. lambda s, l, t: int(s) param witch will do the trick

like image 99
canni Avatar answered Mar 02 '26 12:03

canni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!