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) { for (Token want : tokens) {
if (this.token == want) return; 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 { private void next() throws ScanException {
// Token was = token;
token = scanner.next(); token = scanner.next();
// System.out.printf("'%s' => '%s'\n", was, token);
while (token == Token.WS) { while (token == Token.WS) {
token = scanner.next(); token = scanner.next();
} }

View File

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