Browse Source

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 4 years ago
committed by Futago-za Ryuu
parent
commit
cb3c5f4473
  1. 500
      lib/parser.js
  2. 1
      src/parser.pegjs
  3. 15
      test/unit/parser.spec.js

500
lib/parser.js
File diff suppressed because it is too large
View File

1
src/parser.pegjs

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

15
test/unit/parser.spec.js

@ -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