kach / nearley

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

Possible to use Nearley parser with node version 10? #564

Closed gene-hightower closed 3 years ago

gene-hightower commented 3 years ago

Nearley produces output that uses array.flat() which doesn't exist in node < 11. Nearley installs just fine, it compiles the grammar.js file, but then when using the parser the code crashes because .flat doesn't exist.

Is there a way to make this work?

bd82 commented 3 years ago

Just wondering, does it really matter when Nodejs 10 will reach EOL in (end of?) April?

msimerson commented 3 years ago

does it really matter when Nodejs 10 will reach EOL in (end of?) April?

It almost matters. For our primary project, I have few qualms about dropping node 10 support early. However, because our module that uses nearley has users outside our ecosystem, I felt compelled to not use our nearley based version until node 10 goes EOL.

One thing that would help new users of nearley would be declaring the minimum version of node you require. Adding this to package.json would do so:

  "engines": {
    "node": ">=12.20.1"
  },

We contemplated adding a polyfill for Array.flat() to the compiled .js file. That's always something your compiler can do if your toolchain introduces features that aren't in supported versions of node.js.

Finally, it appears that based on your present testing of just node 6 and 8, you'd never know that your compiled grammars won't actually run on those versions of node. It would seem like a very good idea to compile some sample grammars during your tests and exercise them to assure they don't crash.

bd82 commented 3 years ago

It almost matters.

It guess its a race between nodejs 10 EOL and nearley maintainers for producing a patch or making this issue redundant 😄

kach commented 3 years ago

Sorry, could you give an example of a file that compiles to something containing Array.flat()?

We contemplated adding a polyfill for Array.flat() to the compiled .js file.

Seems like a reasonable solution?

msimerson commented 3 years ago

From: grammer.ne in node-address-rfc2821

generated grammar

// Generated automatically by nearley, version 2.20.1
// http://github.com/Hardmath123/nearley
(function () {
function id(x) { return x[0]; }

function flat_string(d) {
  if (d) {
    if (Array.isArray(d))
      return d.flat(Infinity).join("");
    return d;
  }
  return "";
}
var grammar = {
    Lexer: undefined,
    ParserRules: [
    {"name": "main", "symbols": ["Mailbox"]},
<snip>
    {"name": "non_local_part", "symbols": ["address_literal"]},
    {"name": "Mailbox", "symbols": ["Local_part", {"literal":"@"}, "non_local_part"], "postprocess": 
        function(d) {
          return { local_part: flat_string(d[0]), domain: flat_string(d[2]), };
        }
                          },
    {"name": "Local_part", "symbols": ["Dot_string"]},
    {"name": "Local_part", "symbols": ["Quoted_string"]},
<snip>
    {"name": "ALPHA_DIG_DASH", "symbols": [/[-0-9A-Za-z]/], "postprocess": id},
    {"name": "ALPHA_DIG_DASH_U", "symbols": [/[-0-9A-Za-z\u0080-\uFFFF]/], "postprocess": id},
    {"name": "HEXDIG", "symbols": [/[0-9A-Fa-f]/], "postprocess": id},
    {"name": "DQUOTE", "symbols": [{"literal":"\""}], "postprocess": id}
]
  , ParserStart: "main"
}
if (typeof module !== 'undefined'&& typeof module.exports !== 'undefined') {
   module.exports = grammar;
} else {
   window.grammar = grammar;
}
})();
msimerson commented 3 years ago

Hey @gene-hightower, in looking in the grammar.ne file, you have declared flat_string(), which is where the call to Array.flat() is coming from. Wouldn't that also be the place to polyfill flat()?

gene-hightower commented 3 years ago

I guess so, how do I do that?

msimerson commented 3 years ago

Like this

kach commented 3 years ago

Indeed, it seems like this isn't a nearley issue so I'm closing this issue. But feel free to continue conversing, here or on https://github.com/haraka/node-address-rfc2821/issues/39 or the PR from @msimerson.