pegjs: Inconsistent behaviour of generated parser
Issue type
- Bug Report:
Prerequisites
- Can you reproduce the issue?: yes
- Did you search the repository issues?: yes
- Did you check the forums?: no
- Did you perform a web search (google, yahoo, etc)?: yes
Description
At the moment I’m using the JS API to generate the parser on runtime. This works fine.
I then tried to generate the parser using the CLI, to avoid generating it during runtime. When I use it though I get errors (~half of my tests for parsing the string throw errors).
Steps to Reproduce
- Move the grammar into its own file
grammar.pegjs - Generate the parser using the CLI
pegjs -o parser.js grammar.pegjs
- Remove the
peg.generate('...')and replace it with the new parser
const parser = require('./parser');
parser.parse('...');
- Run the tests
Expected behavior: I would expect that the generated parser from the CLI works the same as the generated parser from the JS API.
Actual behavior:
Using the JS API, when I pass this string ('foo = "bar"') to the parser I get the following AST:
{
kind: 'condition',
target: 'foo',
operator: '=',
value: 'bar',
valueType: 'string',
attributeType: undefined
}
However, when I use the “generated” parser using the CLI, and pass the same string ('foo = "bar"') I get the following error:
SyntaxError: Expected "(", boolean, date, datetime, number, string, or time but "\"" found.
at peg$buildStructuredError (/Users/emmenko/xxx/parser.js:446:12)
at Object.peg$parse [as parse] (/Users/emmenko/xxx/parser.js:2865:11)
at repl:1:7
at ContextifyScript.Script.runInThisContext (vm.js:50:33)
at REPLServer.defaultEval (repl.js:240:29)
at bound (domain.js:301:14)
at REPLServer.runBound [as eval] (domain.js:314:12)
at REPLServer.onLine (repl.js:441:10)
at emitOne (events.js:121:20)
at REPLServer.emit (events.js:211:7)
Software
- PEG.js:
0.10.0 - Node.js:
8.9.1 - NPM or Yarn:
yarn@1.3.2 - Browser: Chrome
- OS: OSX
- Editor: VSCode
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 15 (7 by maintainers)
whoops, my mistake 😨, this should fix that
A little related addition to the bug. I setup
pegjsvia the pegjs-loader. It operates on the JS API under the hood callingparser.generateit also leads to the same error.Many thanks for the project by the way!