%{ /* command line xxflex -d -oscanner.cpp uscanner.ll */ #include "ParseHook.h" #include "scanner.h" #define SCANNER Scanner #define YYSCBEGIN (-3) #define YYSCEND (-4) namespace u { int yycolumn = 1; void myhook() { strlen(""); } #define YY_USER_ACTION yylloc_param->first_line = yylloc_param->last_line = ((ParseInfo*)parm)->m_lineNo; \ yylloc_param->first_column = yycolumn; \ yylloc_param->last_column = yycolumn + yyleng - 1; \ yycolumn += yyleng; %} %X STRING %X LITERAL %X COMMENT %X LINE_COMMENT %S SCEXPANSION %S SCDEFINITION INT ('\-'|'\+')?([[:digit:]])+ UINT ([[:digit:]])+ ID ([[:alpha:]]|_)([[:alnum:]]|_)* REAL ('\-'|'\+')?[[:digit:]]*\.[[:digit:]]+(E[+-]?[[:digit:]]+)? %% "#" {return 0;} "(" {return FTS_ASSIGNMENT;} "(" {return OnToken(yy_val_ptr, parm, FTS_LP);} ")" {return OnToken(yy_val_ptr, parm, FTS_RP);} \{ {return OnToken(yy_val_ptr, parm, FTS_LBC);} \} {return OnToken(yy_val_ptr, parm, FTS_RBC);} \? {return OnToken(yy_val_ptr, parm, FTS_QUESTIONMARK);} \! {return OnToken(yy_val_ptr, parm, FTS_EXCLAMATIONMARK);} \, {return OnToken(yy_val_ptr, parm, FTS_COMMA);} \. {return OnToken(yy_val_ptr, parm, FTS_PERIOD);} "==" {return OnToken(yy_val_ptr, parm, FTS_TARGETLIST);} \; {return OnToken(yy_val_ptr, parm, FTS_SEMICOLON);} \: {return OnToken(yy_val_ptr, parm, FTS_COLON);} \= {return OnToken(yy_val_ptr, parm, FTS_ASSIGNMENT);} \* {return OnToken(yy_val_ptr, parm, FTS_ASTERISK);} \/ {return OnToken(yy_val_ptr, parm, FTS_SLASH);} \+ {return OnToken(yy_val_ptr, parm, FTS_PLUS);} \- {return OnToken(yy_val_ptr, parm, FTS_MINUS);} \` {return OnToken(yy_val_ptr, parm, FTS_BACKTICK);} "&" {return OnToken(yy_val_ptr, parm, FTS_AMPERSAND);} "|" {return OnToken(yy_val_ptr, parm, FTS_PIPE);} "@" {return OnToken(yy_val_ptr, parm, FTS_AT);} @{ID} {return OnToken(yy_val_ptr, parm, FTS_LABEL);} "#" {return OnToken(yy_val_ptr, parm, FTS_HASH);} "$" {return OnToken(yy_val_ptr, parm, FTS_DOLLAR);} "^" {return OnToken(yy_val_ptr, parm, FTS_CAP);} "[" {return OnToken(yy_val_ptr, parm, FTS_LBK);} "]" {return OnToken(yy_val_ptr, parm, FTS_RBK);} ">" {return OnToken(yy_val_ptr, parm, FTS_GREATER);} "<" {return OnToken(yy_val_ptr, parm, FTS_LOWER);} \\ {return OnToken(yy_val_ptr, parm, FTS_BACKSLASH);} "~" {return OnToken(yy_val_ptr, parm, FTS_EQUAL);} "%" {return OnToken(yy_val_ptr, parm, FTS_PERCENT);} "|>" {return OnToken(yy_val_ptr, parm, FTS_ANYGREATER);} "|<" {return OnToken(yy_val_ptr, parm, FTS_ANYLOWER);} "|~" {return OnToken(yy_val_ptr, parm, FTS_ANYEQUAL);} ">=" {return OnToken(yy_val_ptr, parm, FTS_PUT);} "=>" {return OnToken(yy_val_ptr, parm, FTS_DOUBLEARROW);} "->" {return OnToken(yy_val_ptr, parm, FTS_SINGLEARROW);} "+>" {return OnToken(yy_val_ptr, parm, FTS_ADDCTX);} "<=" {return OnToken(yy_val_ptr, parm, FTS_RETURN);} "=<" {return OnToken(yy_val_ptr, parm, FTS_BREAK);} "$<" {return OnToken(yy_val_ptr, parm, FTS_SETTER);} "$>" {return OnToken(yy_val_ptr, parm, FTS_GETTER);} "&~" {return OnToken(yy_val_ptr, parm, FTS_FIELD_VALIDATOR);} "::" {return OnToken(yy_val_ptr, parm, FTS_DOUBLECOLON);} "[\][\]" {return OnToken(yy_val_ptr, parm, FTS_DIFF);} "^^" {return OnToken(yy_val_ptr, parm, FTS_XOR);} ".." {return OnToken(yy_val_ptr, parm, FTS_CONCAT);} "&&" {return OnToken(yy_val_ptr, parm, FTS_AND);} "||" {return OnToken(yy_val_ptr, parm, FTS_OR);} "@@" {return OnToken(yy_val_ptr, parm, FTS_CATCH);} "<<" {return OnToken(yy_val_ptr, parm, FTS_SHL);} ">>" {return OnToken(yy_val_ptr, parm, FTS_SHR);} "$$" {return OnToken(yy_val_ptr, parm, FTS_THIS);} $avg {return OnToken(yy_val_ptr, parm, FTS_AVG);} $sum {return OnToken(yy_val_ptr, parm, FTS_SUM);} $max {return OnToken(yy_val_ptr, parm, FTS_MAX);} $min {return OnToken(yy_val_ptr, parm, FTS_MIN);} $cnt {return OnToken(yy_val_ptr, parm, FTS_CNT);} $all {return OnToken(yy_val_ptr, parm, FTS_CNTA);} "$&" {return OnToken(yy_val_ptr, parm, FTS_SIS);} "$|" {return OnToken(yy_val_ptr, parm, FTS_SUB);} $disk {return OnToken(yy_val_ptr, parm, FTS_DISK);} $0 {return OnToken(yy_val_ptr, parm, FTS_STDIN);} $1 {return OnToken(yy_val_ptr, parm, FTS_STDOUT);} $2 {return OnToken(yy_val_ptr, parm, FTS_STDERR);} [[:alpha:]]+${ID} {return OnToken(yy_val_ptr, parm, FTS_ID);} ${ID} {return OnToken(yy_val_ptr, parm, FTS_DATATYPE);} {ID} {return OnIdentifier(yy_val_ptr, parm, yytext, FTS_ID);} {INT} {return OnInteger(yy_val_ptr, parm, yytext);} {REAL} {return OnNumber(yy_val_ptr, parm, yytext);} "/*" {yy_push_state(COMMENT);} [^\*\n\r]* {/*simply skip them*/} \*+[^/\n\r]* {/*simply skip them*/} \r\n | \r | \n { OnLineBreak(parm);} (\*)+\/ {yy_pop_state();} \" {yy_push_state(STRING);OnBeginLiteral(yy_val_ptr, parm, yytext);} \\\" {OnAddLiteral(yy_val_ptr, parm, "'");} [^"]+ {OnAddLiteral(yy_val_ptr, parm, yytext);} \" {OnEndLiteral(yy_val_ptr, parm, yytext); yy_pop_state(); return FTS_STRING;} \' {yy_push_state(LITERAL);OnBeginLiteral(yy_val_ptr, parm, yytext);} \\\' {OnAddLiteral(yy_val_ptr, parm, "'");} [^']+ {OnAddLiteral(yy_val_ptr, parm, yytext);} \' {OnEndLiteral(yy_val_ptr, parm, yytext); yy_pop_state(); return FTS_LITERAL;} "#{" {yy_push_state(SCEXPANSION);return YYSCBEGIN;} "#}" {yy_pop_state();return YYSCEND;} \#[[:alpha:]]+ {yy_push_state(SCDEFINITION);OnScDefinitionStart(yy_val_ptr, parm, yytext);return FTS_ID;} \)[[:blank:]]*\; {yy_pop_state(); return FTS_SEMICOLON;} "//" yy_push_state(LINE_COMMENT); [^\n\r]*\r\n | [^\n\r]*\n | [^\n\r]*\r {yy_pop_state();OnLineBreak(parm);} "\r\n" | "\n" | "\r" {OnLineBreak(parm);} ([[:cntrl:]]|[[:blank:]]) {/* skip such sequences since they have no sense*/} . {return OnToken(yy_val_ptr, parm, FTS_OTHER);} %% // construction/destruction Scanner::Scanner(FileRef in, FILE* out) :xxflex(in, out), m_lineNo(-1), m_textInfo(0) { yy_flex_debug = 1; } Scanner::~Scanner() { if(m_textInfo) delete m_textInfo; } // Scanner routines void Scanner::OnBeginLiteral(void* yy_val_ptr, void* parm, const char* text) { m_textInfo = new std::ostringstream(); *m_textInfo << text; } void Scanner::OnAddLiteral(void* yy_val_ptr, void* parm, const char* text) { *m_textInfo << text; } void Scanner::OnEndLiteral(void* yy_val_ptr, void* parm, const char* text) { *m_textInfo << text; ParseInfo* info = (ParseInfo*)parm; std::string str = m_textInfo->str(); SetupToken(parm, yy_val_ptr, str.c_str(), FTS_STRING); delete m_textInfo; m_textInfo = 0; } int Scanner::OnToken(void* yy_val_ptr, void* parm, yytokentype token) { SetupToken(parm, yy_val_ptr, yytext, token); return token; } int Scanner::OnInteger(void* yy_val_ptr, void* parm, const char* text) { SetupToken(parm, yy_val_ptr, yytext, FTS_INTEGER); return FTS_INTEGER; } int Scanner::OnNumber(void* yy_val_ptr, void* parm, const char* text) { SetupToken(parm, yy_val_ptr, yytext, FTS_NUMBER); return FTS_NUMBER; } int Scanner::OnIdentifier(void* yy_val_ptr, void* parm, const char* text, yytokentype token) { SetupToken(parm, yy_val_ptr, yytext, token); return token; } void Scanner::SetupToken(void* parm, void* yy_val_ptr, const char* text, yytokentype token) { ParseInfo* infoPtr = (ParseInfo*)parm; TreeItem** ih = (TreeItem**)yy_val_ptr; TreeItem* item = *ih = new TreeItem; item->m_type = FTS_Term; item->m_data.m_token = token; item->m_text = new std::string(text); } void Scanner::OnLineBreak(void* parm) { yycolumn = 1; ParseInfo* infoPtr = (ParseInfo*)parm; infoPtr->m_lineNo ++; if(yy_flex_debug) fprintf(stderr, "---------- getting to the line %ld(1-based)\n", infoPtr->m_lineNo + 1); } // inherited routines void Scanner::user_init(void* parm) { ParseInfo* info = (ParseInfo*)parm; yy_scan_buffer(info->m_text, info->m_textLen); } void Scanner::yy_fatal_error(const char msg[]) { printf("scanner error: %s\n", msg); } void Scanner::OnScDefinitionStart(void* yy_val_ptr, void* parm, const char* text) { } }