int Scanner::getNextToken(ScannerToken &t, Location &l) { int tokid; bool la = !m_lookahead.empty(); tokid = fetchToken(t, l); if (LIKELY(tokid != T_UNRESOLVED_LT)) { // In the common case, we don't have to perform any resolution // and we can just return the token if (UNLIKELY(la)) { // If we pulled a lookahead token, we need to remove it from // the lookahead store m_lookahead.popFront(); } return tokid; } // We encountered a '<' character that needs to be resolved. if (!la) { // If this token didn't come from the lookahead store, we // need to stash it there TokenStore::iterator it = m_lookahead.appendNew(); LookaheadToken ltd = { t, l, tokid }; *it = ltd; } // Look at subsequent tokens to determine if the '<' character // is the start of a type list TokenStore::iterator pos = m_lookahead.begin(); TokenStore::iterator ltPos = pos; nextLookahead(pos); ++m_lookaheadLtDepth; bool isTypeList = tryParseTypeList(pos); --m_lookaheadLtDepth; if (!isTypeList || pos->t != '>') { ltPos->t = '<'; } else { ltPos->t = T_TYPELIST_LT; pos->t = T_TYPELIST_GT; } tokid = fetchToken(t, l); // We pulled a lookahead token, we need to remove it from the // lookahead store m_lookahead.popFront(); return tokid; }
int Scanner::getNextToken(ScannerToken &t, Location &l) { int tokid; bool la = !m_lookahead.empty(); tokid = fetchToken(t, l); if (LIKELY(!isUnresolved(tokid))) { // In the common case, we don't have to perform any resolution // and we can just return the token if (UNLIKELY(la)) { // If we pulled a lookahead token, we need to remove it from // the lookahead store m_lookahead.popFront(); } return tokid; } if (!la) { // If this token didn't come from the lookahead store, we // need to stash it there TokenStore::iterator it = m_lookahead.appendNew(); LookaheadToken ltd = { t, l, tokid }; *it = ltd; } switch (tokid) { case T_UNRESOLVED_NEWTYPE: case T_UNRESOLVED_TYPE: { auto pos = m_lookahead.begin(); auto typePos = pos; nextLookahead(pos); if (isValidClassConstantName(pos->t)) { typePos->t = tokid == T_UNRESOLVED_TYPE ? T_TYPE : T_NEWTYPE; } else { typePos->t = T_STRING; } break; } case T_UNRESOLVED_LT: { // Look at subsequent tokens to determine if the '<' character // is the start of a type list auto pos = m_lookahead.begin(); auto ltPos = pos; nextLookahead(pos); ++m_lookaheadLtDepth; bool isTypeList = tryParseTypeList(pos); --m_lookaheadLtDepth; if (isTypeList && pos->t == '>') { ltPos->t = T_TYPELIST_LT; pos->t = T_TYPELIST_GT; } else { ltPos->t = '<'; } break; } case T_UNRESOLVED_OP: { // Look at subsequent tokens to determine if the '(' character // is the start of a lambda expression auto pos = m_lookahead.begin(); auto opPos = pos; nextLookahead(pos); if (pos->t != ')' && pos->t != T_LAMBDA_CP) { if (!tryParseNonEmptyLambdaParams(pos) || pos->t != ')') { opPos->t = '('; break; } } auto cpPos = pos; nextLookahead(pos); if (pos->t == ':') { nextLookahead(pos); if (!tryParseNSType(pos)) { opPos->t = '('; break; } } if (pos->t == T_LAMBDA_ARROW) { opPos->t = T_LAMBDA_OP; cpPos->t = T_LAMBDA_CP; } else { opPos->t = '('; } break; } default: always_assert(0); } tokid = fetchToken(t, l); // We pulled a lookahead token, we need to remove it from the // lookahead store m_lookahead.popFront(); return tokid; }
int main(int argc, char *argv[]) { if (argc != 4) { fprintf(stderr,"Usage: ./router localhost TCP UDP\n"); exit(1); } int tcpsockfd,udplistener, numbytes; int i; struct LINK newLink; char buf[MAXDATASIZE]; char* pBuf = NULL; char sendbuf[MAXDATASIZE]; char tempbuf[MAXDATASIZE]; struct timeval tval; // time out for select() fd_set readfds; // read fds for select() int ret; FILE *socket_stream_in; struct NetworkTopoStruct* networkTopo = initNetworkTopo(); pGlobalTopo = networkTopo; /* initialize random seed: */ srand ( time(NULL) ); // open tcp tcpsockfd = openTCPSocket(argv[1],argv[2]); socket_stream_in = fdopen(tcpsockfd, "r"); // open the udp listener udplistener = openUDPListenningSocket(argv[3]); // get my address sendmessage(tcpsockfd,"HELO\n"); numbytes = recv(tcpsockfd, buf, MAXDATASIZE-1, 0); buf[numbytes] = '\0'; pBuf = buf+5; printf("manager replied with address %s",pBuf); storesMyAddress(buf,networkTopo); // Stores my address into the database. // register the host with udpport sprintf(sendbuf,"HOST localhost %s\n",argv[3]); sendmessage(tcpsockfd,sendbuf); receive(tcpsockfd,buf); // this is OK // retrive the neighbour links sendmessage(tcpsockfd,"NEIGH?\n"); do{ if ( fgets(buf,MAXDATASIZE-1,socket_stream_in)!=NULL ){ printf("%s",buf); if (isControlInfo(buf,"NEIGH")){ storesNeighFromControlInfo(buf,networkTopo); // stores the neigh information } if (isControlInfo(buf,"DONE")) break; } }while(1); sendmessage(tcpsockfd,"READY\n"); receive(tcpsockfd,buf); // This is OK // Until here, the handshake is done // LOG Information sendmessage(tcpsockfd,"LOG ON\n"); receive(tcpsockfd,buf); // This is LOG ON replied from the server // initialize select() to do multiplexing I/O tval.tv_sec = 0; tval.tv_usec = 80000; FD_ZERO(&readfds); FD_SET(tcpsockfd,&readfds); // input from the tcp FD_SET(udplistener,&readfds); // input from the udp //Broadcast the neighbour links to all nodes to set up the topology of the network for(i=0;i<networkTopo->linkNum;i++) { broadCastLinkInfo(3,networkTopo->Links[i],networkTopo); } // the loop for the select(), and dealing with the input from keyboard and the input from socket. while( (ret=select(max(tcpsockfd,udplistener)+1, &readfds, NULL, NULL, NULL)) >=0 ) { if (FD_ISSET(tcpsockfd,&readfds)) { if (receiveOneLine(socket_stream_in,buf)){ // update and acknowledge link cost if (isControlInfo(buf,"LINKCOST")){ // fetch the cost fetchToken(buf,3,tempbuf); // the cost is in tempbuf sprintf(sendbuf,"COST %s OK\n",tempbuf); sendmessage(tcpsockfd,sendbuf); newLink = storesLinkCostChangeFromControlInfo(buf,networkTopo); // save the link cost change in the data structure and broadcast the change broadCastLinkInfo(4,newLink,networkTopo); } //break if received end if (isControlInfo(buf,"END")){ sleep((rand()%200+100)/300); sendmessage(tcpsockfd,"BYE\n"); break; } } } if (FD_ISSET(udplistener,&readfds)) { if((numbytes = receiveUDPMessage(udplistener,buf)) >0){ dealWithUDPMessage(buf,numbytes, networkTopo,tcpsockfd); // forward message or deal with control information between routers } } FD_ZERO(&readfds); FD_SET(tcpsockfd,&readfds); // got message from the tcp FD_SET(udplistener,&readfds); // got message from the udp server input } if (ret==-1){ perror("select()"); exit(1); } freeTopo(networkTopo); // release the networkTopo close(udplistener); close(tcpsockfd); return 0; }