cleaning up lexing code

This commit is contained in:
Paul Tagliamonte 2013-03-06 18:57:21 -05:00
parent 273d13daed
commit 863d619e4a
2 changed files with 17 additions and 44 deletions

View File

@ -96,7 +96,7 @@ class State(object):
pass # ABC pass # ABC
class List(State): class ListeyThing(State):
def enter(self): def enter(self):
self.buf = "" self.buf = ""
@ -114,7 +114,7 @@ class List(State):
def exit(self): def exit(self):
self.commit() self.commit()
self.result = HyList(self.nodes) self.result = self.result_type(self.nodes)
def process(self, char): def process(self, char):
if char == "(": if char == "(":
@ -129,7 +129,7 @@ class List(State):
self.machine.sub(String) self.machine.sub(String)
return return
if char == "]": if char == self.end_char:
return Idle return Idle
if char in WHITESPACE: if char in WHITESPACE:
@ -143,54 +143,26 @@ class List(State):
self.buf += char self.buf += char
class Expression(List): class List(ListeyThing):
""" """
Expression state. This will handle stuff like: This state parses a Hy list (like a Clojure vector) for use in native
Python interop.
(...... (....)) [foo 1 2 3 4] is a good example.
^^^^^^ -- expression
^^^^^^^^^^^^^^^ -- expression
""" """
def exit(self): result_type = HyList
self.commit() end_char = "]"
if self.nodes != []:
self.result = HyExpression(self.nodes)
else:
self.result = HyList([])
def process(self, char):
class Expression(ListeyThing):
""" """
State transitions: This state parses a Hy expression (statement, to be evaluated at runtime)
for running things & stuff.
- ( - sub Expression
- " - sub String
- (whitespace) - Idle
""" """
if char == "(":
self.machine.sub(Expression)
return
if char == "[": result_type = HyExpression
self.machine.sub(List) end_char = ")"
return
if char == "\"":
self.machine.sub(String)
return
if char == ")":
return Idle
if char in WHITESPACE:
self.commit()
return
if self.buf == "":
self._start_line = self.machine.line
self._start_column = self.machine.column
self.buf += char
class String(State): class String(State):

View File

@ -1,5 +1,6 @@
; ;
(def test_lists (fn [] (def test_lists (fn []
"NATIVE: test lists work right" "NATIVE: test lists work right"
(assert (= [1 2 3 4] (+ [1 2] [3 4]))))) (assert (= [1 2 3 4] (+ [1 2] [3 4])))))