GerHobbelt / jison

bison / YACC / LEX in JavaScript (LALR(1), SLR(1), etc. lexer/parser generator)
https://gerhobbelt.github.io/jison/
MIT License
116 stars 20 forks source link

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

Open murphyke opened 3 years ago

murphyke commented 3 years ago

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;"]
    ]
  }
}
GerHobbelt commented 3 years ago

Tried to reproduce with jison-gho 0.6.1-216, but cannot reproduce this issue.

Test code:

examples/issue-58-gho.make.js :

var jison = require('../');
var fs = require('fs');
var path = require('path');

const input = fs.readFileSync('./issue-58-gho.json', 'utf8');
//console.log('load:', input);
const grammar = JSON.parse(input);
const parser = new jison.Parser(grammar);
const source = parser.generate();
fs.mkdirSync('./output/issue-58-gho/', { recursive: true });
fs.writeFileSync('./output/issue-58-gho/code.output.js', source, 'utf8');

and the relevant section in the examples/Makefile:

issue-58-gho:
    $(NODE) ./$@.make.js
    $(JISON) -n parser --json ./$@.json
    $(NODE) ./output/$@/$@.js
    diff -u -E ./output/$@/$@.js ./output/$@/code.output.js

result is an *empty diff hence no difference between invoking the jison CLI and the generator code listed above.

Note: the -n parameter is to override the CLI's auto-naming of the parser object. Without it a few lines differ, but nothing in the parser core, like, say, a damaged rule.

No idea how this could be happening at your place. 🤔