Skip to content

Parser source produced by generate API is different from CLI output #58

Description

@murphyke

When I use the CLI to create a parser from my grammar, the resulting parser doesn't work properly, but if I use the API and the generate method myself to generate a parser, the parser works.

I'm posting this for posterity in case it's a real issue, but I'm moving forward with the working parser. Still, if there's something dumb I'm doing with the CLI, I'd like to know. I did verify that the JSON file read by the CLI, when parsed to an object, is identical to the grammar object being used by the "generate" approach.

"Generated" method (good):

const parser = new jison.Parser(grammar);
const source = parser.generate();

CLI (bad):

jison --json ./src/components/cohort-space/boolean-grammar.jsonc

Here is the diff between "generated" and CLI:

603c604
< rules: [/^(?:\s+)/i,/^(?:OR\b)/i,/^(?:AND\b)/i,/^(?:NOT\b)/i,/^(?:\()/i,/^(?:\))/i,/^(?:[a-z][a-z0-9_.-]*)/i,/^(?:"(?:\\.|[^"])*")/i],
---
> rules: [/^(?:\s+)/i,/^(?:O)/i,/^(?:AN)/i,/^(?:NO)/i,/^(?:\()/i,/^(?:\))/i,/^(?:[a-z][a-z0-9_.-]*)/i,/^(?:"(?:\\.|[^"])*")/i],

Grammar:

{
  "lex": {
    "options": {
      "case-insensitive": true
    },
    "rules": [
      ["\\s+", "/* skip whitespace */"],
      ["OR\\b", "return 'OR';"],
      ["AND\\b", "return 'AND';"],
      ["NOT\\b", "return 'NOT';"],
      ["\\(", "return 'LPAREN';"],
      ["\\)", "return 'RPAREN';"],
      ["[a-z][a-z0-9_.-]*", "return 'IDENT';"],
      ["\"(?:\\\\.|[^\"])*\"", "return 'STRING';"]
    ]
  },
  "bnf": {
    "start": [["orPhrase", "return $1;"]],
    "orPhrase": [
      ["andPhrase", "$$ = $andPhrase;"],
      [
        "orPhrase OR andPhrase",
        "$$ = {type: 'OR', args: [$orPhrase, $andPhrase]};"
      ]
    ],
    "andPhrase": [
      ["otherBool", "$$ = $otherBool;"],
      [
        "andPhrase AND otherBool",
        "$$ = {type: 'AND', args: [$andPhrase, $otherBool]};"
      ]
    ],
    "otherBool": [
      ["IDENT", "$$ = {type: 'VALUE', args: [$IDENT]};"],
      ["STRING", "$$ = {type: 'VALUE', args: [$STRING]};"],
      ["NOT otherBool", "$$ = {type: 'NOT', args: [$otherBool]};"],
      ["LPAREN orPhrase RPAREN", "$$ = $orPhrase;"]
    ]
  }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions