| 1 |
%{ |
| 2 |
// |
| 3 |
//====================-------------------------------------------------------------- |
| 4 |
// Eran Ifrah 2014 (c) |
| 5 |
//====================-------------------------------------------------------------- |
| 6 |
// |
| 7 |
// To generate a source file from this .l file, you will need |
| 8 |
// a flex version 2.5.34 and later |
| 9 |
// Under Windows, you will need to run the following command |
| 10 |
// from within *MSYS* terminal (or run codelite from an MSYS shell): |
| 11 |
// /usr/bin/flex -Pxml --batch --outfile=XmlLexer.cpp XMLLexer.l |
| 12 |
// |
| 13 |
//====================-------------------------------------------------------------- |
| 14 |
// |
| 15 |
extern "C" int yywrap(void*) { return 1; } |
| 16 |
|
| 17 |
#include <wx/string.h> |
| 18 |
#include <string> |
| 19 |
#include <wx/filename.h> |
| 20 |
#include "XMLLexerAPI.h" |
| 21 |
#include "XMLLexerTokens.h" |
| 22 |
|
| 23 |
#define YY_FATAL_ERROR(x) |
| 24 |
#define YYSTYPE char* |
| 25 |
#define ECHO |
| 26 |
#define P(s) fprintf(stderr, "%s\n", s) |
| 27 |
|
| 28 |
#define YY_NO_UNISTD_H |
| 29 |
#define YY_USER_ACTION yycolumn += yyleng; |
| 30 |
|
| 31 |
#define RETURN_WHITESPACE() |
| 32 |
#define RETURN_NEWLINE() |
| 33 |
|
| 34 |
%} |
| 35 |
|
| 36 |
/* regex and modes */ |
| 37 |
|
| 38 |
/* options */ |
| 39 |
%option yylineno |
| 40 |
%option default |
| 41 |
%option reentrant |
| 42 |
|
| 43 |
identifier [$a-zA-Z_][$0-9a-zA-Z_\-]* |
| 44 |
exponent_part [eE][-+]?[0-9]+ |
| 45 |
fractional_constant ([0-9]*"."[0-9]+)|([0-9]+".") |
| 46 |
floating_constant (({fractional_constant}{exponent_part}?)|([0-9]+{exponent_part}))[FfLl]? |
| 47 |
|
| 48 |
integer_suffix_opt ([uU]?[lL]?)|([lL][uU]) |
| 49 |
decimal_constant [1-9][0-9]*{integer_suffix_opt} |
| 50 |
octal_constant "0"[0-7]*{integer_suffix_opt} |
| 51 |
hex_constant "0"[xX][0-9a-fA-F]+{integer_suffix_opt} |
| 52 |
|
| 53 |
simple_escape [abfnrtv'"?\\] |
| 54 |
octal_escape [0-7]{1,3} |
| 55 |
hex_escape "x"[0-9a-fA-F]+ |
| 56 |
|
| 57 |
escape_sequence [\\]({simple_escape}|{octal_escape}|{hex_escape}) |
| 58 |
c_char [^'\\\n]|{escape_sequence} |
| 59 |
s_char [^"\\\n]|{escape_sequence} |
| 60 |
|
| 61 |
h_tab [\011] |
| 62 |
form_feed [\014] |
| 63 |
v_tab [\013] |
| 64 |
c_return [\015] |
| 65 |
|
| 66 |
horizontal_white [ ]|{h_tab} |
| 67 |
|
| 68 |
%x CDATA |
| 69 |
%x COMMENT |
| 70 |
%x NOXML |
| 71 |
|
| 72 |
%% |
| 73 |
<<EOF>> {yyterminate();} |
| 74 |
<INITIAL>{horizontal_white} {} |
| 75 |
<INITIAL>"/>" {return kXML_T_CLOSE_TAG_SUFFIX;} |
| 76 |
<INITIAL>"</" {return kXML_T_CLOSE_TAG_PREFIX;} |
| 77 |
<INITIAL>"<?" { BEGIN NOXML; return kXML_T_XML_OPEN_TAG;} |
| 78 |
<INITIAL>"<!DOCTYPE" {return kXML_T_HTML_DOCTYPE;} |
| 79 |
<INITIAL>"<![CDATA[" { BEGIN CDATA; return kXML_T_CDATA_START; } |
| 80 |
<INITIAL>"<" {return kXML_T_ENTITY_LT;} |
| 81 |
<INITIAL>">" {return kXML_T_ENTITY_GT;} |
| 82 |
<INITIAL>"&" {return kXML_T_ENTITY_AMP;} |
| 83 |
<INITIAL>"'" {return kXML_T_ENTITY_APOS;} |
| 84 |
<INITIAL>""" {return kXML_T_ENTITY_QUOATATION_MARK;} |
| 85 |
<INITIAL>"<!--" { BEGIN COMMENT; return kXML_T_COMMENT_START;} |
| 86 |
<INITIAL>{identifier} {return kXML_T_IDENTIFIER;} |
| 87 |
<INITIAL>[']{c_char}*['] { return kXML_T_STRING;} |
| 88 |
<INITIAL>["]{s_char}*["] { return kXML_T_STRING;} |
| 89 |
<INITIAL>. { return yytext[0];} |
| 90 |
<CDATA>"]]>" { |
| 91 |
BEGIN INITIAL; |
| 92 |
return kXML_T_CDATA_END; |
| 93 |
} |
| 94 |
<CDATA>. { return yytext[0];} |
| 95 |
<COMMENT>"-->" { |
| 96 |
BEGIN INITIAL; |
| 97 |
return kXML_T_COMMENT_END; |
| 98 |
} |
| 99 |
<COMMENT>. { return yytext[0];} |
| 100 |
<NOXML>"?>" { |
| 101 |
BEGIN INITIAL; |
| 102 |
return kXML_T_XML_CLOSE_TAG; |
| 103 |
} |
| 104 |
<NOXML>. { return yytext[0];} |
| 105 |
%% |
| 106 |
|
| 107 |
//=============------------------------------- |
| 108 |
// API methods implementation |
| 109 |
//=============------------------------------- |
| 110 |
|
| 111 |
void* xmlLexerNew(const wxString& content) |
| 112 |
{ |
| 113 |
yyscan_t scanner; |
| 114 |
yylex_init(&scanner); |
| 115 |
struct yyguts_t * yyg = (struct yyguts_t*)scanner; |
| 116 |
XMLLexerUserData *userData = new XMLLexerUserData(); |
| 117 |
|
| 118 |
// keep the file pointer (and make sure we close it at the end) |
| 119 |
userData->SetCurrentPF(NULL); |
| 120 |
yyg->yyextra_r = userData; |
| 121 |
|
| 122 |
wxCharBuffer cb = content.mb_str(wxConvUTF8); |
| 123 |
yy_switch_to_buffer(yy_scan_string(cb.data(), scanner), scanner); |
| 124 |
yycolumn = 1; |
| 125 |
yylineno = 0; |
| 126 |
return scanner; |
| 127 |
} |
| 128 |
|
| 129 |
void* xmlLexerNew(const wxFileName& filename) |
| 130 |
{ |
| 131 |
wxFileName fn = filename; |
| 132 |
if(fn.IsRelative()) { |
| 133 |
fn.MakeAbsolute(); |
| 134 |
} |
| 135 |
|
| 136 |
FILE* fp = ::fopen(fn.GetFullPath().mb_str(wxConvUTF8).data(), "rb"); |
| 137 |
if(!fp) { |
| 138 |
return NULL; |
| 139 |
} |
| 140 |
yyscan_t scanner; |
| 141 |
yylex_init(&scanner); |
| 142 |
struct yyguts_t * yyg = (struct yyguts_t*)scanner; |
| 143 |
XMLLexerUserData *userData = new XMLLexerUserData(); |
| 144 |
|
| 145 |
// keep the file pointer (and make sure we close it at the end) |
| 146 |
userData->SetCurrentPF(fp); |
| 147 |
yyg->yyextra_r = userData; |
| 148 |
|
| 149 |
yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE, scanner), scanner); |
| 150 |
yycolumn = 1; |
| 151 |
yylineno = 0; |
| 152 |
return scanner; |
| 153 |
} |
| 154 |
|
| 155 |
void xmlLexerDestroy(void** scanner) |
| 156 |
{ |
| 157 |
struct yyguts_t * yyg = (struct yyguts_t*)(*scanner); |
| 158 |
delete (XMLLexerUserData*)yyg->yyextra_r; |
| 159 |
yy_delete_buffer(YY_CURRENT_BUFFER, *scanner); |
| 160 |
|
| 161 |
yylex_destroy(*scanner); |
| 162 |
*scanner = NULL; |
| 163 |
} |
| 164 |
|
| 165 |
void xmlLexerUnget(void* scanner) |
| 166 |
{ |
| 167 |
// return the entire token back to the input stream |
| 168 |
struct yyguts_t * yyg = (struct yyguts_t*)scanner; |
| 169 |
yyless(0); |
| 170 |
} |
| 171 |
|
| 172 |
wxString xmlLexerText(void* scanner) |
| 173 |
{ |
| 174 |
struct yyguts_t * yyg = (struct yyguts_t*)scanner; |
| 175 |
return wxString(yytext, wxConvUTF8); |
| 176 |
} |
| 177 |
|
| 178 |
bool xmlLexerNext(void* scanner, XMLLexerToken& token) |
| 179 |
{ |
| 180 |
struct yyguts_t * yyg = (struct yyguts_t*)scanner; |
| 181 |
token.Clear(); |
| 182 |
token.column = 0; |
| 183 |
token.type = yylex(scanner); |
| 184 |
if(token.type != 0) { |
| 185 |
token.lineNumber = yylineno; |
| 186 |
token.text = wxString(yytext, wxConvUTF8); |
| 187 |
token.column = yycolumn; |
| 188 |
} |
| 189 |
return token.type != 0; |
| 190 |
} |
| 191 |
|
| 192 |
wxString xmlLexerCurrentToken(void* scanner) |
| 193 |
{ |
| 194 |
struct yyguts_t * yyg = (struct yyguts_t*)scanner; |
| 195 |
return yytext; |
| 196 |
} |
| 197 |
|
| 198 |
XMLLexerUserData* xmlLexerGetUserData(void* scanner) |
| 199 |
{ |
| 200 |
struct yyguts_t * yyg = (struct yyguts_t*)scanner; |
| 201 |
return (XMLLexerUserData*) yyg->yyextra_r; |
| 202 |
} |