void GoGenerator::generateEnum() { if (program_->enums_.defs_.empty()) return; for(auto& it :program_->enums_.defs_) { if(it->fileName_!=program_->fileName_) { //包含头文件 不生成代码 continue; } //创建目录 std::string dirName=program_->outputDir_+it->name_+"/"; misc::mkdir(dirName.c_str()); std::string name=dirName+it->name_+".go"; goFile_.open(name.c_str()); goFile_<<indent()<<"package "<<it->name_<<std::endl; goFile_<<indent()<<"const ("<<std::endl; indent_up(); bool flag=true; for (auto& inner:it->defs_) { if (flag) { goFile_<<indent()<<setInitialUpper(inner)<<" =iota "<<std::endl; flag=false; } else { goFile_<<indent()<<inner<<std::endl; } } indent_down(); goFile_<<indent()<<")"<<std::endl; //map goFile_<<indent()<<"var ("<<std::endl; indent_up(); goFile_<<indent()<<setInitialUpper(it->name_)<<"S2I"<<"= map[string] int16 {"<<std::endl; for (auto& inner:it->defs_) { goFile_<<indent()<<"\""<<setInitialUpper(inner)<<"\" : "<<setInitialUpper(inner)<<","<<std::endl; } goFile_<<indent()<<"}"<<std::endl; goFile_<<indent()<<setInitialUpper(it->name_)<<"I2S"<<"= map[int16] string {"<<std::endl; for (auto& inner:it->defs_) { goFile_<<indent()<<""<<setInitialUpper(inner)<<" : "<<"\""<<setInitialUpper(inner)<<"\""<<","<<std::endl; } goFile_<<indent()<<"}"<<std::endl; indent_down(); goFile_<<indent()<<")"<<std::endl; goFile_.close(); } }
void GoGenerator::genServiceStub() { if (program_->services_.defs_.empty()) return; std::string fileName=program_->outputDir_+program_->baseName_+"Stub.go"; goFile_.open(fileName.c_str()); goFile_<<indent()<<"package "<<"rpc"<<std::endl; goFile_<<std::endl; for (auto& it:program_->services_.defs_) { if(it->fileName_!=program_->fileName_) { //包含头文件 不生成代码 continue; } // goFile_<<indent()<<"const "<<it->name_<<"_"<<"strFingerprintStub=\""<<md5(it->getFingerPrint())<<"\""<<std::endl; std::string ifName=it->name_+"Stub"; goFile_<<indent()<<"type "<<ifName<<" struct {"<<std::endl; indent_up(); goFile_<<indent()<<"P__ IProtocol"<<std::endl; indent_down(); goFile_<<indent()<<"}"<<std::endl; goFile_<<std::endl; //get id goFile_<<indent()<<"func (this *"<<it->name_<<"Stub) GetFingerprint() string{"<<std::endl; indent_up(); goFile_<<indent()<<"return "<<it->name_<<"_"<<"strFingerprintStub"<<std::endl; indent_down(); goFile_<<indent()<<"}"<<std::endl; //stub func int i=0; for(auto& inner:it->funs_) { goFile_<<indent()<<"func (this *"<<it->name_<<"Stub) "<<setInitialUpper(inner->name_)<<" ("; genFunAgrList(goFile_,inner->argrs_); goFile_<<") {"<<std::endl; indent_up(); goFile_<<std::endl; goFile_<<indent()<<"this.P__.WriteMsgBegin()"<<std::endl; goFile_<<std::endl; goFile_<<indent()<<"this.P__.WriteUInt16("<<i++<<")"<<std::endl; serializeFields(inner->argrs_); goFile_<<std::endl; goFile_<<indent()<<"this.P__.WriteMsgEnd()"<<std::endl; indent_down(); goFile_<<indent()<<"}"<<std::endl; } goFile_<<indent()<<std::endl; } goFile_.close(); }
void gen_next_compressed_state(char *char_map) { indent_put2s( "YY_CHAR yy_c = %s;", char_map ); /* Save the backing-up info \before/ computing the next state * because we always compute one more state than needed - we * always proceed until we reach a jam state */ gen_backing_up(); indent_puts( "while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )" ); indent_up(); indent_puts( "{" ); indent_puts( "yy_current_state = (int) yy_def[yy_current_state];" ); if ( usemecs ) { /* We've arrange it so that templates are never chained * to one another. This means we can afford to make a * very simple test to see if we need to convert to * yy_c's meta-equivalence class without worrying * about erroneously looking up the meta-equivalence * class twice */ do_indent(); /* lastdfa + 2 is the beginning of the templates */ out_dec( "if ( yy_current_state >= %d )\n", lastdfa + 2 ); indent_up(); indent_puts( "yy_c = yy_meta[(unsigned int) yy_c];" ); indent_down(); } indent_puts( "}" ); indent_down(); indent_puts( "yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];" ); }
void gen_backing_up() { if ( reject || num_backing_up == 0 ) return; if ( fullspd ) indent_puts( "if ( yy_current_state[-1].yy_nxt )" ); else indent_puts( "if ( yy_accept[yy_current_state] )" ); indent_up(); indent_puts( "{" ); indent_puts( "yy_last_accepting_state = yy_current_state;" ); indent_puts( "yy_last_accepting_cpos = yy_cp;" ); indent_puts( "}" ); indent_down(); }
void gen_find_action() { if ( fullspd ) indent_puts( "yy_act = yy_current_state[-1].yy_nxt;" ); else if ( fulltbl ) indent_puts( "yy_act = yy_accept[yy_current_state];" ); else if ( reject ) { indent_puts( "yy_current_state = *--yy_state_ptr;" ); indent_puts( "yy_lp = yy_accept[yy_current_state];" ); outn( "find_rule: /* we branch to this label when backing up */" ); indent_puts( "for ( ; ; ) /* until we find what rule we matched */" ); indent_up(); indent_puts( "{" ); indent_puts( "if ( yy_lp && yy_lp < yy_accept[yy_current_state + 1] )" ); indent_up(); indent_puts( "{" ); indent_puts( "yy_act = yy_acclist[yy_lp];" ); if ( variable_trailing_context_rules ) { indent_puts( "if ( yy_act & YY_TRAILING_HEAD_MASK ||" ); indent_puts( " yy_looking_for_trail_begin )" ); indent_up(); indent_puts( "{" ); indent_puts( "if ( yy_act == yy_looking_for_trail_begin )" ); indent_up(); indent_puts( "{" ); indent_puts( "yy_looking_for_trail_begin = 0;" ); indent_puts( "yy_act &= ~YY_TRAILING_HEAD_MASK;" ); indent_puts( "break;" ); indent_puts( "}" ); indent_down(); indent_puts( "}" ); indent_down(); indent_puts( "else if ( yy_act & YY_TRAILING_MASK )" ); indent_up(); indent_puts( "{" ); indent_puts( "yy_looking_for_trail_begin = yy_act & ~YY_TRAILING_MASK;" ); indent_puts( "yy_looking_for_trail_begin |= YY_TRAILING_HEAD_MASK;" ); if ( real_reject ) { /* Remember matched text in case we back up * due to REJECT. */ indent_puts( "yy_full_match = yy_cp;" ); indent_puts( "yy_full_state = yy_state_ptr;" ); indent_puts( "yy_full_lp = yy_lp;" ); } indent_puts( "}" ); indent_down(); indent_puts( "else" ); indent_up(); indent_puts( "{" ); indent_puts( "yy_full_match = yy_cp;" ); indent_puts( "yy_full_state = yy_state_ptr;" ); indent_puts( "yy_full_lp = yy_lp;" ); indent_puts( "break;" ); indent_puts( "}" ); indent_down(); indent_puts( "++yy_lp;" ); indent_puts( "goto find_rule;" ); } else { /* Remember matched text in case we back up due to * trailing context plus REJECT. */ indent_up(); indent_puts( "{" ); indent_puts( "yy_full_match = yy_cp;" ); indent_puts( "break;" ); indent_puts( "}" ); indent_down(); } indent_puts( "}" ); indent_down(); indent_puts( "--yy_cp;" ); /* We could consolidate the following two lines with those at * the beginning, but at the cost of complaints that we're * branching inside a loop. */ indent_puts( "yy_current_state = *--yy_state_ptr;" ); indent_puts( "yy_lp = yy_accept[yy_current_state];" ); indent_puts( "}" ); indent_down(); } else { /* compressed */ indent_puts( "yy_act = yy_accept[yy_current_state];" ); if ( interactive && ! reject ) { /* Do the guaranteed-needed backing up to figure out * the match. */ indent_puts( "if ( yy_act == 0 )" ); indent_up(); indent_puts( "{ /* have to back up */" ); indent_puts( "yy_cp = yy_last_accepting_cpos;" ); indent_puts( "yy_current_state = yy_last_accepting_state;" ); indent_puts( "yy_act = yy_accept[yy_current_state];" ); indent_puts( "}" ); indent_down(); } } }
void gen_NUL_trans(void) { /* NOTE - changes in here should be reflected in gen_next_match() */ /* Only generate a definition for "yy_cp" if we'll generate code * that uses it. Otherwise lint and the like complain. */ int need_backing_up = (num_backing_up > 0 && ! reject); if ( need_backing_up && (! nultrans || fullspd || fulltbl) ) /* We're going to need yy_cp lying around for the call * below to gen_backing_up(). */ indent_puts( "char *yy_cp = yy_c_buf_p;" ); outc( '\n' ); if ( nultrans ) { indent_puts( "yy_current_state = yy_NUL_trans[yy_current_state];" ); indent_puts( "yy_is_jam = (yy_current_state == 0);" ); } else if ( fulltbl ) { do_indent(); out_dec( "yy_current_state = yy_nxt[yy_current_state][%d];\n", NUL_ec ); indent_puts( "yy_is_jam = (yy_current_state <= 0);" ); } else if ( fullspd ) { do_indent(); out_dec( "int yy_c = %d;\n", NUL_ec ); indent_puts( "yyconst struct yy_trans_info *yy_trans_info;\n" ); indent_puts( "yy_trans_info = &yy_current_state[(unsigned int) yy_c];" ); indent_puts( "yy_current_state += yy_trans_info->yy_nxt;" ); indent_puts( "yy_is_jam = (yy_trans_info->yy_verify != yy_c);" ); } else { char NUL_ec_str[20]; (void) sprintf( NUL_ec_str, "%d", NUL_ec ); gen_next_compressed_state( NUL_ec_str ); do_indent(); out_dec( "yy_is_jam = (yy_current_state == %d);\n", jamstate ); if ( reject ) { /* Only stack this state if it's a transition we * actually make. If we stack it on a jam, then * the state stack and yy_c_buf_p get out of sync. */ indent_puts( "if ( ! yy_is_jam )" ); indent_up(); indent_puts( "*yy_state_ptr++ = yy_current_state;" ); indent_down(); } } /* If we've entered an accepting state, back up; note that * compressed tables have *already* done such backing up, so * we needn't bother with it again. */ if ( need_backing_up && (fullspd || fulltbl) ) { outc( '\n' ); indent_puts( "if ( ! yy_is_jam )" ); indent_up(); indent_puts( "{" ); gen_backing_up(); indent_puts( "}" ); indent_down(); } }
void gen_next_state(int worry_about_NULs) { /* NOTE - changes in here should be reflected in gen_next_match() */ char char_map[256]; if ( worry_about_NULs && ! nultrans ) { if ( useecs ) (void) sprintf( char_map, "(*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : %d)", NUL_ec ); else (void) sprintf( char_map, "(*yy_cp ? YY_SC_TO_UI(*yy_cp) : %d)", NUL_ec ); } else strcpy( char_map, useecs ? "yy_ec[YY_SC_TO_UI(*yy_cp)]" : "YY_SC_TO_UI(*yy_cp)" ); if ( worry_about_NULs && nultrans ) { if ( ! fulltbl && ! fullspd ) /* Compressed tables back up *before* they match. */ gen_backing_up(); indent_puts( "if ( *yy_cp )" ); indent_up(); indent_puts( "{" ); /* } for vi */ } if ( fulltbl ) indent_put2s( "yy_current_state = yy_nxt[yy_current_state][%s];", char_map ); else if ( fullspd ) indent_put2s( "yy_current_state += yy_current_state[%s].yy_nxt;", char_map ); else gen_next_compressed_state( char_map ); if ( worry_about_NULs && nultrans ) { /* { for vi */ indent_puts( "}" ); indent_down(); indent_puts( "else" ); indent_up(); indent_puts( "yy_current_state = yy_NUL_trans[yy_current_state];" ); indent_down(); } if ( fullspd || fulltbl ) gen_backing_up(); if ( reject ) indent_puts( "*yy_state_ptr++ = yy_current_state;" ); }
void gen_next_match(void) { /* NOTE - changes in here should be reflected in gen_next_state() and * gen_NUL_trans(). */ char *char_map = useecs ? "yy_ec[YY_SC_TO_UI(*yy_cp)]" : "YY_SC_TO_UI(*yy_cp)"; char *char_map_2 = useecs ? "yy_ec[YY_SC_TO_UI(*++yy_cp)]" : "YY_SC_TO_UI(*++yy_cp)"; if ( fulltbl ) { indent_put2s( "while ( (yy_current_state = yy_nxt[yy_current_state][%s]) > 0 )", char_map ); indent_up(); if ( num_backing_up > 0 ) { indent_puts( "{" ); /* } for vi */ gen_backing_up(); outc( '\n' ); } indent_puts( "++yy_cp;" ); if ( num_backing_up > 0 ) /* { for vi */ indent_puts( "}" ); indent_down(); outc( '\n' ); indent_puts( "yy_current_state = -yy_current_state;" ); } else if ( fullspd ) { indent_puts( "{" ); /* } for vi */ indent_puts( "yyconst struct yy_trans_info *yy_trans_info;\n" ); indent_puts( "YY_CHAR yy_c;\n" ); indent_put2s( "for ( yy_c = %s;", char_map ); indent_puts( " (yy_trans_info = &yy_current_state[(unsigned int) yy_c])->" ); indent_puts( "yy_verify == yy_c;" ); indent_put2s( " yy_c = %s )", char_map_2 ); indent_up(); if ( num_backing_up > 0 ) indent_puts( "{" ); /* } for vi */ indent_puts( "yy_current_state += yy_trans_info->yy_nxt;" ); if ( num_backing_up > 0 ) { outc( '\n' ); gen_backing_up(); /* { for vi */ indent_puts( "}" ); } indent_down(); /* { for vi */ indent_puts( "}" ); } else { /* compressed */ indent_puts( "do" ); indent_up(); indent_puts( "{" ); /* } for vi */ gen_next_state( false ); indent_puts( "++yy_cp;" ); /* { for vi */ indent_puts( "}" ); indent_down(); do_indent(); if ( interactive ) out_dec( "while ( yy_base[yy_current_state] != %d );\n", jambase ); else out_dec( "while ( yy_current_state != %d );\n", jamstate ); if ( ! reject && ! interactive ) { /* Do the guaranteed-needed backing up to figure out * the match. */ indent_puts( "yy_cp = yy_last_accepting_cpos;" ); indent_puts( "yy_current_state = yy_last_accepting_state;" ); } } }
void make_tables(void) { int i; int did_eof_rule = false; skelout(); /* First, take care of YY_DO_BEFORE_ACTION depending on yymore * being used. */ set_indent( 1 ); if ( yymore_used && ! yytext_is_array ) { indent_puts( "yytext_ptr -= yy_more_len; \\" ); indent_puts( "yyleng = (int) (yy_cp - yytext_ptr); \\" ); } else indent_puts( "yyleng = (int) (yy_cp - yy_bp); \\" ); /* Now also deal with copying yytext_ptr to yytext if needed. */ skelout(); if ( yytext_is_array ) { if ( yymore_used ) indent_puts( "if ( yyleng + yy_more_offset >= YYLMAX ) \\" ); else indent_puts( "if ( yyleng >= YYLMAX ) \\" ); indent_up(); indent_puts( "YY_FATAL_ERROR( \"token too large, exceeds YYLMAX\" ); \\" ); indent_down(); if ( yymore_used ) { indent_puts( "yy_flex_strncpy( &yytext[yy_more_offset], yytext_ptr, yyleng + 1 ); \\" ); indent_puts( "yyleng += yy_more_offset; \\" ); indent_puts( "yy_prev_more_offset = yy_more_offset; \\" ); indent_puts( "yy_more_offset = 0; \\" ); } else { indent_puts( "yy_flex_strncpy( yytext, yytext_ptr, yyleng + 1 ); \\" ); } } set_indent( 0 ); skelout(); out_dec( "#define YY_NUM_RULES %d\n", num_rules ); out_dec( "#define YY_END_OF_BUFFER %d\n", num_rules + 1 ); if ( fullspd ) { /* Need to define the transet type as a size large * enough to hold the biggest offset. */ int total_table_size = tblend + numecs + 1; char *trans_offset_type = (total_table_size >= MAX_SHORT || long_align) ? "long" : "short"; set_indent( 0 ); indent_puts( "struct yy_trans_info" ); indent_up(); indent_puts( "{" ); /* } for vi */ if ( long_align ) indent_puts( "long yy_verify;" ); else indent_puts( "short yy_verify;" ); /* In cases where its sister yy_verify *is* a "yes, there is * a transition", yy_nxt is the offset (in records) to the * next state. In most cases where there is no transition, * the value of yy_nxt is irrelevant. If yy_nxt is the -1th * record of a state, though, then yy_nxt is the action number * for that state. */ indent_put2s( "%s yy_nxt;", trans_offset_type ); indent_puts( "};" ); indent_down(); } if ( fullspd ) genctbl(); else if ( fulltbl ) genftbl(); else gentabs(); /* Definitions for backing up. We don't need them if REJECT * is being used because then we use an alternative backin-up * technique instead. */ if ( num_backing_up > 0 && ! reject ) { if ( ! C_plus_plus ) { indent_puts( "static yy_state_type yy_last_accepting_state;" ); indent_puts( "static char *yy_last_accepting_cpos;\n" ); } } if ( nultrans ) { out_str_dec( C_state_decl, "yy_NUL_trans", lastdfa + 1 ); for ( i = 1; i <= lastdfa; ++i ) { if ( fullspd ) out_dec( " &yy_transition[%d],\n", base[i] ); else mkdata( nultrans[i] ); } dataend(); } if ( ddebug ) { /* Spit out table mapping rules to line numbers. */ if ( ! C_plus_plus ) { indent_puts( "extern int yy_flex_debug;" ); indent_puts( "int yy_flex_debug = 1;\n" ); } out_str_dec( long_align ? C_long_decl : C_short_decl, "yy_rule_linenum", num_rules ); for ( i = 1; i < num_rules; ++i ) mkdata( rule_linenum[i] ); dataend(); } if ( reject ) { /* Declare state buffer variables. */ if ( ! C_plus_plus ) { outn( "static yy_state_type yy_state_buf[YY_BUF_SIZE + 2], *yy_state_ptr;" ); outn( "static char *yy_full_match;" ); outn( "static int yy_lp;" ); } if ( variable_trailing_context_rules ) { if ( ! C_plus_plus ) { outn( "static int yy_looking_for_trail_begin = 0;" ); outn( "static int yy_full_lp;" ); outn( "static int *yy_full_state;" ); } out_hex( "#define YY_TRAILING_MASK 0x%x\n", (unsigned int) YY_TRAILING_MASK ); out_hex( "#define YY_TRAILING_HEAD_MASK 0x%x\n", (unsigned int) YY_TRAILING_HEAD_MASK ); } outn( "#define REJECT \\" ); outn( "{ \\" ); /* } for vi */ outn( "*yy_cp = yy_hold_char; /* undo effects of setting up yytext */ \\" ); outn( "yy_cp = yy_full_match; /* restore poss. backed-over text */ \\" ); if ( variable_trailing_context_rules ) { outn( "yy_lp = yy_full_lp; /* restore orig. accepting pos. */ \\" ); outn( "yy_state_ptr = yy_full_state; /* restore orig. state */ \\" ); outn( "yy_current_state = *yy_state_ptr; /* restore curr. state */ \\" ); } outn( "++yy_lp; \\" ); outn( "goto find_rule; \\" ); /* { for vi */ outn( "}" ); } else { outn( "/* The intent behind this definition is that it'll catch" ); outn( " * any uses of REJECT which flex missed." ); outn( " */" ); outn( "#define REJECT reject_used_but_not_detected" ); } if ( yymore_used ) { if ( ! C_plus_plus ) { if ( yytext_is_array ) { indent_puts( "static int yy_more_offset = 0;" ); indent_puts( "static int yy_prev_more_offset = 0;" ); } else { indent_puts( "static int yy_more_flag = 0;" ); indent_puts( "static int yy_more_len = 0;" ); } } if ( yytext_is_array ) { indent_puts( "#define yymore() (yy_more_offset = yy_flex_strlen( yytext ))" ); indent_puts( "#define YY_NEED_STRLEN" ); indent_puts( "#define YY_MORE_ADJ 0" ); indent_puts( "#define YY_RESTORE_YY_MORE_OFFSET \\" ); indent_up(); indent_puts( "{ \\" ); indent_puts( "yy_more_offset = yy_prev_more_offset; \\" ); indent_puts( "yyleng -= yy_more_offset; \\" ); indent_puts( "}" ); indent_down(); } else { indent_puts( "#define yymore() (yy_more_flag = 1)" ); indent_puts( "#define YY_MORE_ADJ yy_more_len" ); indent_puts( "#define YY_RESTORE_YY_MORE_OFFSET" ); } } else { indent_puts( "#define yymore() yymore_used_but_not_detected" ); indent_puts( "#define YY_MORE_ADJ 0" ); indent_puts( "#define YY_RESTORE_YY_MORE_OFFSET" ); } if ( ! C_plus_plus ) { if ( yytext_is_array ) { outn( "#ifndef YYLMAX" ); outn( "#define YYLMAX 8192" ); outn( "#endif\n" ); outn( "char yytext[YYLMAX];" ); outn( "char *yytext_ptr;" ); } else outn( "char *yytext;" ); } out( &action_array[defs1_offset] ); line_directive_out( stdout, 0 ); skelout(); if ( ! C_plus_plus ) { if ( use_read ) { outn( "\tif ( (result = read( fileno(yyin), (char *) buf, max_size )) < 0 ) \\" ); outn( "\t\tYY_FATAL_ERROR( \"input in flex scanner failed\" );" ); } else { outn( "\tif ( yy_current_buffer->yy_is_interactive ) \\" ); outn( "\t\t{ \\" ); outn( "\t\tint c = '*', n; \\" ); outn( "\t\tfor ( n = 0; n < max_size && \\" ); outn( "\t\t\t (c = getc( yyin )) != EOF && c != '\\n'; ++n ) \\" ); outn( "\t\t\tbuf[n] = (char) c; \\" ); outn( "\t\tif ( c == '\\n' ) \\" ); outn( "\t\t\tbuf[n++] = (char) c; \\" ); outn( "\t\tif ( c == EOF && ferror( yyin ) ) \\" ); outn( "\t\t\tYY_FATAL_ERROR( \"input in flex scanner failed\" ); \\" ); outn( "\t\tresult = n; \\" ); outn( "\t\t} \\" ); outn( "\telse if ( ((result = fread( buf, 1, max_size, yyin )) == 0) \\" ); outn( "\t\t && ferror( yyin ) ) \\" ); outn( "\t\tYY_FATAL_ERROR( \"input in flex scanner failed\" );" ); } } skelout(); indent_puts( "#define YY_RULE_SETUP \\" ); indent_up(); if ( bol_needed ) { indent_puts( "if ( yyleng > 0 ) \\" ); indent_up(); indent_puts( "yy_current_buffer->yy_at_bol = \\" ); indent_puts( "\t\t(yytext[yyleng - 1] == '\\n'); \\" ); indent_down(); } indent_puts( "YY_USER_ACTION" ); indent_down(); skelout(); /* Copy prolog to output file. */ out( &action_array[prolog_offset] ); line_directive_out( stdout, 0 ); skelout(); set_indent( 2 ); if ( yymore_used && ! yytext_is_array ) { indent_puts( "yy_more_len = 0;" ); indent_puts( "if ( yy_more_flag )" ); indent_up(); indent_puts( "{" ); indent_puts( "yy_more_len = yy_c_buf_p - yytext_ptr;" ); indent_puts( "yy_more_flag = 0;" ); indent_puts( "}" ); indent_down(); } skelout(); gen_start_state(); /* Note, don't use any indentation. */ outn( "yy_match:" ); gen_next_match(); skelout(); set_indent( 2 ); gen_find_action(); skelout(); if ( do_yylineno ) { indent_puts( "if ( yy_act != YY_END_OF_BUFFER )" ); indent_up(); indent_puts( "{" ); indent_puts( "int yyl;" ); indent_puts( "for ( yyl = 0; yyl < yyleng; ++yyl )" ); indent_up(); indent_puts( "if ( yytext[yyl] == '\\n' )" ); indent_up(); indent_puts( "++yylineno;" ); indent_down(); indent_down(); indent_puts( "}" ); indent_down(); } skelout(); if ( ddebug ) { indent_puts( "if ( yy_flex_debug )" ); indent_up(); indent_puts( "{" ); indent_puts( "if ( yy_act == 0 )" ); indent_up(); indent_puts( C_plus_plus ? "cerr << \"--scanner backing up\\n\";" : "fprintf( stderr, \"--scanner backing up\\n\" );" ); indent_down(); do_indent(); out_dec( "else if ( yy_act < %d )\n", num_rules ); indent_up(); if ( C_plus_plus ) { indent_puts( "cerr << \"--accepting rule at line \" << yy_rule_linenum[yy_act] <<" ); indent_puts( " \"(\\\"\" << yytext << \"\\\")\\n\";" ); } else { indent_puts( "fprintf( stderr, \"--accepting rule at line %d (\\\"%s\\\")\\n\"," ); indent_puts( " yy_rule_linenum[yy_act], yytext );" ); } indent_down(); do_indent(); out_dec( "else if ( yy_act == %d )\n", num_rules ); indent_up(); if ( C_plus_plus ) { indent_puts( "cerr << \"--accepting default rule (\\\"\" << yytext << \"\\\")\\n\";" ); } else { indent_puts( "fprintf( stderr, \"--accepting default rule (\\\"%s\\\")\\n\"," ); indent_puts( " yytext );" ); } indent_down(); do_indent(); out_dec( "else if ( yy_act == %d )\n", num_rules + 1 ); indent_up(); indent_puts( C_plus_plus ? "cerr << \"--(end of buffer or a NUL)\\n\";" : "fprintf( stderr, \"--(end of buffer or a NUL)\\n\" );" ); indent_down(); do_indent(); outn( "else" ); indent_up(); if ( C_plus_plus ) { indent_puts( "cerr << \"--EOF (start condition \" << YY_START << \")\\n\";" ); } else { indent_puts( "fprintf( stderr, \"--EOF (start condition %d)\\n\", YY_START );" ); } indent_down(); indent_puts( "}" ); indent_down(); } /* Copy actions to output file. */ skelout(); indent_up(); gen_bu_action(); out( &action_array[action_offset] ); line_directive_out( stdout, 0 ); /* generate cases for any missing EOF rules */ for ( i = 1; i <= lastsc; ++i ) if ( ! sceof[i] ) { do_indent(); out_str( "case YY_STATE_EOF(%s):\n", scname[i] ); did_eof_rule = true; } if ( did_eof_rule ) { indent_up(); indent_puts( "yyterminate();" ); indent_down(); } /* Generate code for handling NUL's, if needed. */ /* First, deal with backing up and setting up yy_cp if the scanner * finds that it should JAM on the NUL. */ skelout(); set_indent( 4 ); if ( fullspd || fulltbl ) indent_puts( "yy_cp = yy_c_buf_p;" ); else { /* compressed table */ if ( ! reject && ! interactive ) { /* Do the guaranteed-needed backing up to figure * out the match. */ indent_puts( "yy_cp = yy_last_accepting_cpos;" ); indent_puts( "yy_current_state = yy_last_accepting_state;" ); } else /* Still need to initialize yy_cp, though * yy_current_state was set up by * yy_get_previous_state(). */ indent_puts( "yy_cp = yy_c_buf_p;" ); } /* Generate code for yy_get_previous_state(). */ set_indent( 1 ); skelout(); gen_start_state(); set_indent( 2 ); skelout(); gen_next_state( true ); set_indent( 1 ); skelout(); gen_NUL_trans(); skelout(); if ( do_yylineno ) { /* update yylineno inside of unput() */ indent_puts( "if ( c == '\\n' )" ); indent_up(); indent_puts( "--yylineno;" ); indent_down(); } skelout(); /* Update BOL and yylineno inside of input(). */ if ( bol_needed ) { indent_puts( "yy_current_buffer->yy_at_bol = (c == '\\n');" ); if ( do_yylineno ) { indent_puts( "if ( yy_current_buffer->yy_at_bol )" ); indent_up(); indent_puts( "++yylineno;" ); indent_down(); } } else if ( do_yylineno ) { indent_puts( "if ( c == '\\n' )" ); indent_up(); indent_puts( "++yylineno;" ); indent_down(); } skelout(); /* Copy remainder of input to output. */ line_directive_out( stdout, 1 ); if ( sectnum == 3 ) (void) flexscan(); /* copy remainder of input to output */ }
void GoGenerator::generateStruct() { if (program_->structs_.defs_.empty()) return; for(auto& it :program_->structs_.defs_) { if(it->fileName_!=program_->fileName_) { //包含头文件 不生成代码 continue; } std::string name=program_->outputDir_+it->name_+".go"; goFile_.open(name.c_str()); goFile_<<indent()<<"package "<<"rpc"<<std::endl; ///struct //fingerprint goFile_<<std::endl; goFile_<<indent()<<"const "<<it->name_<<"_"<<"strFingerprint=\""<<md5(it->getFingerPrint())<<"\""<<std::endl; goFile_<<indent()<<"type "<<it->name_<<" struct{"<<std::endl; indent_up(); //属性 for(auto& inner:it->members_) { goFile_<<indent()<<setInitialUpper(inner->name_)<<" "<<typeName(inner->type_)<<" `name:\""<<inner->name_<<"\"`"<<std::endl; } indent_down(); goFile_<<indent()<<"}" <<std::endl; //struct id goFile_<<indent()<<"func (this *"<<it->name_<<") GetFingerprint() string{"<<std::endl; indent_up(); goFile_<<indent()<<"return "<<it->name_<<"_"<<"strFingerprint"<<std::endl; indent_down(); goFile_<<"}"<<std::endl; //struct 序列化 goFile_<<indent()<<"func (this *"<<it->name_<<") Serialize( P__ IProtocol){"<<std::endl; indent_up(); for(auto& inner:it->members_) { serializeField(inner->type_,"this."+setInitialUpper(inner->name_),"P__"); goFile_<<std::endl; } indent_down(); goFile_<<"}"<<std::endl; //struct 反序列化 goFile_<<indent()<<"func (this *"<<it->name_<<") DeSerialize( P__ IProtocol) bool{"<<std::endl; indent_up(); for(auto& inner:it->members_) { deSerializeField(inner->type_,"this."+setInitialUpper(inner->name_)); goFile_<<std::endl; } goFile_<<indent()<<"return true"<<std::endl; indent_down(); goFile_<<"}"<<std::endl; goFile_<<std::endl; goFile_.close(); } }
void GoGenerator::deSerializeField( DefType* t ,const std::string& fieldName ) { if (t->is_struct()) { goFile_<<indent()<<fieldName<<".DeSerialize(P__)"<<std::endl; } else if (t->is_simple_type()) { SimpleDefType* s=(SimpleDefType*)t; switch (s->t_) { case SimpleDefType::boolType : { goFile_<<indent()<<fieldName<<"=P__.ReadBool()"<<std::endl; break; } case SimpleDefType::uint8Type : { goFile_<<indent()<<fieldName<<"=P__.ReadUInt8() "<<std::endl; break; } case SimpleDefType::int8Type : { goFile_<<indent()<<fieldName<<"=P__.ReadInt8()"<<std::endl; break; } case SimpleDefType::uint16Type : { goFile_<<indent()<<fieldName<<"=P__.ReadUInt16()"<<std::endl; break; } case SimpleDefType::int16Type : { goFile_<<indent()<<fieldName<<"=P__.ReadInt16()"<<std::endl; break; } case SimpleDefType::uint32Type : { goFile_<<indent()<<fieldName<<"=P__.ReadUInt32()"<<std::endl; break; } case SimpleDefType::int32Type : { goFile_<<indent()<<fieldName<<"=P__.ReadInt32()"<<std::endl; break; } case SimpleDefType::int64Type : { goFile_<<indent()<<fieldName<<"=P__.ReadInt64()"<<std::endl; break; } case SimpleDefType::floatType : { goFile_<<indent()<<fieldName<<"=P__.ReadFloat()"<<std::endl; break; } case SimpleDefType::stringType : { goFile_<<indent()<<fieldName<<"=P__.ReadString()"<<std::endl; break; } } } else if(t->is_array()) { static int i=0; std::stringstream str; str<<"_n_"<<i<<"_array"; goFile_<<indent()<<str.str()<<":=P__.ReadUInt16()"<<std::endl; std::stringstream count;count<<"_i_"<<i<<"_"; i++; goFile_<<indent()<<"for "<<count.str()<<":=0; "<<"uint16("<<count.str()<<")<"<<str.str()<<"; "<<count.str()<<"++ {"<<std::endl; indent_up(); goFile_<<indent()<<"var tmp "<<typeName(((ArrayDefType*)t)->valueDef_)<<std::endl; deSerializeField(((ArrayDefType*)t)->valueDef_,"tmp"); goFile_<<indent()<<fieldName<<"= append("<<fieldName<<",tmp)"<<std::endl; indent_down(); goFile_<<indent()<<"}"<<std::endl; }else if (t->is_enum()) { goFile_<<indent()<<fieldName<<"=P__.ReadInt16()"<<std::endl; }else if(t->is_map()) { } }
void GoGenerator::serializeField( DefType* t ,const std::string& fieldName ,const std::string& inner) { if (t->is_struct()) { goFile_<<indent()<<fieldName<<".Serialize("<<inner<<")"<<std::endl; } else if (t->is_simple_type()) { SimpleDefType* s=(SimpleDefType*)t; switch (s->t_) { case SimpleDefType::boolType : { goFile_<<indent()<<inner<<".WriteBool("<<fieldName<<")"<<std::endl; break; } case SimpleDefType::uint8Type : { goFile_<<indent()<<inner<<".WriteUInt8("<<fieldName<<")"<<std::endl; break; } case SimpleDefType::int8Type : { goFile_<<indent()<<inner<<".WriteInt8("<<fieldName<<")"<<std::endl; break; } case SimpleDefType::uint16Type : { goFile_<<indent()<<inner<<".WriteUInt16("<<fieldName<<")"<<std::endl; break; } case SimpleDefType::int16Type : { goFile_<<indent()<<inner<<".WriteInt16("<<fieldName<<")"<<std::endl; break; } case SimpleDefType::uint32Type : { goFile_<<indent()<<inner<<".WriteUInt32("<<fieldName<<")"<<std::endl; break; } case SimpleDefType::int32Type : { goFile_<<indent()<<inner<<".WriteInt32("<<fieldName<<")"<<std::endl; break; } case SimpleDefType::int64Type : { goFile_<<indent()<<inner<<".WriteInt64("<<fieldName<<")"<<std::endl; break; } case SimpleDefType::floatType : { goFile_<<indent()<<inner<<".WriteFloat("<<fieldName<<")"<<std::endl; break; } case SimpleDefType::stringType : { goFile_<<indent()<<inner<<".WriteString("<<fieldName<<")"<<std::endl; break; } } } else if(t->is_array()) { goFile_<<indent()<<inner<<".WriteUInt16(uint16(len("<<fieldName<<")))"<<std::endl; goFile_<<indent()<<"for _ ,v := range "<<fieldName<<" {"<<std::endl; indent_up(); serializeField(((ArrayDefType*)t)->valueDef_,"v",inner); indent_down(); goFile_<<indent()<<"}"<<std::endl; }else if (t->is_enum()) { goFile_<<indent()<<inner<<".WriteInt16("<<fieldName<<")"<<std::endl; }else if(t->is_map()) { } }
void GoGenerator::genServiceProxy() { if (program_->services_.defs_.empty()) return; //接口文件 std::string fileName=program_->outputDir_+"I"+program_->baseName_+"Proxy.go"; goFile_.open(fileName.c_str()); goFile_<<indent()<<"package "<<"rpc"<<std::endl; goFile_<<std::endl; for (auto& it:program_->services_.defs_) { if(it->fileName_!=program_->fileName_) { //包含头文件 不生成代码 continue; } std::string ifName="I"+it->name_+"Proxy"; goFile_<<indent()<<"type "<<ifName<<" interface {"<<std::endl; indent_up(); for(auto& inner:it->funs_) { goFile_<<indent()<<setInitialUpper(inner->name_)<<" ("; genFunAgrList(goFile_,inner->argrs_); goFile_<<") bool"; goFile_<<std::endl; } indent_down(); goFile_<<indent()<<"}"<<std::endl; goFile_<<std::endl; } goFile_.close(); // fileName=program_->outputDir_+program_->baseName_+"Proxy.go"; goFile_.open(fileName.c_str()); goFile_<<indent()<<"package "<<"rpc"<<std::endl; goFile_<<std::endl; for (auto& it:program_->services_.defs_) { if(it->fileName_!=program_->fileName_) { //包含头文件 不生成代码 continue; } std::string ifName=it->name_+"Proxy"; goFile_<<indent()<<"const "<<it->name_<<"_"<<"strFingerprintProxy=\""<<md5(it->getFingerPrint())<<"\""<<std::endl; goFile_<<indent()<<"type "<<ifName<<" struct {"<<std::endl; indent_up(); goFile_<<indent()<<"I__ I"<<ifName<<std::endl; indent_down(); goFile_<<indent()<<"}"<<std::endl; goFile_<<std::endl; //get id goFile_<<indent()<<"func (this *"<<it->name_<<"Proxy) GetFingerprint() string{"<<std::endl; indent_up(); goFile_<<indent()<<"return "<<it->name_<<"_"<<"strFingerprintProxy"<<std::endl; indent_down(); goFile_<<indent()<<"}"<<std::endl; //dispatch goFile_<<indent()<<"func (this *"<<it->name_<<"Proxy) "<<"Dispatch"<<"("; goFile_<<" P__ IProtocol"; goFile_<<") bool {"<<std::endl; indent_up(); goFile_<<indent()<<"id :=P__.ReadUInt16()"<<std::endl; goFile_<<indent()<<"switch id {"<<std::endl; int i=0; for(auto& it:it->funs_) { goFile_<<indent()<<"case "<<i++<<":"<<std::endl; indent_up(); goFile_<<indent()<<"return "<<"this.recv_"<<it->name_<<"(P__)"<<std::endl; indent_down(); } goFile_<<indent()<<"default:"<<std::endl; goFile_<<indent()<<" return false"<<std::endl; goFile_<<indent()<<"}//switch "<<std::endl; indent_down(); goFile_<<indent()<<"}//dispatch func"<<std::endl; //func for(auto& inner:it->funs_) { goFile_<<indent()<<"func (this *"<<it->name_<<"Proxy) "<<"recv_"<<inner->name_<<" ("; goFile_<<" P__ IProtocol "; goFile_<<") bool {"<<std::endl; indent_up(); goFile_<<std::endl; deSerializeFields(inner->argrs_); goFile_<<indent()<<"return this.I__."<<setInitialUpper(inner->name_)<<"("; genFunAgrList(goFile_,inner->argrs_,true); goFile_<<")"<<std::endl; indent_down(); goFile_<<indent()<<"}"<<std::endl; } } goFile_.close(); }