开发者

Irony: How to give KeyTerm precedence over variable?

开发者 https://www.devze.com 2023-02-23 05:11 出处:网络
Relevant chunk of Irony grammar: var VARIABLE = new RegexBasedTerminal("variable", @"(?-i)\\$?\\w+");

Relevant chunk of Irony grammar:

var VARIABLE = new RegexBasedTerminal("variable", @"(?-i)\$?\w+");

variable.Rule = VARIABLE;
tag_blk.Rule = html_tag_kw + attr_args_opt + block;
term_simple.Rule = NUMBER | STRING | variable | boolean | "null" | term_list;
term.Rule = term_simple | term_filter;
block.Rule = statement_list | statement | ";";
statement.Rule = tag_blk | directive_blk | term;

The problem is that both a "tag" and a "variable" can appear in the same place. I want my parser to prefer the tag over the variable, but it always prefers the variable. How can I change that?

I've tried c开发者_JS百科hanging tag_blk.Rule to PreferShiftHere() + html_tag_kw + attr_args_opt + block; and ImplyPrecedenceHere(-100) + html_tag_kw + attr_args_opt + block; but it doesn't help any. The parser doesn't even complain of an ambiguity.


Try changing the order of 'tag_blk.Rule' and 'variable.Rule' as tokenisers usually go after first match, and variable is first in your list.


You can increase the Priority of the tag_blk Terminal or decrease the one of variable whichever suits your purpose. Terminal class has a Priority field defaulting to 0. According to the comment right above it

// Priority is used when more than one terminal may match the input char. 
// It determines the order in which terminals will try to match input for a given char in the input.
// For a given input char the scanner uses the hash table to look up the collection of terminals that may match this input symbol. 
// It is the order in this collection that is determined by Priority property - the higher the priority, 
// the earlier the terminal gets a chance to check the input.

Unfortunately I can't test this at the moment as the code fragment provided needs work and lots of assumptions to be made compilable. But from the description above this should be the one you are looking for. Hope it helps someone -even 10 years after the question aired.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号