1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
%{
// Copyright Eran Ifrah(c)
%}
%{
/*************** Includes and Defines *****************************/
#include <wx/string.h>
#include "pptable.h"
#define YYSTYPE wxString
#define YYDEBUG 0 /* get the pretty debugging code to compile*/
#ifdef yylex
#undef yylex
#define yylex pp_lex
#endif
extern void pp_error(char *st);
extern int pp_lex();
extern wxString g_definition;
extern int in_if_1;
extern bool g_forCC;
extern wxString g_filename;
// Static
static std::vector<wxString> g_tmpMacros;
/*************** Standard ytab.c continues here *********************/
%}
/* This group is used by the C/C++ language parser */
%token PP_DEFINE PP_IF PP_IFDEF PP_DEFINED
%token PP_UNDEF PP_ELSE PP_ELIF PP_ENDIF
%token PP_POUND PP_IDENTIFIER PP_COMPLEX_REPLACEMENT PP_IFNDEF
%token PP_ZERO PP_CPLUSPLUS PP_INCLUDE PP_DEFINED
%token PP_AND PP_OR PP_EQUAL PP_NEQUAL
%token PP_INT PP_LOWERTHAN PP_GREATERTHAN
%start translation_unit
%%
translation_unit : /*empty*/
| translation_unit the_macros_rule
;
/** proxy rule so we can perform our cleanup **/
the_macros_rule: {g_tmpMacros.clear();} macros
;
/**
* We currently support 3 types of macros:
* 1) Simple define macros (e.g. #define VALUE 1)
* 2) Function like macros (e.g. #define FOO(x) printf("%s\n", x);)
* 3) Conditional macros:
* 3.1) #if (condition)
* 3.2) #ifdef (NAME)
* 3.3) #if defined(COND) && defiend(COND2)
*/
macros: define_simple_macros
| define_func_like_macros
| if_cplusplus
| ifdef_simple_macro
| if_macros
| elif_macros
| end_if
| error {
//wxPrintf(wxT("CodeLite: syntax error, unexpected token '%s' found\n"), pp_lval.c_str());
}
;
end_if : PP_ENDIF
;
if_cplusplus : PP_IF PP_CPLUSPLUS
{
if(in_if_1 == 0)
in_if_1 = 1;
}
;
define_simple_macros: PP_DEFINE PP_IDENTIFIER PP_COMPLEX_REPLACEMENT
{
PPToken token;
token.fileName = g_filename;
token.name = $2;
token.flags = (PPToken::IsValid | PPToken::IsOverridable);
if(in_if_1) {
// the token was found in a '#if 1' block - dont allow overriding it
token.flags &= ~(PPToken::IsOverridable);
}
token.replacement = g_definition;
if(g_forCC) {
if(!token.replacement.empty()) {
wxChar ch = token.replacement.at(0);
if( !(ch >= (int)wxT('0') && ch <= (int)wxT('9')) )
PPTable::Instance()->Add( token );
}
} else {
PPTable::Instance()->Add( token );
}
}
;
define_func_like_macros: PP_DEFINE PP_IDENTIFIER '(' args_list ')' PP_COMPLEX_REPLACEMENT
{
PPToken token;
token.fileName = g_filename;
token.name = $2;
token.replacement = g_definition;
token.flags = (PPToken::IsFunctionLike | PPToken::IsValid | PPToken::IsOverridable);
if(in_if_1) {
// the token was found in a '#if 1' block - dont allow overriding it
token.flags &= ~(PPToken::IsOverridable);
}
token.processArgs( $4 );
PPTable::Instance()->Add( token );
}
;
args_list: /* empty */
| PP_IDENTIFIER { $$ = $1; }
| args_list ',' PP_IDENTIFIER { $$ = $1 + $2 + $3; }
;
ifdef_simple_macro: PP_IFDEF PP_IDENTIFIER
{
PPTable::Instance()->AddUsed($2);
}
;
if_macros: PP_IF if_condition
;
elif_macros: PP_ELIF if_condition
;
if_condition: generic_condition
{
for(size_t i=0; i<g_tmpMacros.size(); i++) {
//wxPrintf(wxT("Collected: %s\n"), g_tmpMacros[i].c_str());
PPTable::Instance()->AddUsed(g_tmpMacros[i]);
}
}
;
generic_condition: generic_condition_base
| generic_condition logical_operator generic_condition_base
;
generic_condition_base: /*empty*/
| '(' PP_IDENTIFIER ')' {g_tmpMacros.push_back($2);}
| PP_IDENTIFIER {g_tmpMacros.push_back($1);}
| PP_IDENTIFIER test_operator PP_INT {g_tmpMacros.push_back($1);}
| '(' PP_IDENTIFIER test_operator PP_INT ')' {g_tmpMacros.push_back($2);}
| PP_DEFINED '(' PP_IDENTIFIER ')' {g_tmpMacros.push_back($3);}
;
logical_operator: PP_AND
| PP_OR
;
test_operator: PP_EQUAL
| PP_NEQUAL
| PP_LOWERTHAN
| PP_GREATERTHAN
;
%%
|