Add some line bits.

This commit is contained in:
Paul R. Tagliamonte 2013-03-02 19:41:55 -05:00
parent f14ccb6d22
commit 797656b1fd
3 changed files with 26 additions and 2 deletions

View File

@ -2,7 +2,7 @@ from hy.lex.machine import Machine
from hy.lex.states import Idle, LexException from hy.lex.states import Idle, LexException
def tokenize(buf): def tokenize(buf):
machine = Machine(Idle, 0, 0) machine = Machine(Idle, 1, 0)
machine.process(buf) machine.process(buf)
if type(machine.state) != Idle: if type(machine.state) != Idle:
raise LexException("Incomplete Lex.") raise LexException("Incomplete Lex.")

View File

@ -31,10 +31,21 @@ class Machine(object):
def accept_result(self, state): def accept_result(self, state):
if state and state.result: if state and state.result:
self.nodes.append(state.result) result = state.result
result.start_line, result.end_line = (self.start_line, self.line)
result.start_column, result.end_column = (self.start_column,
self.column)
self.nodes.append(result)
def process(self, buf): def process(self, buf):
for char in buf: for char in buf:
self.column += 1
if char == "\n":
self.line += 1
self.column = 0
if self.submachine: if self.submachine:
self.submachine.process([char]) self.submachine.process([char])
if type(self.submachine.state) == Idle: if type(self.submachine.state) == Idle:

View File

@ -10,10 +10,23 @@ def test_lex_expression_symbols():
objs = tokenize("(foo bar)") objs = tokenize("(foo bar)")
assert objs == [HyExpression([HySymbol("foo"), HySymbol("bar")])] assert objs == [HyExpression([HySymbol("foo"), HySymbol("bar")])]
def test_lex_expression_strings(): def test_lex_expression_strings():
objs = tokenize("(foo \"bar\")") objs = tokenize("(foo \"bar\")")
assert objs == [HyExpression([HySymbol("foo"), HyString("bar")])] assert objs == [HyExpression([HySymbol("foo"), HyString("bar")])]
def test_lex_expression_integer(): def test_lex_expression_integer():
objs = tokenize("(foo 2)") objs = tokenize("(foo 2)")
assert objs == [HyExpression([HySymbol("foo"), HyInteger(2)])] assert objs == [HyExpression([HySymbol("foo"), HyInteger(2)])]
def test_lex_line_counting():
objs = tokenize("(foo 2)")
entry = objs[0]
assert entry.start_line == 1
assert entry.start_column == 1
assert entry.end_line == 1
assert entry.end_column == 7