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:
Ilja 2022-07-25 01:21:02 +02:00
parent ed970aa77d
commit 0a2894f07a
4 changed files with 43 additions and 5 deletions

View file

@ -31,11 +31,24 @@ defmodule MfmParser.Lexer do
defp get_empty_token(input) do
case Reader.peek(input) do
:eof -> :eof
"$" -> %MFM.Open{}
"]" -> %MFM.Close{}
"\n" -> %Newline{}
_ -> %Text{}
:eof ->
:eof
"$" ->
if Reader.peek(input, 2) == "[" do
%MFM.Open{}
else
%Text{}
end
"]" ->
%MFM.Close{}
"\n" ->
%Newline{}
_ ->
%Text{}
end
end

View file

@ -8,6 +8,15 @@ defmodule MfmParser.Reader do
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
{next_char, rest} = String.split_at(input, 1)

View file

@ -57,6 +57,15 @@ defmodule MfmParser.LexerTest do
assert Lexer.peek("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
test "it ends when a mfm token closes" do

View file

@ -6,12 +6,19 @@ defmodule MfmParser.ReaderTest do
assert Reader.peek("chocolatine") == "c"
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
assert Reader.next("chocolatine") == {"c", "hocolatine"}
end
test "it returns eof" do
assert Reader.peek("") == :eof
assert Reader.peek("", 1) == :eof
assert Reader.peek("", 2) == :eof
assert Reader.peek("c", 3) == :eof
assert Reader.next("") == :eof
end
end