Allow atoms (integers, booleans, symbols) as top-level expressions
This commit is contained in:
parent
753460884e
commit
4f98ea22e8
@ -255,6 +255,46 @@ class String(State):
|
|||||||
self.nodes.append(char)
|
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):
|
class Idle(State):
|
||||||
"""
|
"""
|
||||||
Idle state. This is the first (and last) thing that we should
|
Idle state. This is the first (and last) thing that we should
|
||||||
@ -266,7 +306,12 @@ class Idle(State):
|
|||||||
State transitions:
|
State transitions:
|
||||||
|
|
||||||
- ( - Expression
|
- ( - Expression
|
||||||
- (default) - Error
|
- [ - List
|
||||||
|
- { - Dict
|
||||||
|
- \" - String
|
||||||
|
- ; - Comment
|
||||||
|
- # - Hash
|
||||||
|
- (default) - Atom
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if char == "(":
|
if char == "(":
|
||||||
@ -278,19 +323,19 @@ class Idle(State):
|
|||||||
if char == "{":
|
if char == "{":
|
||||||
return Dict
|
return Dict
|
||||||
|
|
||||||
|
if char == "\"":
|
||||||
|
return String
|
||||||
|
|
||||||
if char == ";":
|
if char == ";":
|
||||||
return Comment
|
return Comment
|
||||||
|
|
||||||
if char == "#":
|
if char == "#":
|
||||||
return Hash
|
return Hash
|
||||||
|
|
||||||
if char == "\"":
|
|
||||||
return String
|
|
||||||
|
|
||||||
if char in WHITESPACE:
|
if char in WHITESPACE:
|
||||||
return
|
return
|
||||||
|
|
||||||
raise LexException("Unknown char (Idle state): `%s`" % (char))
|
return AtomStartingWith(char)
|
||||||
|
|
||||||
|
|
||||||
class Comment(State):
|
class Comment(State):
|
||||||
|
@ -77,6 +77,24 @@ def test_lex_expression_integer():
|
|||||||
assert objs == [HyExpression([HySymbol("foo"), HyInteger(2)])]
|
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():
|
def test_lex_line_counting():
|
||||||
""" Make sure we can count lines / columns """
|
""" Make sure we can count lines / columns """
|
||||||
entry = tokenize("(foo (one two))")[0]
|
entry = tokenize("(foo (one two))")[0]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user