use of net.robinfriedli.aiode.exceptions.UnclosedQuotationsException 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();
}
Aggregations