Slight scanner and parser error handling improvements
This commit is contained in:
parent
c44c02294b
commit
10f88ad9f0
|
|
@ -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,10 +163,15 @@ class Parser {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void expect(Token...tokens) throws ParseException {
|
private void expect(Token...tokens) throws ParseException {
|
||||||
|
if (!is(tokens))
|
||||||
|
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) {
|
for (Token want : tokens) {
|
||||||
if (this.token == want) return;
|
if (this.token == want) return true;
|
||||||
}
|
}
|
||||||
throw new ParseException(String.format("Expected %s but found %s at position %d", Arrays.asList(tokens), this.token, scanner.pos()));
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** move to the next token returned by the scanner */
|
/** move to the next token returned by the scanner */
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue