diff --git a/src/errors.js b/src/errors.js index 96675c5..3d1c252 100644 --- a/src/errors.js +++ b/src/errors.js @@ -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({ diff --git a/src/parser.js b/src/parser.js index 38b18bf..53e2e8f 100644 --- a/src/parser.js +++ b/src/parser.js @@ -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; @@ -15,7 +16,6 @@ class Visitor { visitRuleList(ctx) { const rules = []; const nonTerminals = new Set(); - const warnings = []; let startSymbol = undefined; ctx.children.forEach(child => { @@ -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); }); diff --git a/src/warnings.js b/src/warnings.js new file mode 100644 index 0000000..ba74fb3 --- /dev/null +++ b/src/warnings.js @@ -0,0 +1,9 @@ +class DuplicatedRuleWarning { + constructor(nonTerminal, index) { + this.message = 'Duplicated rule'; + this.nonTerminal = nonTerminal; + this.index = index; + } +} + +module.exports = Object.freeze({ DuplicatedRuleWarning }); \ No newline at end of file diff --git a/test/test-parser.js b/test/test-parser.js index 837894d..5a274b1 100644 --- a/test/test-parser.js +++ b/test/test-parser.js @@ -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;` @@ -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)], }); }); @@ -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), ], }); }); @@ -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)], }); }); \ No newline at end of file