Fix bug when starting with $-sign
When a text token was running, it wouldn't stop on a $-sign if it wasn't followed with a "[". This is good. But when a token was finished and the next char was a $-sign, it would consider it an MFMOpen, even when not followed by a "[". This is now fixed.
This commit is contained in:
parent
ed970aa77d
commit
0a2894f07a
4 changed files with 43 additions and 5 deletions
23
lib/lexer.ex
23
lib/lexer.ex
|
@ -31,11 +31,24 @@ defmodule MfmParser.Lexer do
|
||||||
|
|
||||||
defp get_empty_token(input) do
|
defp get_empty_token(input) do
|
||||||
case Reader.peek(input) do
|
case Reader.peek(input) do
|
||||||
:eof -> :eof
|
:eof ->
|
||||||
"$" -> %MFM.Open{}
|
:eof
|
||||||
"]" -> %MFM.Close{}
|
|
||||||
"\n" -> %Newline{}
|
"$" ->
|
||||||
_ -> %Text{}
|
if Reader.peek(input, 2) == "[" do
|
||||||
|
%MFM.Open{}
|
||||||
|
else
|
||||||
|
%Text{}
|
||||||
|
end
|
||||||
|
|
||||||
|
"]" ->
|
||||||
|
%MFM.Close{}
|
||||||
|
|
||||||
|
"\n" ->
|
||||||
|
%Newline{}
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
%Text{}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,15 @@ defmodule MfmParser.Reader do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def peek(input, steps) do
|
||||||
|
nth_char = input |> String.at(steps - 1)
|
||||||
|
|
||||||
|
case nth_char do
|
||||||
|
nil -> :eof
|
||||||
|
_ -> nth_char
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def next(input) do
|
def next(input) do
|
||||||
{next_char, rest} = String.split_at(input, 1)
|
{next_char, rest} = String.split_at(input, 1)
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,15 @@ defmodule MfmParser.LexerTest do
|
||||||
|
|
||||||
assert Lexer.peek("Eyes like $$") == %Text{content: "Eyes like $$"}
|
assert Lexer.peek("Eyes like $$") == %Text{content: "Eyes like $$"}
|
||||||
assert Lexer.next("Eyes like $$") == {%Text{content: "Eyes like $$"}, ""}
|
assert Lexer.next("Eyes like $$") == {%Text{content: "Eyes like $$"}, ""}
|
||||||
|
|
||||||
|
assert Lexer.peek("$2 chocolatine") == %Text{content: "$2 chocolatine"}
|
||||||
|
assert Lexer.next("$2 chocolatine") == {%Text{content: "$2 chocolatine"}, ""}
|
||||||
|
|
||||||
|
assert Lexer.peek("$2") == %Text{content: "$2"}
|
||||||
|
assert Lexer.next("$2") == {%Text{content: "$2"}, ""}
|
||||||
|
|
||||||
|
assert Lexer.peek("$") == %Text{content: "$"}
|
||||||
|
assert Lexer.next("$") == {%Text{content: "$"}, ""}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "it ends when a mfm token closes" do
|
test "it ends when a mfm token closes" do
|
||||||
|
|
|
@ -6,12 +6,19 @@ defmodule MfmParser.ReaderTest do
|
||||||
assert Reader.peek("chocolatine") == "c"
|
assert Reader.peek("chocolatine") == "c"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "it can peek at the nth character" do
|
||||||
|
assert Reader.peek("chocolatine", 7) == "a"
|
||||||
|
end
|
||||||
|
|
||||||
test "it step to the next character" do
|
test "it step to the next character" do
|
||||||
assert Reader.next("chocolatine") == {"c", "hocolatine"}
|
assert Reader.next("chocolatine") == {"c", "hocolatine"}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "it returns eof" do
|
test "it returns eof" do
|
||||||
assert Reader.peek("") == :eof
|
assert Reader.peek("") == :eof
|
||||||
|
assert Reader.peek("", 1) == :eof
|
||||||
|
assert Reader.peek("", 2) == :eof
|
||||||
|
assert Reader.peek("c", 3) == :eof
|
||||||
assert Reader.next("") == :eof
|
assert Reader.next("") == :eof
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue