m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-07-12 22:54:43 -0400
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-07-12 22:54:43 -0400
commit80c3d09308d4d168658ee7af34e026ff37e1e2ce (patch)
tree74a4a0485bacfe1f6fa267d6bfa910c8e6e2e67c
parent38e82ade6fd207af21f2df6a3e98298338e85eb8 (diff)
Ignore whitespace
-rw-r--r--__tests__/lexer.test.js14
-rw-r--r--src/lexer.js5
2 files changed, 19 insertions, 0 deletions
diff --git a/__tests__/lexer.test.js b/__tests__/lexer.test.js
index 20d461f..8d47ff5 100644
--- a/__tests__/lexer.test.js
+++ b/__tests__/lexer.test.js
@@ -9,6 +9,20 @@ describe('lex', () => {
expect(lex('q')).toBe('error')
})
+ describe('ignores whitespace', () => {
+ it('2 d 4', () => {
+ expect(lex('2 d 4')).not.toBe('error')
+ })
+
+ it(' 1d8', () => {
+ expect(lex(' 1d8')).not.toBe('error')
+ })
+
+ it('3d4 ', () => {
+ expect(lex('3d4 ')).not.toBe('error')
+ })
+ })
+
describe('basic dice', () => {
it('1d6', () => {
expect(lex('1d6')).toEqual([
diff --git a/src/lexer.js b/src/lexer.js
index ff7b0d4..7d876fb 100644
--- a/src/lexer.js
+++ b/src/lexer.js
@@ -14,8 +14,13 @@ const newValueLexeme = (type, regex, converter = v => v) => {
})
}
+const newSkippableLexeme = (type, regex) => {
+ newLexemeType(type, regex, () => {})
+}
+
newValueLexeme('number', '\\d+', Number)
newLexemeType('d', 'd')
+newSkippableLexeme('whitespace', '\\s+')
const lex = (expressionString) => {
let lexemes = []