aboutsummaryrefslogtreecommitdiff
path: root/neb/parser.py
diff options
context:
space:
mode:
authormryouse2022-06-28 23:44:22 +0000
committermryouse2022-06-28 23:44:22 +0000
commit76800f84af4833755c0f8b53d7768b02c7560cd4 (patch)
tree0b48cdc47d0af857c6e9975394ecc308e9337400 /neb/parser.py
parent3e2d950f7632d015e36007baeb7e39e16246436b (diff)
bugfix: fail gracefully when parens are uneven
Diffstat (limited to 'neb/parser.py')
-rw-r--r--neb/parser.py8
1 files changed, 7 insertions, 1 deletions
diff --git a/neb/parser.py b/neb/parser.py
index 5352ee5..08ab0be 100644
--- a/neb/parser.py
+++ b/neb/parser.py
@@ -1,4 +1,6 @@
from .structs import *
+from .exceptions import ParseError
+
def parseExpression(token, prev, tokens):
idx = 0
@@ -6,6 +8,8 @@ def parseExpression(token, prev, tokens):
prev = token
while tokens[idx].type_ != TokenType.CLOSE_PAREN:
token = tokens[idx]
+ if token.type_ == TokenType.EOF:
+ raise ParseError("uneven parens: not enough closing!", token.line)
inc = 1
if token.type_ == TokenType.OPEN_PAREN:
expr, inc = parseExpression(token, prev, tokens[idx+1:])
@@ -49,7 +53,9 @@ def parse(tokens):
while tokens[idx].type_ != TokenType.EOF:
token = tokens[idx]
counter = 1
- if token.type_ == TokenType.OPEN_PAREN:
+ if token.type_ == TokenType.CLOSE_PAREN:
+ raise ParseError("uneven parens: too many closing!", token.line)
+ elif token.type_ == TokenType.OPEN_PAREN:
expr, counter = parseExpression(token, prev, tokens[idx+1:])
exprs.append(expr)
elif token.type_ in (TokenType.FALSE, TokenType.TRUE, TokenType.STRING, TokenType.INT, TokenType.FLOAT):