peggyjs / peggy

Peggy: Parser generator for JavaScript
https://peggyjs.org/
MIT License
883 stars 63 forks source link

Failed to compile grammar containing imports #517

Open damienflament opened 1 month ago

damienflament commented 1 month ago

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 ?

hildjj commented 1 month ago

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.

hildjj commented 1 month ago

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

hildjj commented 1 month ago

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.