From 3f01cccb53b67814aee7080d221cbabfe56d94af Mon Sep 17 00:00:00 2001 From: Nathan Date: Sat, 18 Aug 2018 10:23:41 -0400 Subject: [PATCH 1/3] Fix reUnion() for empty regex lists Return (?!), which never matches anything. --- moo.js | 1 + 1 file changed, 1 insertion(+) diff --git a/moo.js b/moo.js index 2cdb0d2..d1939e6 100644 --- a/moo.js +++ b/moo.js @@ -28,6 +28,7 @@ return '(' + s + ')' } function reUnion(regexps) { + if (!regexps.length) return '(?!)' var source = regexps.map(function(s) { return "(?:" + s + ")" }).join('|') From 6ee26215b8289c3624a0528836c3b685cbfe9436 Mon Sep 17 00:00:00 2001 From: Nathan Date: Sat, 18 Aug 2018 10:34:01 -0400 Subject: [PATCH 2/3] Test: handles empty rule set --- test/test.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/test.js b/test/test.js index 3d811fc..58c866e 100644 --- a/test/test.js +++ b/test/test.js @@ -10,7 +10,15 @@ function lexAll(lexer) {return Array.from(lexer)} describe('compiler', () => { - // TODO handles empty rule set + test('handles empty rule set', () => { + const lex = compile({}) + lex.reset('nope!') + expect(() => lex.next()).toThrow('invalid syntax') + + const lex2 = compile({err: moo.error}) + lex2.reset('nope!') + expect(lex2.next()).toMatchObject({type: 'err', text: 'nope!'}) + }) test("warns for /g, /y, /i, /m", () => { expect(() => compile({ word: /foo/ })).not.toThrow() From bc9ff58aa0330ac0c481a6520dcc369f2e195dcb Mon Sep 17 00:00:00 2001 From: Nathan Date: Sat, 18 Aug 2018 10:35:26 -0400 Subject: [PATCH 3/3] Test: stateful compiler handles empty rule set --- test/test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/test.js b/test/test.js index 58c866e..461b0f8 100644 --- a/test/test.js +++ b/test/test.js @@ -18,6 +18,14 @@ describe('compiler', () => { const lex2 = compile({err: moo.error}) lex2.reset('nope!') expect(lex2.next()).toMatchObject({type: 'err', text: 'nope!'}) + + const lex3 = moo.states({main: {}}) + lex3.reset('nope!') + expect(() => lex3.next()).toThrow('invalid syntax') + + const lex4 = moo.states({main: {err: moo.error}}) + lex4.reset('nope!') + expect(lex4.next()).toMatchObject({type: 'err', text: 'nope!'}) }) test("warns for /g, /y, /i, /m", () => {