Replies: 4 comments
-
I don't think passing |
Beta Was this translation helpful? Give feedback.
-
Thanks! I see. It's not just this specific case though, there's many variants like this where the concrete AST depends on type info: a * (*b) (c, d, f); (either a variable declaration, a multiplication of I don't know how many other special cases there are, probably more. Basically this means you have to fall back to emitting tokens as soon as you see a Is there really no way to get that context method to work? Idk it seems to me that would be the cleaner solution (if there even is a clean way to parse C) Btw I really appreciate your work on this package. I think it's really useful and well designed. |
Beta Was this translation helpful? Give feedback.
-
A possible workaround is to introduce side-effects, like the indent parser does on line 42: dart-petitparser/lib/src/indent/indent.dart Lines 38 to 44 in 235fd02 In your example you would have the fields It will be tricky to get everything right and a bit ugly to loose the functional properties of the parser. For example, you need to somehow manage the scope of your variables. However, this might be easier than rewriting the AST in your case? |
Beta Was this translation helpful? Give feedback.
-
Right, yeah that's a good solution. Another idea I came up with (just for completeness) is just producing both AST nodes, one for the case where |
Beta Was this translation helpful? Give feedback.
-
Some languages require more extensive context to parse correctly. For example in C, this example should produce different AST depending on whether
a
is a type or a variable:If
a
is a type, this is a variable declaration of variableb
with typea*
. Ifa
is a variable name, this is an expression that multipliesa
withb
.How should this kind of context be tracked? My idea would be to implement a custom
Context
that tracks declared variables and types, i.e.:and then instead parse like this:
However, it seems like that doesn't work, as there's multiple places where a parser will just create a new
Context
from scratch reintroduce that into the parsing:fastParseOn
will apparently always create a new context from scratch:dart-petitparser/lib/src/core/parser.dart
Lines 32 to 35 in 235fd02
MatchesIterator.parseOn
:dart-petitparser/lib/src/matcher/matches/matches_iterator.dart
Lines 17 to 33 in 235fd02
TrimmingParser
will create a new context from scratch:dart-petitparser/lib/src/parser/action/trimming.dart
Lines 38 to 50 in 235fd02
Is this even the right way to use the Context? Should I do it completely differently for context-aware parsing? Should I just try to avoid
fastParseOn
andMatchesIterator
&TrimmingParser
? (I don't even know when fastParseOn will be called instead of parseOn)Beta Was this translation helpful? Give feedback.
All reactions