📄 parser.cpp
字号:
{ CToken *port, *token; CToken *args; CToken *name=NULL; port_name=""; array=false; string return_type=""; string full_list=""; string arg_list=""; CToken *return_type_begin=NULL, *return_type_end=NULL; CToken *full_list_begin=NULL, *full_list_end=NULL; if(NextToken()==INPORT) { port=Match(INPORT); } else { port=Match(OUTPORT); } if(NextToken()==ARRAY) { CToken * t =Match(ARRAY); t->text = ""; if(port->type==INPORT) { CTranslation::Error(port,17,"inport array not support\n"); } array=true; } while(NextToken()!='(') { token=MatchAny(); if(return_type_begin==NULL)return_type_begin=token; if(name==NULL&&NextToken()=='(') { port_name=token->GetID(); name=token; return_type_end=name; } else { return_type += token->text; } port->AppendChild(token); } args=parse_arglist(full_list,arg_list); port->AppendChild(args); full_list_begin=args; //printf("%s\n%s\n",full_list.c_str(),arg_list.c_str()); if(NextToken()=='{') { token=MatchPair('{','}'); CTranslation::Error(token,9,"in-class port implementation not allowed\n"); return port; } CToken* tend = Match(';'); port->AppendChild(tend); full_list_end=tend; CPort* port_class; if(port->type==INPORT) { port_class = m_current_component->AddPort(port,port_name,INPORT); port_class -> array = array; } else { port_class = m_current_component->AddPort(port,port_name,OUTPORT); port_class -> array = array; port_class -> functor_type = m_current_component->class_name + "_" + port_name + "_f_t"; if(array) port_class -> functor_name = port_name; else port_class -> functor_name = port_name + "_f"; } port_class -> full_list_begin = full_list_begin; port_class -> full_list_end = full_list_end; port_class -> return_type_begin = return_type_begin; port_class -> return_type_end = return_type_end; port_class -> arg_list = arg_list; if(m_current_component!=NULL&&m_current_component->composite==true) { port->text=string("/*") + port->text + string("*/"); CToken* mem_def=CToken::New(port,inport_definition); mem_def->text = port_name; mem_def->line_info=true; token=return_type_begin; while(token!=return_type_end) { mem_def->AppendChild(CToken::Copy(token)); token=token->sibling; } token=CToken::New(port,MAINID); token->text=string("::")+port_name; mem_def->AppendChild(token); token=full_list_begin; while(token!=full_list_end) { mem_def->AppendChild(CToken::Copy(token)); token=token->sibling; } token=CToken::New(token,UNKNOWN); token->text="{return "; mem_def->AppendChild(token); token=CToken::New(port,PORTID); token->text=port_name; mem_def->AppendChild(token); CToken* t = CToken::New(port,UNKNOWN); t->text = arg_list; token->AppendChild(t); token=CToken::New(port,UNKNOWN); token->text=";}"; mem_def->AppendChild(token); m_current_component->AddToken(mem_def); } else { if(port->type==INPORT) { port->text=string("/*")+port->text+string("*/"); } else { port->text=string("/*")+port->text; tend->text=string("*/")+tend->text; } } if(g_debug) { if(port->type==INPORT) printf(" inport %s %s %s %s\n", return_type.c_str(), port_name.c_str(), full_list.c_str(), arg_list.c_str()); else printf(" outport %s %s %s %s\n", return_type.c_str(), port_name.c_str(), full_list.c_str(), arg_list.c_str()); } return port;}CToken* Parser::parse_arglist(string& full_list, string& arg_list){ CToken *s, *t, *last; s=t=Match('('); full_list = s->text; arg_list = s->text; last=s; int level=0; while(level!=0||NextToken()!=')') { if(NextToken()=='('||NextToken()=='<')level--; if(NextToken()==')'||NextToken()=='>')level++; t->sibling=MatchAny(); t=t->sibling; full_list += t->text; if(level==0&&t->type==','&&last->type==VARID) { arg_list += last->text; arg_list += ','; } last=t; } if(last->type==VARID) { arg_list += last->text; } t->sibling=MatchAny(); t=t->sibling; full_list += t->text; arg_list += t->text; return s;}CToken* Parser::parse_connect(){ string from_c_name, from_p_name, to_c_name, to_p_name, code; string from_c_index,from_p_index, to_c_index, to_p_index; ComponentInstance *from_c=NULL, *to_c=NULL; CToken *con, *token; con=Match(CONNECT); if(NextToken()==VARID) { token=Match(VARID); from_c_name=token->GetID(); con->AppendChild(token); if(NextToken()=='[') con->AppendChild(MatchPair('[',']',from_c_index)); con->AppendChild(Match('.')); token=Match(VARID); from_p_name=token->GetID(); con->AppendChild(token); if(NextToken()=='[') con->AppendChild(MatchPair('[',']',from_p_index)); } else { token=Match(PORTID); from_p_name=token->GetID(); if(NextToken()=='[') con->AppendChild(MatchPair('[',']',from_p_index)); } con->AppendChild(Match(',')); if(NextToken()==VARID) { token=Match(VARID); to_c_name=token->GetID(); con->AppendChild(token); if(NextToken()=='[') con->AppendChild(MatchPair('[',']',to_c_index)); con->AppendChild(Match('.')); token=Match(VARID); to_p_name=token->GetID(); con->AppendChild(token); if(NextToken()=='[') con->AppendChild(MatchPair('[',']',to_p_index)); } else { token=Match(PORTID); to_p_name=token->GetID(); if(NextToken()=='[') con->AppendChild(MatchPair('[',']',to_p_index)); } token = Match(';'); con->AppendChild(token); if(m_component_table==NULL) return con; CPort *from_p, *to_p; if(from_c_name!="") { from_c=m_component_table->LookupComponent(from_c_name); if(from_c==NULL) { CTranslation::Error(con,10,"component '%s' not declared\n", from_c_name.c_str()); return con; } if(from_c->array&&from_c_index=="") { CTranslation::Error(con,18,"component '%s' was declared as an array\n", from_c_name.c_str()); } if(!from_c->array&&from_c_index!="") { CTranslation::Error(con,18,"component '%s' was not declared as an array\n", from_c_name.c_str()); } assert(from_c->GetClass()!=NULL); from_p=from_c->GetClass()->LookupPort(from_p_name); if(from_p==NULL) { CTranslation::Error(con,11,"port '%s' cannot be found\n", from_p_name.c_str()); return con; } if(from_p->type!=OUTPORT) { CTranslation::Error(con,14,"port '%s' is an inport (outport required)\n", from_p_name.c_str()); return con; } } else { if(m_current_component==NULL) { CTranslation::Error(con,12,"component connections out of range\n"); return con; } from_p=m_current_component->LookupPort(from_p_name); if(from_p==NULL) { CTranslation::Error(con,11,"port '%s' cannot be found\n", from_p_name.c_str()); return con; } if(from_p->type!=INPORT) { CTranslation::Error(con,14,"port '%s' is an outport (inport required)\n", from_p_name.c_str()); return con; } } if(from_p->array&&from_p_index=="") { CTranslation::Error(con,18,"port '%s' was declared as an array\n", from_p_name.c_str()); } if(!from_p->array&&from_p_index!="") { CTranslation::Error(con,18,"port '%s' was not declared as an array\n", from_p_name.c_str()); } if(to_c_name!="") { to_c=m_component_table->LookupComponent(to_c_name); if(to_c==NULL) { CTranslation::Error(con,10,"component '%s' not declared\n", to_c_name.c_str()); return con; } assert(to_c->GetClass()!=NULL); if(to_c->array&&to_c_index=="") { CTranslation::Error(con,18,"component '%s' was declared as an array\n", to_c_name.c_str()); } if(!to_c->array&&to_c_index!="") { CTranslation::Error(con,18,"component '%s' was not declared as an array\n", to_c_name.c_str()); } to_p=to_c->GetClass()->LookupPort(to_p_name); if(to_p==NULL) { CTranslation::Error(con,11,"port '%s' cannot be found in component '%s'\n", to_p_name.c_str(),to_c_name.c_str()); return con; } if (to_p->type!=INPORT) { CTranslation::Error(con,14,"port '%s' is an outport (inport required)\n", to_p_name.c_str()); return con; } } else { if(m_current_component==NULL) { CTranslation::Error(con,12,"component connections out of range"); return con; } to_p=m_current_component->LookupPort(to_p_name); if(to_p==NULL) { CTranslation::Error(con,11,"port '%s' cannot be found in component '%s'\n", to_p_name.c_str(),to_c_name.c_str()); return con; } } if(!to_p->array&&to_p_index!="") { CTranslation::Error(con,18,"port '%s' was not declared as an array\n", to_p_name.c_str()); } if(from_c!=NULL&&to_c!=NULL) // primitive outport -> primitive inport { if(from_c->array||from_p->array) { code = from_c_name + from_c_index + "." + from_p->functor_name + from_p_index + ".Connect(" + to_c_name + to_c_index + ",(" COMPCXX "component::" + from_p -> functor_type + ")&" + to_c-> class_name + "::" + to_p_name + ")"; CPort* inst_port = from_c->LookupPort(from_p_name); inst_port->connected=true; } else { from_c->AddConnection(from_p_name,to_c,to_p_name); code = from_c_name + ".p_" + to_c->class_name + "=&" + to_c_name; } } if(from_c!=NULL&&to_c==NULL) // primitive outport -> composite outport/inport { if(from_c->array||from_p->array) { code = from_c_name + from_c_index + "." + from_p->functor_name + from_p_index + ".Connect(this,(" "component::" + from_p -> functor_type + ")&" + to_c-> class_name + "::" + to_p_name + ")"; CPort* inst_port = from_c->LookupPort(from_p_name); inst_port->connected=true; } else { from_c->AddConnection(from_p_name,NULL,to_p_name); code = from_c_name + ".p_" COMPCXX "parent=this"; } } if(from_c==NULL&&to_c!=NULL) // composite inport -> primitive inport { from_p->bound=true; from_p->connections.push_back(make_pair(to_c,to_p_name)); } if(from_c==NULL&&to_c==NULL) // composite outport -> composite inport { CTranslation::Error(con,15,"connot connect an inport and an outport of the same component\n"); } con->text = code + " /*" + con->text; token->text = string("*/") + token->text; return con;}CToken* Parser::parse_compid(){ CToken* comp=Match(COMPID); CToken* tmpl=NULL; CToken* real_comp=comp; string comp_name = comp->GetID(); CToken *var=NULL; string tmpl_args; bool array=false; unsigned int i; if(NextToken()=='<') { tmpl = MatchPair('<','>'); tmpl->text = string("/*") + tmpl->text; comp->AppendChild(tmpl); CToken* t = CToken::New(tmpl,UNKNOWN); t->text = "*/"; comp->AppendChild(t); } switch(NextToken()) { case SCOPE: // comp::var(...):...{...} if(!m_program.empty()&&m_program.back()->type==TEMPLATE) { CToken* tmpl = m_program.back(); m_program.pop_back(); comp->InsertChild(tmpl); tmpl->text = string("/*") + tmpl->text; assert(tmpl->last_child!=NULL); tmpl->last_child->text += "*/"; } comp->type=MAINID; comp->AppendChild(Match(SCOPE)); m_current_component=ComponentClass::Lookup(comp->GetID()); assert(m_current_component!=NULL); if(NextToken()=='~') { comp->AppendChild(Match('~')); var=Match(COMPID); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -