Home > Blockchain >  Parsing strings in RPLY
Parsing strings in RPLY

Time:07-21

lg.add("STRING", r"\"(\\.|[^\"\\])*\"")

I have this rule in RPLY but when I try to parse e.g. "string1" it parses it as "string1". How can I make it parse "string1" into string1?

CodePudding user response:

Unlike Ply, RPly's lexer does not provide any mechanism to execute an action on a lexical token. What you get from the RPly lexer is a Token object containing the matched text, unmodified.

You can use a unit production in your grammar in order to impose a transformation on a given token type. For example, you could put the following rule in your grammar:

@pg.production('string : STRING')
def dequote(p):
    return p[0].getstr()[1:-1]

Then, everywhere else in your grammar, you use the non-terminal string instead of the terminal STRING.

I'm ignoring the needs of RPython here; if you're actually using RPython, you probably need to wrap the return value of dequote in some kind of object instead of returning a str. The above snippet works with Python 3.9 (for example). But, of course, if you're not using RPython, you could just use Ply as your parser generator.

  • Related