Skip to content

Having trouble with a grammar that has a choice that varies only deeper in the parse tree #2179

Closed Answered by sogaiu
nicklan asked this question in Q&A
Discussion options

You must be logged in to vote

I also tried the following:

module.exports = grammar({
  name: 'buglang',

  conflicts: $ => [
      [$.hsep, $.with_foo],
      [$.with_foo, $.with_foo]
  ],

  rules: {
    source_file: $ => $.expr,

    expr: $ =>
      choice($.txt,
             $.without_foo,
             $.with_foo),

    txt: $ => "txt",

    hsep: $ =>
      choice(":",
             "::",
             ":::"),

    without_foo: $ =>
      seq($.expr,
          $.hsep,
          $.txt),

    with_foo: $ =>
      seq(optional(seq($.bar, ",")),
          $.expr,
          ":",
          $.txt,
          $.foo),

    foo: $ => "foo",

    bar: $ => "bar",

  }
});

The results with parse were:

$ echo "txt: txt" | tree-s…

Replies: 3 comments 6 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
6 replies
@sogaiu
Comment options

Answer selected by nicklan
@nicklan
Comment options

@ahlinc
Comment options

@ahlinc
Comment options

@nicklan
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants