kach / nearley

📜🔜🌲 Simple, fast, powerful parser toolkit for JavaScript.
https://nearley.js.org
MIT License
3.57k stars 231 forks source link

nearley-unparser binary: rules.map is not a function #620

Closed the-jasoney closed 1 year ago

the-jasoney commented 2 years ago

I am using nearley with the typescript preprocessor and I get this error when I try to use nearley.test:

TypeError: rules.map is not a function
    at Grammar.fromCompiled (C:\Users\jasoney\Documents\GitHub\kun\node_modules\nearley\lib\nearley.js:172:27)
    at Unparse (C:\Users\jasoney\Documents\GitHub\kun\node_modules\nearley\lib\unparse.js:128:43)
    at Object.<anonymous> (C:\Users\jasoney\Documents\GitHub\kun\node_modules\nearley\bin\nearley-unparse.js:27:18)
    at Module._compile (node:internal/modules/cjs/loader:1112:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1166:10)
    at Module.load (node:internal/modules/cjs/loader:988:32)
    at Module._load (node:internal/modules/cjs/loader:834:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

Node.js v18.4.0

nearley-unparse seems to be working fine with a JS grammar.

audreyseo commented 1 year ago

I also get the same error, and I'm also using the typescript preprocessor:

TypeError: rules.map is not a function
    at Function.Grammar.fromCompiled (/Users/audrey/.nvm/versions/node/v17.4.0/lib/node_modules/nearley/lib/nearley.js:172:27)
    at Object.<anonymous> (/Users/audrey/.nvm/versions/node/v17.4.0/lib/node_modules/nearley/bin/nearley-test.js:27:31)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

Node.js v17.4.0
audreyseo commented 1 year ago

It turns out that the problem was that rules was of the form:

{
  default: { ... }
}

so locally, adding

if ("default" in rules) {
  rules = rules.default
}

at the top of Grammar.fromCompiled did the trick.

the-jasoney commented 1 year ago

For any people looking for the answer to this problem: this repo seems to be dead, so the only way to solve this problem is to implement the above fix yourself.