Cleanup to parser

This commit is contained in:
Gideon Le Grange 2020-01-03 08:52:01 +02:00
parent 7d29f9265e
commit 1f31a6f3b5
1 changed files with 41 additions and 28 deletions

View File

@ -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));
}
}
@ -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();
@ -197,7 +208,9 @@ class Parser {
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) {