From 806f0d900c9e39da7b6ea88f7bf7ab5f138d87cc Mon Sep 17 00:00:00 2001 From: Nicolas Dandrimont Date: Mon, 8 Apr 2013 00:17:42 +0200 Subject: [PATCH] Throw an error on unbalanced expressions --- hy/lex/states.py | 3 +++ tests/lex/test_lex.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/hy/lex/states.py b/hy/lex/states.py index 72caba6..52d6fa3 100644 --- a/hy/lex/states.py +++ b/hy/lex/states.py @@ -161,6 +161,9 @@ class ListeyThing(State): if char == self.end_char: return Idle + if char in ")]}": + raise LexException("Unexpected closing character: `%s'" % (char)) + if char in WHITESPACE: self.commit() return diff --git a/tests/lex/test_lex.py b/tests/lex/test_lex.py index d3b3706..45922b9 100644 --- a/tests/lex/test_lex.py +++ b/tests/lex/test_lex.py @@ -44,6 +44,21 @@ def test_lex_exception(): pass +def test_unbalanced_exception(): + """Ensure the tokenization fails on unbalanced expressions""" + try: + tokenize("(bar))") + assert True is False + except LexException: + pass + + try: + tokenize("(baz [quux]])") + assert True is False + except LexException: + pass + + def test_lex_expression_symbols(): """ Make sure that expressions produce symbols """ objs = tokenize("(foo bar)")