peggyjs / peggy

Peggy: Parser generator for JavaScript

Home Page:https://peggyjs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Failed to compile grammar containing imports

damienflament opened this issue · comments

Hi,

My grammar is now too big to be kept in a single file. I gave a try to the import mechanism but the grammar failed to compile.

So I tested with two simple files and got the same error:

main.pegjs:

import { Digit } from 'lib.js'

Number = Digit+

lib.js:

Digit = [0-9]
$ yarn peggy lib.pegjs
$ yarn peggy main.pegjs
Error parsing grammar
error: Possible infinite loop when parsing (repetition used with an expression that may not consume any input)
 --> main.pegjs:3:10
  |
3 | Number = Digit+
  |          ^^^^^^
$ yarn peggy --version
4.0.2

What am I doing wrong ?

This is a real problem with how we're currently doing imports. I think I might have marked all imported rules as "might consume nothing", rather than marking each one with the correct information.

I think I know how to fix this, so assigning it to myself.

In the meantime, you should be able to achieve your goals by removing the import statement and compiling with peggy main.pegjs lib.pegjs.

In order to solve this, I think we're going to need to either import or otherwise read in the library grammar when generating the main grammar. That likely means that we're going to need a new async top-level entrypoint.

This is something that has been contemplated in the past, but is going to require some discussion. Join us on the Peggy Discord if you want to follow along.