m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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 = []