#ifndef __NASAL_PARSE_H__ #define __NAsAL_PARSE_H__ /* _,,,_ .' `'. / ____ \ Fucking Nasal Parser | .-'_ _\/ / \_/ a a| / (,` \ | .----. | -' | /| '--. \ '= / || ]| `-. /`-.__.' || ]| ::| .-'`-.__ \__ || ]| ::| / `` `. || ]| ::| _ | \ \ \ \| ]| .-' / \| \ | \ L.__ .--'( | |\ `. | \ ,---|_ \---------, | | '. './\ \/ .--._|=- |_ /| | \ '. `'.'. /`\/ .-' '. / | | | `'. `;-:-;`)| |-./ | | /_ `'--./_ ` )/'-------------')/) | \ | `""""----"`\//`""`/,===..'`````````/ ( | | | / `---` `===' / ) | / \ / / ( | | '------. |'--------------------'| ) | \ `-| | / | `--...,______| | ( | | | | | ) ,| | | | | ( /|| | | | | )/ `" / \ | | (/ .' /I\ '.| | /) .-'_.'/ \'. | | / ``` `"""` `| .-------------------.|| `"` `"` */ class nasal_parse { #ifndef error_line #define error_line (tok_list[ptr>=tok_list_size? tok_list_size-1:ptr].line) #endif private: int tok_list_size; int ptr; int error; nasal_ast root; std::vector tok_list; void reset(); void die(int,std::string); bool check_multi_definition(); bool check_multi_scalar(); bool check_function_end(nasal_ast&); bool check_special_call(); bool need_semi_check(nasal_ast&); nasal_ast null_node_gen(); nasal_ast nil_gen(); nasal_ast number_gen(); nasal_ast string_gen(); nasal_ast id_gen(); nasal_ast vector_gen(); nasal_ast hash_gen(); nasal_ast hash_member_gen(); nasal_ast func_gen(); nasal_ast args_list_gen(); nasal_ast expr(); nasal_ast exprs_gen(); nasal_ast calculation(); nasal_ast or_expr(); nasal_ast and_expr(); nasal_ast cmp_expr(); nasal_ast additive_expr(); nasal_ast multive_expr(); nasal_ast unary(); nasal_ast scalar(); nasal_ast call_scalar(); nasal_ast call_hash(); nasal_ast call_vector(); nasal_ast call_func(); nasal_ast subvec(); nasal_ast definition(); nasal_ast normal_def(); nasal_ast var_incurve_def(); nasal_ast var_outcurve_def(); nasal_ast multi_id(); nasal_ast multi_scalar(); nasal_ast multi_assgin(); nasal_ast loop(); nasal_ast while_loop(); nasal_ast for_loop(); nasal_ast forei_loop(); nasal_ast new_iter_gen(); nasal_ast conditional(); nasal_ast continue_expr(); nasal_ast break_expr(); nasal_ast return_expr(); public: int get_error(); void clear(); void set_toklist(std::vector&); void main_process(); nasal_ast& get_root(); }; int nasal_parse::get_error() { return this->error; } void nasal_parse::clear() { this->tok_list_size=0; this->ptr=0; this->error=0; this->tok_list.clear(); this->root.clear(); return; } void nasal_parse::set_toklist(std::vector& lex_token) { this->tok_list=lex_token; this->tok_list_size=this->tok_list.size(); return; } void nasal_parse::main_process() { this->reset(); root.set_line(1); root.set_type(ast_root); while(ptr=tok_list_size) break; if(tok_list[ptr].type==tok_semi) ++ptr; else if(need_semi_check(root.get_children().back())) { // the last expression can be recognized without semi if(ptrptr=0; this->error=0; this->root.clear(); return; } void nasal_parse::die(int line,std::string info) { ++error; std::cout<<">> [parse] line "<=tok_list_size) return node; node.set_line(tok_list[ptr].line); node.set_type(ast_nil); return node; } nasal_ast nasal_parse::number_gen() { nasal_ast node; node.set_line(tok_list[ptr].line); node.set_type(ast_number); node.set_num(trans_string_to_number(tok_list[ptr].str)); return node; } nasal_ast nasal_parse::string_gen() { nasal_ast node; node.set_line(tok_list[ptr].line); node.set_type(ast_string); node.set_str(tok_list[ptr].str); return node; } nasal_ast nasal_parse::id_gen() { nasal_ast node; node.set_line(tok_list[ptr].line); node.set_type(ast_identifier); node.set_str(tok_list[ptr].str); return node; } nasal_ast nasal_parse::vector_gen() { nasal_ast node; node.set_line(tok_list[ptr].line); node.set_type(ast_vector); ++ptr; while(ptr=tok_list_size) break; if(tok_list[ptr].type==tok_comma) ++ptr; else if(tok_list[ptr].type!=tok_comma && tok_list[ptr].type!=tok_right_bracket) break; } if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_bracket) die(error_line,"expected \"]\""); return node; } nasal_ast nasal_parse::hash_gen() { nasal_ast node; node.set_line(tok_list[ptr].line); node.set_type(ast_hash); ++ptr; while (ptr=tok_list_size) break; if(tok_list[ptr].type==tok_comma) ++ptr; else if(tok_list[ptr].type!=tok_comma && tok_list[ptr].type!=tok_right_brace) break; } if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_brace) die(error_line,"expected \"}\""); return node; } nasal_ast nasal_parse::hash_member_gen() { nasal_ast node; if(ptr>=tok_list_size || (tok_list[ptr].type!=tok_identifier && tok_list[ptr].type!=tok_string)) { die(error_line,"expected identifier/string"); return node; } node.set_line(tok_list[ptr].line); node.set_type(ast_hashmember); node.add_child(tok_list[ptr].type==tok_identifier?id_gen():string_gen()); ++ptr; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_colon) { die(error_line,"expected \":\""); return node; } ++ptr; if(ptr=tok_list_size) { die(error_line,"expected \"(\""); return node; } if(tok_list[ptr].type!=tok_left_curve && tok_list[ptr].type!=tok_left_brace) { die(error_line,"expected argument(s)/expression block"); return node; } if(tok_list[ptr].type==tok_left_curve) { node.add_child(args_list_gen()); ++ptr; } else { nasal_ast null_argument_list; node.add_child(null_argument_list); } if(ptr>=tok_list_size) { die(error_line,"expected expression block"); return node; } node.add_child(exprs_gen()); return node; } nasal_ast nasal_parse::args_list_gen() { nasal_ast node; node.set_line(tok_list[ptr].line); node.set_type(ast_args); ++ptr; while(ptr=tok_list_size) break; if(tok_list[ptr].type==tok_comma) ++ptr; else if(tok_list[ptr].type!=tok_right_curve) break; } if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) { die(error_line,"expected \")\""); return node; } std::string args_format="func("; int node_child_size=node.get_children().size(); for(int i=0;i argname_table; for(int i=0;i=tok_list_size) { die(error_line,"expected expression block"); return node; } node.set_line(tok_list[ptr].line); node.set_type(ast_block); if(tok_list[ptr].type==tok_left_brace) { int left_brace_line=tok_list[ptr].line; ++ptr; while(ptr=tok_list_size) break; else if(tok_list[ptr].type==tok_semi) ++ptr; else if(need_semi_check(node.get_children().back())) { // the last expression can be recognized without semi if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_brace) { die(error_line,"expected \";\""); break; } } } if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_brace) { std::string lb_line=""; while(left_brace_line>1) { lb_line=(char)('0'+(left_brace_line%10))+lb_line; left_brace_line/=10; } die(error_line,"expected \"}\" to match \"{\" in line "+lb_line); } } else { node.add_child(expr()); ++ptr; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_semi) --ptr; } return node; } nasal_ast nasal_parse::calculation() { nasal_ast node; if(ptr>=tok_list_size) { die(error_line,"expected calculation"); return node; } node=or_expr(); ++ptr; if(ptr=tok_list_size || tok_list[ptr].type!=tok_colon) { die(error_line,"expected \":\""); return node; } ++ptr; if(ptr=tok_list_size || tok_list[ptr].type!=tok_right_curve) die(error_line,"expected \")\""); } else { die(error_line,"expected scalar"); return node; } ++ptr; if(ptr=tok_list_size) break; else if(tok_list[ptr].type==tok_comma) ++ptr; else if(tok_list[ptr].type!=tok_right_bracket) break; } if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_bracket) die(error_line,"expected \"]\""); return node; } nasal_ast nasal_parse::call_func() { nasal_ast node; node.set_line(tok_list[ptr].line); node.set_type(ast_call_func); bool special_call=check_special_call(); ++ptr; while(ptr=tok_list_size) break; else if(tok_list[ptr].type==tok_comma) ++ptr; else if(tok_list[ptr].type!=tok_right_curve) break; } if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) die(error_line,"expected \")\""); return node; } nasal_ast nasal_parse::subvec() { nasal_ast node; if(tok_list[ptr].type==tok_colon) { --ptr; node=nil_gen(); } else node=calculation(); ++ptr; if(ptr=tok_list_size || tok_list[ptr].type!=tok_equal) { die(error_line,"expected \"=\" / don\'t call identifier in definition"); return node; } ++ptr; if(ptr>=tok_list_size) { die(error_line,"expected scalar"); return node; } if(tok_list[ptr].type==tok_left_curve) node.add_child(check_multi_scalar()?multi_scalar():calculation()); else node.add_child(calculation()); return node; } nasal_ast nasal_parse::normal_def() { nasal_ast node; node.set_line(tok_list[ptr].line); node.set_str(tok_list[ptr].str); node.set_type(ast_identifier); return node; } nasal_ast nasal_parse::var_incurve_def() { nasal_ast node; ++ptr;// check_multi_definition will check the 'var' ++ptr; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_identifier) { die(error_line,"expected identifier"); return node; } node=multi_id(); ++ptr; if(ptr=tok_list_size || tok_list[ptr].type!=tok_right_curve) die(error_line,"expected \")\""); return node; } nasal_ast nasal_parse::var_outcurve_def() { nasal_ast node; ++ptr; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_identifier) { die(error_line,"expected identifier"); return node; } node=multi_id(); ++ptr; if(ptr=tok_list_size || tok_list[ptr].type!=tok_right_curve) die(error_line,"expected \")\""); return node; } nasal_ast nasal_parse::multi_id() { nasal_ast node; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_identifier) { die(error_line,"expected identifier"); return node; } node.set_line(tok_list[ptr].line); node.set_type(ast_multi_id); while(ptr=tok_list_size || tok_list[ptr].type!=tok_comma) { --ptr; break; } ++ptr; } return node; } nasal_ast nasal_parse::multi_scalar() { nasal_ast node; node.set_line(tok_list[ptr].line); node.set_type(ast_multi_scalar); ++ptr; while(ptr=tok_list_size) break; if(tok_list[ptr].type==tok_comma) ++ptr; else if(tok_list[ptr].type!=tok_right_curve) break; } if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) die(error_line,"expected \")\""); return node; } nasal_ast nasal_parse::multi_assgin() { nasal_ast node; node.set_line(tok_list[ptr].line); node.set_type(ast_multi_assign); node.add_child(multi_scalar()); ++ptr; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_equal) { die(error_line,"expected \"=\""); return node; } ++ptr; if(ptr>=tok_list_size) { die(error_line,"expected value list"); return node; } if(tok_list[ptr].type==tok_left_curve) node.add_child(check_multi_scalar()?multi_scalar():calculation()); else node.add_child(calculation()); return node; } nasal_ast nasal_parse::loop() { nasal_ast node; switch(tok_list[ptr].type) { case tok_while: node=while_loop(); break; case tok_for: node=for_loop(); break; case tok_forindex: case tok_foreach: node=forei_loop(); break; } return node; } nasal_ast nasal_parse::while_loop() { nasal_ast node; node.set_line(tok_list[ptr].line); node.set_type(ast_while); ++ptr; if(ptr=tok_list_size || tok_list[ptr].type!=tok_right_curve) { die(error_line,"expected \")\""); return node; } ++ptr; node.add_child(exprs_gen()); return node; } nasal_ast nasal_parse::for_loop() { nasal_ast node; node.set_line(tok_list[ptr].line); node.set_type(ast_for); ++ptr; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve) { die(error_line,"expected \"(\""); return node; } // first expression ++ptr; if(ptr>=tok_list_size) { die(error_line,"expected definition"); return node; } if(tok_list[ptr].type==tok_semi) { node.add_child(null_node_gen()); --ptr; } else if(tok_list[ptr].type==tok_var) node.add_child(definition()); else node.add_child(calculation()); ++ptr; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_semi) { die(error_line,"expected \";\""); return node; } // conditional expression ++ptr; if(ptr>=tok_list_size) { die(error_line,"expected conditional expression"); return node; } if(tok_list[ptr].type==tok_semi) { node.add_child(null_node_gen()); --ptr; } else node.add_child(calculation()); ++ptr; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_semi) { die(error_line,"expected \";\""); return node; } //after loop expression ++ptr; if(ptr>=tok_list_size) { die(error_line,"expected calculation"); return node; } if(tok_list[ptr].type==tok_right_curve) { node.add_child(null_node_gen()); --ptr; } else node.add_child(calculation()); ++ptr; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) { die(error_line,"expected \")\""); return node; } ++ptr; node.add_child(exprs_gen()); return node; } nasal_ast nasal_parse::forei_loop() { nasal_ast node; node.set_line(tok_list[ptr].line); switch(tok_list[ptr].type) { case tok_forindex: node.set_type(ast_forindex);break; case tok_foreach: node.set_type(ast_foreach); break; } ++ptr; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve) { die(error_line,"expected \"(\""); return node; } // first expression // foreach/forindex must have an iterator to loop through ++ptr; if(ptr>=tok_list_size || (tok_list[ptr].type!=tok_var && tok_list[ptr].type!=tok_identifier)) { die(error_line,"expected iterable value"); return node; } node.add_child(new_iter_gen()); ++ptr; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_semi) { die(error_line,"expected \";\""); return node; } ++ptr; if(ptr>=tok_list_size) { die(error_line,"expected vector"); return node; } node.add_child(calculation()); ++ptr; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) { die(error_line,"expected \")\""); return node; } ++ptr; node.add_child(exprs_gen()); return node; } nasal_ast nasal_parse::new_iter_gen() { nasal_ast node; if(tok_list[ptr].type==tok_var) { node.set_line(tok_list[ptr].line); node.set_type(ast_new_iter); ++ptr; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_identifier) { die(error_line,"expected identifier"); return node; } node.add_child(id_gen()); } else { node.set_line(tok_list[ptr].line); node.set_type(ast_call); node.add_child(id_gen()); ++ptr; while(ptr=tok_list_size || tok_list[ptr].type!=tok_left_curve) { die(error_line,"expected \"(\""); return node; } ++ptr; tmp.add_child(calculation()); ++ptr; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) { die(error_line,"expected \")\""); return node; } ++ptr; tmp.add_child(exprs_gen()); node.add_child(tmp); // end of if-expression ++ptr; while(ptr=tok_list_size || tok_list[ptr].type!=tok_left_curve) { die(error_line,"expected \"(\""); return node; } tmp.set_line(tok_list[ptr].line); tmp.set_type(ast_elsif); tmp.get_children().clear(); ++ptr; tmp.add_child(calculation()); ++ptr; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) { die(error_line,"expected \")\""); return node; } ++ptr; tmp.add_child(exprs_gen()); node.add_child(tmp); ++ptr; } // end of elsif-expression // after this process,ptr will point to the next token of exprs_gen()'s last token // for example // else if(scalar){} else {} // ptr^ if(ptr