better line bits.

This commit is contained in:
Paul R. Tagliamonte 2013-03-03 14:03:59 -05:00
parent e992c8dd38
commit b37d92fe28
3 changed files with 33 additions and 5 deletions

View File

@ -19,12 +19,11 @@
# DEALINGS IN THE SOFTWARE. # DEALINGS IN THE SOFTWARE.
from hy.compilers import HyCompiler from hy.compilers import HyCompiler
from hy.errors import HyError
from hy.models.expression import HyExpression from hy.models.expression import HyExpression
from hy.models.symbol import HySymbol from hy.models.symbol import HySymbol
from hy.errors import HyError
import ast import ast
@ -63,8 +62,12 @@ class HyASTCompiler(HyCompiler):
args=[self.compile(x) for x in expression[1:]], args=[self.compile(x) for x in expression[1:]],
keywords=[], keywords=[],
starargs=None, starargs=None,
kwargs=None) kwargs=None,
lineno=expression.start_line,
col_offset=expression.start_column)
@builds(HySymbol) @builds(HySymbol)
def compile_symbol(self, symbol): def compile_symbol(self, symbol):
return ast.Name(id=str(symbol), ctx=ast.Load()) return ast.Name(id=str(symbol), ctx=ast.Load(),
lineno=symbol.start_line,
col_offset=symbol.start_column)

View File

@ -109,7 +109,13 @@ class Expression(State):
def commit(self): def commit(self):
if self.buf != "": if self.buf != "":
self.nodes.append(_resolve_atom(self.buf)) ret = _resolve_atom(self.buf)
ret.start_line = self._start_line
ret.start_column = self._start_column
ret.end_line = self.machine.line
ret.end_column = (self.machine.column - 1)
self.nodes.append(ret)
self.buf = "" self.buf = ""
def exit(self): def exit(self):
@ -139,6 +145,10 @@ class Expression(State):
self.commit() self.commit()
return return
if self.buf == "":
self._start_line = self.machine.line
self._start_column = self.machine.column
self.buf += char self.buf += char

View File

@ -100,3 +100,18 @@ def test_lex_line_counting_multi():
assert entry.end_line == 3 assert entry.end_line == 3
assert entry.end_column == 9 assert entry.end_column == 9
def test_lex_line_counting_multi_inner():
""" Make sure we can do multi-line tokenization (inner) """
entry = tokenize("""(foo
bar)""")[0]
inner = entry[0]
assert inner.start_line == 1
assert inner.start_column == 2
inner = entry[1]
assert inner.start_line == 2
assert inner.start_column == 5