Implement the Word type

This commit is contained in:
2025-01-24 11:41:14 +01:00
parent 5548b66b15
commit 005e9dcc52
8 changed files with 109 additions and 32 deletions

View File

@@ -106,6 +106,9 @@ return {
[A-Za-z_][A-Za-z0-9_]* {
return yy::parser::make_IDENTIFIER(yytext, this->location);
}
[0-9]+u {
return yy::parser::make_WORD(strtoul(yytext, NULL, 10), this->location);
}
[0-9]+ {
return yy::parser::make_INTEGER(strtol(yytext, NULL, 10), this->location);
}
@@ -113,7 +116,7 @@ return {
return yy::parser::make_FLOAT(strtof(yytext, NULL), this->location);
}
'[[:print:]]' {
if (yytext[1] == '\\')
if (yytext[1] == '\\' || yytext[1] == '\'')
{
REJECT;
}
@@ -122,45 +125,66 @@ return {
return yy::parser::make_CHARACTER(std::string(yytext, 1, 1), this->location);
}
}
'\\x[0-9a-fA-F]{2}' {
'\\x[0-9a-fA-F]{1,2}' {
char character = static_cast<char>(std::stoi(yytext + 3, nullptr, 16));
return yy::parser::make_CHARACTER(std::string(&character, 1), this->location);
}
'\\[0nabtfrv\\'"?]' {
switch (yytext[2])
std::optional<char> escape = source::escape_char(yytext[2]);
if (escape.has_value())
{
return yy::parser::make_CHARACTER(std::string(&escape.value(), 1), this->location);
}
else
{
case 'n':
return yy::parser::make_CHARACTER(std::string("\n"), this->location);
case 'a':
return yy::parser::make_CHARACTER(std::string("\a"), this->location);
case 'b':
return yy::parser::make_CHARACTER(std::string("\b"), this->location);
case 't':
return yy::parser::make_CHARACTER(std::string("\t"), this->location);
case 'f':
return yy::parser::make_CHARACTER(std::string("\f"), this->location);
case 'r':
return yy::parser::make_CHARACTER(std::string("\r"), this->location);
case 'v':
return yy::parser::make_CHARACTER(std::string("\v"), this->location);
case '\\':
return yy::parser::make_CHARACTER(std::string("\\"), this->location);
case '\'':
return yy::parser::make_CHARACTER(std::string("'"), this->location);
case '"':
return yy::parser::make_CHARACTER(std::string("\""), this->location);
case '?':
return yy::parser::make_CHARACTER(std::string("\?"), this->location);
case '0':
return yy::parser::make_CHARACTER(std::string("\0", 1), this->location);
default:
REJECT;
}
}
\"[^\"]*\" {
return yy::parser::make_STRING(
std::string(yytext, 1, strlen(yytext) - 2), this->location);
\"[[:print:]]*\" {
std::string result;
const char *current_position = yytext + 1;
while (*current_position != '\0')
{
if (*current_position == '\\' && *(current_position + 1) == 'x')
{
current_position += 2;
std::size_t processed;
char character = static_cast<char>(std::stoi(current_position, &processed, 16));
if (processed == 0)
{
REJECT;
}
else
{
current_position += processed - 1;
result.push_back(character);
}
}
else if (*current_position == '\\')
{
++current_position;
std::optional<char> escape = source::escape_char(*current_position);
if (escape.has_value())
{
result.push_back(escape.value());
}
else
{
REJECT;
}
}
else
{
result.push_back(*current_position);
}
++current_position;
}
result.pop_back();
return yy::parser::make_STRING(result, this->location);
}
\( {
return yy::parser::make_LEFT_PAREN(this->location);