Cleanup to parser
This commit is contained in:
parent
7d29f9265e
commit
1f31a6f3b5
|
|
@ -3,21 +3,27 @@ package me.legrange.mikrotik.impl;
|
|||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import me.legrange.mikrotik.impl.Scanner.Token;
|
||||
|
||||
/**
|
||||
* Parse the pseudo-command line into command objects.
|
||||
*
|
||||
* @author GideonLeGrange
|
||||
*/
|
||||
class Parser {
|
||||
|
||||
/** parse the given bit of text into a Command object */
|
||||
/**
|
||||
* parse the given bit of text into a Command object
|
||||
*/
|
||||
static Command parse(String text) throws ParseException {
|
||||
Parser parser = new Parser(text);
|
||||
return parser.parse();
|
||||
}
|
||||
|
||||
/** 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 {
|
||||
command();
|
||||
while (!is(Token.WHERE, Token.RETURN, Token.EOL)) {
|
||||
|
|
@ -67,8 +73,7 @@ class Parser {
|
|||
next();
|
||||
}
|
||||
cmd.addParameter(new Parameter(name, val.toString()));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
cmd.addParameter(new Parameter(name));
|
||||
}
|
||||
}
|
||||
|
|
@ -92,7 +97,7 @@ class Parser {
|
|||
case EQUALS:
|
||||
eqExpr(name);
|
||||
break;
|
||||
case NOT_EQUALS :
|
||||
case NOT_EQUALS:
|
||||
notExpr(name);
|
||||
break;
|
||||
case LESS:
|
||||
|
|
@ -106,23 +111,29 @@ class Parser {
|
|||
}
|
||||
}
|
||||
break;
|
||||
case LEFT_BRACKET : {
|
||||
next();
|
||||
expr();
|
||||
expect(Token.RIGHT_BRACKET);
|
||||
next();
|
||||
}
|
||||
case LEFT_BRACKET:
|
||||
nestedExpr();
|
||||
break;
|
||||
}
|
||||
// if you get here, you had a expression, see if you want more.
|
||||
switch (token) {
|
||||
case AND : andExpr();
|
||||
case AND:
|
||||
andExpr();
|
||||
break;
|
||||
case OR : orExpr();
|
||||
case OR:
|
||||
orExpr();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void nestedExpr() throws ParseException {
|
||||
expect(Token.LEFT_BRACKET);
|
||||
next();
|
||||
expr();
|
||||
expect(Token.RIGHT_BRACKET);
|
||||
next();
|
||||
}
|
||||
|
||||
private void andExpr() throws ParseException {
|
||||
next(); // eat and
|
||||
expr();
|
||||
|
|
@ -185,19 +196,21 @@ class Parser {
|
|||
cmd.addProperty(props.toArray(new String[props.size()]));
|
||||
}
|
||||
|
||||
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) {
|
||||
private boolean is(Token... tokens) {
|
||||
for (Token want : tokens) {
|
||||
if (this.token == want) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** move to the next token returned by the scanner */
|
||||
/**
|
||||
* move to the next token returned by the scanner
|
||||
*/
|
||||
private void next() throws ScanException {
|
||||
token = scanner.next();
|
||||
while (token == Token.WS) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue