Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow an option to specify a start offset #281

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion .jshintrc
Expand Up @@ -8,7 +8,9 @@
"latedef": "nofunc",
"laxbreak": true,
"noarg": true,
"node": true,
"noempty": true,
"nonew": true,
"trailing": true
"trailing": true,
"undef": true
}
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -132,9 +132,10 @@ the input is invalid. The exception will contain `offset`, `line`, `column`,
parser.parse("abcd"); // throws an exception

You can tweak parser behavior by passing a second parameter with an options
object to the `parse` method. Only one option is currently supported:
object to the `parse` method. Two options are currently supported:

* `startRule` — name of the rule to start parsing from
* `startOffset` — start parsing the input at this position

Parsers can also support their own custom options.

Expand Down
8 changes: 4 additions & 4 deletions lib/compiler/passes/generate-bytecode.js
Expand Up @@ -351,7 +351,7 @@ module.exports = function(ast) {
},

sequence: function(node, context) {
var emptyArrayIndex;
var emptyArrayIndex, failedIndex;

function buildElementsCode(elements, context) {
var processedCount, functionIndex;
Expand Down Expand Up @@ -477,7 +477,7 @@ module.exports = function(ast) {
},

zero_or_more: function(node, context) {
var emptyArrayIndex = addConst('[]');
var emptyArrayIndex = addConst('[]'),
expressionCode = generate(node.expression, {
sp: context.sp + 1,
env: { },
Expand All @@ -493,8 +493,8 @@ module.exports = function(ast) {
},

one_or_more: function(node, context) {
var emptyArrayIndex = addConst('[]');
failedIndex = addConst('peg$FAILED');
var emptyArrayIndex = addConst('[]'),
failedIndex = addConst('peg$FAILED'),
expressionCode = generate(node.expression, {
sp: context.sp + 1,
env: { },
Expand Down
4 changes: 2 additions & 2 deletions lib/compiler/passes/generate-javascript.js
Expand Up @@ -731,8 +731,8 @@ module.exports = function(ast, options) {

parts.push([
'',
' peg$currPos = 0,',
' peg$reportedPos = 0,',
' peg$currPos = options.startOffset || 0,',
' peg$reportedPos = options.startOffset || 0,',
' peg$cachedPos = 0,',
' peg$cachedPosDetails = { line: 1, column: 1, seenCR: false },',
' peg$maxFailPos = 0,',
Expand Down
4 changes: 2 additions & 2 deletions lib/parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions spec/generated-parser.spec.js
Expand Up @@ -173,6 +173,27 @@ describe("generated parser", function() {
});
});

describe("parse", function() {
var parser = PEG.buildParser([
'a = "ab" { return "ab"; }',
' / "b" { return "b"; }'
].join("\n"));

describe("start offset", function() {
describe("without the |startOffset| option", function() {
it("starts at the beginning", function() {
expect(parser).toParse("ab", "ab");
});
});

describe("when the |startOffset| option specifies a position", function() {
it("starts at the offset", function() {
expect(parser).toParse("ab", { startOffset: 1 }, "b");
});
});
});
});

varyAll(function(options) {
describe("initializer code", function() {
it("runs before the parsing begins", function() {
Expand Down