in odps-console-basic/src/main/java/com/aliyun/openservices/odps/console/utils/CommandSplitter.java [139:219]
private void parse() throws ODPSConsoleException {
for(int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
switch(state) {
case START:
switch(c) {
case ' ':
case '\t':
case '\f':
case '\n':
case '\r':
commandBuffer.append(c);
break;
case '#':
state = COMMENT;
break;
default:
normalSwitch(c);
}
break;
case PRE_COMMENT:
if (c == '-') {
state = COMMENT;
flushTokenBuffer();
} else {
state = NORMAL;
commandBuffer.append('-');
tokenBuffer.append('-');
i--;
}
break;
case COMMENT:
if (c == '\n' || c == '\r') {
commandBuffer.append(c);
state = END;
}
break;
case END:
if (c != '\n' && c != '\r') {
state = START;
i--;
} else {
commandBuffer.append(c);
}
break;
case QUOTE:
tokenBuffer.append(c);
commandBuffer.append(c);
if (c == quoteType) {
state = NORMAL;
flushTokenBuffer();
} else if (c == '\\') {
state = ESCAPE;
}
break;
case ESCAPE:
tokenBuffer.append(c);
commandBuffer.append(c);
state = QUOTE;
break;
case NORMAL:
normalSwitch(c);
break;
default:
throw new IllegalArgumentException(
String.format("impossible thing happened. pos=%s, char='%s'", i, c));
}
}
switch(state) {
case QUOTE:
case ESCAPE:
throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + " string not closed");
default:
// in case last token/command without ";"
flushTokenBuffer();
flushCommandBuffer();
}
parsed = true;
}