I'm trying to create a parser for a preprocessor.
I need to understand just a subset of the preprocesor key words (i.e. if/else blocks). I have a working example but it's very fragile. To illustrate the problem, here is a minimal example
Does not work
import pypeg2 as pp class Line(pp.Plain): # #grammar = pp.restline, pp.endl # Infinite loop # #grammar = pp.maybe_some(pp.word), pp.endl # Infinite loop # #grammar = pp.some([pp.word, pp.endl]) # Infinite loop grammar = pp.word # breaks #class Line(str): # pass # breaks class IfDefBlock: grammar = "`ifdef", pp.blank, pp.name(), pp.maybe_some(Line), "`endif" res = pp.parse("`ifdef SYMBOL1\n/*test*/\nmore code2;\n`endif", IfDefBlock)
works, but no use as any attempt to further process the block has the above problems
import pypeg2 as pp class IfDef(pp.Plain): grammar = pp.RegEx(r"(?ms)`ifdef.*?`endif") res2 = pp.parse("`ifdef SYMBOL1\n/*test*/\nmore code2;\n`endif", IfDef) print(res2)