allow unique, named precedence level for `prec.dynamic`
I'm making a tree-sitter parser for Norg, and I'm having problem with conflict case described below.
I read official document several times, tried a lot of variations for months, but can't find a way to accomplish what I want.
Problem
*/`word/*`
Syntactically this can be bold containing italic with following punctuation or verbatim with preceding punctuations. But as verbatim has higher precedence level than bold/italic in Norg spec, this should be parsed as second one.
As this does not seem to be solvable in parser generation time, I give precedence level 2 to verbatim and 1 to both bold and italic.
But this doesn't work because paragraph with bold's precedence level becomes 2, not 1 as it sums all descendant's precedence levels.
Simply giving more high precedence level to verbatim doesn't works because of following case
*/`word/ /word/ /word/*`
Now paragraph with bold's precedence level becomes 4. So theoretically dynamic precedence level can grow up endlessly.
Solution
I suggest to have something like prec.reset() to allow parent node not contain child's precedence level.
So in cases above, only bold's precedence level will be compared to verbatim's, and italic's will be considered inside the bold (inside the prec.reset() function.)
I apologize if my explanation is unclear. I have bad English skill.
Just found resetting is not enough for this problem because of following case:
/`word/ *word* /word/ /word/`
all 4 italic and bolds' precedence level should not summed up neither.
So I think allowing named precedence level for prec.dynamic like prec does might solve this problem.
e.g.
...
precedences: ($) => [
["verbatim", "standard"],
],
rules: [
...
bold: prec.dynamic("standard", ...),
italic: prec.dynamic("standard", ...),
verbatim: prec.dynamic("verbatim", ...),
...
],
...
Now when parsing document above, first stack (paragraph's segment with child italic->bold->italic->italic) will have precedence level "standard" and not something like "standard" x4
Another idea to solve this issue is to make parent node have max precedence level of children's.
This is way easier to implement, but can be breaking change.
Edit: more logic is needed to give higher precedence to punc -> italic -> punc than bold (italic). Need to compare dynamic level first, and if levels are same. compare count of level-n-child