aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormryouse2022-06-28 23:44:22 +0000
committermryouse2022-06-28 23:44:22 +0000
commit76800f84af4833755c0f8b53d7768b02c7560cd4 (patch)
tree0b48cdc47d0af857c6e9975394ecc308e9337400
parent3e2d950f7632d015e36007baeb7e39e16246436b (diff)
bugfix: fail gracefully when parens are uneven
-rw-r--r--neb/exceptions.py4
-rw-r--r--neb/parser.py8
2 files changed, 11 insertions, 1 deletions
diff --git a/neb/exceptions.py b/neb/exceptions.py
index 8bbe000..cc0f915 100644
--- a/neb/exceptions.py
+++ b/neb/exceptions.py
@@ -11,3 +11,7 @@ class InterpretPanic(NebPanic):
class LexError(NebPanic):
def __init__(self, message, line):
super().__init__(f"line {line}: {message}")
+
+class ParseError(NebPanic):
+ def __init__(self, message, line):
+ super().__init__(f"line {line}: {message}")
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):