lexer: return TYPE_NAME instead of IDENTIFIER for identifiers that were previously typedeffed

This commit is contained in:
Josh Holtrop 2018-05-08 22:16:53 -04:00
parent 0a2e719f2c
commit 25f7c43d3b
4 changed files with 29 additions and 1 deletions

View File

@ -199,3 +199,8 @@ void observe_type_name(const std::string & type_name)
{
type_names.insert(type_name);
}
bool is_type_name(const std::string & type_name)
{
return type_names.count(type_name) != 0u;
}

View File

@ -13,5 +13,6 @@ extern std::unordered_set<std::string> type_names;
void parse(const char * filename);
void handle_parse_error(const char * str, const YYLTYPE * yylloc);
void observe_type_name(const std::string & type_name);
bool is_type_name(const std::string & type_name);
#endif

View File

@ -175,7 +175,16 @@ L?\" {
[^\\\"]+ *build_string += yytext;
}
[a-zA-Z_][a-zA-Z_0-9]* return IDENTIFIER;
[a-zA-Z_][a-zA-Z_0-9]* {
if (is_type_name(yytext))
{
return TYPE_NAME;
}
else
{
return IDENTIFIER;
}
}
^[ ]*#[ ]+[0-9]+[ ]+\".+\".*$ {
handle_loc(yytext);

View File

@ -2,6 +2,7 @@
#include <stdio.h>
#include "parser.h"
#include <iostream>
#define yyerror(msg) handle_parse_error(msg, &yylloc)
@ -262,6 +263,18 @@ constant_expression
declaration
: declaration_specifiers SEMICOLON
| declaration_specifiers init_declarator_list SEMICOLON {
for (auto d_s : *$1->list)
{
if ((d_s->type == NODE_TYPE_TOKEN) &&
(*d_s->token.text == "typedef"))
{
for (auto d : *$2->list)
{
observe_type_name(*d->declarator.name);
}
break;
}
}
}
;