From 25f7c43d3b2d6ca4f2194e407399af12573b4fd9 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 8 May 2018 22:16:53 -0400 Subject: [PATCH] lexer: return TYPE_NAME instead of IDENTIFIER for identifiers that were previously typedeffed --- src/parser/parser.cc | 5 +++++ src/parser/parser.h | 1 + src/parser/parser.l | 11 ++++++++++- src/parser/parser.yc | 13 +++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/parser/parser.cc b/src/parser/parser.cc index 74b0b50..ad94dd5 100644 --- a/src/parser/parser.cc +++ b/src/parser/parser.cc @@ -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; +} diff --git a/src/parser/parser.h b/src/parser/parser.h index 7666566..aff5e90 100644 --- a/src/parser/parser.h +++ b/src/parser/parser.h @@ -13,5 +13,6 @@ extern std::unordered_set 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 diff --git a/src/parser/parser.l b/src/parser/parser.l index 4ea9843..068d828 100644 --- a/src/parser/parser.l +++ b/src/parser/parser.l @@ -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); diff --git a/src/parser/parser.yc b/src/parser/parser.yc index d828a27..fde0fcb 100644 --- a/src/parser/parser.yc +++ b/src/parser/parser.yc @@ -2,6 +2,7 @@ #include #include "parser.h" +#include #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; + } + } } ;