Fix line number incrementing. Add test.

This commit is contained in:
James Harton 2015-03-26 20:51:01 +13:00
parent 8d7b741173
commit dce82668ce
2 changed files with 9 additions and 1 deletions

View file

@ -80,7 +80,7 @@ defmodule LexLuthor do
lexer
_ ->
# Increment lexer position
line = (String.slice(string, 0, len) |> String.split(~r{(\r|\n|\r\n)}) |> Enum.count) + lexer.line
line = (String.slice(string, 0, len) |> String.split(~r{(\r|\n|\r\n)}) |> Enum.count) - 1 + lexer.line
lexer = %State{ pos: lexer.pos + len, line: line, states: lexer.states, tokens: lexer.tokens }
# Are we at the end of the string?

View file

@ -34,4 +34,12 @@ defmodule AcceptanceTest do
end
end
test "String #{inspect "foo\nbar"} has correct line numbers" do
{ok, tokens} = ExampleLexer.lex "'foo'\n'bar'"
assert ok == :ok
token = List.last tokens
assert token.line == 2
end
end