Slight scanner and parser error handling improvements

This commit is contained in:
GideonLeGrange 2014-04-14 16:19:03 +02:00
parent c44c02294b
commit 10f88ad9f0
1 changed files with 10 additions and 5 deletions

View File

@ -20,7 +20,7 @@ class Parser {
/** run parse on the internal data and return the command object */ /** run parse on the internal data and return the command object */
private Command parse() throws ParseException { private Command parse() throws ParseException {
command(); command();
while (!((token == Token.WHERE) || (token == Token.RETURN) || (token == Token.EOL))) { while (!is(Token.WHERE, Token.RETURN, Token.EOL)) {
param(); param();
} }
if (token == Token.WHERE) { if (token == Token.WHERE) {
@ -54,8 +54,8 @@ class Parser {
expect(Token.TEXT); expect(Token.TEXT);
StringBuilder val = new StringBuilder(text); StringBuilder val = new StringBuilder(text);
next(); next();
while (token == Token.COMMA) { while (is(Token.COMMA, Token.SLASH)) {
val.append(","); val.append(token);
next(); next();
expect(Token.TEXT); expect(Token.TEXT);
val.append(text); val.append(text);
@ -163,12 +163,17 @@ class Parser {
} }
private void expect(Token...tokens) throws ParseException { private void expect(Token...tokens) throws ParseException {
for (Token want : tokens) { if (!is(tokens))
if (this.token == want) return;
}
throw new ParseException(String.format("Expected %s but found %s at position %d", Arrays.asList(tokens), this.token, scanner.pos())); throw new ParseException(String.format("Expected %s but found %s at position %d", Arrays.asList(tokens), this.token, scanner.pos()));
} }
private boolean is(Token...tokens) {
for (Token want : tokens) {
if (this.token == want) return true;
}
return false;
}
/** move to the next token returned by the scanner */ /** move to the next token returned by the scanner */
private void next() throws ScanException { private void next() throws ScanException {
token = scanner.next(); token = scanner.next();