diff --git a/nasal.h b/nasal.h index 520abd6..6c6fd35 100644 --- a/nasal.h +++ b/nasal.h @@ -148,6 +148,14 @@ int utf8_hdchk(char head) return nbytes; } +std::string chrhex(const char c) +{ + std::string res=""; + res+="0123456789abcdef"[(c&0xf0)>>4]; + res+="0123456789abcdef"[c&0x0f]; + return res; +} + std::string rawstr(const std::string& str) { std::string ret(""); @@ -159,8 +167,7 @@ std::string rawstr(const std::string& str) if(i<=0) { ret+="\\x"; - ret+="0123456789abcdef"[(i>>4)&15]; - ret+="0123456789abcdef"[i&15]; + ret+=chrhex(i); continue; } #endif diff --git a/nasal_err.h b/nasal_err.h index cda2f53..1edacf9 100644 --- a/nasal_err.h +++ b/nasal_err.h @@ -46,31 +46,24 @@ private: uint32_t error; public: nasal_err():error(0){} - void err(const char* stage,const std::string& info,const char end='\n') + void err(const char* stage,const std::string& info) { ++error; - std::cerr<<"["< libpath= { - "lib.nas", - "stl/lib.nas" +#ifdef __WIN32 + ".\\lib.nas", + ".\\stl\\lib.nas" +#else + "./lib.nas", + "./stl/lib.nas" +#endif }; - std::string filename=""; for(auto& i:libpath) if(access(i.c_str(),F_OK)!=-1) @@ -103,7 +107,7 @@ nasal_ast nasal_import::lib_import() std::string paths=""; for(auto& i:libpath) paths+=" "+i+"\n"; - nerr.err("link","cannot find lib file in these paths:\n"+paths,' '); + nerr.err("link","cannot find lib file in these paths:\n"+paths); nerr.chkerr(); return {0,ast_root}; } diff --git a/nasal_lexer.h b/nasal_lexer.h index 84d4568..11106b3 100644 --- a/nasal_lexer.h +++ b/nasal_lexer.h @@ -15,11 +15,14 @@ enum token_type { - tok_null=0,// null token default token type - tok_num, // number basic token type - tok_str, // string basic token type - tok_id, // identifier basic token type - tok_for,tok_forindex,tok_foreach,tok_while, + tok_null=0, // null token (default token type) + tok_num, // number basic token type + tok_str, // string basic token type + tok_id, // identifier basic token type + tok_for, // loop keyword for + tok_forindex,// loop keyword forindex + tok_foreach, // loop keyword foreach + tok_while, // loop keyword while tok_var,tok_func,tok_break,tok_continue, tok_ret,tok_if,tok_elsif,tok_else,tok_nil, tok_lcurve,tok_rcurve, @@ -106,20 +109,24 @@ class nasal_lexer private: uint32_t line; uint32_t column; - uint32_t ptr; + size_t ptr; nasal_err& nerr; std::string res; std::vector tokens; uint32_t get_type(const std::string&); - void die(std::string info){nerr.err("lexer",line,column,info);}; + void die(const std::string& info){nerr.err("lexer",line,column,info);} void open(const std::string&); std::string utf8_gen(); std::string id_gen(); std::string num_gen(); std::string str_gen(); public: - nasal_lexer(nasal_err& e):line(0),column(0),ptr(0),nerr(e){} + nasal_lexer(nasal_err& e): + line(1), + column(0), + ptr(0), + nerr(e){} void scan(const std::string&); void print(); const std::vector& get_tokens() const {return tokens;} @@ -162,16 +169,25 @@ std::string nasal_lexer::utf8_gen() if(nbytes) { tmp+=res[ptr++]; - for(uint32_t i=0;i=0) // used to report lack of ',' ';' + { + line=tokens[ptr-1].line; + col=tokens[ptr-1].column+1; + } nerr.err("parse",line,col<0?0:col,info); } void nasal_parse::match(uint32_t type,const char* info) @@ -155,7 +160,7 @@ bool nasal_parse::check_comma(const uint32_t* panic_set) for(uint32_t i=0;panic_set[i];++i) if(tokens[ptr].type==panic_set[i]) { - die(error_line,"expected \',\' between scalars"); + die(error_line,"expected \',\' between scalars",true); return true; } return false; @@ -309,7 +314,7 @@ nasal_ast nasal_parse::hash() if(tokens[ptr].type==tok_comma) match(tok_comma); else if(tokens[ptr].type==tok_id || tokens[ptr].type==tok_str)// first set of hashmember - die(error_line,"expected \',\' between hash members"); + die(error_line,"expected \',\' between hash members",true); else break; } @@ -377,7 +382,7 @@ nasal_ast nasal_parse::args() if(tokens[ptr].type==tok_comma) match(tok_comma); else if(tokens[ptr].type==tok_id)// first set of identifier - die(error_line,"expected \',\' between identifiers"); + die(error_line,"expected \',\' between identifiers",true); else break; } @@ -484,7 +489,7 @@ nasal_ast nasal_parse::exprs() match(tok_semi); // the last expression can be recognized without semi else if(need_semi_check(node.child().back()) && tokens[ptr].type!=tok_rbrace) - die(error_line,"expected \";\""); + die(error_line,"expected \';\'",true); } match(tok_rbrace,"expected \'}\' when generating expressions"); } @@ -796,7 +801,7 @@ nasal_ast nasal_parse::multi_id() if(tokens[ptr].type==tok_comma) match(tok_comma); else if(tokens[ptr].type==tok_id)// first set of identifier - die(error_line,"expected \',\' between identifiers"); + die(error_line,"expected \',\' between identifiers",true); else break; }