Changeset 1424 for code/branches/console/src/core/CommandExecutor.cc
- Timestamp:
- May 25, 2008, 9:58:14 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/console/src/core/CommandExecutor.cc
r1417 r1424 108 108 { 109 109 CommandExecutor::parseIfNeeded(command); 110 111 if (!CommandExecutor::getEvaluation().bNewCommand_)112 CommandExecutor::parse(CommandExecutor::getEvaluation().command_, false);113 else114 CommandExecutor::getEvaluation().bNewCommand_ = false;115 116 110 return CommandExecutor::getEvaluation().complete(); 117 111 } … … 132 126 void CommandExecutor::parseIfNeeded(const std::string& command) 133 127 { 134 if ((CommandExecutor::getEvaluation().originalCommand_ != command) || (CommandExecutor::getEvaluation().state_ == CS_Uninitialized)) 128 if (CommandExecutor::getEvaluation().state_ == CS_Uninitialized) 129 { 135 130 CommandExecutor::parse(command); 131 } 132 else if (CommandExecutor::getEvaluation().originalCommand_ != command) 133 { 134 if (CommandExecutor::getEvaluation().command_ == command) 135 { 136 CommandExecutor::parse(command); 137 CommandExecutor::getEvaluation().bNewCommand_ = false; 138 } 139 else 140 { 141 CommandExecutor::parse(command); 142 } 143 } 136 144 } 137 145 … … 149 157 case CS_Uninitialized: 150 158 { 151 std::cout << "parse: state: CS_Uninitialized" << std::endl;152 159 // Impossible 153 160 break; … … 155 162 case CS_Empty: 156 163 { 157 std::cout << "parse: state: CS_Empty" << std::endl; 158 CommandExecutor::createListOfPossibleIdentifiers(CommandExecutor::getArgument(0)); 159 CommandExecutor::createListOfPossibleFunctions(CommandExecutor::getArgument(0)); 160 161 if (CommandExecutor::argumentsGiven() > 0) 164 if (CommandExecutor::argumentsGiven() == 0) 165 { 166 CommandExecutor::createListOfPossibleFunctions(""); 167 CommandExecutor::createListOfPossibleIdentifiers(""); 168 break; 169 } 170 else 162 171 { 163 172 CommandExecutor::getEvaluation().state_ = CS_ShortcutOrIdentifier; 164 CommandExecutor::parse(command, false); 165 return; 166 } 173 // Move on to next case 174 } 175 } 176 case CS_ShortcutOrIdentifier: 177 { 178 if (CommandExecutor::argumentsGiven() > 1) 179 { 180 // There's a finished first argument - check if it's a shortcut or a classname 181 CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(0)); 182 CommandExecutor::getEvaluation().functionclass_ = CommandExecutor::getPossibleIdentifier(CommandExecutor::getArgument(0)); 183 184 if (CommandExecutor::getEvaluation().function_) 185 { 186 // It's a shortcut 187 CommandExecutor::getEvaluation().state_ = CS_Params; 188 CommandExecutor::getEvaluation().functionclass_ = 0; 189 // Move on to next case 190 } 191 else if (CommandExecutor::getEvaluation().functionclass_) 192 { 193 // It's a functionname 194 CommandExecutor::getEvaluation().state_ = CS_Function; 195 CommandExecutor::getEvaluation().function_ = 0; 196 // Move on to next case 197 } 198 else 199 { 200 // The first argument is bad 201 CommandExecutor::getEvaluation().state_ = CS_Error; 202 AddLanguageEntry("commandexecutorunknownfirstargument", "is not a shortcut nor a classname"); 203 CommandExecutor::getEvaluation().errorMessage_ = "Error: " + CommandExecutor::getArgument(0) + " " + GetLocalisation("commandexecutorunknownfirstargument") + "."; 204 return; 205 } 206 } 207 else 208 { 209 // There's no finished first argument - search possible shortcuts or classnames 210 CommandExecutor::createListOfPossibleFunctions(CommandExecutor::getArgument(0)); 211 CommandExecutor::createListOfPossibleIdentifiers(CommandExecutor::getArgument(0)); 212 213 unsigned int num_functions = CommandExecutor::getEvaluation().listOfPossibleFunctions_.size(); 214 unsigned int num_identifiers = CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.size(); 215 216 if (num_functions == 1 && num_identifiers == 0) 217 { 218 // It's a shortcut 219 std::string functionname = *(*CommandExecutor::getEvaluation().listOfPossibleFunctions_.begin()).first; 220 CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(functionname); 221 if (getLowercase(functionname) != getLowercase(CommandExecutor::getArgument(0))) 222 { 223 // Unfinished shortcut 224 CommandExecutor::getEvaluation().bCommandChanged_ = true; 225 } 226 CommandExecutor::getEvaluation().state_ = CS_Params; 227 CommandExecutor::getEvaluation().functionclass_ = 0; 228 CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().function_->getName(); 229 if (CommandExecutor::getEvaluation().function_->getParamCount() > 0) 230 CommandExecutor::getEvaluation().command_ += " "; 231 // Move on to next case 232 } 233 else if (num_identifiers == 1 && num_functions == 0) 234 { 235 // It's a classname 236 std::string classname = *(*CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.begin()).first; 237 CommandExecutor::getEvaluation().functionclass_ = CommandExecutor::getPossibleIdentifier(classname); 238 if (getLowercase(classname) != getLowercase(CommandExecutor::getArgument(0))) 239 { 240 // Unfinished classname 241 CommandExecutor::getEvaluation().bCommandChanged_ = true; 242 } 243 CommandExecutor::getEvaluation().state_ = CS_Function; 244 CommandExecutor::getEvaluation().function_ = 0; 245 CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().functionclass_->getName() + " "; 246 // Move on to next case 247 } 248 else if (num_identifiers == 0 && num_functions == 0) 249 { 250 // No possibilities 251 CommandExecutor::getEvaluation().state_ = CS_Error; 252 AddLanguageEntry("commandexecutorunknownfirstargumentstart", "There is no command or classname starting with"); 253 CommandExecutor::getEvaluation().errorMessage_ = "Error: " + GetLocalisation("commandexecutorunknownfirstargumentstart") + " " + CommandExecutor::getArgument(0) + "."; 254 return; 255 } 256 else 257 { 258 // There are several possiblilities 259 std::list<std::pair<const std::string*, const std::string*> > temp; 260 temp.insert(temp.end(), CommandExecutor::getEvaluation().listOfPossibleFunctions_.begin(), CommandExecutor::getEvaluation().listOfPossibleFunctions_.end()); 261 temp.insert(temp.end(), CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.begin(), CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.end()); 262 CommandExecutor::getEvaluation().command_ = CommandExecutor::getCommonBegin(temp); 263 CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(0)); 264 CommandExecutor::getEvaluation().functionclass_ = CommandExecutor::getPossibleIdentifier(CommandExecutor::getArgument(0)); 265 return; 266 } 267 } 268 } 269 case CS_Function: 270 { 271 if (CommandExecutor::getEvaluation().functionclass_) 272 { 273 // There is a classname - search for the commandname 274 if (CommandExecutor::argumentsGiven() > 2) 275 { 276 // There is a finished second argument - check if it's a commandname 277 CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(1), CommandExecutor::getEvaluation().functionclass_); 278 279 if (CommandExecutor::getEvaluation().function_) 280 { 281 // It's a function 282 CommandExecutor::getEvaluation().state_ = CS_Params; 283 // Move on to next case 284 } 285 else 286 { 287 // The second argument is bad 288 CommandExecutor::getEvaluation().state_ = CS_Error; 289 AddLanguageEntry("commandexecutorunknownsecondargument", "is not a function of"); 290 CommandExecutor::getEvaluation().errorMessage_ = "Error: " + CommandExecutor::getArgument(1) + " " + GetLocalisation("commandexecutorunknownsecondargument") + " " + CommandExecutor::getEvaluation().functionclass_->getName() + "."; 291 return; 292 } 293 } 294 else 295 { 296 // There is no finished second argument - search for possibilities 297 CommandExecutor::createListOfPossibleFunctions(CommandExecutor::getArgument(1), CommandExecutor::getEvaluation().functionclass_); 298 unsigned int num_functions = CommandExecutor::getEvaluation().listOfPossibleFunctions_.size(); 299 300 if (num_functions == 1) 301 { 302 // It's a function 303 std::string functionname = *(*CommandExecutor::getEvaluation().listOfPossibleFunctions_.begin()).first; 304 CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(functionname, CommandExecutor::getEvaluation().functionclass_); 305 if (getLowercase(functionname) != getLowercase(CommandExecutor::getArgument(1))) 306 { 307 // Unfinished function 308 CommandExecutor::getEvaluation().bCommandChanged_ = true; 309 } 310 CommandExecutor::getEvaluation().state_ = CS_Params; 311 CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().functionclass_->getName() + " " + CommandExecutor::getEvaluation().function_->getName(); 312 if (CommandExecutor::getEvaluation().function_->getParamCount() > 0) 313 CommandExecutor::getEvaluation().command_ += " "; 314 // Move on to next case 315 } 316 else if (num_functions == 0) 317 { 318 // No possibilities 319 CommandExecutor::getEvaluation().state_ = CS_Error; 320 AddLanguageEntry("commandexecutorunknownsecondargumentstart", "has no function starting with"); 321 CommandExecutor::getEvaluation().errorMessage_ = "Error: " + CommandExecutor::getEvaluation().functionclass_->getName() + " " + GetLocalisation("commandexecutorunknownsecondargumentstart") + " " + CommandExecutor::getArgument(1) + "."; 322 return; 323 } 324 else 325 { 326 // There are several possibilities 327 CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().functionclass_->getName() + " " + CommandExecutor::getCommonBegin(CommandExecutor::getEvaluation().listOfPossibleFunctions_); 328 CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(1), CommandExecutor::getEvaluation().functionclass_); 329 return; 330 } 331 } 332 } 333 else 334 { 335 // There is no classname - move on to CS_Shortcut_Params 336 } 337 } 338 std::cout << "Waiting for arguments" << std::endl; 339 case CS_Params: 340 { 341 } 342 case CS_Finished: 343 { 344 // Nothing more to do 167 345 break; 168 346 } 169 case CS_ShortcutOrIdentifier: 170 { 171 std::cout << "parse: state: CS_ShortcutOrIdentifier" << std::endl; 172 if (CommandExecutor::argumentsFinished() > 0 || !CommandExecutor::getEvaluation().bNewCommand_) 173 { 174 // There's already a finished first argument - check if it's function or a classname 175 CommandExecutor::getEvaluation().functionclass_ = CommandExecutor::getPossibleIdentifier(CommandExecutor::getArgument(0)); 176 CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(0)); 177 178 if (CommandExecutor::getEvaluation().function_) 179 { 180 // It's a shortcut - continue parsing 181 CommandExecutor::getEvaluation().state_ = CS_Shortcut_Params; 182 if (CommandExecutor::argumentsFinished() > 0 ) 183 CommandExecutor::parse(command, false); 184 else 185 CommandExecutor::parse(command + " ", false); 186 return; 187 } 188 else if (CommandExecutor::getEvaluation().functionclass_) 189 { 190 // It's a classname - continue parsing 191 CommandExecutor::getEvaluation().state_ = CS_Function; 192 if (CommandExecutor::argumentsFinished() > 0 ) 193 CommandExecutor::parse(command, false); 194 else 195 CommandExecutor::parse(command + " ", false); 196 return; 197 } 198 } 199 200 unsigned int numIdentifiers = CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.size(); 201 unsigned int numCommands = CommandExecutor::getEvaluation().listOfPossibleFunctions_.size(); 202 203 if (CommandExecutor::argumentsFinished() == 0) 204 { 205 // There is no finished first argument 206 if (numCommands == 1 && numIdentifiers == 0) 207 { 208 // It must be this command 209 const std::string* possibleCommand = (*CommandExecutor::getEvaluation().listOfPossibleFunctions_.begin()).second; 210 CommandExecutor::getEvaluation().state_ = CS_Shortcut_Params; 211 CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(*possibleCommand); 212 CommandExecutor::parse(*possibleCommand + " ", false); 213 return; 214 } 215 else if (numIdentifiers == 1 && numCommands == 0) 216 { 217 // It must be this classname 218 const std::string* possibleIdentifier = (*CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.begin()).second; 219 CommandExecutor::getEvaluation().state_ = CS_Function; 220 CommandExecutor::getEvaluation().functionclass_ = CommandExecutor::getPossibleIdentifier(*possibleIdentifier); 221 CommandExecutor::parse(*possibleIdentifier + " ", false); 222 return; 223 } 224 } 225 226 if (numCommands == 0 && numIdentifiers == 0) 227 { 228 // It's not a shortcut nor a classname 229 CommandExecutor::getEvaluation().state_ = CS_Error; 230 AddLanguageEntry("commandexecutorunknownfirstargument", "is not a shortcut nor a classname"); 231 CommandExecutor::getEvaluation().errorMessage_ = "Error: " + CommandExecutor::getArgument(0) + " " + GetLocalisation("commandexecutorunknownfirstargument") + "."; 232 } 347 case CS_Error: 348 { 349 // Bad, very bad 233 350 break; 234 351 } 235 case CS_Function:236 std::cout << "parse: state: CS_Function" << std::endl;237 {238 if (CommandExecutor::getEvaluation().functionclass_ && CommandExecutor::argumentsGiven() > 0)239 {240 if (CommandExecutor::argumentsFinished() > 1 || !CommandExecutor::getEvaluation().bNewCommand_)241 {242 // There is already a second argument - check if it's a valid function243 CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(1), CommandExecutor::getEvaluation().functionclass_);244 245 if (CommandExecutor::getEvaluation().function_)246 {247 // It's a shortcut - continue parsing248 CommandExecutor::getEvaluation().state_ = CS_Function_Params;249 if (CommandExecutor::argumentsFinished() > 1 )250 CommandExecutor::parse(command, false);251 else252 CommandExecutor::parse(command + " ", false);253 return;254 }255 else if (CommandExecutor::argumentsFinished() > 1)256 {257 // It's not a function258 AddLanguageEntry("commandexecutorunknowncommand", "is not a valid commandname");259 CommandExecutor::getEvaluation().errorMessage_ = "Error: " + CommandExecutor::getArgument(1) + " " + GetLocalisation("commandexecutorunknowncommand") + ".";260 CommandExecutor::getEvaluation().state_ = CS_Error;261 }262 }263 264 CommandExecutor::createListOfPossibleFunctions(CommandExecutor::getArgument(1), CommandExecutor::getEvaluation().functionclass_);265 266 if (CommandExecutor::argumentsFinished() <= 1)267 {268 // There is no finished second argument269 unsigned int numFunctions = CommandExecutor::getEvaluation().listOfPossibleFunctions_.size();270 271 if (numFunctions == 1)272 {273 // It must be this command274 const std::string* possibleCommand = (*CommandExecutor::getEvaluation().listOfPossibleFunctions_.begin()).second;275 CommandExecutor::getEvaluation().state_ = CS_Function_Params;276 CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(*possibleCommand, CommandExecutor::getEvaluation().functionclass_);277 CommandExecutor::parse(CommandExecutor::getArgument(0) + " " + *possibleCommand + " ", false);278 return;279 }280 else if (numFunctions == 0)281 {282 // It's not a function283 AddLanguageEntry("commandexecutorunknowncommand", "is not a valid commandname");284 CommandExecutor::getEvaluation().errorMessage_ = "Error: " + CommandExecutor::getArgument(1) + " " + GetLocalisation("commandexecutorunknowncommand") + ".";285 CommandExecutor::getEvaluation().state_ = CS_Error;286 }287 }288 289 // It's ambiguous290 return;291 }292 293 // Bad state294 CommandExecutor::getEvaluation().state_ = CS_Error;295 break;296 }297 case CS_Shortcut_Params:298 std::cout << "parse: state: CS_Shortcut_Params" << std::endl;299 case CS_Function_Params:300 {301 std::cout << "parse: state: CS_Function_Params" << std::endl;302 if (CommandExecutor::getEvaluation().function_)303 {304 unsigned int startindex = 0;305 if (CommandExecutor::getEvaluation().state_ == CS_Shortcut_Params)306 startindex = 1;307 else if (CommandExecutor::getEvaluation().state_ == CS_Function_Params)308 startindex = 2;309 310 if (CommandExecutor::argumentsGiven() >= startindex)311 {312 if ((CommandExecutor::argumentsGiven() == CommandExecutor::argumentsFinished() || !CommandExecutor::getEvaluation().bNewCommand_) && CommandExecutor::enoughArgumentsGiven(CommandExecutor::getEvaluation().function_))313 {314 if (CommandExecutor::getEvaluation().state_ == CS_Shortcut_Params)315 CommandExecutor::getEvaluation().state_ = CS_Shortcut_Finished;316 else if (CommandExecutor::getEvaluation().state_ == CS_Function_Params)317 CommandExecutor::getEvaluation().state_ = CS_Function_Finished;318 319 return;320 }321 else322 {323 CommandExecutor::createListOfPossibleArguments(CommandExecutor::getLastArgument(), CommandExecutor::getEvaluation().function_, CommandExecutor::getEvaluation().commandTokens_.size() - startindex);324 unsigned int numArguments = CommandExecutor::getEvaluation().listOfPossibleArguments_.size();325 326 if (numArguments == 1)327 {328 // There is exactly one possible argument329 const std::string* possibleArgument = (*CommandExecutor::getEvaluation().listOfPossibleArguments_.begin()).second;330 if (CommandExecutor::argumentsGiven() > CommandExecutor::argumentsFinished())331 CommandExecutor::parse(CommandExecutor::getEvaluation().commandTokens_.subSet(0, CommandExecutor::getEvaluation().commandTokens_.size() - 1).join() + " " + (*possibleArgument) + " ", false);332 else333 CommandExecutor::parse(CommandExecutor::getEvaluation().commandTokens_.subSet(0, CommandExecutor::getEvaluation().commandTokens_.size()).join() + " " + (*possibleArgument) + " ", false);334 335 return;336 }337 338 if ((CommandExecutor::argumentsGiven() > CommandExecutor::argumentsFinished()) && (!CommandExecutor::getEvaluation().bNewCommand_))339 {340 // There is more than one argument, but the user wants to use this - check if there is a perfect match341 const std::string* possibleArgument = CommandExecutor::getPossibleArgument(CommandExecutor::getLastArgument(), CommandExecutor::getEvaluation().function_, CommandExecutor::getEvaluation().commandTokens_.size() - startindex);342 if (possibleArgument)343 {344 // There is such an argument - use it345 CommandExecutor::parse(command + " ", false);346 return;347 }348 }349 }350 351 // Nothing to do352 return;353 }354 }355 356 // Bad state357 CommandExecutor::getEvaluation().state_ = CS_Error;358 break;359 }360 case CS_Shortcut_Finished:361 std::cout << "parse: state: CS_Shortcut_Finished" << std::endl;362 break;363 case CS_Function_Finished:364 std::cout << "parse: state: CS_Function_Finished" << std::endl;365 break;366 case CS_Error:367 std::cout << "parse: state: CS_Error" << std::endl;368 break;369 352 } 370 353 } … … 372 355 unsigned int CommandExecutor::argumentsFinished() 373 356 { 374 if (CommandExecutor::getEvaluation().command_.size() > 0) 375 { 376 if (CommandExecutor::getEvaluation().command_[CommandExecutor::getEvaluation().command_.size() - 1] == ' ') 377 return CommandExecutor::getEvaluation().commandTokens_.size(); 378 else if (CommandExecutor::getEvaluation().commandTokens_.size() > 0) 379 return CommandExecutor::getEvaluation().commandTokens_.size() - 1; 380 } 381 return 0; 357 unsigned int argumentsGiven = CommandExecutor::argumentsGiven(); 358 if (argumentsGiven > 0) 359 return argumentsGiven - 1; 360 else 361 return 0; 382 362 } 383 363 384 364 unsigned int CommandExecutor::argumentsGiven() 385 365 { 386 return CommandExecutor::getEvaluation().commandTokens_.size(); 366 if (CommandExecutor::getEvaluation().command_.size() > 0 && CommandExecutor::getEvaluation().command_[CommandExecutor::getEvaluation().command_.size() - 1] == ' ') 367 return CommandExecutor::getEvaluation().commandTokens_.size() + 1; 368 else 369 return CommandExecutor::getEvaluation().commandTokens_.size(); 387 370 } 388 371 … … 390 373 { 391 374 if (CommandExecutor::getEvaluation().functionclass_) 392 return (CommandExecutor:: argumentsGiven() >= (2 + command->getParamCount()));393 else 394 return (CommandExecutor:: argumentsGiven() >= (1 + command->getParamCount()));375 return (CommandExecutor::getEvaluation().commandTokens_.size() >= (2 + command->getParamCount())); 376 else 377 return (CommandExecutor::getEvaluation().commandTokens_.size() >= (1 + command->getParamCount())); 395 378 } 396 379 397 380 std::string CommandExecutor::getArgument(unsigned int index) 398 381 { 399 if ( (index >= 0) &&index < (CommandExecutor::getEvaluation().commandTokens_.size()))382 if (index < (CommandExecutor::getEvaluation().commandTokens_.size())) 400 383 return CommandExecutor::getEvaluation().commandTokens_[index]; 401 384 else … … 405 388 std::string CommandExecutor::getLastArgument() 406 389 { 407 if (CommandExecutor::getEvaluation().commandTokens_.size() > 0) 408 if (CommandExecutor::getEvaluation().commandTokens_.size() > 0 && CommandExecutor::getEvaluation().command_[CommandExecutor::getEvaluation().command_.size() - 1] != ' ') 409 return CommandExecutor::getEvaluation().commandTokens_[CommandExecutor::getEvaluation().commandTokens_.size() - 1]; 410 411 return ""; 390 return CommandExecutor::getArgument(CommandExecutor::argumentsGiven()); 412 391 } 413 392 … … 493 472 } 494 473 474 std::string CommandExecutor::getCommonBegin(const std::list<std::pair<const std::string*, const std::string*> >& list) 475 { 476 if (list.size() == 0) 477 { 478 return ""; 479 } 480 else if (list.size() == 1) 481 { 482 return ((*(*list.begin()).first) + " "); 483 } 484 else 485 { 486 std::string output = ""; 487 for (unsigned int i = 0; true; i++) 488 { 489 char temp = 0; 490 for (std::list<std::pair<const std::string*, const std::string*> >::const_iterator it = list.begin(); it != list.end(); ++it) 491 { 492 if ((*(*it).first).size() > i) 493 { 494 if (it == list.begin()) 495 { 496 temp = (*(*it).first)[i]; 497 } 498 else 499 { 500 if (temp != (*(*it).first)[i]) 501 return output; 502 } 503 } 504 else 505 { 506 return output; 507 } 508 } 509 output += temp; 510 } 511 return output; 512 } 513 } 514 495 515 bool CommandExecutor::compareStringsInList(const std::pair<const std::string*, const std::string*>& first, const std::pair<const std::string*, const std::string*>& second) 496 516 {
Note: See TracChangeset
for help on using the changeset viewer.