Skip to content

Commit

Permalink
Add sameAs field to DuplicatedRuleWarning
Browse files Browse the repository at this point in the history
  • Loading branch information
imcatta committed Jan 21, 2020
1 parent 63b5e2a commit e2271e4
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ll1-validator",
"version": "0.2.1",
"version": "0.2.2",
"description": "A tool that checks if a given grammar is LL(1).",
"main": "src/index.js",
"scripts": {
Expand Down
5 changes: 3 additions & 2 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ class Visitor {
const tmpRules = [];
grammar[nonTerminal].forEach((rule, index) => {
const equalToRule = v => JSON.stringify(v) === JSON.stringify(rule);
if (tmpRules.some(equalToRule)) {
result.push(new warnings.DuplicatedRuleWarning(nonTerminal, index));
const sameAs = tmpRules.findIndex(equalToRule);
if (sameAs !== -1) {
result.push(new warnings.DuplicatedRuleWarning(nonTerminal, index, sameAs));
}
tmpRules.push(rule);
});
Expand Down
3 changes: 2 additions & 1 deletion src/warnings.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class DuplicatedRuleWarning {
constructor(nonTerminal, index) {
constructor(nonTerminal, index, sameAs) {
this.message = 'Duplicated rule';
this.nonTerminal = nonTerminal;
this.index = index;
this.sameAs = sameAs;
}
}

Expand Down
8 changes: 4 additions & 4 deletions test/test-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ test('duplicated rule case 1', t => {
rulesNumber: 2,
terminals: ['a'],
nonTerminals: ['S'],
warnings: [new warnings.DuplicatedRuleWarning('S', 1)],
warnings: [new warnings.DuplicatedRuleWarning('S', 1, 0)],
});
});

Expand Down Expand Up @@ -249,8 +249,8 @@ test('duplicated rule case 2', t => {
terminals: ['a', 'b'],
nonTerminals: ['A', 'S'],
warnings: [
new warnings.DuplicatedRuleWarning('S', 2),
new warnings.DuplicatedRuleWarning('S', 3),
new warnings.DuplicatedRuleWarning('S', 2, 0),
new warnings.DuplicatedRuleWarning('S', 3, 0),
],
});
});
Expand All @@ -277,7 +277,7 @@ test('duplicated rule case 3', t => {
rulesNumber: 3,
terminals: ['a'],
nonTerminals: ['A', 'S'],
warnings: [new warnings.DuplicatedRuleWarning('A', 1)],
warnings: [new warnings.DuplicatedRuleWarning('A', 1, 0)],
});
});

Expand Down

0 comments on commit e2271e4

Please sign in to comment.