Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bin/nearley-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,3 @@ if (typeof(opts.input) === "undefined") {
if (!opts.quiet) writeTable(output, parser);
writeResults(output, parser);
}

1 change: 1 addition & 0 deletions bin/nearleyc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ opts.version(version, '-v, --version')
.arguments('<file.ne>')
.option('-o, --out [filename.js]', 'File to output to (defaults to stdout)', false)
.option('-e, --export [name]', 'Variable to set parser to', 'grammar')
.option('--include-paths [includePaths]', 'Paths to lookup include files')
.option('-q, --quiet', 'Suppress linter')
.option('--nojs', 'Do not compile postprocessors')
.parse(process.argv);
Expand Down
24 changes: 19 additions & 5 deletions lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
opts.alreadycompiled = [];
}

var nodePath = require('path');
var nodeFs = require('fs');
var includePaths = [];
if (opts.includePaths && opts.includePaths.split) {
includePaths = opts.includePaths.split(',').map(function(p) {
return nodePath.resolve(process.cwd(), p);
});
}
var firstIncludePath = (opts.args && opts.args[0]) ? nodePath.dirname(opts.args[0]) : process.cwd();
includePaths.push(firstIncludePath);

var result = {
rules: [],
body: [], // @directives list
Expand All @@ -33,12 +44,15 @@
// Include file
var path;
if (!productionRule.builtin) {
path = require('path').resolve(
opts.args[0] ? require('path').dirname(opts.args[0]) : process.cwd(),
productionRule.include
);
for (let i = 0; i < includePaths.length; i++) {
var pathToLookup = nodePath.resolve(includePaths[i], productionRule.include);
if (nodeFs.existsSync(pathToLookup)) {
path = pathToLookup;
break;
}
}
} else {
path = require('path').resolve(
path = nodePath.resolve(
__dirname,
'../builtin/',
productionRule.include
Expand Down
1 change: 1 addition & 0 deletions test/grammars/include-paths/include-example.ne
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@include "multi-include/a.ne"
8 changes: 7 additions & 1 deletion test/nearleyc.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

const fs = require('fs');
const expect = require('expect');
const path = require('path');

const nearley = require('../lib/nearley');
const {compile, evalGrammar, parse, nearleyc} = require('./_shared');
Expand Down Expand Up @@ -110,7 +111,11 @@ describe("bin/nearleyc", function() {
expect(stderr).toBe("");
});


it("accepets --include-paths", function () {
const includePath = path.join(__dirname, 'grammars');
const {stdout, stderr} = externalNearleyc("grammars/include-paths/include-example.ne", '.js', ['--include-paths', includePath]);
expect(stderr).toBe("");
});
})

describe('nearleyc: example grammars', function() {
Expand Down Expand Up @@ -219,6 +224,7 @@ describe('nearleyc: builtins', () => {
const g = evalGrammar(source)
expect(parse(g, " ")).toEqual([null])
})

})

describe('nearleyc: macros', () => {
Expand Down