mirror of
				https://github.com/Laupetin/OpenAssetTools.git
				synced 2025-10-30 18:17:15 +00:00 
			
		
		
		
	Fix SimpleLexer not being able to read escaped strings
This commit is contained in:
		| @@ -2,11 +2,13 @@ | ||||
|  | ||||
| #include <cassert> | ||||
| #include <deque> | ||||
| #include <sstream> | ||||
|  | ||||
| #include "Utils/ClassUtils.h" | ||||
| #include "Parsing/ILexer.h" | ||||
| #include "Parsing/IParserLineStream.h" | ||||
| #include "Parsing/ParsingException.h" | ||||
| #include "Utils/StringUtils.h" | ||||
|  | ||||
| template <typename TokenType> | ||||
| class AbstractLexer : public ILexer<TokenType> | ||||
| @@ -140,6 +142,52 @@ protected: | ||||
|         return std::string(currentLine.m_line, startPos, m_current_line_offset - startPos); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * \brief Reads an identifier from the current position | ||||
|      * \return The value of the read identifier | ||||
|      */ | ||||
|     std::string ReadStringWithEscapeSequences() | ||||
|     { | ||||
|         const auto& currentLine = CurrentLine(); | ||||
|         assert(m_current_line_offset >= 1); | ||||
|         assert(currentLine.m_line[m_current_line_offset - 1] == '"'); | ||||
|  | ||||
|         const auto startPos = m_current_line_offset; | ||||
|         const auto lineSize = currentLine.m_line.size(); | ||||
|         auto isEscaped = false; | ||||
|         auto inEscape = false; | ||||
|         while (true) | ||||
|         { | ||||
|             if (m_current_line_offset >= lineSize) | ||||
|                 throw ParsingException(TokenPos(*currentLine.m_filename, currentLine.m_line_number, m_current_line_offset), "Unclosed string"); | ||||
|  | ||||
|             const auto c = currentLine.m_line[m_current_line_offset]; | ||||
|  | ||||
|             if (c == '\"' && !inEscape) | ||||
|                 break; | ||||
|  | ||||
|             if (c == '\\' && !inEscape) | ||||
|             { | ||||
|                 isEscaped = true; | ||||
|                 inEscape = true; | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 inEscape = false; | ||||
|             } | ||||
|  | ||||
|             m_current_line_offset++; | ||||
|         } | ||||
|  | ||||
|         std::string str(currentLine.m_line, startPos, m_current_line_offset++ - startPos); | ||||
|         if (!isEscaped) | ||||
|             return str; | ||||
|  | ||||
|         std::ostringstream ss; | ||||
|         utils::UnescapeStringFromQuotationMarks(ss, std::move(str)); | ||||
|         return ss.str(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * \brief Reads an identifier from the current position | ||||
|      * \return The value of the read identifier | ||||
|   | ||||
		Reference in New Issue
	
	Block a user