use of net.robinfriedli.aiode.exceptions.CommandParseException in project aiode by robinfriedli.
the class CommandParser method parse.
public void parse(String input) throws CommandParseException, IllegalEscapeCharacterException, UnclosedQuotationsException {
chars = input.toCharArray();
for (; currentPosition < chars.length; currentPosition++) {
char character = chars[currentPosition];
Mode previousMode = currentMode;
try {
if (isEscaped) {
if (!checkLegalEscape(character)) {
throw new IllegalEscapeCharacterException("Illegal escape character at " + (currentPosition - 1));
}
currentMode = currentMode.handleLiteral(character);
isEscaped = false;
} else if (isOpenQuotation) {
if (character == '\\') {
isEscaped = true;
} else if (character == '"') {
isOpenQuotation = false;
} else {
currentMode = currentMode.handleLiteral(character);
}
} else {
if (character == '\\') {
isEscaped = true;
} else if (character == '"') {
isOpenQuotation = true;
} else {
currentMode = currentMode.handle(character);
}
}
if (currentPosition == chars.length - 1) {
if (isOpenQuotation) {
throw new UnclosedQuotationsException("Unclosed double quotes");
}
if (isEscaped) {
throw new IllegalEscapeCharacterException("Illegal trailing escape character");
}
currentMode.terminate();
}
} catch (CommandParseException e) {
throw e;
} catch (UserException e) {
throw new CommandParseException(e.getMessage(), command.getCommandBody(), e, currentPosition);
}
// fire mode switch event even if it's a different instance of the same mode
if (previousMode != currentMode) {
fireOnModeSwitch(previousMode, currentMode, currentPosition, character);
}
}
fireOnParseFinished();
}
use of net.robinfriedli.aiode.exceptions.CommandParseException in project aiode by robinfriedli.
the class CommandParserInterceptor method performChained.
@Override
public void performChained(Command command) {
if (command instanceof AbstractCommand) {
AbstractCommand textBasedCommand = (AbstractCommand) command;
CommandParser commandParser = new CommandParser(textBasedCommand, ArgumentPrefixProperty.getForCurrentContext());
try {
commandParser.parse();
} catch (CommandParseException e) {
throw e;
} catch (Exception e) {
throw new UnexpectedCommandSetupException(e);
}
}
}
use of net.robinfriedli.aiode.exceptions.CommandParseException in project aiode by robinfriedli.
the class ArgumentBuildingMode method terminate.
@Override
public void terminate() {
try {
if (argumentBuilder.length() == 0) {
throw new InvalidArgumentException("Missing argument identifier");
}
ArgumentController argumentController = command.getArgumentController();
String argument = argumentBuilder.toString().trim();
String argumentValue = argumentValueBuilder.toString().trim();
argumentController.setArgument(argument, argumentValue);
commandParser.fireOnArgumentParsed(argument, argumentValue);
} catch (UserException e) {
throw new CommandParseException(e.getMessage(), command.getCommandBody(), e, conceptionIndex);
}
}
Aggregations