int GetTokenSet(int cmdId, CmCToken **tokenSet, CmCParser *synParser) { //obtain token count int tokenCount, cmdTypeId = CommandType(cmdId); switch(cmdTypeId) { case CMD_IO : switch(cmdId) { case CMD_SAVE : tokenCount = 10; break; case CMD_LOAD : tokenCount = 8; break; default : tokenCount = 4; break; } break; case CMD_EXECUTION: tokenCount = 1; break; case CMD_FLAGS : tokenCount = 2; break; default : return UNKNOWN_COMMAND; } //populate token set int i; for(i = 0; i < tokenCount; i++) { if(!(tokenSet[i] = synParser->GetToken())) return SYN_ERROR; } //Appropriate number of tokens read. return NO_ERRORS; }
CommandType matchCommand(const char* str) { for (size_t pos = 0; pos < AmountOfTypes; ++pos) { size_t size = strlen(commandTypes[pos]); if (!strncmp(commandTypes[pos], str + 2, size)) return CommandType (pos + 1); } return NONE; }
CmCError CheckSyntax(char *filename, CmCParser *synParser) { //***************************************** //parse the file //***************************************** int error; synParser->SetDelimiters("=(){},;'"); synParser->StoreDelimiters(true); error = synParser->Parse(filename); if(error) return (CmCError((CmCToken *) NULL, SYN_INVALID_FILE)); //***************************************** //check syntax //***************************************** int curveId; int parameterId; int commandTypeId, commandId; CmCToken *token, *(tokenSet)[MAX_TOKEN_NUM]; while(token = synParser->GetToken()) { //identify the command... commandId = Command(token->token_); commandTypeId = CommandType(commandId); //based on the command, identify the number of tokens //associated with it and obtain those tokens error = GetTokenSet(commandId, tokenSet, synParser); //if the specified number of tokens does not exist //then return an error if(error == SYN_ERROR) return CmCError((CmCToken *) NULL, error); //if the specified tokens exist, then check them for //their validity switch(commandTypeId) { case CMD_IO: switch(commandId) { case CMD_SAVE: //check structure if(strcmp(tokenSet[0]->token_, "(")) return CmCError(tokenSet[0], SYN_MISSING_LEFT_PARENTHESIS); if(strcmp(tokenSet[1]->token_, "'")) return CmCError(tokenSet[1],SYN_MISSING_QUOTATION); if(strcmp(tokenSet[3]->token_, "'")) return CmCError(tokenSet[3],SYN_MISSING_QUOTATION); if(strcmp(tokenSet[4]->token_, ",")) return CmCError(tokenSet[4],SYN_MISSING_COMMA); if(strcmp(tokenSet[6]->token_, ",")) return CmCError(tokenSet[6],SYN_MISSING_COMMA); if(strcmp(tokenSet[8]->token_, ")")) return CmCError(tokenSet[8],SYN_MISSING_RIGHT_PARENTHESIS); if(strcmp(tokenSet[9]->token_, ";")) return CmCError(tokenSet[9],SYN_MISSING_SEMICOLON); //check constants if(!validFileType(tokenSet[5]->token_)) return CmCError(tokenSet[5],SYN_INVALID_FILETYPE); if(!validOutputType(tokenSet[7]->token_))return CmCError(tokenSet[7],SYN_INVALID_OUTPUTTYPE); if(checkSupported(FileType(tokenSet[5]->token_))) return CmCError(tokenSet[5],SYN_UNSUPPORTED_FILETYPE); break; case CMD_LOAD: //check structure if(strcmp(tokenSet[0]->token_, "(")) return CmCError(tokenSet[0],SYN_MISSING_LEFT_PARENTHESIS); if(strcmp(tokenSet[1]->token_, "'")) return CmCError(tokenSet[1],SYN_MISSING_QUOTATION); if(strcmp(tokenSet[3]->token_, "'")) return CmCError(tokenSet[3],SYN_MISSING_QUOTATION); if(strcmp(tokenSet[4]->token_, ",")) return CmCError(tokenSet[4],SYN_MISSING_COMMA); if(strcmp(tokenSet[6]->token_, ")")) return CmCError(tokenSet[6],SYN_MISSING_RIGHT_PARENTHESIS); if(strcmp(tokenSet[7]->token_, ";")) return CmCError(tokenSet[7],SYN_MISSING_SEMICOLON); //check constants if(!validInputType(tokenSet[5]->token_)) return CmCError(tokenSet[5],SYN_INVALID_INPUTTYPE); break; case CMD_USE_RESULT: if(strcmp(tokenSet[0]->token_, "(")) return CmCError(tokenSet[0], SYN_MISSING_LEFT_PARENTHESIS); if(strcmp(tokenSet[2]->token_, ")")) return CmCError(tokenSet[2], SYN_MISSING_RIGHT_PARENTHESIS); if(strcmp(tokenSet[3]->token_, ";")) return CmCError(tokenSet[3], SYN_MISSING_SEMICOLON); if((OutputType(tokenSet[1]->token_) != OUTPUT_SEGM_IMAGE) && (OutputType(tokenSet[1]->token_) != OUTPUT_FILT_IMAGE)) { if(OutputType(tokenSet[1]->token_) != OUTPUT_UNKNOWN) { return CmCError(tokenSet[1], SYN_ASSIGN_INVALID_ARG); } else { return CmCError(tokenSet[1], SYN_INVALID_OUTPUTTYPE); } } break; } break; case CMD_EXECUTION: if(tokenSet[0]->token_[0] != ';') return CmCError(tokenSet[0],SYN_MISSING_SEMICOLON); break; case CMD_FLAGS: if(!validFlag(tokenSet[0]->token_)) return CmCError(tokenSet[0],SYN_INVALID_FLAG); if(tokenSet[1]->token_[0] != ';') return CmCError(tokenSet[1],SYN_MISSING_SEMICOLON); break; //unknown command default: break; } //if its not a command, then maybe its a parameter if(commandTypeId == UNKNOWN_COMMAND) { //get the parameter type parameterId = Parameter(token->token_); if(parameterId != UNKNOWN_PARAMETER) { //retreive tokens expected given a parameter error = GetParamTokenSet(tokenSet, synParser); if(error == SYN_ERROR) return CmCError(token, SYN_ERROR); //check those tokens for validity if(strcmp(tokenSet[0]->token_, "=")) return CmCError(tokenSet[0],SYN_MISSING_EQUALS); if(strcmp(tokenSet[2]->token_, ";")) return CmCError(tokenSet[2],SYN_MISSING_SEMICOLON); //make sure parameter is of the right type error = CheckParameter(parameterId, tokenSet[1]->token_); if(error) return CmCError(token, error); //if its an unknown parameter then maybe its a curve list } else { //get custom curve curveId = CustomCurve(token->token_); //if its not a custom curve list then flag an error if(curveId == UNKNOWN_CURVE) return CmCError(token,SYN_INVALID_PARAMCMD); //check for equals token = synParser->GetToken(); if(token->token_[0] != '=') return CmCError(token,SYN_MISSING_EQUALS); //if its a curve list, then check that a proper point list //is provided error = CheckList(synParser, &token); if(error) return CmCError(token, error); //check for semicolon token = synParser->GetToken(); if(token->token_[0] != ';') return CmCError(token,SYN_MISSING_SEMICOLON); } } //command/parameter identified and verified for //its validity, next command/parameter } //reset parser synParser->StartOver(); //file is syntaxically correct return CmCError((CmCToken *) NULL, NO_ERRORS); }
static CommandType Id(){return CommandType();} // nothing means bad ...