From 753460884ec81da784230885ce9e563fcda085d2 Mon Sep 17 00:00:00 2001 From: Konrad Hinsen Date: Wed, 10 Apr 2013 12:32:39 +0200 Subject: [PATCH 1/3] Accept strings as top-level expressions --- hy/lex/states.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hy/lex/states.py b/hy/lex/states.py index 52d6fa3..005e905 100644 --- a/hy/lex/states.py +++ b/hy/lex/states.py @@ -284,6 +284,9 @@ class Idle(State): if char == "#": return Hash + if char == "\"": + return String + if char in WHITESPACE: return From 4f98ea22e8c18e68d64d8e8a3976605e4a6f22b6 Mon Sep 17 00:00:00 2001 From: Konrad Hinsen Date: Wed, 10 Apr 2013 13:08:32 +0200 Subject: [PATCH 2/3] Allow atoms (integers, booleans, symbols) as top-level expressions --- hy/lex/states.py | 55 +++++++++++++++++++++++++++++++++++++++---- tests/lex/test_lex.py | 18 ++++++++++++++ 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/hy/lex/states.py b/hy/lex/states.py index 005e905..f6ec6eb 100644 --- a/hy/lex/states.py +++ b/hy/lex/states.py @@ -255,6 +255,46 @@ class String(State): self.nodes.append(char) +class Atom(State): + """ + This state parses integer constants, boolean constants, and symbols + """ + + def __init__(self, machine): + State.__init__(self, machine) + self.initial_buf = '' + + def enter(self): + self.buf = self.initial_buf + + def exit(self): + self.result = _resolve_atom(self.buf) + + def process(self, char): + """ + State transitions: + + - WHITESPACE - Idle + - ; - Comment + """ + + if char in WHITESPACE: + return Idle + + if char == ";": + return Comment + + self.buf += char + + +def AtomStartingWith(initial_char): + def AtomFactory(machine): + state = Atom(machine) + state.initial_buf = initial_char + return state + return AtomFactory + + class Idle(State): """ Idle state. This is the first (and last) thing that we should @@ -266,7 +306,12 @@ class Idle(State): State transitions: - ( - Expression - - (default) - Error + - [ - List + - { - Dict + - \" - String + - ; - Comment + - # - Hash + - (default) - Atom """ if char == "(": @@ -278,19 +323,19 @@ class Idle(State): if char == "{": return Dict + if char == "\"": + return String + if char == ";": return Comment if char == "#": return Hash - if char == "\"": - return String - if char in WHITESPACE: return - raise LexException("Unknown char (Idle state): `%s`" % (char)) + return AtomStartingWith(char) class Comment(State): diff --git a/tests/lex/test_lex.py b/tests/lex/test_lex.py index 45922b9..06ac7eb 100644 --- a/tests/lex/test_lex.py +++ b/tests/lex/test_lex.py @@ -77,6 +77,24 @@ def test_lex_expression_integer(): assert objs == [HyExpression([HySymbol("foo"), HyInteger(2)])] +def test_lex_symbols(): + """ Make sure that symbols are valid expressions""" + objs = tokenize("foo ") + assert objs == [HySymbol("foo")] + + +def test_lex_symbols(): + """ Make sure that strings are valid expressions""" + objs = tokenize("\"foo\" ") + assert objs == [HyString("foo")] + + +def test_lex_integers(): + """ Make sure that integers are valid expressions""" + objs = tokenize("42 ") + assert objs == [HyInteger(42)] + + def test_lex_line_counting(): """ Make sure we can count lines / columns """ entry = tokenize("(foo (one two))")[0] From fd99f497a40ae4e860e7994c9707c8b4ec814ffc Mon Sep 17 00:00:00 2001 From: Konrad Hinsen Date: Thu, 11 Apr 2013 08:26:56 +0200 Subject: [PATCH 3/3] Fixed typo in test_lex.py --- tests/lex/test_lex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lex/test_lex.py b/tests/lex/test_lex.py index 06ac7eb..0172242 100644 --- a/tests/lex/test_lex.py +++ b/tests/lex/test_lex.py @@ -83,7 +83,7 @@ def test_lex_symbols(): assert objs == [HySymbol("foo")] -def test_lex_symbols(): +def test_lex_strings(): """ Make sure that strings are valid expressions""" objs = tokenize("\"foo\" ") assert objs == [HyString("foo")]