satoru
satoru

Reputation: 33215

How to parse multiple elements in one go using pyparsing?

I'm trying to write a thrift parser with pyparsing.

The parse result I want to see is a dict that maps element names to parsed tokens. After defining the elements, I call scanString on each of them to parse for the corresponding tokens, and then make a dict from the results.

But this requires multiple pass through the source, one for each of the elements, eg. one for parsing constants, one for exceptions, one for structs ...

Is it possible to parse multiple elements in one go and still be able to separate the tokens according to their types?

Upvotes: 2

Views: 966

Answers (1)

PaulMcG
PaulMcG

Reputation: 63709

Define a single parser containing all of the elements you are looking for:

parser = OneOrMore(parserA | parserB | parserC)

If you have overlapping names, then group the subparsers, and keep them by name:

parser = OneOrMore(Group(parserA)("A*") | Group(parserB)("B*") | Group(parserC)("C*"))

The results names with the trailing asterisks will keep all of the parsed matches, not just the last one (take the '*' off and see the difference in the parsed results).

Now you can do:

results = parser.parseString(input)  # or use scanString or searchString
for aresult in results['A']:
    ...
for bresult in results['B']:
    ...

Upvotes: 1

Related Questions