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.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import me.legrange.mikrotik.impl.Scanner.Token; import me.legrange.mikrotik.impl.Scanner.Token;
/** /**
* Parse the pseudo-command line into command objects. * Parse the pseudo-command line into command objects.
*
* @author GideonLeGrange * @author GideonLeGrange
*/ */
class Parser { 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 { static Command parse(String text) throws ParseException {
Parser parser = new Parser(text); Parser parser = new Parser(text);
return parser.parse(); 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 { private Command parse() throws ParseException {
command(); command();
while (!is(Token.WHERE, Token.RETURN, Token.EOL)) { while (!is(Token.WHERE, Token.RETURN, Token.EOL)) {
@ -67,8 +73,7 @@ class Parser {
next(); next();
} }
cmd.addParameter(new Parameter(name, val.toString())); cmd.addParameter(new Parameter(name, val.toString()));
} } else {
else {
cmd.addParameter(new Parameter(name)); cmd.addParameter(new Parameter(name));
} }
} }
@ -106,23 +111,29 @@ class Parser {
} }
} }
break; break;
case LEFT_BRACKET : { case LEFT_BRACKET:
next(); nestedExpr();
expr();
expect(Token.RIGHT_BRACKET);
next();
}
break; break;
} }
// if you get here, you had a expression, see if you want more. // if you get here, you had a expression, see if you want more.
switch (token) { switch (token) {
case AND : andExpr(); case AND:
andExpr();
break; break;
case OR : orExpr(); case OR:
orExpr();
break; break;
} }
} }
private void nestedExpr() throws ParseException {
expect(Token.LEFT_BRACKET);
next();
expr();
expect(Token.RIGHT_BRACKET);
next();
}
private void andExpr() throws ParseException { private void andExpr() throws ParseException {
next(); // eat and next(); // eat and
expr(); expr();
@ -197,7 +208,9 @@ class Parser {
return false; 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 { private void next() throws ScanException {
token = scanner.next(); token = scanner.next();
while (token == Token.WS) { while (token == Token.WS) {