Fixed regression to #7 caused by fix for #11

This commit is contained in:
Gideon le Grange 2014-10-08 06:08:19 +02:00
parent 935c76ae34
commit 00275d61a4
3 changed files with 49 additions and 31 deletions

View File

@ -19,9 +19,10 @@ public class Example8 extends Example {
} }
private void test() throws MikrotikApiException, InterruptedException { private void test() throws MikrotikApiException, InterruptedException {
List<Map<String, String>> res = con.execute("/ip/hotspot/user/print where uptime!=1"); List<Map<String, String>> res = con.execute("/ip/hotspot/user/print where uptime!=1");
for (Map<String, String> r : res) { for (Map<String, String> r : res) {
System.out.println(r); System.out.println(r);
} }
// con.execute("/ip/firewall/filter/add chain=forward hotspot=!auth protocol=tcp src-port=8000-8084");
} }
} }

View File

@ -51,8 +51,13 @@ class Parser {
next(); next();
if (token == Token.EQUALS) { if (token == Token.EQUALS) {
next(); next();
StringBuilder val = new StringBuilder();
if (token == Token.PIPE) { // handle cases like hotspot=!auth
val.append(token);
next();
}
expect(Token.TEXT); expect(Token.TEXT);
StringBuilder val = new StringBuilder(text); val.append(text);
next(); next();
while (is(Token.COMMA, Token.SLASH)) { while (is(Token.COMMA, Token.SLASH)) {
val.append(token); val.append(token);

View File

@ -15,6 +15,7 @@
*/ */
package me.legrange.mikrotik.impl; package me.legrange.mikrotik.impl;
import static me.legrange.mikrotik.impl.Scanner.Token.*;
/** /**
* A simple scanner. * A simple scanner.
* *
@ -25,7 +26,7 @@ class Scanner {
enum Token { enum Token {
SLASH("/"), COMMA(","), EOL(), WS, TEXT, SLASH("/"), COMMA(","), EOL(), WS, TEXT,
LESS("<"), MORE(">"), EQUALS("="), NOT_EQUALS("!="), LESS("<"), MORE(">"), EQUALS("="), NOT_EQUALS("!="), PIPE("!"),
WHERE, NOT, AND, OR, RETURN; WHERE, NOT, AND, OR, RETURN;
@Override @Override
@ -59,27 +60,27 @@ class Scanner {
text = null; text = null;
switch (c) { switch (c) {
case '\n': case '\n':
return Token.EOL; return EOL;
case ' ': case ' ':
case '\t': case '\t':
return whiteSpace(); return whiteSpace();
case ',': case ',':
nextChar(); nextChar();
return Token.COMMA; return COMMA;
case '/': case '/':
nextChar(); nextChar();
return Token.SLASH; return SLASH;
case '<': case '<':
nextChar(); nextChar();
return Token.LESS; return LESS;
case '>': case '>':
nextChar(); nextChar();
return Token.MORE; return MORE;
case '=': case '=':
nextChar(); nextChar();
return Token.EQUALS; return EQUALS;
case '!': case '!':
return notEquals(); return pipe();
case '"': case '"':
return quotedText('"'); return quotedText('"');
case '\'': case '\'':
@ -119,17 +120,17 @@ class Scanner {
String val = text.toString().toLowerCase(); String val = text.toString().toLowerCase();
switch (val) { switch (val) {
case "where": case "where":
return Token.WHERE; return WHERE;
case "not": case "not":
return Token.NOT; return NOT;
case "and": case "and":
return Token.AND; return AND;
case "or": case "or":
return Token.OR; return OR;
case "return": case "return":
return Token.RETURN; return RETURN;
} }
return Token.TEXT; return TEXT;
} }
/** /**
@ -146,7 +147,19 @@ class Scanner {
nextChar(); nextChar();
} }
nextChar(); // eat the '"' nextChar(); // eat the '"'
return Token.TEXT; return TEXT;
}
/**
* process notEquals !
*/
private Token pipe() {
nextChar(); // eat !
if (c == '=') {
nextChar(); // eat =
return NOT_EQUALS;
}
return PIPE;
} }
/** /**
@ -156,19 +169,7 @@ class Scanner {
while ((c == ' ') || (c == '\t')) { while ((c == ' ') || (c == '\t')) {
nextChar(); nextChar();
} }
return Token.WS; return WS;
}
/**
* process the not equals token
*/
private Token notEquals() throws ScanException {
nextChar(); // eat the !
if (c != '=') {
throw new ScanException(String.format("Expected = after !, found '%c'", c));
}
nextChar(); // eat the =
return Token.NOT_EQUALS;
} }
/** /**
@ -183,6 +184,17 @@ class Scanner {
} }
} }
/**
* look ahead one character
*/
private char peek() {
if (pos < line.length()) {
return line.charAt(pos);
} else {
return '\n';
}
}
/** /**
* check if the character matches the give expression * check if the character matches the give expression
*/ */