From 1f4401a827e7711960eb704c7fb967483dee7213 Mon Sep 17 00:00:00 2001 From: martin dobrev Date: Sun, 14 Oct 2018 13:02:25 +0300 Subject: Fix parsing of single quoted strings --- JSImpl/src/Lexer.cpp | 12 ++++++++++++ JSImpl/src/main.cpp | 8 ++++++++ 2 files changed, 20 insertions(+) (limited to 'JSImpl/src') diff --git a/JSImpl/src/Lexer.cpp b/JSImpl/src/Lexer.cpp index 1d9035a..22ffab8 100644 --- a/JSImpl/src/Lexer.cpp +++ b/JSImpl/src/Lexer.cpp @@ -204,6 +204,18 @@ Token Tokenizer::NextToken() return Token{ TokenType::String, m_Line, IPLString(m_Code + start, m_Code + m_Current), 0.0 }; } + if (c == '\'') + { + auto start = m_Current; + m_Current++; // skip first ' + while (m_Code[m_Current] != '\'') + { + ++m_Current; + } + m_Current++; // skip second ' + return Token{ TokenType::String, m_Line, IPLString(m_Code + start, m_Code + m_Current), 0.0 }; + } + // Key words if (c >= 'a' && c <= 'z') { diff --git a/JSImpl/src/main.cpp b/JSImpl/src/main.cpp index 13fdafc..eec4c80 100644 --- a/JSImpl/src/main.cpp +++ b/JSImpl/src/main.cpp @@ -51,6 +51,12 @@ void TestString() CHECH(tokens.size() == 2 && tokens[0].Type == TokenType::String && tokens[0].Lexeme == "\"alabala\""); } +void TestStringSingleQuotedStrings() +{ + auto tokens = Tokenize("'alabala'"); + CHECH(tokens.size() == 2 && tokens[0].Type == TokenType::String && tokens[0].Lexeme == "'alabala'"); +} + void TestKeyWord() { auto tokens = Tokenize("for"); @@ -82,9 +88,11 @@ int main() EXECUTE_TEST(TestNumberStartWithZero); EXECUTE_TEST(TestString); EXECUTE_TEST(TestSpaceNewLineSpace); + EXECUTE_TEST(TestStringSingleQuotedStrings); EXECUTE_TEST(TestKeyWord); EXECUTE_TEST(TestVariableDeclaration); + //EXECUTE_TEST(TestParseUnaryExpr); #if defined(_WIN32) std::system("pause"); -- cgit v1.3.1-23-gd82d