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