Skip to content

Cannot trigger conflict in ambiguous grammar OR How to specify a "fallback rule" #3334

Answered by wldmr
wldmr asked this question in Q&A
Discussion options

You must be logged in to vote

OK, I've cracked it (I think):

IDENT_REGEX = /[a-zA-z_][a-zA-Z0-9_]*/

module.exports = grammar({
  // …
  conflicts: $ => [
    [$.identifier, $.text],
  ],

  rules: {
    // …
    text: $ => prec.right(repeat1(choice(
    IDENT_REGEX,  // this needs to come first, because the next regex is more general and tree-sitter goes by rule order. If the next regex matched an identifier, it wouldn't trigger the conflict.
      /[^\s\{\}|:]+/,
      ':',
      $._binary_operator,
    ))),
    // …
    identifier: _ => IDENT_REGEX,
  },
});

My mistakes:

  1. I had (stupidly) pre-decided the conflicts by giving text a lexical precedence (token(prec(…))) for the text.
  2. Text tokenized differently from _.expr

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by wldmr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant