%{ // //====================-------------------------------------------------------------- // Eran Ifrah 2014 (c) //====================-------------------------------------------------------------- // // To generate a source file from this .l file, you will need // a flex version 2.5.34 and later // Under Windows, you will need to run the following command // from within *MSYS* terminal (or run codelite from an MSYS shell): // /usr/bin/flex --noline --batch --outfile=CxxLexer.cpp CxxScanner.l // //====================-------------------------------------------------------------- // extern "C" int yywrap(void*) { return 1; } #include #include #include #include "CxxLexerAPI.h" #include "CxxScannerTokens.h" #define YY_FATAL_ERROR(x) #define YYSTYPE char* #define ECHO #define P(s) #define YY_NO_UNISTD_H #define YY_USER_ACTION yycolumn += yyleng; #define RETURN_WHITESPACE() \ CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; \ if(userData->IsCollectingWhitespace()) { \ return T_WHITESPACE; \ } #define RETURN_NEWLINE() \ CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; \ if(userData->IsCollectingWhitespace()) { \ return T_NEWLINE; \ } %} /* regex and modes */ /* options */ %option yylineno %option default %option reentrant %x PREPR %x WRAP_PREP %x CPP_COMMENT %x C_COMMENT %x INCLUDE %x RAW_STRING identifier [a-zA-Z_][0-9a-zA-Z_]* exponent_part [eE][-+]?[0-9]+ fractional_constant ([0-9]*"."[0-9]+)|([0-9]+".") floating_constant (({fractional_constant}{exponent_part}?)|([0-9]+{exponent_part}))[FfLl]? dchar_seq [^()\\[:space:]]{0,16} integer_suffix_opt ([uU]?[lL]?)|([lL][uU]) decimal_constant [1-9][0-9]*{integer_suffix_opt} octal_constant "0"[0-7]*{integer_suffix_opt} hex_constant "0"[xX][0-9a-fA-F]+{integer_suffix_opt} 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} h_tab [\011] form_feed [\014] v_tab [\013] c_return [\015] horizontal_white [ ]|{h_tab} %% <> {yyterminate();} alignas { return T_ALIGNAS; } alignof { return T_ALIGNOF; } and { return T_AND; } and_eq { return T_AND_EQ; } asm { return T_ASM; } auto { return T_AUTO; } bitand { return T_BITAND; } bitor { return T_BITOR; } bool { return T_BOOL; } break { return T_BREAK; } case { return T_CASE; } catch { return T_CATCH;} char { return T_CHAR;} char16_t { return T_CHAR16_T;} char32_t { return T_CHAR32_T;} class { return T_CLASS;} compl { return T_COMPL;} const { return T_CONST;} constexpr { return T_CONSTEXPR;} const_cast { return T_CONST_CAST;} continue { return T_CONTINUE;} decltype { return T_DECLTYPE;} default { return T_DEFAULT;} delete { return T_DELETE;} do { return T_DO;} double { return T_DOUBLE;} dynamic_cast { return T_DYNAMIC_CAST;} else { return T_ELSE;} enum { return T_ENUM;} explicit { return T_EXPLICIT;} export { return T_EXPORT;} extern { return T_EXTERN;} false { return T_FALSE;} final { return T_FINAL;} float { return T_FLOAT;} for { return T_FOR;} friend { return T_FRIEND;} goto { return T_GOTO;} if { return T_IF;} inline { return T_INLINE;} int { return T_INT;} long { return T_LONG;} mutable { return T_MUTABLE;} namespace { return T_NAMESPACE;} new { return T_NEW;} noexcept { return T_NOEXCEPT;} not { return T_NOT;} not_eq { return T_NOT_EQ;} nullptr { return T_NULLPTR;} operator { return T_OPERATOR;} or { return T_OR;} or_eq { return T_OR_EQ;} override { return T_OVERRIDE;} private { return T_PRIVATE;} protected { return T_PROTECTED;} public { return T_PUBLIC;} register { return T_REGISTER;} reinterpret_cast { return T_REINTERPRET_CAST;} return { return T_RETURN;} short { return T_SHORT;} signed { return T_SIGNED;} sizeof { return T_SIZEOF;} static { return T_STATIC;} static_assert { return T_STATIC_ASSERT;} static_cast { return T_STATIC_CAST;} struct { return T_STRUCT;} switch { return T_SWITCH;} template { return T_TEMPLATE;} this { return T_THIS;} thread_local { return T_THREAD_LOCAL;} throw { return T_THROW;} true { return T_TRUE;} try { return T_TRY;} typedef { return T_TYPEDEF;} typeid { return T_TYPEID;} typename { return T_TYPENAME;} union { return T_UNION;} unsigned { return T_UNSIGNED;} using { return T_USING;} virtual { return T_VIRTUAL;} void { return T_VOID;} volatile { return T_VOLATILE;} wchar_t { return T_WCHAR_T;} while { return T_WHILE;} xor { return T_XOR;} xor_eq { return T_XOR_EQ;} \n {} {horizontal_white}+ { RETURN_WHITESPACE(); } ({v_tab}|{c_return}|{form_feed})+ {RETURN_WHITESPACE();} ({horizontal_white}|{v_tab}|{c_return}|{form_feed})*"\n" { RETURN_NEWLINE(); } {decimal_constant} { return T_DEC_NUMBER;} {octal_constant} { return T_OCTAL_NUMBER;} {hex_constant} { return T_HEX_NUMBER; } {floating_constant} { return T_FLOAT_NUMBER;} ".*" { return T_DOT_STAR; } "::" { return T_DOUBLE_COLONS; } "->" { return T_ARROW; } "->*" { return T_ARROW_STAR; } "++" { return T_PLUS_PLUS; } "--" { return T_MINUS_MINUS; } "<<" { return T_LS; } "<=" { return T_LE; } ">=" { return T_GE; } "==" { return T_EQUAL; } "!=" { return T_NOT_EQUAL; } "&&" { return T_AND_AND;} "||" { return T_OR_OR;} "*=" { return T_STAR_EQUAL;} "/=" { return T_SLASH_EQUAL;} "%=" { return T_DIV_EQUAL;} "+=" { return T_PLUS_EQUAL;} "-=" { return T_MINUS_EQUAL;} "<<=" { return T_LS_ASSIGN;} ">>=" { return T_RS_ASSIGN;} "&=" { return T_AND_EQUAL;} "^=" { return T_POW_EQUAL;} "|=" { return T_OR_EQUAL;} "..." { return T_3_DOTS;} "L"?[']{c_char}+['] { return T_STRING; } "L"?["]{s_char}*["] { return T_STRING; } R["]{dchar_seq}[(] { BEGIN(RAW_STRING); CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; // R"( userData->set_raw_string_label(yytext + 2 /* skip the `R"` */, yyleng - 3 /* we want just the d-char */); userData->clear_raw_string(); } "/*" { // Clear the comment collected BEGIN C_COMMENT; CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; if(userData->IsCollectingComments()) { userData->ClearComment(); userData->AppendToComment("/*"); userData->SetCommentStartLine(yylineno); } } "*/" { CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; if(userData->IsCollectingComments()) { userData->AppendToComment("*/"); userData->SetCommentEndLine(yylineno); } BEGIN INITIAL; if(userData->IsCollectingComments()) { return T_C_COMMENT; } } "\n" { CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; if(userData->IsCollectingComments()) { userData->AppendToComment("\n"); } } . { CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; if(userData->IsCollectingComments()) { userData->AppendToComment(yytext[0]); } } "//" { int oldState = yyg->yy_start; BEGIN CPP_COMMENT; CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; if(userData->IsCollectingComments()) { userData->ClearComment(); userData->AppendToComment("//"); userData->SetCommentStartLine(yylineno); } if((1 + 2 * PREPR) == oldState) { return T_PP_STATE_EXIT; } } "\\\n" { CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; if(userData->IsCollectingComments()) { userData->AppendToComment("\\\n"); } } "\\\r\n" { CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; if(userData->IsCollectingComments()) { userData->AppendToComment("\\\r\n"); } } "\n" { BEGIN INITIAL; CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; if(userData->IsCollectingComments()) { userData->AppendToComment("\n"); return T_CXX_COMMENT; } } . { CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; if(userData->IsCollectingComments()) { userData->AppendToComment(yytext[0]); } } {identifier} { return T_IDENTIFIER; } "#" { BEGIN(PREPR); CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; userData->SetPreProcessorSection(true); } . { return yytext[0]; } "\\\n" { /* keep the current state */ } "\\\r\n" { /* keep the current state */ } [ \t\r\v\b]+ { /* do nothing */ RETURN_WHITESPACE(); } "\n" { BEGIN(INITIAL); CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; userData->SetPreProcessorSection(false); return T_PP_STATE_EXIT; } "define" { return T_PP_DEFINE; } "defined" { return T_PP_DEFINED; } "if" { return T_PP_IF; } "ifdef" { return T_PP_IFDEF; } "ifndef" { return T_PP_IFNDEF; } "else" { return T_PP_ELSE; } "elif" { return T_PP_ELIF; } "endif" { return T_PP_ENDIF; } "line" { return T_PP_LINE; } "pragma" { return T_PP_PRAGMA; } "undef" { return T_PP_UNDEF; } "error" { return T_PP_ERROR; } "&&" { return T_PP_AND; } "||" { return T_PP_OR; } ">" { return T_PP_GT; } ">=" { return T_PP_GTEQ; } "<" { return T_PP_LT; } "<=" { return T_PP_LTEQ; } "include" { BEGIN(INCLUDE); } "L"?[']{c_char}+['] { return T_PP_STRING; } "L"?["]{s_char}*["] { return T_PP_STRING; } {decimal_constant} { return T_PP_DEC_NUMBER;} {octal_constant} { return T_PP_OCTAL_NUMBER;} {hex_constant} { return T_PP_HEX_NUMBER; } {floating_constant} { return T_PP_FLOAT_NUMBER;} {identifier} { return T_PP_IDENTIFIER; } . { return yytext[0]; } [)]{dchar_seq}["] { CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; std::string delim_found(yytext + 1, yyleng - 2); // we just want the `dchar_seq` sequence if(delim_found == userData->get_raw_string_label()) { BEGIN(INITIAL); return T_RAW_STRING; } else { userData->append_raw_string(yytext, yyleng); } } \n { CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; userData->append_raw_string('\n'); } . { CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; userData->append_raw_string(yytext[0]); } \n { BEGIN(INITIAL); CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; userData->SetPreProcessorSection(false); return T_PP_STATE_EXIT; } [ \t]* { RETURN_WHITESPACE(); } ["<][^ \t\n]+[">] { /* got the include file name */ return T_PP_INCLUDE_FILENAME; } %% //=============------------------------------- // API methods implementation //=============------------------------------- void* LexerNew(const wxString& content, size_t options ) { yyscan_t scanner; yylex_init(&scanner); struct yyguts_t * yyg = (struct yyguts_t*)scanner; CppLexerUserData *userData = new CppLexerUserData(options); // keep the file pointer (and make sure we close it at the end) userData->SetCurrentPF(NULL); yyg->yyextra_r = userData; wxCharBuffer cb = content.mb_str(wxConvUTF8); yy_switch_to_buffer(yy_scan_string(cb.data(), scanner), scanner); yycolumn = 0; yylineno = 0; return scanner; } void* LexerNew(const wxFileName& filename, size_t options ) { wxFileName fn = filename; if(fn.IsRelative()) { fn.MakeAbsolute(); } FILE* fp = ::fopen(fn.GetFullPath().mb_str(wxConvUTF8).data(), "rb"); if(!fp) { return NULL; } yyscan_t scanner; yylex_init(&scanner); struct yyguts_t * yyg = (struct yyguts_t*)scanner; CppLexerUserData *userData = new CppLexerUserData(options); // keep the file pointer (and make sure we close it at the end) userData->SetCurrentPF(fp); yyg->yyextra_r = userData; yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE, scanner), scanner); yycolumn = 1; yylineno = 0; return scanner; } void LexerDestroy(void** scanner) { struct yyguts_t * yyg = (struct yyguts_t*)(*scanner); delete (CppLexerUserData*)yyg->yyextra_r; yy_delete_buffer(YY_CURRENT_BUFFER, *scanner); yylex_destroy(*scanner); *scanner = NULL; } void LexerUnget(void* scanner) { // return the entire token back to the input stream struct yyguts_t * yyg = (struct yyguts_t*)scanner; yyless(0); } bool LexerNext(void* scanner, CxxLexerToken& token) { token.SetColumn(0); token.SetType(yylex(scanner)); token.ClearComment(); token.ClearRawString(); if(!token.IsEOF()) { struct yyguts_t * yyg = (struct yyguts_t*)scanner; CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; switch(token.GetType()) { case T_RAW_STRING: token.SetLineNumber(yylineno); token.SetRawString(userData->get_raw_string()); userData->clear_raw_string_label(); break; case T_CXX_COMMENT: // One line up for CXX comments token.SetLineNumber(userData->GetCommentStartLine()); token.SetComment(userData->GetComment()); userData->ClearComment(); break; case T_C_COMMENT: token.SetLineNumber(userData->GetCommentStartLine()); token.SetComment(userData->GetComment()); userData->ClearComment(); break; default: token.SetLineNumber(yylineno); token.SetText(yytext); token.SetColumn(yycolumn); break; } } else { token.SetText(NULL); token.SetLineNumber(0); token.SetColumn(0); } return !token.IsEOF(); } wxString LexerCurrentToken(void* scanner) { struct yyguts_t * yyg = (struct yyguts_t*)scanner; return yytext; } CppLexerUserData* LexerGetUserData(void* scanner) { struct yyguts_t * yyg = (struct yyguts_t*)scanner; return (CppLexerUserData*) yyg->yyextra_r; }