You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Im finding that the order I declare my delegates in a parser grammar affects whether or not it parses. I have a grammar like the following:
internalclassParser: Grammar<List<Command>>() {
internalval comments by regexToken("#.*\n", true)
internalval str by regexToken("\".*\"")
internalval queryType by regexToken("[A-Z]+(?:_[A-Z]+)*")
internalval word by regexToken("[A-Za-z]+")
internalvalLPAR by literalToken("(")
internalvalRPAR by literalToken(")")
internalvalCOLON by literalToken(":")
internalvalLBRACE by literalToken("{")
internalvalRBRACE by literalToken("}")
internalval equals by literalToken("=")
internalval ws by regexToken("\\s+",true)
internalval newline by regexToken("[\r\n]+",true)
internalval comma by literalToken(",")
internalval param:Parser<ValueMetadata> by (word and-COLONand word) map { (p, t) ->ValueMetadata(p.text, Type.valueOf(t.text))
}
val params by -LPARand separatedTerms(param, comma, true) and-RPARval outputs by -LPARand separatedTerms(param, comma, true) and-RPARval cmdParser by ( -LBRACEand queryType and-equals and str and-RBRACE )
val funcParser:Parser<Command> by (word and params and-COLONand params and cmdParser) map {
(name, inputs, outputs, cmdFunc) ->val (type,cmd) = cmdFunc
Command(name.text,
inputs,
outputs,
cmd.text.subSequence(1, cmd.length -1).toString(),
QueryType.valueOf(type.text)
)
}
overrideval rootParser:Parser<List<Command>> by zeroOrMore(funcParser)
}
thats meant to parse
# Documentation that should be ignored
findFoo(test:String,entity:String):(foo:String,bar:Int) {
SQL_QUERY = "select foo,bar from baz where z = :name and y = :entity"
}
# Documentation that should be ignored
findBar(test:String,entity:String):(foo:String,bar:Int) {
SQL_QUERY = "select foo,bar from baz where z = :name and y = :entity"
}
into a list of Commands. By just switching the order of str, queryType, and word the parse will fail / pass on different test cases with errors like Could not parse input: UnparsedRemainder(startsWith=word@2 for "findFoo" at 39 (2:1))
The text was updated successfully, but these errors were encountered:
The tokens you declare with delegation are matched in the same order as declared. So if the tokenizing is ambiguous (which is often the case) then the tokens declared earlier are prioritized.
Note also this section in the README:
Note: the tokens order matters in some cases, because the tokenizer tries to match them in exactly this order.
For instance, if literalToken("a")
is listed before literalToken("aa"), the latter will never be matched. Be careful with keyword tokens!
If you match them with regexes, a word boundary \b in the end may help against ambiguity.
Im finding that the order I declare my delegates in a parser grammar affects whether or not it parses. I have a grammar like the following:
thats meant to parse
into a list of Commands. By just switching the order of str, queryType, and word the parse will fail / pass on different test cases with errors like
Could not parse input: UnparsedRemainder(startsWith=word@2 for "findFoo" at 39 (2:1))
The text was updated successfully, but these errors were encountered: