Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
murphyke opened this issue Aug 10, 2020 · 1 comment
Open
Labels

Comments

@murphyke
Copy link

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 GerHobbelt added the bug label Nov 7, 2020
@GerHobbelt
Copy link
Owner

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. 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants