m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lexer.js2
-rw-r--r--src/parser.js18
2 files changed, 16 insertions, 4 deletions
diff --git a/src/lexer.js b/src/lexer.js
index f559816..5e29e42 100644
--- a/src/lexer.js
+++ b/src/lexer.js
@@ -43,7 +43,7 @@ const lex = (expressionString) => {
})
if (!matched) {
- return 'error'
+ throw new Error('Syntax error: unrecognized token')
}
}
diff --git a/src/parser.js b/src/parser.js
index 5b9b718..fc9add3 100644
--- a/src/parser.js
+++ b/src/parser.js
@@ -2,8 +2,17 @@ const { lex } = require('./lexer.js')
let symbols = {}
+let throwSyntaxError = () => {
+ throw new Error('Syntax error: unexpected token')
+}
+
let newSymbol = (type, nud, lbp, led) => {
- symbols[type] = { type, nud, lbp, led }
+ symbols[type] = {
+ type,
+ nud: nud || throwSyntaxError,
+ lbp,
+ led: led || throwSyntaxError
+ }
}
let lexemeToToken = lexeme => {
@@ -65,7 +74,7 @@ const newParser = (tokens) => {
if (this.token().type === token) {
this.advanceToken()
} else {
- throw 'error'
+ throw throwSyntaxError()
}
},
expression: function(rbp) {
@@ -89,7 +98,10 @@ const parse = expressionString => {
tokens.push(symbols.end)
const parser = newParser(tokens)
- return parser.expression(0)
+ const expression = parser.expression(0)
+ parser.match('end')
+
+ return expression
}