Slight scanner and parser error handling improvements

This commit is contained in:
GideonLeGrange 2014-04-14 14:52:47 +02:00
parent 3d7fee5a4b
commit b5eb058f25
2 changed files with 13 additions and 5 deletions

View File

@ -175,13 +175,12 @@ class Parser {
for (Token want : tokens) {
if (this.token == want) return;
}
throw new ParseException(String.format("Expected %s but found %s", Arrays.asList(tokens), this.token));
throw new ParseException(String.format("Expected %s but found %s at position %d", Arrays.asList(tokens), this.token, scanner.pos()));
}
/** move to the next token returned by the scanner */
private void next() throws ScanException {
// Token was = token;
token = scanner.next();
// System.out.printf("'%s' => '%s'\n", was, token);
while (token == Token.WS) {
token = scanner.next();
}

View File

@ -44,11 +44,13 @@ class Scanner {
private final String symb;
}
/** create a scanner for the given line of text */
Scanner(String line) {
this.line = line;
nextChar();
}
/** return the next token from the text */
Token next() throws ScanException {
text = null;
switch (c) {
@ -79,18 +81,23 @@ class Scanner {
}
/** return the text associated with the last token returned */
String text() {
if (text != null) return text.toString();
return "";
}
/** return the position of the scanner */
int pos() { return pos; }
/** process 'name' tokens which could be key words or text */
private Token name() throws ScanException {
text = new StringBuilder();
while (in(c,"[A-Za-z0-9-\\.]")) {
text.append(c);
nextChar();
}
String val = text.toString();
String val = text.toString().toLowerCase();
switch (val) {
case "where" : return Token.WHERE;
case "not" : return Token.NOT;
@ -107,7 +114,7 @@ class Scanner {
text = new StringBuilder();
while (c != '"') {
if (c == '\n') {
throw new ScanException("Unclosed quoted text");
throw new ScanException("Unclosed quoted text, reached end of line.");
}
text.append(c);
nextChar();
@ -124,6 +131,7 @@ class Scanner {
return Token.WS;
}
/** return the next character from the line of text */
private void nextChar() {
if (pos < line.length()) {
c = line.charAt(pos);
@ -134,6 +142,7 @@ class Scanner {
}
}
/** check if the character matches the give expression */
private boolean in(char c, String cs) {
return ("" + c).matches(cs);
}