Skip to content

Commit

Permalink
Add DuplicatedRuleWarning class
Browse files Browse the repository at this point in the history
  • Loading branch information
imcatta committed Jan 21, 2020
1 parent c7b51c4 commit 344d036
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/errors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class LexerError extends Error { }
class ParserError extends Error { }
class SemanticError extends Error {}
class SemanticError extends Error { }
class StartSymbolNotFound extends ParserError { }

module.exports = Object.freeze({
Expand Down
5 changes: 3 additions & 2 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const antlr4 = require('antlr4');
const GrammarlangLexer = require('../grammarlang/grammarlangLexer').grammarlangLexer;
const GrammarlangParser = require('../grammarlang/grammarlangParser').grammarlangParser;
const errors = require('./errors');
const DuplicatedRuleWarning = require('./warnings').DuplicatedRuleWarning;

const NONTERMINAL = 0;
const TERMINAL = 1;
Expand All @@ -15,7 +16,6 @@ class Visitor {
visitRuleList(ctx) {
const rules = [];
const nonTerminals = new Set();
const warnings = [];
let startSymbol = undefined;

ctx.children.forEach(child => {
Expand Down Expand Up @@ -60,12 +60,13 @@ class Visitor {
});

// checks for duplicates
const warnings = [];
Object.keys(grammar).forEach(nonTerminal => {
const tmpRules = [];
grammar[nonTerminal].forEach((rule, index) => {
const equalToRule = v => JSON.stringify(v) === JSON.stringify(rule);
if (tmpRules.some(equalToRule)) {
warnings.push({ message: 'Duplicated rule', nonTerminal, index });
warnings.push(new DuplicatedRuleWarning(nonTerminal, index));
}
tmpRules.push(rule);
});
Expand Down
9 changes: 9 additions & 0 deletions src/warnings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class DuplicatedRuleWarning {
constructor(nonTerminal, index) {
this.message = 'Duplicated rule';
this.nonTerminal = nonTerminal;
this.index = index;
}
}

module.exports = Object.freeze({ DuplicatedRuleWarning });
11 changes: 5 additions & 6 deletions test/test-parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import test from 'ava';
const parser = require('../src/parser.js');
const errors = require('../src/errors');
const warnings = require('../src/warnings');

test('simple case', t => {
const input = `S -> a S;`
Expand Down Expand Up @@ -210,7 +211,7 @@ test('duplicated rule case 1', t => {
rulesNumber: 2,
terminals: ['a'],
nonTerminals: ['S'],
warnings: [{ message: 'Duplicated rule', nonTerminal: 'S', index: 1 }],
warnings: [new warnings.DuplicatedRuleWarning('S', 1)],
});
});

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

0 comments on commit 344d036

Please sign in to comment.