ignore output of part of a rule #51
-
First of all: this is great. I haven't had that much fun writing a grammar for ages. I want to use PEGTL for something relatively straightforward, parsing Lua tables. I've got something working based upon your Lua example and I'm happy with the progress I've made in just a few hours. But as I am getting into attaching actions to the rules (to construct values from the parser run), I see that I will hit a snag with the unary plus/minus operator that I need for numerals. Unfortunately, In my rule:
I'd like to ignore the |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
First of all: Thanks for the kind words, we are glad that you enjoy using the PEGTL :-) Regarding your question, there is (currently) no way to obtain only part of the matched input, short of attaching multiple actions to the sub-rules, which should be the easiest way to achieve what you need: Let the action attached to the (Presumably you need the sign and the digits together in order to to call a standard string-to-integer function; in that case, since the data might not be contiguous in the input, it will have to be somehow "copied together". Or you can roll your own string-to-integer function spread over the two actions...) |
Beta Was this translation helpful? Give feedback.
-
Adding another idea:
Then attach your actions to |
Beta Was this translation helpful? Give feedback.
-
Nice! Thank you! I think I'll use a variant from @d-frey with actions attached to
|
Beta Was this translation helpful? Give feedback.
-
You're welcome. If similar problems come up more often in the future and the work-arounds get out of hand, feel free to re-open this issue. |
Beta Was this translation helpful? Give feedback.
Adding another idea:
Then attach your actions to
decimal_plus
anddecimal_minus
. In the latter case, just convert the input to a number and negate that (positive) number after a proper range check.