Skip to content

Commit

Permalink
Import (pegjs/pegjs#38): Add testcases for import clause and regenera…
Browse files Browse the repository at this point in the history
…te parser
  • Loading branch information
Mingun committed Jun 11, 2022
1 parent 4ed472e commit 131574c
Show file tree
Hide file tree
Showing 8 changed files with 1,012 additions and 524 deletions.
1,132 changes: 674 additions & 458 deletions lib/parser.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions test/behavior/generated-parser-behavior.spec.js
Expand Up @@ -2897,7 +2897,7 @@ describe("generated parser behavior", () => {
} catch (er) {
expect(er).to.be.an.instanceof(peg.parser.SyntaxError);
expect(er.format([source])).to.equal(`\
Error: Expected "{", code block, comment, end of line, identifier, or whitespace but "=" found.
Error: Expected "import", "{", code block, comment, end of line, identifier, or whitespace but "=" found.
--> stdin:1:1
|
1 | ===
Expand All @@ -2909,7 +2909,7 @@ Error: Expected "{", code block, comment, end of line, identifier, or whitespace
} catch (er) {
expect(er).to.be.an.instanceof(peg.parser.SyntaxError);
expect(er.format([])).to.equal(`\
Error: Expected "{", code block, comment, end of line, identifier, or whitespace but "=" found.
Error: Expected "import", "{", code block, comment, end of line, identifier, or whitespace but "=" found.
at stdin:1:1`);
}
});
Expand Down
24 changes: 24 additions & 0 deletions test/types/peg.test-d.ts
Expand Up @@ -190,12 +190,16 @@ describe("peg.d.ts", () => {
expectType<peggy.ast.Grammar>(node);
expectType<"grammar">(node.type);
expectType<peggy.LocationRange>(node.location);
expectType<peggy.ast.Import[]>(node.imports);
expectType<peggy.ast.TopLevelInitializer | undefined>(
node.topLevelInitializer
);
expectType<peggy.ast.Initializer | undefined>(node.initializer);
expectType<peggy.ast.Rule[]>(node.rules);

if (node.imports) {
node.imports.forEach(visit);
}
if (node.topLevelInitializer) {
visit(node.topLevelInitializer);
}
Expand All @@ -204,6 +208,26 @@ describe("peg.d.ts", () => {
}
node.rules.forEach(visit);
},
import(node) {
add(node.type);
expectType<peggy.ast.Import>(node);
expectType<"import">(node.type);
expectType<peggy.LocationRange>(node.location);
expectType<string>(node.path);
expectType<peggy.ast.ImportedRule[]>(node.rules);

node.rules.forEach(visit);
},
imported_rule(node) {
add(node.type);
expectType<peggy.ast.ImportedRule>(node);
expectType<"imported_rule">(node.type);
expectType<peggy.LocationRange>(node.location);
expectType<string>(node.name);
expectType<string>(node.alias);
expectType<peggy.LocationRange>(node.nameLocation);
expectType<peggy.LocationRange | null>(node.aliasLocation);
},
top_level_initializer(node) {
add(node.type);
expectType<peggy.ast.TopLevelInitializer>(node);
Expand Down
9 changes: 9 additions & 0 deletions test/unit/compiler/passes/generate-bytecode.spec.js
Expand Up @@ -1749,6 +1749,15 @@ describe("compiler pass |generateBytecode|", () => {
{},
],
});

expect(pass).to.changeAST([
"import { imported } from '1';",
"start = imported",
].join("\n"), {
rules: [{
bytecode: [43, 0, 0], // IMPORTED_RULE <module '1'> <rule 0>
}],
});
});
});

Expand Down
38 changes: 38 additions & 0 deletions test/unit/compiler/passes/remove-proxy-rules.spec.js
Expand Up @@ -28,6 +28,23 @@ describe("compiler pass |removeProxyRules|", () => {
},
{ allowedStartRules: ["start"] }
);

expect(pass).to.changeAST(
[
"import { imported } from '';",
"start = proxy",
"proxy = imported",
].join("\n"),
{
rules: [
{
name: "start",
expression: { type: "rule_ref", name: "imported" },
},
],
},
{ allowedStartRules: ["start"] }
);
});
});

Expand All @@ -54,6 +71,27 @@ describe("compiler pass |removeProxyRules|", () => {
},
{ allowedStartRules: ["start", "proxy"] }
);

expect(pass).to.changeAST(
[
"import { imported } from '';",
"start = proxy",
"proxy = imported",
].join("\n"),
{
rules: [
{
name: "start",
expression: { type: "rule_ref", name: "imported" },
},
{
name: "proxy",
expression: { type: "rule_ref", name: "imported" },
},
],
},
{ allowedStartRules: ["start", "proxy"] }
);
});
});
});
44 changes: 44 additions & 0 deletions test/unit/compiler/passes/report-duplicate-rules.spec.js
Expand Up @@ -30,4 +30,48 @@ describe("compiler pass |reportDuplicateRules|", () => {
}],
});
});

it("reports duplicate imports", () => {
expect(pass).to.reportError([
"import { rule, rule } from '';",
"start = 'a'",
].join("\n"), {
message: "Rule \"rule\" is already imported",
location: {
source: undefined,
start: { offset: 15, line: 1, column: 16 },
end: { offset: 19, line: 1, column: 20 },
},
diagnostics: [{
message: "Original import location",
location: {
source: undefined,
start: { offset: 9, line: 1, column: 10 },
end: { offset: 13, line: 1, column: 14 },
},
}],
});
});

it("reports overrides of imported rules", () => {
expect(pass).to.reportError([
"import { start } from '';",
"start = 'a'",
].join("\n"), {
message: "Rule with the same name \"start\" is already defined in the grammar, try to add `as <alias_name>` to the imported one",
location: {
source: undefined,
start: { offset: 9, line: 1, column: 10 },
end: { offset: 14, line: 1, column: 15 },
},
diagnostics: [{
message: "Rule defined here",
location: {
source: undefined,
start: { offset: 26, line: 2, column: 1 },
end: { offset: 31, line: 2, column: 6 },
},
}],
});
});
});
12 changes: 12 additions & 0 deletions test/unit/compiler/passes/report-undefined-rules.spec.js
Expand Up @@ -19,4 +19,16 @@ describe("compiler pass |reportUndefinedRules|", () => {
},
});
});

it("do not report imported rules as undefined", () => {
expect(pass).to.not.reportError([
"import { imported } from '';",
"start = imported;",
].join("\n"));

expect(pass).to.not.reportError(
"import { imported } from ''; start = 'a'",
{ allowedStartRules: ["imported"] }
);
});
});

0 comments on commit 131574c

Please sign in to comment.