kach / nearley

šŸ“œšŸ”œšŸŒ² Simple, fast, powerful parser toolkit for JavaScript.
https://nearley.js.org
MIT License
3.59k stars 232 forks source link

Feature Request: includePaths option for nearleyc #604

Open hikerpig opened 2 years ago

hikerpig commented 2 years ago

Some background

I'm developing a text-to-diagram library, using nearley to generate parsers for different types of diagrams - which have quite different syntaxes so I tend to write separate parser for each one of them. I use @include to include common syntaxes.

Also this library is based on a plugin structure, developers can write their own diagrams, I think maybe I can publish the common syntaxes files and they can include them from node_modules.

But as far as I know, one can't include another .ne file without knowing the exact relative path of it. But writing something like @include "../../node_modules/@pintora/diagrams/lib/syntaxes/config.ne" is not so elegant, IMO.

Feature request

Here in Compile.js, I see there is either directory of current .ne file or process.cwd() for resolving include paths.

        if (!productionRule.builtin) {
            path = require('path').resolve(
                opts.args[0] ? require('path').dirname(opts.args[0]) : process.cwd(),
                productionRule.include
            );

Can we add an option --include-paths to nearleyc CLI for syntax file lookup ?

If this is not a feature that, for some reason, is deliberately not implemented. I will be happy to add it.