Improve error message for unbalanced brace. (#534)

Currently, an open brace without a corresponding brace will emit this confusing error message:

> Expected "!", "$", "&", "(", "*", "+", ".", "/", "/*", "//", ";", "?", character class, code block, comment, end of line, identifier, literal, or whitespace but "{" found.

This change adds an error case to the grammar to make it clear what the problem is.
master
felix 7 years ago committed by Futago-za Ryuu
parent 369b8cdcc6
commit cb3c5f4473

File diff suppressed because one or more lines are too long

@ -437,6 +437,7 @@ AnyMatcher
CodeBlock "code block"
= "{" code:Code "}" { return code; }
/ "{" { error("Unbalanced brace."); }
Code
= $((![{}] SourceCharacter)+ / "{" Code "}")*

@ -5,6 +5,9 @@ let parser = require("../../lib/parser");
let expect = chai.expect;
// better diagnostics for deep eq failure
chai.config.truncateThreshold = 0;
describe("PEG.js grammar parser", function() {
let literalAbcd = { type: "literal", value: "abcd", ignoreCase: false };
let literalEfgh = { type: "literal", value: "efgh", ignoreCase: false };
@ -657,4 +660,16 @@ describe("PEG.js grammar parser", function() {
it("parses EOF", function() {
expect("start = 'abcd'\n").to.parseAs(trivialGrammar);
});
it("reports unmatched brace", function() {
const text = "rule = \n 'x' { y \n z";
const errorLocation = {
start: { offset: 13, line: 2, column: 6 },
end: { offset: 14, line: 2, column: 7 }
};
expect(() => parser.parse(text))
.to.throw("Unbalanced brace.")
.with.property("location")
.that.deep.equals(errorLocation);
});
});

Loading…
Cancel
Save