#ifndef __NASAL_LEXER_H__ #define __NASAL_LEXER_H__ #include #include #include #include #define OPERATOR 1 // operator #define IDENTIFIER 2 // id #define NUMBER 3 // number #define RESERVEWORD 4 // reserve word #define STRING 5 // string #define CHAR 6 // char #define CALL_LIST 7 // id[ #define CALL_FUNC 8 // id( #define FUNC_HEAD 9 // func( #define DYNAMIC_ID 10 // id... #define IF_HEAD 11 // if ( #define ELSIF_HEAD 12 // elsif ( #define FAIL -1 //fail #define SCANEND -2 //complete scanning #define ERRORFOUND -3 //error occurred std::string reserve_word[15]= { "for","foreach","forindex","while", "var","func","break","continue","return", "if","else","elsif","nil","and","or" }; int isReserveWord(std::string &p) { for(int i=0;i<15;++i) if(reserve_word[i]==p) return i+1; return FAIL; } bool isLetter(char t) { return (('a'<=t) && (t<='z') || ('A'<=t) && (t<='Z')); } bool isNumber(char t) { return (('0'<=t) && (t<='9')); } bool isHex(char t) { return ((('0'<=t) && (t<='9')) || (('a'<=t) && (t<='f'))); } bool isOct(char t) { return (('0'<=t) && (t<='7')); } class resource_programme_process { private: char *resource; public: resource_programme_process() { resource=NULL; resource=new char[16777216]; } ~resource_programme_process() { if(resource) delete []resource; } char* use_file() { return resource; } void input_file(std::string& filename) { std::ifstream fin(filename); if(fin.fail()) { std::cout<<">>[Error] Cannot load file: "< lexer; public: void scanner(int &syn,const char* source,std::string &__token,int &ptr,int &line) { char temp; temp=source[ptr]; while(temp==' ' || temp=='\n' || temp=='\t' || temp=='\r' || temp<0 || temp>127) { ++ptr; if(temp=='\n') ++line; temp=source[ptr]; } __token=""; if(isLetter(temp) || temp=='_') { __token+=temp; ++ptr; temp=source[ptr]; while(isLetter(temp) || isNumber(temp) || temp=='_') { __token+=temp; ++ptr; temp=source[ptr]; } syn=isReserveWord(__token); if(syn==FAIL) syn=IDENTIFIER; else syn=RESERVEWORD; if((syn==IDENTIFIER) && ((source[ptr]=='(') || (source[ptr]=='[') || (source[ptr]=='.' && source[ptr+1]=='.' && source[ptr+2]=='.'))) { __token+=source[ptr]; if(source[ptr]=='(') syn=CALL_FUNC; else if(source[ptr]=='[') syn=CALL_LIST; else if(source[ptr]=='.' && source[ptr+1]=='.' && source[ptr+2]=='.') { __token+=".."; syn=DYNAMIC_ID; ptr+=2; } ++ptr; } else if((syn==RESERVEWORD) && ((__token=="func") || (__token=="if") || (__token=="elsif")) && (source[ptr]=='(')) { if(__token=="func") syn=FUNC_HEAD; else if(__token=="if") syn=IF_HEAD; else if(__token=="elsif") syn=ELSIF_HEAD; __token+=source[ptr]; ++ptr; } } else if(isNumber(temp)) { if((source[ptr]=='0') && (source[ptr+1]=='x')) { __token+=source[ptr]; __token+=source[ptr+1]; ptr+=2; temp=source[ptr]; while(isNumber(temp) || isHex(temp)) { __token+=temp; ++ptr; temp=source[ptr]; } } else if((source[ptr]=='0') && (source[ptr+1]=='o')) { __token+=source[ptr]; __token+=source[ptr+1]; ptr+=2; temp=source[ptr]; while(isNumber(temp) || isOct(temp)) { __token+=temp; ++ptr; temp=source[ptr]; } } else { int PointCnt=0; while(isNumber(temp)) { __token+=temp; ++ptr; temp=source[ptr]; if(temp=='.' && !PointCnt) { ++PointCnt; __token+=temp; ++ptr; temp=source[ptr]; } } } syn=NUMBER; } else if(temp=='(' || temp==')' || temp=='[' || temp==']' || temp=='{' || temp=='}' || temp==',' || temp==';' || temp=='|' || temp==':' || temp=='?' || temp=='.' || temp=='`' || temp=='&'|| temp=='%' || temp=='$' || temp=='^') { __token+=temp; ++ptr; syn=OPERATOR; } else if(temp=='\'') { __token+=temp; ++ptr; temp=source[ptr]; __token+=temp; ++ptr; temp=source[ptr]; __token+=temp; ++ptr; if(temp!='\'') std::cout<<">>[Lexer] Abnormal char type detected: "<<__token<<" ."<' || temp=='~') { syn=OPERATOR; __token+=temp; ++ptr; temp=source[ptr]; if(temp=='=') { __token+=temp; ++ptr; } } else if(temp=='\\') { syn=OPERATOR; __token+=temp; ++ptr; temp=source[ptr]; if(temp=='=' || temp=='n' || temp=='t' || temp=='r' || temp=='\\' || temp=='\'' || temp=='\"') { __token+=temp; ++ptr; } } else if(temp=='\"') { syn=STRING; __token+=temp; ++ptr; temp=source[ptr]; while(temp!='\"') { if(temp=='\\') { __token+=temp; ++ptr; temp=source[ptr]; __token+=temp; ++ptr; temp=source[ptr]; } else { __token+=temp; ++ptr; temp=source[ptr]; } if(temp==0 || temp=='\n') break; } //add the last char \" if(temp=='\"') { __token+=temp; ++ptr; } else __token+=" __missing_end_of_string"; } else if(temp==0) { syn=SCANEND; return; } else { syn=FAIL; std::cout<<">>[Error] Unexpected error occurred: "<>[Error] Cannot identify "<>[Lexer] max size: "<0)//all Syn type is larger than zero { temp.line=line; temp.type=syn; temp.content=__token; lexer.push_back(temp); } } std::cout<<">>[Lexer] Complete scanning."<::iterator i=lexer.begin();i!=lexer.end();++i) { temp=*i; std::cout<<"line "<& return_list() { return lexer; } }; #endif