From ee9a05d3c4fbdcb34777ae75a9ae1499b1098add Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Tue, 22 Aug 2017 15:15:07 -0400 Subject: Change arithmetic lexing - Rename tokens from symbols ('+', '-'), to words - Differentiate between symbols with and without surrounding whitespace - Update parser to work with new token names --- __tests__/lexer.test.js | 14 +++++++------- __tests__/parser.test.js | 4 ++-- src/lexer.js | 5 +++-- src/parser.js | 6 +++--- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/__tests__/lexer.test.js b/__tests__/lexer.test.js index 04a95e1..76cb421 100644 --- a/__tests__/lexer.test.js +++ b/__tests__/lexer.test.js @@ -55,7 +55,7 @@ describe('lex', () => { { type: 'constant', value: 1 }, { type: 'd' }, { type: 'constant', value: 6 }, - { type: '+' }, + { type: 'bigPlus' }, { type: 'constant', value: 1 }, { type: 'd' }, { type: 'constant', value: 4 } @@ -67,7 +67,7 @@ describe('lex', () => { { type: 'constant', value: 2 }, { type: 'd' }, { type: 'constant', value: 17 }, - { type: '+' }, + { type: 'bigPlus' }, { type: 'constant', value: 4 } ]) }) @@ -79,7 +79,7 @@ describe('lex', () => { { type: 'constant', value: 1 }, { type: 'd' }, { type: 'constant', value: 6 }, - { type: '-' }, + { type: 'bigMinus' }, { type: 'constant', value: 1 }, { type: 'd' }, { type: 'constant', value: 4 } @@ -91,7 +91,7 @@ describe('lex', () => { { type: 'constant', value: 2 }, { type: 'd' }, { type: 'constant', value: 17 }, - { type: '-' }, + { type: 'bigMinus' }, { type: 'constant', value: 4 } ]) }) @@ -110,13 +110,13 @@ describe('lex', () => { ]) }) - it('2d(6+3)d4', () => { - expect(lex('2d(6+3)d4')).toEqual([ + it('2d(6 + 3)d4', () => { + expect(lex('2d(6 + 3)d4')).toEqual([ { type: 'constant', value: 2 }, { type: 'd' }, { type: '(' }, { type: 'constant', value: 6 }, - { type: '+' }, + { type: 'bigPlus' }, { type: 'constant', value: 3 }, { type: ')' }, { type: 'd' }, diff --git a/__tests__/parser.test.js b/__tests__/parser.test.js index fb554f9..bc88894 100644 --- a/__tests__/parser.test.js +++ b/__tests__/parser.test.js @@ -152,8 +152,8 @@ describe('parse', () => { }) }) - test('2d(6+3)d4', () => { - expect(parse('2d(6+3)d4')).toEqual({ + test('2d(6 + 3)d4', () => { + expect(parse('2d(6 + 3)d4')).toEqual({ type: 'd', left: { type: 'constant', value: 2 }, right: { diff --git a/src/lexer.js b/src/lexer.js index fd0d0a3..5831541 100644 --- a/src/lexer.js +++ b/src/lexer.js @@ -20,8 +20,9 @@ const newSkippableLexeme = (type, regex) => { newValueLexeme('constant', '\\d+', Number) newLexemeType('d', 'd') -newLexemeType('+', '\\+') -newLexemeType('-', '-') +newLexemeType('bigPlus', ' \\+ ' ) +newLexemeType('bigMinus', ' - ') +newLexemeType('minus', '-') newLexemeType('(', '\\(') newLexemeType(')', '\\)') newLexemeType('E', 'E') diff --git a/src/parser.js b/src/parser.js index fd974bc..588f480 100644 --- a/src/parser.js +++ b/src/parser.js @@ -63,9 +63,9 @@ newSymbol('d', (parser) => { newDieOperation('E') newDieOperation('K') -newInfix('+', 20, { type: 'add' }) -newInfix('-', 20, { type: 'subtract' }) -newSymbol('-', (parser) => { +newInfix('bigPlus', 20, { type: 'add' }) +newInfix('bigMinus', 20, { type: 'subtract' }) +newSymbol('minus', (parser) => { return { type: 'negative', value: parser.expression(40) -- cgit v1.2.3 From b84fe7672ad62a7e507b10704fbbd216afaaff44 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Tue, 22 Aug 2017 15:19:12 -0400 Subject: Stop ignoring whitespace --- __tests__/lexer.test.js | 12 ++++++------ src/lexer.js | 5 ----- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/__tests__/lexer.test.js b/__tests__/lexer.test.js index 76cb421..12835df 100644 --- a/__tests__/lexer.test.js +++ b/__tests__/lexer.test.js @@ -10,24 +10,24 @@ describe('lex', () => { }) it('throws on unexpected input at the end', () => { - expect(() => { lex('1d6 `') }).toThrow(/Syntax error/) + expect(() => { lex('1d6`') }).toThrow(/Syntax error/) }) it('throws on unexpected input in the middle', () => { - expect(() => { lex('2d3 + b 3d4') }).toThrow(/Syntax error/) + expect(() => { lex('2d3 + b3d4') }).toThrow(/Syntax error/) }) - describe('ignores whitespace', () => { + describe('throws on unexpected whitespace', () => { it('2 d 4', () => { - expect(lex('2 d 4')).not.toBe('error') + expect(() => { lex('2 d 4') }).toThrow(/Syntax error/) }) it(' 1d8', () => { - expect(lex(' 1d8')).not.toBe('error') + expect(() => { lex(' 1d8') }).toThrow(/Syntax error/) }) it('3d4 ', () => { - expect(lex('3d4 ')).not.toBe('error') + expect(() => { lex('3d4 ') }).toThrow(/Syntax error/) }) }) diff --git a/src/lexer.js b/src/lexer.js index 5831541..8635e4a 100644 --- a/src/lexer.js +++ b/src/lexer.js @@ -14,10 +14,6 @@ const newValueLexeme = (type, regex, converter = v => v) => { }) } -const newSkippableLexeme = (type, regex) => { - newLexemeType(type, regex, () => {}) -} - newValueLexeme('constant', '\\d+', Number) newLexemeType('d', 'd') newLexemeType('bigPlus', ' \\+ ' ) @@ -27,7 +23,6 @@ newLexemeType('(', '\\(') newLexemeType(')', '\\)') newLexemeType('E', 'E') newLexemeType('K', 'K') -newSkippableLexeme('whitespace', '\\s+') const lex = (expressionString) => { let lexemes = [] -- cgit v1.2.3 From 034d62f90d9c748f38209991c812692e5a99bbd9 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Tue, 22 Aug 2017 15:19:32 -0400 Subject: Add test for lexing negatives --- __tests__/lexer.test.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/__tests__/lexer.test.js b/__tests__/lexer.test.js index 12835df..6900764 100644 --- a/__tests__/lexer.test.js +++ b/__tests__/lexer.test.js @@ -125,6 +125,19 @@ describe('lex', () => { }) }) + describe('lexes negatives', () => { + it('-(1d6)', () => { + expect(lex('-(1d6)')).toEqual([ + { type: 'minus' }, + { type: '(' }, + { type: 'constant', value: 1 }, + { type: 'd' }, + { type: 'constant', value: 6 }, + { type: ')' } + ]) + }) + }) + describe('exploding dice', () => { test('1E1d6', () => { expect(lex('1E1d6')).toEqual([ -- cgit v1.2.3