| 1 |
/** |
| 2 |
* the "incl" state is used for picking up the name |
| 3 |
* of an include file |
| 4 |
*/ |
| 5 |
%x incl |
| 6 |
%x c_comment |
| 7 |
%x cpp_comment |
| 8 |
%x using_namespace |
| 9 |
%x PP |
| 10 |
|
| 11 |
/** #define related states **/ |
| 12 |
%x define_state |
| 13 |
%x define_state_2 |
| 14 |
%x define_state_signature |
| 15 |
%x define_state_definition |
| 16 |
%x define_generic_c_comment |
| 17 |
%x define_generic_cpp_comment |
| 18 |
%x ifdef_state |
| 19 |
%x if_state |
| 20 |
|
| 21 |
%option yylineno |
| 22 |
|
| 23 |
identifier [a-zA-Z_][0-9a-zA-Z_]* |
| 24 |
simple_escape [abfnrtv'"?\\] |
| 25 |
octal_escape [0-7]{1,3} |
| 26 |
hex_escape "x"[0-9a-fA-F]+ |
| 27 |
|
| 28 |
escape_sequence [\\]({simple_escape}|{octal_escape}|{hex_escape}) |
| 29 |
c_char [^'\\\n]|{escape_sequence} |
| 30 |
s_char [^"\\\n]|{escape_sequence} |
| 31 |
|
| 32 |
ns_name [a-zA-Z][a-zA-Z:_]* |
| 33 |
using_ns "using "[ ]*"namespace" |
| 34 |
ns_alias "namespace "{ns_name}[ ]*"="[ ]*{ns_name}[ ]*";" |
| 35 |
|
| 36 |
%{ |
| 37 |
// Avoid spam output |
| 38 |
#ifdef ECHO |
| 39 |
#undef ECHO |
| 40 |
#endif |
| 41 |
#define ECHO |
| 42 |
|
| 43 |
// Never exit |
| 44 |
#ifdef YY_FATAL_ERROR |
| 45 |
#undef YY_FATAL_ERROR |
| 46 |
#endif |
| 47 |
#define YY_FATAL_ERROR(msg) |
| 48 |
|
| 49 |
#include <list> |
| 50 |
#include <wx/string.h> |
| 51 |
#include <string> |
| 52 |
#include "pp_include.h" |
| 53 |
#include "pp_lexer.h" |
| 54 |
#include "pptable.h" |
| 55 |
|
| 56 |
#define YYSTYPE wxString |
| 57 |
extern wxString pp_lval; |
| 58 |
|
| 59 |
int return_to_state = 0; |
| 60 |
int in_if_1 = 0; |
| 61 |
|
| 62 |
#define RET_VAL(x) {\ |
| 63 |
pp_lval = wxString::From8BitData(pp_text);\ |
| 64 |
return x;\ |
| 65 |
} |
| 66 |
|
| 67 |
std::string _definition; |
| 68 |
wxString g_definition; |
| 69 |
bool g_forCC = false; |
| 70 |
wxString g_filename; |
| 71 |
|
| 72 |
extern int pp_parse(); |
| 73 |
%} |
| 74 |
|
| 75 |
%option yylineno |
| 76 |
|
| 77 |
%% |
| 78 |
|
| 79 |
"//" { |
| 80 |
BEGIN(cpp_comment); |
| 81 |
} |
| 82 |
|
| 83 |
"/*" { |
| 84 |
BEGIN(c_comment); |
| 85 |
} |
| 86 |
|
| 87 |
"L"?[']{c_char}+['] {/* eat a string */} |
| 88 |
"L"?["]{s_char}*["] {/* eat a string */} |
| 89 |
|
| 90 |
# { BEGIN(PP); } |
| 91 |
|
| 92 |
<PP>define { BEGIN(define_state); RET_VAL(PP_DEFINE); } |
| 93 |
<PP>if { if(in_if_1) in_if_1++; BEGIN(if_state); RET_VAL(PP_IF); } |
| 94 |
<PP>0 { RET_VAL(PP_ZERO); } |
| 95 |
<PP>__cplusplus { RET_VAL(PP_CPLUSPLUS); } |
| 96 |
<PP>ifdef { if(in_if_1) in_if_1++;BEGIN(ifdef_state); RET_VAL(PP_IFDEF); } |
| 97 |
<PP>defined { RET_VAL(PP_DEFINED); } |
| 98 |
<PP>ifndef { if(in_if_1) in_if_1++; BEGIN(ifdef_state); RET_VAL(PP_IFDEF); } |
| 99 |
<PP>undef { RET_VAL(PP_UNDEF); } |
| 100 |
<PP>else { if(in_if_1 == 1) in_if_1 = 0;RET_VAL(PP_ELSE); } |
| 101 |
<PP>elif { if(in_if_1 == 1) in_if_1--; BEGIN(if_state); RET_VAL(PP_ELIF); } |
| 102 |
<PP>endif { if(in_if_1) in_if_1--; RET_VAL(PP_ENDIF); } |
| 103 |
<PP>include { BEGIN(incl); RET_VAL(PP_INCLUDE); } |
| 104 |
<PP>\n { BEGIN(INITIAL); } |
| 105 |
<PP>. {} |
| 106 |
|
| 107 |
<ifdef_state>__cplusplus { RET_VAL(PP_CPLUSPLUS); } |
| 108 |
<ifdef_state>{identifier} { RET_VAL(PP_IDENTIFIER); } |
| 109 |
<ifdef_state>"(" { RET_VAL(((int)'(')); } |
| 110 |
<ifdef_state>")" { RET_VAL(((int)')')); } |
| 111 |
<ifdef_state>\n { BEGIN(INITIAL); } |
| 112 |
<ifdef_state>. {} |
| 113 |
|
| 114 |
<if_state>__cplusplus { RET_VAL(PP_CPLUSPLUS); } |
| 115 |
<if_state>!defined { RET_VAL(PP_DEFINED); } |
| 116 |
<if_state>defined { RET_VAL(PP_DEFINED); } |
| 117 |
<if_state>{identifier} { RET_VAL(PP_IDENTIFIER); } |
| 118 |
<if_state>"(" { RET_VAL(((int)'(')); } |
| 119 |
<if_state>")" { RET_VAL(((int)')')); } |
| 120 |
<if_state>"&&" { RET_VAL(PP_AND); } |
| 121 |
<if_state>"||" { RET_VAL(PP_OR); } |
| 122 |
<if_state>"=" { RET_VAL(PP_EQUAL); } |
| 123 |
<if_state>"!=" { RET_VAL(PP_NEQUAL); } |
| 124 |
<if_state>">" { RET_VAL(PP_GREATERTHAN); } |
| 125 |
<if_state>"<" { RET_VAL(PP_LOWERTHAN); } |
| 126 |
<if_state>[0-9]+ { RET_VAL(PP_INT); } |
| 127 |
<if_state>\n { BEGIN(INITIAL); } |
| 128 |
<if_state>. {} |
| 129 |
|
| 130 |
<define_state>{identifier} { BEGIN(define_state_2); RET_VAL(PP_IDENTIFIER); } |
| 131 |
<define_state>\\[\n\r]{1,2} { /* continue define_state */} |
| 132 |
<define_state>\n { BEGIN(INITIAL);} |
| 133 |
<define_state>. {} |
| 134 |
|
| 135 |
<define_state_2>[ \t] { |
| 136 |
/* whitespaces are NOT allowed between the signature and the macro's name, |
| 137 |
* if we found a whitespace, handle this macro as a simple macro */ |
| 138 |
BEGIN(define_state_definition); |
| 139 |
_definition.clear(); |
| 140 |
} |
| 141 |
<define_state_2>"/*" { return_to_state = define_state_2; BEGIN(define_generic_c_comment);} |
| 142 |
<define_state_2>"//" { |
| 143 |
BEGIN(define_generic_cpp_comment); |
| 144 |
_definition.clear(); |
| 145 |
g_definition.Clear(); |
| 146 |
RET_VAL(PP_COMPLEX_REPLACEMENT); |
| 147 |
} |
| 148 |
<define_state_2>"(" { BEGIN(define_state_signature); RET_VAL(((int)'(')); } |
| 149 |
<define_state_2>\n { BEGIN(INITIAL); _definition.clear(); g_definition.Clear(); RET_VAL(PP_COMPLEX_REPLACEMENT);} |
| 150 |
<define_state_2>. { BEGIN(define_state_definition); _definition = yytext;} |
| 151 |
|
| 152 |
<define_state_signature>{identifier} { RET_VAL(PP_IDENTIFIER); } |
| 153 |
<define_state_signature>, { RET_VAL(((int)*yytext)); } |
| 154 |
<define_state_signature>")" { BEGIN(define_state_definition); _definition.clear(); g_definition.Clear(); RET_VAL(((int)*yytext));} |
| 155 |
<define_state_signature>. {} |
| 156 |
|
| 157 |
<define_state_definition>"/*" { return_to_state = define_state_definition; BEGIN(define_generic_c_comment);} |
| 158 |
<define_state_definition>"//" { |
| 159 |
BEGIN(define_generic_cpp_comment); |
| 160 |
g_definition = wxString::From8BitData(_definition.c_str()); |
| 161 |
RET_VAL(PP_COMPLEX_REPLACEMENT); |
| 162 |
} |
| 163 |
<define_state_definition>\\\n {} |
| 164 |
<define_state_definition>\n { BEGIN(INITIAL); g_definition = wxString::From8BitData(_definition.c_str()); RET_VAL(PP_COMPLEX_REPLACEMENT);} |
| 165 |
<define_state_definition>\t { _definition += " ";} |
| 166 |
<define_state_definition>\r {} |
| 167 |
<define_state_definition>. { _definition += yytext;} |
| 168 |
|
| 169 |
. {} |
| 170 |
|
| 171 |
<incl>\n {BEGIN(INITIAL);} |
| 172 |
<incl>. {} |
| 173 |
|
| 174 |
<define_generic_c_comment>"*/" { |
| 175 |
BEGIN(return_to_state); |
| 176 |
} |
| 177 |
|
| 178 |
<define_generic_c_comment>. {} |
| 179 |
|
| 180 |
<define_generic_cpp_comment>\n { |
| 181 |
BEGIN(INITIAL); |
| 182 |
} |
| 183 |
<define_generic_cpp_comment>. {} |
| 184 |
|
| 185 |
<cpp_comment>\n { |
| 186 |
BEGIN(INITIAL); |
| 187 |
} |
| 188 |
<cpp_comment>. {} /* do nothing */ |
| 189 |
|
| 190 |
<c_comment>"*/" { |
| 191 |
BEGIN(INITIAL); |
| 192 |
} |
| 193 |
|
| 194 |
<c_comment>. {} |
| 195 |
|
| 196 |
<<EOF>> { |
| 197 |
yyterminate(); |
| 198 |
} |
| 199 |
|
| 200 |
%% |
| 201 |
|
| 202 |
int yywrap() { |
| 203 |
return 1; |
| 204 |
} |
| 205 |
|
| 206 |
int PPScan( const wxString &filePath, bool forCC ) |
| 207 |
{ |
| 208 |
g_filename = filePath; |
| 209 |
|
| 210 |
BEGIN INITIAL; |
| 211 |
pp_lineno = 1; |
| 212 |
FILE* fp = fopen(filePath.To8BitData(), "r"); |
| 213 |
if ( fp == NULL ) { |
| 214 |
//printf("%s\n", strerror(errno)); |
| 215 |
// failed to open input file... |
| 216 |
return -1; |
| 217 |
} |
| 218 |
|
| 219 |
yy_switch_to_buffer( yy_create_buffer(fp, YY_BUF_SIZE) ); |
| 220 |
pp_in = fp; |
| 221 |
g_forCC = forCC; |
| 222 |
int rc = pp_parse(); |
| 223 |
g_forCC = false; |
| 224 |
|
| 225 |
if ( YY_CURRENT_BUFFER->yy_input_file ) { |
| 226 |
fclose( YY_CURRENT_BUFFER->yy_input_file ); |
| 227 |
YY_CURRENT_BUFFER->yy_input_file = NULL; |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
yy_delete_buffer ( YY_CURRENT_BUFFER ); |
| 232 |
yyterminate(); |
| 233 |
return rc; |
| 234 |
} |
| 235 |
|
| 236 |
int PPLex( const wxString &filePath) |
| 237 |
{ |
| 238 |
g_filename = filePath; |
| 239 |
|
| 240 |
BEGIN INITIAL; |
| 241 |
pp_lineno = 1; |
| 242 |
FILE* fp = fopen(filePath.To8BitData(), "r"); |
| 243 |
if ( fp == NULL ) { |
| 244 |
return -1; |
| 245 |
} |
| 246 |
|
| 247 |
yy_switch_to_buffer( yy_create_buffer(fp, YY_BUF_SIZE) ); |
| 248 |
pp_in = fp; |
| 249 |
g_forCC = false; |
| 250 |
int type = 0; |
| 251 |
while( true ) { |
| 252 |
type = pp_lex(); |
| 253 |
if(type == 0) break; |
| 254 |
//printf("%s, %d\n", pp_lval.c_str(), type); |
| 255 |
} |
| 256 |
|
| 257 |
if ( YY_CURRENT_BUFFER->yy_input_file ) { |
| 258 |
fclose( YY_CURRENT_BUFFER->yy_input_file ); |
| 259 |
YY_CURRENT_BUFFER->yy_input_file = NULL; |
| 260 |
|
| 261 |
} |
| 262 |
|
| 263 |
yy_delete_buffer ( YY_CURRENT_BUFFER ); |
| 264 |
return type; |
| 265 |
} |
| 266 |
|
| 267 |
int PPScanString( const wxString &inputString ) |
| 268 |
{ |
| 269 |
BEGIN INITIAL; |
| 270 |
yy_scan_string( inputString.To8BitData() ); |
| 271 |
pp_lineno = 1; |
| 272 |
|
| 273 |
int rc = pp_parse(); |
| 274 |
|
| 275 |
yyterminate(); |
| 276 |
return rc; |
| 277 |
} |
| 278 |
|
| 279 |
void pp_error(char* st) |
| 280 |
{ |
| 281 |
} |