/** * the "incl" state is used for picking up the name * of an include file */ %x incl %x c_comment %x cpp_comment %x using_namespace %x PP /** #define related states **/ %x define_state %x define_state_2 %x define_state_signature %x define_state_definition %x define_generic_c_comment %x define_generic_cpp_comment %x ifdef_state %x if_state %option yylineno identifier [a-zA-Z_][0-9a-zA-Z_]* simple_escape [abfnrtv'"?\\] octal_escape [0-7]{1,3} hex_escape "x"[0-9a-fA-F]+ escape_sequence [\\]({simple_escape}|{octal_escape}|{hex_escape}) c_char [^'\\\n]|{escape_sequence} s_char [^"\\\n]|{escape_sequence} ns_name [a-zA-Z][a-zA-Z:_]* using_ns "using "[ ]*"namespace" ns_alias "namespace "{ns_name}[ ]*"="[ ]*{ns_name}[ ]*";" %{ // Avoid spam output #ifdef ECHO #undef ECHO #endif #define ECHO // Never exit #ifdef YY_FATAL_ERROR #undef YY_FATAL_ERROR #endif #define YY_FATAL_ERROR(msg) #include #include #include #include "pp_include.h" #include "pp_lexer.h" #include "pptable.h" #define YYSTYPE wxString extern wxString pp_lval; int return_to_state = 0; int in_if_1 = 0; #define RET_VAL(x) {\ pp_lval = wxString::From8BitData(pp_text);\ return x;\ } std::string _definition; wxString g_definition; bool g_forCC = false; wxString g_filename; extern int pp_parse(); %} %option yylineno %% "//" { BEGIN(cpp_comment); } "/*" { BEGIN(c_comment); } "L"?[']{c_char}+['] {/* eat a string */} "L"?["]{s_char}*["] {/* eat a string */} # { BEGIN(PP); } define { BEGIN(define_state); RET_VAL(PP_DEFINE); } if { if(in_if_1) in_if_1++; BEGIN(if_state); RET_VAL(PP_IF); } 0 { RET_VAL(PP_ZERO); } __cplusplus { RET_VAL(PP_CPLUSPLUS); } ifdef { if(in_if_1) in_if_1++;BEGIN(ifdef_state); RET_VAL(PP_IFDEF); } defined { RET_VAL(PP_DEFINED); } ifndef { if(in_if_1) in_if_1++; BEGIN(ifdef_state); RET_VAL(PP_IFDEF); } undef { RET_VAL(PP_UNDEF); } else { if(in_if_1 == 1) in_if_1 = 0;RET_VAL(PP_ELSE); } elif { if(in_if_1 == 1) in_if_1--; BEGIN(if_state); RET_VAL(PP_ELIF); } endif { if(in_if_1) in_if_1--; RET_VAL(PP_ENDIF); } include { BEGIN(incl); RET_VAL(PP_INCLUDE); } \n { BEGIN(INITIAL); } . {} __cplusplus { RET_VAL(PP_CPLUSPLUS); } {identifier} { RET_VAL(PP_IDENTIFIER); } "(" { RET_VAL(((int)'(')); } ")" { RET_VAL(((int)')')); } \n { BEGIN(INITIAL); } . {} __cplusplus { RET_VAL(PP_CPLUSPLUS); } !defined { RET_VAL(PP_DEFINED); } defined { RET_VAL(PP_DEFINED); } {identifier} { RET_VAL(PP_IDENTIFIER); } "(" { RET_VAL(((int)'(')); } ")" { RET_VAL(((int)')')); } "&&" { RET_VAL(PP_AND); } "||" { RET_VAL(PP_OR); } "=" { RET_VAL(PP_EQUAL); } "!=" { RET_VAL(PP_NEQUAL); } ">" { RET_VAL(PP_GREATERTHAN); } "<" { RET_VAL(PP_LOWERTHAN); } [0-9]+ { RET_VAL(PP_INT); } \n { BEGIN(INITIAL); } . {} {identifier} { BEGIN(define_state_2); RET_VAL(PP_IDENTIFIER); } \\[\n\r]{1,2} { /* continue define_state */} \n { BEGIN(INITIAL);} . {} [ \t] { /* whitespaces are NOT allowed between the signature and the macro's name, * if we found a whitespace, handle this macro as a simple macro */ BEGIN(define_state_definition); _definition.clear(); } "/*" { return_to_state = define_state_2; BEGIN(define_generic_c_comment);} "//" { BEGIN(define_generic_cpp_comment); _definition.clear(); g_definition.Clear(); RET_VAL(PP_COMPLEX_REPLACEMENT); } "(" { BEGIN(define_state_signature); RET_VAL(((int)'(')); } \n { BEGIN(INITIAL); _definition.clear(); g_definition.Clear(); RET_VAL(PP_COMPLEX_REPLACEMENT);} . { BEGIN(define_state_definition); _definition = yytext;} {identifier} { RET_VAL(PP_IDENTIFIER); } , { RET_VAL(((int)*yytext)); } ")" { BEGIN(define_state_definition); _definition.clear(); g_definition.Clear(); RET_VAL(((int)*yytext));} . {} "/*" { return_to_state = define_state_definition; BEGIN(define_generic_c_comment);} "//" { BEGIN(define_generic_cpp_comment); g_definition = wxString::From8BitData(_definition.c_str()); RET_VAL(PP_COMPLEX_REPLACEMENT); } \\\n {} \n { BEGIN(INITIAL); g_definition = wxString::From8BitData(_definition.c_str()); RET_VAL(PP_COMPLEX_REPLACEMENT);} \t { _definition += " ";} \r {} . { _definition += yytext;} . {} \n {BEGIN(INITIAL);} . {} "*/" { BEGIN(return_to_state); } . {} \n { BEGIN(INITIAL); } . {} \n { BEGIN(INITIAL); } . {} /* do nothing */ "*/" { BEGIN(INITIAL); } . {} <> { yyterminate(); } %% int yywrap() { return 1; } int PPScan( const wxString &filePath, bool forCC ) { g_filename = filePath; BEGIN INITIAL; pp_lineno = 1; FILE* fp = fopen(filePath.To8BitData(), "r"); if ( fp == NULL ) { //printf("%s\n", strerror(errno)); // failed to open input file... return -1; } yy_switch_to_buffer( yy_create_buffer(fp, YY_BUF_SIZE) ); pp_in = fp; g_forCC = forCC; int rc = pp_parse(); g_forCC = false; if ( YY_CURRENT_BUFFER->yy_input_file ) { fclose( YY_CURRENT_BUFFER->yy_input_file ); YY_CURRENT_BUFFER->yy_input_file = NULL; } yy_delete_buffer ( YY_CURRENT_BUFFER ); yyterminate(); return rc; } int PPLex( const wxString &filePath) { g_filename = filePath; BEGIN INITIAL; pp_lineno = 1; FILE* fp = fopen(filePath.To8BitData(), "r"); if ( fp == NULL ) { return -1; } yy_switch_to_buffer( yy_create_buffer(fp, YY_BUF_SIZE) ); pp_in = fp; g_forCC = false; int type = 0; while( true ) { type = pp_lex(); if(type == 0) break; //printf("%s, %d\n", pp_lval.c_str(), type); } if ( YY_CURRENT_BUFFER->yy_input_file ) { fclose( YY_CURRENT_BUFFER->yy_input_file ); YY_CURRENT_BUFFER->yy_input_file = NULL; } yy_delete_buffer ( YY_CURRENT_BUFFER ); return type; } int PPScanString( const wxString &inputString ) { BEGIN INITIAL; yy_scan_string( inputString.To8BitData() ); pp_lineno = 1; int rc = pp_parse(); yyterminate(); return rc; } void pp_error(char* st) { }