From 03f25f0cbe5164ea1ef6a897b65a37d704ceaf57 Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Sat, 15 Dec 2012 18:38:34 -0500 Subject: [PATCH] updating tests --- hy/lex/states.py | 60 +++++++++++--------------------- tests/lexer/test_basic_lexing.py | 15 ++++++++ tests/lexer/test_list_lexing.py | 19 ++++++++++ 3 files changed, 54 insertions(+), 40 deletions(-) diff --git a/hy/lex/states.py b/hy/lex/states.py index 99131c6..37ea2b3 100644 --- a/hy/lex/states.py +++ b/hy/lex/states.py @@ -4,6 +4,9 @@ from hy.lex.machine import Machine from hy.lang.list import HYList +WHITESPACE = [" ", "\t", "\n", "\r"] + + class State(object): def __init__(self, machine): self.machine = machine @@ -38,12 +41,9 @@ class Comment(State): class Idle(State): def p(self, x): - if x == ";": - return Comment - if x == "(": - return Expression - if x in [" ", "\t", "\n", "\r"]: - return + if x == ";": return Comment + if x == "(": return Expression + if x in WHITESPACE: return raise LexException("Unknown char: %s" % (x)) @@ -65,20 +65,12 @@ class Expression(State): self.bulk = "" def p(self, x): - if x == ")": - return Idle - if x == " ": - self.commit() - return - if x == "\"": - self.sub(String) - return - if x == "(": - self.sub(Expression) - return - if x == "[": - self.sub(List) - return + if x == ")": return Idle + if x in WHITESPACE: self.commit(); return + if x == "\"": self.sub(String); return + if x == "(": self.sub(Expression); return + if x == "[": self.sub(List); return + if x == ";": self.sub(Comment); return self.bulk += x @@ -98,30 +90,17 @@ class List(State): self.bulk = "" def p(self, x): - if x == "]": - return Idle - if x == " ": - self.commit() - return - if x == "\"": - self.sub(String) - return - if x == "[": - self.sub(List) - return - if x == "(": - self.sub(Expression) - return + if x == "]": return Idle + if x in WHITESPACE: self.commit(); return + if x == "\"": self.sub(String); return + if x == "[": self.sub(List); return + if x == "(": self.sub(Expression); return + if x == ";": self.sub(Comment); return self.bulk += x class String(State): - magic = { - "n": "\n", - "t": "\t", - "\\": "\\", - "\"": "\"" - } + magic = { "n": "\n", "t": "\t", "\\": "\\", "\"": "\"" } def enter(self): self.buf = "" @@ -140,6 +119,7 @@ class String(State): if self.esc and x not in self.magic: raise LexException("Unknown escape: \\%s" % (x)) + elif self.esc: x = self.magic[x] diff --git a/tests/lexer/test_basic_lexing.py b/tests/lexer/test_basic_lexing.py index 2aa9d15..996f6e5 100644 --- a/tests/lexer/test_basic_lexing.py +++ b/tests/lexer/test_basic_lexing.py @@ -38,6 +38,21 @@ def test_mid_recurse(): ] == tokenize("(fn one (fn two)(fn three))") +def test_mid_recurse_comment(): + """ Test some crazy recursion with a comment """ + + assert [ + ['fn', + 'one', + ['fn', 'two'], + ['fn', 'three'], + ] + ] == tokenize(""" +(fn one ; this is a test + (fn two)(fn three)) ; and so is this +""") + + def test_full_recurse(): """ Test something we could see for real """ assert [ diff --git a/tests/lexer/test_list_lexing.py b/tests/lexer/test_list_lexing.py index 9cab25e..c50a72b 100644 --- a/tests/lexer/test_list_lexing.py +++ b/tests/lexer/test_list_lexing.py @@ -23,3 +23,22 @@ def test_double_rainbow(): assert fn == [ "fn", ["1", "2", "3", "4"], ["5", "6", "7"] ] + + +def test_string_in_list(): + """ String in list """ + fn = tokenize('(fn [1 2 "three four" 5])')[0] + assert fn == [ + "fn", ["1", "2", "three four", "5"] + ] + + +def test_list_recurse_with_comment(): + """ test we can recurse lists """ + fn = tokenize(""" +(fn [1 ; this is a test + 2 3 4 [5 6 7]]) +""")[0] + assert fn == [ + "fn", ["1", "2", "3", "4", ["5", "6", "7"]] + ]