m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--__tests__/lexer.test.js28
-rw-r--r--__tests__/parser.test.js30
-rw-r--r--src/lexer.js2
-rw-r--r--src/parser.js8
4 files changed, 68 insertions, 0 deletions
diff --git a/__tests__/lexer.test.js b/__tests__/lexer.test.js
index e9115a1..38c880e 100644
--- a/__tests__/lexer.test.js
+++ b/__tests__/lexer.test.js
@@ -88,4 +88,32 @@ describe('lex', () => {
])
})
})
+
+ describe('lexes parentheses', () => {
+ it('(1d6)d6', () => {
+ expect(lex('(1d6)d6')).toEqual([
+ { type: '(' },
+ { type: 'constant', value: 1 },
+ { type: 'd' },
+ { type: 'constant', value: 6 },
+ { type: ')' },
+ { type: 'd' },
+ { type: 'constant', value: 6 }
+ ])
+ })
+
+ 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: 'constant', value: 3 },
+ { type: ')' },
+ { type: 'd' },
+ { type: 'constant', value: 4 }
+ ])
+ })
+ })
})
diff --git a/__tests__/parser.test.js b/__tests__/parser.test.js
index 6fe296c..a741007 100644
--- a/__tests__/parser.test.js
+++ b/__tests__/parser.test.js
@@ -72,4 +72,34 @@ describe('parse', () => {
}
})
})
+
+ describe('parsing parentheses', () => {
+ test('(1d6)d6', () => {
+ expect(parse('(1d6)d6')).toEqual({
+ type: 'd',
+ left: {
+ type: 'd',
+ left: { type: 'constant', value: 1 },
+ right: { type: 'constant', value: 6 }
+ },
+ right: { type: 'constant', value: 6 },
+ })
+ })
+
+ test('2d(6+3)d4', () => {
+ expect(parse('2d(6+3)d4')).toEqual({
+ type: 'd',
+ left: { type: 'constant', value: 2 },
+ right: {
+ type: 'd',
+ left: {
+ type: 'add',
+ left: { type: 'constant', value: 6 },
+ right: { type: 'constant', value: 3 }
+ },
+ right: { type: 'constant', value: 4 }
+ },
+ })
+ })
+ })
})
diff --git a/src/lexer.js b/src/lexer.js
index fd347c6..f559816 100644
--- a/src/lexer.js
+++ b/src/lexer.js
@@ -22,6 +22,8 @@ newValueLexeme('constant', '\\d+', Number)
newLexemeType('d', 'd')
newLexemeType('+', '\\+')
newLexemeType('-', '-')
+newLexemeType('(', '\\(')
+newLexemeType(')', '\\)')
newSkippableLexeme('whitespace', '\\s+')
const lex = (expressionString) => {
diff --git a/src/parser.js b/src/parser.js
index 6c39c77..9f2c6ac 100644
--- a/src/parser.js
+++ b/src/parser.js
@@ -16,6 +16,14 @@ newSymbol('constant', function() {
return { type: 'constant', value: this.value }
})
+newSymbol('(', function(parser) {
+ const value = parser.expression(1)
+ parser.match(')')
+ return value
+})
+
+newSymbol(')')
+
newSymbol('d', null, 30, (left, parser) => {
return {
type: 'd',