diff --git a/src/Lexer.cc b/src/Lexer.cc index 115db17..01e43c4 100644 --- a/src/Lexer.cc +++ b/src/Lexer.cc @@ -20,6 +20,10 @@ #include "Lexer.h" #include +// debugging help! +// prints the line number of where the peek is called +// #define peek() peek(__LINE__) + bool isSpace(char c) noexcept { switch (c) { case ' ': @@ -161,8 +165,8 @@ fysh::Fysh fysh::FyshLexer::fyshOutline() noexcept { get(); return Fysh(Species::DIVIDE, start, current); } - return Fysh( - Species::INVALID); // should be fixed since comments have slashes too + // should be fixed since comments have slashes too + return Fysh(Species::INVALID); } // swim right @@ -184,15 +188,15 @@ fysh::Fysh fysh::FyshLexer::fyshOutline() noexcept { return Fysh(Species::INVALID); } } - - // swim left - else if (*start == '<') { - char c = peek(); - if (isFyshEye(c) || isScale(c)) { - return scales(false); // false for negative - } - return Fysh(Species::INVALID); + + // swim left + else if (*start == '<') { + char c = peek(); + if (isFyshEye(c) || isScale(c)) { + return scales(false); // false for negative } + return Fysh(Species::INVALID); + } return Fysh(Species::END); } @@ -211,7 +215,6 @@ fysh::Fysh fysh::FyshLexer::random() noexcept { return Fysh(Species::INVALID); } - fysh::Fysh fysh::FyshLexer::scales(bool positive = true) noexcept { auto c{get()}; uint32_t value{c == '{' || c == '}'}; @@ -219,7 +222,7 @@ fysh::Fysh fysh::FyshLexer::scales(bool positive = true) noexcept { auto c{get()}; value = (value << 1) | (c == '{' || c == '}'); } - + /* validate end of fysh valid ends: @@ -239,14 +242,15 @@ fysh::Fysh fysh::FyshLexer::scales(bool positive = true) noexcept { // check if its the end of the token or not (2nd end character) if (!isSpace(peek())) { c = get(); - if ((c != '>') && (c != '<' && !positive)) { + if ((positive && (c != '>' || c == '<')) || + (!positive && (c != '<' || c == '>'))) { gotoEndOfToken(); return Fysh{Species::INVALID}; } } // make sure the token ends - if (!isSpace(peek()) && peek() != '\0'){ + if (!isSpace(peek()) && peek() != '\0') { gotoEndOfToken(); return Fysh{Species::INVALID}; } @@ -262,5 +266,4 @@ void fysh::FyshLexer::gotoEndOfToken() noexcept { while (!isSpace(peek())) { get(); } - } diff --git a/src/Lexer.h b/src/Lexer.h index fbb5720..23a7add 100644 --- a/src/Lexer.h +++ b/src/Lexer.h @@ -18,6 +18,7 @@ * \file Lexer.h */ #include "Fysh.h" +#include #ifndef FYSH_LEXER_H_ #define FYSH_LEXER_H_ @@ -33,6 +34,7 @@ class FyshLexer { FyshLexer(const char *streamStart) noexcept : current{streamStart} {} Fysh nextFysh() noexcept; + const char *rest() const noexcept { return current; } private: Fysh identifier() noexcept; @@ -49,7 +51,12 @@ class FyshLexer { Fysh random() noexcept; bool isFyshEye(char) noexcept; bool isPositiveScale() noexcept; - char peek() const noexcept { return *current; } + char peek(int line = -1) const noexcept { + if (line > 0) { + std::cout << "Current (line " << line << "): " << *current << std::endl; + } + return *current; + } char get() noexcept { return *current++; } void gotoEndOfToken() noexcept; diff --git a/test/Lexer_test.cc b/test/Lexer_test.cc index 4bf2f39..ca93a0f 100644 --- a/test/Lexer_test.cc +++ b/test/Lexer_test.cc @@ -1,6 +1,7 @@ #include "../src/Lexer.h" #include "doctest.h" +#include using namespace fysh; @@ -41,7 +42,6 @@ TEST_CASE("fysh open & wtf open") { CHECK(lexer.nextFysh() == Species::WTF_OPEN); } - TEST_CASE("random fysh") { std::string_view input{"><##> ><###> ><####> <###><"}; // Assuming `input` is a std::string or std::string_view @@ -52,7 +52,6 @@ TEST_CASE("random fysh") { CHECK(lexer.nextFysh() == Species::INVALID); } - TEST_CASE("negative fysh") { std::string_view input{"><{{({(o> <3 < <"}; // Assuming `input` is a std::string or std::string_view @@ -75,12 +74,13 @@ TEST_CASE("weird fysh") { } TEST_CASE("Bad fysh") { - std::string_view input{"><{{({(o><{{({(o>< ><{{((>< ><{{{< ><"}; + std::string_view input{ + "><{{({(o><{{({(o>< ><{{((>< ><{{{< ><"}; // Assuming `input` is a std::string or std::string_view FyshLexer lexer{input.data()}; CHECK(lexer.nextFysh() == Species::INVALID); CHECK(lexer.nextFysh() == Species::INVALID); - CHECK(lexer.nextFysh() == Species::INVALID); // this keeps failing for some reason CHECK(lexer.nextFysh() == Species::INVALID); CHECK(lexer.nextFysh() == Species::INVALID); -} \ No newline at end of file + CHECK(lexer.nextFysh() == Species::INVALID); +}