aboutsummaryrefslogtreecommitdiff
path: root/asm/lexer.cpp
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2024-04-14 17:01:35 +0630
committerAryadev Chavali <aryadev@aryadevchavali.com>2024-04-14 17:01:35 +0630
commit50e9a4eef52a1123fd9eebcb9d0de483b79be061 (patch)
tree61df4b88bc720f699324c5ab69c980b8d6b6a689 /asm/lexer.cpp
parent3c46fde66ac71e4804cdd5fe076ed930db5d7122 (diff)
downloadovm-50e9a4eef52a1123fd9eebcb9d0de483b79be061.tar.gz
ovm-50e9a4eef52a1123fd9eebcb9d0de483b79be061.tar.bz2
ovm-50e9a4eef52a1123fd9eebcb9d0de483b79be061.zip
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.
Diffstat (limited to 'asm/lexer.cpp')
-rw-r--r--asm/lexer.cpp43
1 files changed, 43 insertions, 0 deletions
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<token_t, lerr_t> 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);
+}