From b5eb058f2507e6f2a4d9be983c9f2d3296641f67 Mon Sep 17 00:00:00 2001 From: GideonLeGrange Date: Mon, 14 Apr 2014 14:52:47 +0200 Subject: [PATCH] Slight scanner and parser error handling improvements --- src/main/java/me/legrange/mikrotik/impl/Parser.java | 5 ++--- .../java/me/legrange/mikrotik/impl/Scanner.java | 13 +++++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/me/legrange/mikrotik/impl/Parser.java b/src/main/java/me/legrange/mikrotik/impl/Parser.java index ea2185a..519d786 100644 --- a/src/main/java/me/legrange/mikrotik/impl/Parser.java +++ b/src/main/java/me/legrange/mikrotik/impl/Parser.java @@ -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(); } diff --git a/src/main/java/me/legrange/mikrotik/impl/Scanner.java b/src/main/java/me/legrange/mikrotik/impl/Scanner.java index 11f9fb8..49b445b 100644 --- a/src/main/java/me/legrange/mikrotik/impl/Scanner.java +++ b/src/main/java/me/legrange/mikrotik/impl/Scanner.java @@ -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); }