#5 Please explain what I am doing wrong

Open
opened 6 months ago by Jason · 0 comments
Jason commented 6 months ago
Collaborator

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)
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 ```Python 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 ```Python 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) ```
Sign in to join this conversation.
No Label
No Milestone
No Assignees
1 Participants
Notifications
Due Date

No due date set.

Dependencies

This issue currently doesn't have any dependencies.

Loading…
There is no content yet.