Restrict cast types

This commit is contained in:
2025-03-21 10:24:57 +01:00
parent 5e8555b4f4
commit 07ed40cc24
7 changed files with 168 additions and 134 deletions

View File

@ -23,12 +23,12 @@ along with GCC; see the file COPYING3. If not see
#include "parser.hh"
#undef YY_DECL
#define YY_DECL yy::parser::symbol_type elna::boot::lexer::lex(elna::boot::driver& driver)
#define YY_DECL yy::parser::symbol_type elna::boot::lexer::lex(driver& driver)
#define yyterminate() return yy::parser::make_YYEOF(this->location)
%}
%option c++ noyywrap never-interactive
%option yyclass="elna::boot::lexer"
%option yyclass="lexer"
%x IN_COMMENT
@ -159,7 +159,7 @@ defer {
return yy::parser::make_CHARACTER(std::string(&character, 1), this->location);
}
'\\[0nabtfrv\\'"?]' {
char escape = elna::boot::escape_char(yytext[2]);
char escape = escape_char(yytext[2]);
if (escape == escape_invalid_char)
{
REJECT;
@ -167,46 +167,12 @@ defer {
return yy::parser::make_CHARACTER(std::string(&escape, 1), this->location);
}
\"[[:print:]]*\" {
std::string result;
const char *current_position = yytext + 1;
while (*current_position != '\0')
std::optional<std::string> result = escape_string(yytext);
if (!result.has_value())
{
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;
char escape = elna::boot::escape_char(*current_position);
if (escape == elna::boot::escape_invalid_char)
{
REJECT;
}
result.push_back(escape);
}
else
{
result.push_back(*current_position);
}
++current_position;
REJECT;
}
result.pop_back();
return yy::parser::make_STRING(result, this->location);
return yy::parser::make_STRING(result.value(), this->location);
}
\( {
return yy::parser::make_LEFT_PAREN(this->location);
@ -290,6 +256,6 @@ defer {
std::stringstream ss;
ss << "Illegal character 0x" << std::hex << static_cast<unsigned int>(yytext[0]);
driver.add_error<elna::boot::syntax_error>(ss.str(), driver.input_file, this->location);
driver.add_error<syntax_error>(ss.str(), driver.input_file, this->location);
}
%%