Hi,
I am instantiating part of my parser using a dynamic block so that it gets
evaluated at runtime, but the error backtrace simply indicates that the
top-most containing grammar rule has failed. To give an example which is a
somewhat simplified version of to the one i am experiencing, if we had:
rule(:data) { left_brace >> expression?.as(:expression) >> right_brace }
rule(:expression) { keyval.repeat(1) }
rule(:keyval) { field.as(:field) >> str(':') >> value.as(:value) >> comma? }
rule(:field) { (match('[A-Za-z0-9._]').repeat(1)).capture(:field) }
rule(:value) {
dynamic { |source,context|
field = context.captures[:field]
type = ... # get type of field here
if type == String
double_quoted_string
elsif type == Integer
integer.as(:integer)
elsif type == Boolean
str('true').as(:true) | str('false').as(:false)
...
}
}
And I tried to parse the string '{name:true}', where name is a string, I
would get a failure.cause.ascii_tree that would give me an error which
resembles the following:
Failed to match sequence (LEFT_BRACE expression:EXPRESSION? RIGHT_BRACE) at
line 1 char 2.
`- Expected "}", but got "n" at line 1 char 2.
What I want to be able to do, is get detailed information in the ascii_tree
of 1, the fact that it failed at the value rule specifically within the
expression & 2. *why *the parse failed at the value rule ( be able to say
which particular criteria in the conditional failed ).
I realize this is possible to do with not using dynamic and simply
declaring more rules (e.g. boolean_field, int_field, str_field, and then
match all the values separately depending on what's passed in), but I want
to keep a minimal set of rules in this case and was generally curious about
what the best approach would be to improve the failure cause message given
the implementation.
Any help would be appreciated.
Thanks! and thanks for taking the time to read this.