From 50e9a4eef52a1123fd9eebcb9d0de483b79be061 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sun, 14 Apr 2024 17:01:35 +0630 Subject: Implemented tokenise_literal_char (tokenise_char_literal) I made the escape sequence parsing occur here instead of leaving it to the main tokenise_buffer function as I think it's better suited here. --- asm/lexer.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/asm/lexer.cpp b/asm/lexer.cpp index f8abf0a..2bf7169 100644 --- a/asm/lexer.cpp +++ b/asm/lexer.cpp @@ -253,3 +253,46 @@ token_t tokenise_literal_hex(string_view &source, size_t &column) column += digits.size() + 1; return t; } + +pair tokenise_literal_char(string_view &source, size_t &column) +{ + token_t t{}; + if (source.size() < 3) + return make_pair(t, lerr_t::INVALID_CHAR_LITERAL); + else if (source[1] == '\\') + { + // Escape sequence + char escape = '\0'; + if (source.size() < 4 || source[3] != '\'') + return make_pair(t, lerr_t::INVALID_CHAR_LITERAL_ESCAPE_SEQUENCE); + switch (source[2]) + { + case 'n': + escape = '\n'; + break; + case 't': + escape = '\t'; + break; + case 'r': + escape = '\r'; + break; + case '\\': + escape = '\\'; + break; + default: + column += 2; + return make_pair(t, lerr_t::INVALID_CHAR_LITERAL_ESCAPE_SEQUENCE); + break; + } + t = token_t{token_type_t::LITERAL_CHAR, std::to_string(escape), column}; + column += 4; + source.remove_prefix(4); + } + else + { + t = token_t(token_type_t::LITERAL_CHAR, std::to_string(source[1])); + column += 3; + source.remove_prefix(3); + } + return make_pair(t, lerr_t::OK); +} -- cgit v1.2.3-13-gbd6f