Changeset 1390 for code/branches/console/src/core/CommandExecutor.cc
- Timestamp:
- May 23, 2008, 2:10:52 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/console/src/core/CommandExecutor.cc
r1351 r1390 56 56 ConsoleCommand& CommandExecutor::addConsoleCommandShortcut(ConsoleCommand* command) 57 57 { 58 std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_.find(command->getName()); 59 if (it != CommandExecutor::getInstance().consoleCommandShortcuts_.end()) 60 { 61 COUT(2) << "Warning: Overwriting console-command shortcut with name " << command->getName() << "." << std::endl; 62 } 63 64 58 65 CommandExecutor::getInstance().consoleCommandShortcuts_[command->getName()] = command; 59 66 CommandExecutor::getInstance().consoleCommandShortcuts_LC_[getLowercase(command->getName())] = command; … … 135 142 switch (CommandExecutor::getEvaluation().getState()) 136 143 { 137 } 138 } 139 140 bool CommandExecutor::argumentsFinished(unsigned int num) 141 { 142 return (CommandExecutor::argumentsFinished() >= num); 144 case CS_Uninitialized: 145 // Impossible 146 break; 147 case CS_Empty: 148 CommandExecutor::createListOfPossibleIdentifiers(CommandExecutor::getArgument(0)); 149 CommandExecutor::createListOfPossibleFunctions(CommandExecutor::getArgument(0)); 150 151 if (CommandExecutor::argumentsGiven() > 0) 152 { 153 CommandExecutor::getEvaluation().setState(CS_ShortcutOrIdentifier); 154 CommandExecutor::parse(command, false); 155 return; 156 } 157 break; 158 case CS_ShortcutOrIdentifier: 159 if (CommandExecutor::argumentsFinished() > 0 || !CommandExecutor::getEvaluation().isNewCommand()) 160 { 161 // There's already a finished first argument - check if it's function or a classname 162 CommandExecutor::getEvaluation().setIdentifier(CommandExecutor::getPossibleIdentifier(CommandExecutor::getArgument(0))); 163 CommandExecutor::getEvaluation().setFunction(CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(0))); 164 165 if (CommandExecutor::getEvaluation().getFunction()) 166 { 167 // It's a shortcut - continue parsing 168 CommandExecutor::getEvaluation().setState(CS_Shortcut_Params); 169 if (CommandExecutor::argumentsFinished() > 0 ) 170 CommandExecutor::parse(command, false); 171 else 172 CommandExecutor::parse(command + " ", false); 173 return; 174 } 175 else if (CommandExecutor::getEvaluation().getIdentifier()) 176 { 177 // It's a classname - continue parsing 178 CommandExecutor::getEvaluation().setState(CS_Function); 179 if (CommandExecutor::argumentsFinished() > 0 ) 180 CommandExecutor::parse(command, false); 181 else 182 CommandExecutor::parse(command + " ", false); 183 return; 184 } 185 } 186 187 if (CommandExecutor::argumentsFinished() == 0) 188 { 189 // There is no finished first argument 190 unsigned int numIdentifiers = CommandExecutor::getEvaluation().getListOfPossibleIdentifiers().size(); 191 unsigned int numCommands = CommandExecutor::getEvaluation().getListOfPossibleFunctions().size(); 192 193 if (numCommands == 1 && numIdentifiers == 0) 194 { 195 // It must be this command 196 const std::string* possibleCommand = (*CommandExecutor::getEvaluation().getListOfPossibleFunctions().begin()).second; 197 CommandExecutor::getEvaluation().setState(CS_Shortcut_Params); 198 CommandExecutor::getEvaluation().setFunction(CommandExecutor::getPossibleCommand(*possibleCommand)); 199 CommandExecutor::parse(*possibleCommand + " ", false); 200 return; 201 } 202 else if (numIdentifiers == 1 && numCommands == 0) 203 { 204 // It must be this classname 205 const std::string* possibleIdentifier = (*CommandExecutor::getEvaluation().getListOfPossibleIdentifiers().begin()).second; 206 CommandExecutor::getEvaluation().setState(CS_Function); 207 CommandExecutor::getEvaluation().setIdentifier(CommandExecutor::getPossibleIdentifier(*possibleIdentifier)); 208 CommandExecutor::parse(*possibleIdentifier + " ", false); 209 return; 210 } 211 } 212 213 // It's not a shortcut nor a classname 214 CommandExecutor::getEvaluation().setState(CS_Error); 215 AddLanguageEntry("commandexecutorunknownfirstargument", "is not a shortcut nor a classname"); 216 CommandExecutor::getEvaluation().setError("Error: " + CommandExecutor::getArgument(0) + " " + GetLocalisation("commandexecutorunknownfirstargument") + "."); 217 break; 218 case CS_Function: 219 if (CommandExecutor::getEvaluation().getIdentifier() && CommandExecutor::argumentsGiven() > 1) 220 { 221 if (CommandExecutor::argumentsFinished() > 1 || !CommandExecutor::getEvaluation().isNewCommand()) 222 { 223 // There is already a second argument - check if it's a valid function 224 CommandExecutor::getEvaluation().setFunction(CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(1), CommandExecutor::getEvaluation().getIdentifier())); 225 226 if (CommandExecutor::getEvaluation().getFunction()) 227 { 228 // It's a shortcut - continue parsing 229 CommandExecutor::getEvaluation().setState(CS_Function_Params); 230 if (CommandExecutor::argumentsFinished() > 1 ) 231 CommandExecutor::parse(command, false); 232 else 233 CommandExecutor::parse(command + " ", false); 234 return; 235 } 236 } 237 238 if (CommandExecutor::argumentsFinished() <= 1) 239 { 240 // There is no finished second argument 241 CommandExecutor::createListOfPossibleFunctions(CommandExecutor::getArgument(0), CommandExecutor::getEvaluation().getIdentifier()); 242 unsigned int numFunctions = CommandExecutor::getEvaluation().getListOfPossibleFunctions().size(); 243 244 if (numFunctions == 1) 245 { 246 // It must be this command 247 const std::string* possibleCommand = (*CommandExecutor::getEvaluation().getListOfPossibleFunctions().begin()).second; 248 CommandExecutor::getEvaluation().setState(CS_Function_Params); 249 CommandExecutor::getEvaluation().setFunction(CommandExecutor::getPossibleCommand(*possibleCommand)); 250 CommandExecutor::parse(*possibleCommand + " ", false); 251 return; 252 } 253 } 254 255 // It's not a function 256 AddLanguageEntry("commandexecutorunknowncommand", "is not a valid commandname"); 257 CommandExecutor::getEvaluation().setError("Error: " + CommandExecutor::getArgument(1) + " " + GetLocalisation("commandexecutorunknowncommand") + "."); 258 } 259 260 // Bad state 261 CommandExecutor::getEvaluation().setState(CS_Error); 262 break; 263 case CS_Shortcut_Params: 264 case CS_Function_Params: 265 if (CommandExecutor::getEvaluation().getFunction()) 266 { 267 unsigned int startindex = 0; 268 if (CommandExecutor::getEvaluation().getState() == CS_Shortcut_Params) 269 startindex = 1; 270 else if (CommandExecutor::getEvaluation().getState() == CS_Function_Params) 271 startindex = 2; 272 273 if (CommandExecutor::argumentsGiven() >= startindex) 274 { 275 if (CommandExecutor::enoughArgumentsGiven(CommandExecutor::getEvaluation().getFunction())) 276 { 277 if (CommandExecutor::getEvaluation().getState() == CS_Shortcut_Params) 278 CommandExecutor::getEvaluation().setState(CS_Shortcut_Finished); 279 else if (CommandExecutor::getEvaluation().getState() == CS_Function_Params) 280 CommandExecutor::getEvaluation().setState(CS_Function_Finished); 281 return; 282 } 283 else 284 { 285 CommandExecutor::createListOfPossibleArguments(CommandExecutor::getLastArgument(), CommandExecutor::getEvaluation().getFunction(), CommandExecutor::getEvaluation().getOriginalTokens().size() - startindex); 286 unsigned int numArguments = CommandExecutor::getEvaluation().getListOfPossibleArguments().size(); 287 288 if (numArguments == 1) 289 { 290 // There is exactly one possible argument 291 const std::string* possibleArgument = (*CommandExecutor::getEvaluation().getListOfPossibleArguments().begin()).second; 292 CommandExecutor::parse(CommandExecutor::getEvaluation().getTokens().subSet(0, CommandExecutor::getEvaluation().getTokens().size() - 1 - startindex).join() + " " + (*possibleArgument) + " "); 293 return; 294 } 295 296 if (!CommandExecutor::getEvaluation().isNewCommand()) 297 { 298 // There is more than one argument, but the user wants to use this - check if there is a perfect match 299 const std::string* possibleArgument = CommandExecutor::getPossibleArgument(CommandExecutor::getLastArgument(), CommandExecutor::getEvaluation().getFunction(), CommandExecutor::getEvaluation().getOriginalTokens().size() - startindex); 300 if (possibleArgument) 301 { 302 // There is such an argument - use it 303 CommandExecutor::parse(command + " ", false); 304 return; 305 } 306 } 307 } 308 } 309 } 310 311 // Bad state 312 CommandExecutor::getEvaluation().setState(CS_Error); 313 break; 314 case CS_Shortcut_Finished: 315 case CS_Function_Finished: 316 case CS_Error: 317 break; 318 } 143 319 } 144 320 … … 152 328 } 153 329 154 std::string CommandExecutor::getToken(unsigned int index) 330 unsigned int CommandExecutor::argumentsGiven() 331 { 332 return CommandExecutor::getEvaluation().getOriginalTokens().size(); 333 } 334 335 bool CommandExecutor::enoughArgumentsGiven(ConsoleCommand* command) 336 { 337 if (CommandExecutor::getEvaluation().getIdentifier()) 338 return (CommandExecutor::argumentsGiven() >= (2 + command->getParamCount())); 339 else 340 return (CommandExecutor::argumentsGiven() >= (1 + command->getParamCount())); 341 } 342 343 std::string CommandExecutor::getArgument(unsigned int index) 155 344 { 156 345 if ((index >= 0) && index < (CommandExecutor::getEvaluation().getOriginalTokens().size())) … … 160 349 } 161 350 162 unsigned int CommandExecutor::argumentsGiven() 163 { 164 return CommandExecutor::getEvaluation().getOriginalTokens().size(); 165 } 166 167 bool CommandExecutor::enoughArgumentsGiven(ConsoleCommand* command, unsigned int head) 168 { 169 return (CommandExecutor::argumentsGiven() >= (head + command->getParamCount())); 170 } 171 172 void CommandExecutor::createListOfPossibleFunctionClasses(const std::string& fragment) 351 std::string CommandExecutor::getLastArgument() 352 { 353 if (CommandExecutor::getEvaluation().getOriginalTokens().size() > 0) 354 return CommandExecutor::getEvaluation().getOriginalTokens()[0]; 355 else 356 return ""; 357 } 358 359 void CommandExecutor::createListOfPossibleIdentifiers(const std::string& fragment) 173 360 { 174 361 for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getLowercaseIdentifierMapBegin(); it != Identifier::getLowercaseIdentifierMapEnd(); ++it) 175 362 if ((*it).second->hasConsoleCommands()) 176 363 if ((*it).first.find(getLowercase(fragment)) == 0 || fragment == "") 177 CommandExecutor::getEvaluation().getListOfPossible FunctionClasses().push_back(std::pair<const std::string*, const std::string*>(&(*it).first, &(*it).second->getName()));178 179 CommandExecutor::getEvaluation().getListOfPossible FunctionClasses().sort(CommandExecutor::compareStringsInList);364 CommandExecutor::getEvaluation().getListOfPossibleIdentifiers().push_back(std::pair<const std::string*, const std::string*>(&(*it).first, &(*it).second->getName())); 365 366 CommandExecutor::getEvaluation().getListOfPossibleIdentifiers().sort(CommandExecutor::compareStringsInList); 180 367 } 181 368 … … 197 384 } 198 385 386 void CommandExecutor::createListOfPossibleArguments(const std::string& fragment, ConsoleCommand* command, unsigned int param) 387 { 388 for (std::list<std::pair<std::string, std::string> >::const_iterator it = command->getArgumentCompletionListBegin(param); it != command->getArgumentCompletionListEnd(param); ++it) 389 if ((*it).first.find(getLowercase(fragment)) == 0 || fragment == "") 390 CommandExecutor::getEvaluation().getListOfPossibleArguments().push_back(std::pair<const std::string*, const std::string*>(&(*it).first, &(*it).second)); 391 392 CommandExecutor::getEvaluation().getListOfPossibleIdentifiers().sort(CommandExecutor::compareStringsInList); 393 394 } 395 199 396 Identifier* CommandExecutor::getPossibleIdentifier(const std::string& name) 200 397 { … … 223 420 } 224 421 422 const std::string* CommandExecutor::getPossibleArgument(const std::string& name, ConsoleCommand* command, unsigned int param) 423 { 424 std::string lowercasename = getLowercase(name); 425 for (std::list<std::pair<std::string, std::string> >::const_iterator it = command->getArgumentCompletionListBegin(param); it != command->getArgumentCompletionListEnd(param); ++it) 426 if ((*it).first == lowercasename) 427 return &(*it).second; 428 429 return 0; 430 } 431 225 432 bool CommandExecutor::compareStringsInList(const std::pair<const std::string*, const std::string*>& first, const std::pair<const std::string*, const std::string*>& second) 226 433 {
Note: See TracChangeset
for help on using the changeset viewer.