Skip to content

Commit

Permalink
Add more SourceMap tests to pegjs-api.spec.js
Browse files Browse the repository at this point in the history
  • Loading branch information
markw65 committed Mar 30, 2022
1 parent f05d42c commit 4a0a2b6
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions test/api/pegjs-api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ describe("Peggy API", () => {

describe("generates source map", () => {
function findLocationOf(input, chunk) {
const offset = input.indexOf(chunk);
const offset = chunk instanceof RegExp
? input.search(chunk)
: input.indexOf(chunk);
let line = 1;
let column = 0;

Expand All @@ -235,8 +237,9 @@ describe("Peggy API", () => {
const SOURCE = `
{{${GLOBAL_INITIALIZER}}}
{${PER_PARSE_INITIALIZER}}
RULE_1 = !{${NOT_BLOCK}} 'a' rule:RULE_2 {${ACTION_BLOCK}};
RULE_2 'named' = &{${AND_BLOCK}} @'b';
RULE_1 = !{${NOT_BLOCK}} 'a' rule:RULE_2 {${ACTION_BLOCK}}
RULE_2 'named' = &{${AND_BLOCK}} @'b' [abc] 'def'
RULE_3 = RULE_1 / RULE_2
`;

function check(chunk, source, name, generatedChunk = chunk) {
Expand Down Expand Up @@ -271,6 +274,21 @@ describe("Peggy API", () => {

it("rule name", () => check("RULE_1", source, "RULE_1", "peg$parseRULE_1() {"));
it("labelled rule name", () => check("RULE_2 'named'", source, "RULE_2", "peg$parseRULE_2() {"));
it("literal expression", () => check("'a'", source, null, "input.charCodeAt(peg$currPos) === 97"));
it("multichar literal", () => check("'def'", source, null, "input.substr(peg$currPos, 3) === peg$c2"));
it("chars expression", () => check("[abc]", source, null, "peg$r0.test(input.charAt(peg$currPos))"));
it("rule expression", () => check("RULE_2", source, null, "peg$parseRULE_2();"));
it("choice expression", () => check(
"RULE_1 / RULE_2",
source,
null,
new RegExp([
// Lint complained about a long regex, so split and join.
/peg\$parseRULE_1\(\);\s*/,
/if \(s. === peg\$FAILED\) \{\s*/,
/s. = peg\$parseRULE_2/,
].map(r => r.source).join(""))
));
});
}
});
Expand Down

0 comments on commit 4a0a2b6

Please sign in to comment.