Search in sources :

Example 1 with CommandParseException

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();
}
Also used : CommandParseException(net.robinfriedli.aiode.exceptions.CommandParseException) UnclosedQuotationsException(net.robinfriedli.aiode.exceptions.UnclosedQuotationsException) UserException(net.robinfriedli.aiode.exceptions.UserException) IllegalEscapeCharacterException(net.robinfriedli.aiode.exceptions.IllegalEscapeCharacterException)

Example 2 with CommandParseException

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);
        }
    }
}
Also used : CommandParseException(net.robinfriedli.aiode.exceptions.CommandParseException) AbstractCommand(net.robinfriedli.aiode.command.AbstractCommand) UnexpectedCommandSetupException(net.robinfriedli.aiode.exceptions.UnexpectedCommandSetupException) CommandParseException(net.robinfriedli.aiode.exceptions.CommandParseException) UnexpectedCommandSetupException(net.robinfriedli.aiode.exceptions.UnexpectedCommandSetupException) CommandParser(net.robinfriedli.aiode.command.parser.CommandParser)

Example 3 with CommandParseException

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);
    }
}
Also used : CommandParseException(net.robinfriedli.aiode.exceptions.CommandParseException) InvalidArgumentException(net.robinfriedli.aiode.exceptions.InvalidArgumentException) ArgumentController(net.robinfriedli.aiode.command.argument.ArgumentController) UserException(net.robinfriedli.aiode.exceptions.UserException)

Aggregations

CommandParseException (net.robinfriedli.aiode.exceptions.CommandParseException)3 UserException (net.robinfriedli.aiode.exceptions.UserException)2 AbstractCommand (net.robinfriedli.aiode.command.AbstractCommand)1 ArgumentController (net.robinfriedli.aiode.command.argument.ArgumentController)1 CommandParser (net.robinfriedli.aiode.command.parser.CommandParser)1 IllegalEscapeCharacterException (net.robinfriedli.aiode.exceptions.IllegalEscapeCharacterException)1 InvalidArgumentException (net.robinfriedli.aiode.exceptions.InvalidArgumentException)1 UnclosedQuotationsException (net.robinfriedli.aiode.exceptions.UnclosedQuotationsException)1 UnexpectedCommandSetupException (net.robinfriedli.aiode.exceptions.UnexpectedCommandSetupException)1