Support hexadecimal literals

This commit is contained in:
2025-08-04 22:49:49 +02:00
parent 4b18a98581
commit b18effbdd8
3 changed files with 282 additions and 235 deletions

View File

@@ -117,7 +117,7 @@ namespace elna::boot
} }
++current_position; ++current_position;
} }
result.pop_back(); result.pop_back(); // Remove the terminating quote character.
return result; return result;
} }

View File

@@ -32,6 +32,11 @@ along with GCC; see the file COPYING3. If not see
%x IN_COMMENT %x IN_COMMENT
ID1 [A-Za-z_]
ID2 [A-Za-z0-9_]
HIGIT [0-9a-fA-F]
BIGIT [01]
%% %%
%{ %{
this->location.step(); this->location.step();
@@ -146,43 +151,79 @@ case {
of { of {
return yy::parser::make_OF(this->location); return yy::parser::make_OF(this->location);
} }
[A-Za-z_][A-Za-z0-9_]* { {ID1}{ID2}* {
return yy::parser::make_IDENTIFIER(yytext, this->location); return yy::parser::make_IDENTIFIER(yytext, this->location);
} }
#[A-Za-z_][A-Za-z0-9_]* { #{ID1}{ID2}* {
return yy::parser::make_TRAIT(yytext + 1, this->location); return yy::parser::make_TRAIT(yytext + 1, this->location);
} }
[0-9]+u { [[:digit:]]+u {
return yy::parser::make_WORD(strtoul(yytext, NULL, 10), this->location); unsigned long result = strtoul(yytext, NULL, 10);
}
[0-9]+ { if (errno == ERANGE)
return yy::parser::make_INTEGER(strtol(yytext, NULL, 10), this->location);
}
[0-9]+\.[0-9] {
return yy::parser::make_FLOAT(strtof(yytext, NULL), this->location);
}
'[[:print:]]' {
if (yytext[1] == '\\' || yytext[1] == '\'')
{ {
REJECT; REJECT;
} }
else else
{ {
return yy::parser::make_CHARACTER(std::string(yytext, 1, 1), this->location); return yy::parser::make_WORD(result, this->location);
} }
} }
'\\x[0-9a-fA-F]{1,2}' { [[:digit:]]+ {
char character = static_cast<char>(std::stoi(yytext + 3, nullptr, 16)); long result = strtol(yytext, NULL, 10);
return yy::parser::make_CHARACTER(std::string(&character, 1), this->location); if (errno == ERANGE)
}
'\\[0nabtfrv\\'"?]' {
char escape = escape_char(yytext[2]);
if (escape == escape_invalid_char)
{ {
REJECT; REJECT;
} }
return yy::parser::make_CHARACTER(std::string(&escape, 1), this->location); else
{
return yy::parser::make_INTEGER(result, this->location);
}
}
0x{HIGIT}+ {
unsigned long result = strtoul(yytext, NULL, 16);
if (errno == ERANGE)
{
REJECT;
}
else
{
return yy::parser::make_WORD(result, this->location);
}
}
0b{BIGIT}+ {
unsigned long result = strtoul(yytext, NULL, 2);
if (errno == ERANGE)
{
REJECT;
}
else
{
return yy::parser::make_WORD(result, this->location);
}
}
[[:digit:]]+\.[[:digit:]]+ {
float result = strtof(yytext, NULL);
if (errno == ERANGE)
{
REJECT;
}
else
{
return yy::parser::make_FLOAT(result, this->location);
}
}
'[[:print:]]+' {
std::optional<std::string> result = escape_string(yytext);
if (!result.has_value() || result.value().size() != 1)
{
REJECT;
}
return yy::parser::make_CHARACTER(result.value(), this->location);
} }
\"[[:print:]]*\" { \"[[:print:]]*\" {
std::optional<std::string> result = escape_string(yytext); std::optional<std::string> result = escape_string(yytext);

View File

@@ -1074,6 +1074,12 @@ begin
return return_code return return_code
end; end;
proc f();
var x: Char;
begin
x := '\x4'
end;
begin begin
exit(process(count, parameters)) exit(process(count, parameters))
end. end.