Skip to content

Commit

Permalink
Add test coverage, remove padEnd from generated source
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Apr 30, 2021
1 parent 1c67b8a commit a69730a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
21 changes: 17 additions & 4 deletions lib/compiler/passes/generate-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,14 @@ function generateJS(ast, options) {
"",
"peg$subclass(peg$SyntaxError, Error);",
"",
"function peg$padEnd(str, targetLength, padString) {",
" padString = padString || \" \";",
" if (str.length > targetLength) { return str; }",
" targetLength -= str.length;",
" padString += padString.repeat(targetLength);",
" return str + padString.slice(0, targetLength);",
"}",
"",
"peg$SyntaxError.prototype.format = function(sources) {",
" var str = \"Error: \" + this.message;",
" if (this.location) {",
Expand All @@ -811,15 +819,20 @@ function generateJS(ast, options) {
" }",
" var maxLine = this.location.start.line.toString().length;",
" if (!srcLines[this.location.source]) {",
" str += \"\\n at \" + this.location.source + \":\" + this.location.start.line + \":\" + this.location.start.column;",
" str += \"\\n at \" + this.location.source + \":\" + this.location.start.line + \":\"",
" + this.location.start.column;",
" } else {",
" var line = srcLines[this.location.source][this.location.start.line - 1];",
" str += \"\\n --> \" + this.location.source + \":\" + this.location.start.line + \":\" + this.location.start.column + \"\\n\" + \"\".padEnd(maxLine) + \" |\\n\" + this.location.start.line.toString().padEnd(maxLine) + \" | \" + line + \"\\n\" + \"\".padEnd(maxLine) + \" | \" + \"\".padEnd(this.location.start.column - 1);",
" str += \"\\n --> \" + this.location.source + \":\" + this.location.start.line + \":\"",
" + this.location.start.column + \"\\n\" + peg$padEnd(\"\",maxLine)",
" + \" |\\n\" + peg$padEnd(this.location.start.line.toString(), maxLine)",
" + \" | \" + line + \"\\n\" + peg$padEnd(\"\", maxLine) + \" | \"",
" + peg$padEnd(\"\", this.location.start.column - 1);",
" if (this.location.start.line === this.location.end.line) {",
" str += \"\".padEnd(this.location.end.column - this.location.start.column, \"^\");",
" str += peg$padEnd(\"\", this.location.end.column - this.location.start.column, \"^\");",
" }",
" else {",
" str += \"\".padEnd(line.length - this.location.start.column + 1, \"^\");",
" str += peg$padEnd(\"\", line.length - this.location.start.column + 1, \"^\");",
" }",
" }",
" }",
Expand Down
21 changes: 17 additions & 4 deletions lib/parser.js

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

11 changes: 11 additions & 0 deletions test/behavior/generated-parser-behavior.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,17 @@ Error: Expected "{", code block, comment, end of line, identifier, or whitespace
throw er;
}
}).to.throw(peg.parser.SyntaxError);

expect(function() {
try {
peg.generate("===", { grammarSource: "stdin" });
} catch (er) {
expect(er.format({})).to.equal(`\
Error: Expected "{", code block, comment, end of line, identifier, or whitespace but "=" found.
at stdin:1:1`);
throw er;
}
}).to.throw(peg.parser.SyntaxError);
});
});
});
10 changes: 10 additions & 0 deletions test/unit/grammar-error.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ describe("Grammar Errors", () => {
GrammarError: message
at 1:1`);

e = new GrammarError("message", null, [{ message: "Subinfo", location }]);
expect(e.location).to.equal(null);
expect(e.toString()).to.equal(`\
GrammarError: message
from 1:1: Subinfo`);

location.source = "foo.peggy";
e = new GrammarError("message", location, [{ message: "Subinfo", location }]);
expect(e.toString()).to.equal(`\
Expand Down Expand Up @@ -57,6 +63,10 @@ Error: message
1 | some error
| ^^^^^
note: Subinfo`);
expect (e.format({})).to.equal(`\
Error: message
at foo.peggy:1:1
at foo.peggy:1:6: Subinfo`);

e = new GrammarError("message", null, [{
message: "Subinfo",
Expand Down

0 comments on commit a69730a

Please sign in to comment.