From dce82668ce6c54de47aef2a8d4564d313bbb790d Mon Sep 17 00:00:00 2001 From: James Harton Date: Thu, 26 Mar 2015 20:51:01 +1300 Subject: [PATCH] Fix line number incrementing. Add test. --- lib/lex_luthor.ex | 2 +- test/acceptance_test.exs | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/lex_luthor.ex b/lib/lex_luthor.ex index 48e6771..9d3cde5 100644 --- a/lib/lex_luthor.ex +++ b/lib/lex_luthor.ex @@ -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? diff --git a/test/acceptance_test.exs b/test/acceptance_test.exs index 35666af..6fc4a62 100644 --- a/test/acceptance_test.exs +++ b/test/acceptance_test.exs @@ -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