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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,18 @@ start
= grammar
```

When you ```@import``` another grammar, that grammar's topmost rule becomes accessible in the context of the current grammar under one of two possible rule names:
When you ```@import``` another grammar, that grammar's topmost rule becomes accessible in the context of the current grammar under one of two possible rule names:
- A name you provide explicitly through the use of the optional "as" parameter
- The basename of the file, corrected to be a valid Javascript identifier (so if the file were called /foo/bar/my-grammar.peg, the rule name would be my_grammar).

pegjs-import will cache rules it has parsed. You can clear this local cache by calling `clearCache`:
```javascript
var pegimport = require('pegjs-import');

pegimport.clearCache();
```


## Changes from stock PEG.js behavior

* You cannot supply ```options.allowedStartRules``` anymore. The only allowed start rule is always the first rule in the grammar.
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,9 @@ function buildParser(filename, options) {
return newParser;

};
module.exports = { buildParser: buildParser };

function clearCache() {
parsers = {};
}

module.exports = { buildParser: buildParser, clearCache: clearCache };
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
},
"devDependencies": {
"chai": "^1.9.2",
"mocha": "^1.21.5"
"mocha": "^1.21.5",
"mock-fs": "^3.8.0"
},
"author": "Harry Schmidt",
"license": "MIT",
Expand Down
44 changes: 42 additions & 2 deletions test/spec/core.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

'use strict';

var pegimport = require('../../index'),
peg = require('pegjs');
peg = require('pegjs'),
path = require('path'),
mockFs = require('mock-fs');

describe('peg-import', function() {

Expand Down Expand Up @@ -66,4 +67,43 @@ describe('peg-import', function() {

});

describe('caching', function() {
var absoluteFilename = path.resolve('test/fixtures/import.peg')

afterEach(function() {
mockFs.restore();
});

function enableMock() {
var stubbedFixture = 'start = a:.* { return "abc" }';

mockFs({
'test/fixtures': {
'import.peg': stubbedFixture
}
});
}

function result() {
return pegimport.buildParser(absoluteFilename).parse('123');
}

it('works with absolute filenames', function() {
expect(result()).to.deep.equal(['1', '2', '3']);

enableMock();

expect(result()).to.not.equal('abc');
});

it('can be cleared', function() {
expect(result()).to.deep.equal(['1', '2', '3']);

enableMock();
pegimport.clearCache();

expect(result()).to.equal('abc');
});

});
});