Working out ANTLR grammar
This commit is contained in:
parent
c4f2b9ae65
commit
2633088303
|
|
@ -4,7 +4,7 @@ grammar Command;
|
||||||
line: command param* where? retrn? ;
|
line: command param* where? retrn? ;
|
||||||
command: CMD ;
|
command: CMD ;
|
||||||
param: NAME ('=' text )? ;
|
param: NAME ('=' text )? ;
|
||||||
text: NO_SPACE | QUOTED ;
|
text: (QUOTED|VALUE) ;
|
||||||
where: 'where' expr ;
|
where: 'where' expr ;
|
||||||
expr: ( eqExpr | hasExpr | moreExpr | lessExpr ) binExpr? ;
|
expr: ( eqExpr | hasExpr | moreExpr | lessExpr ) binExpr? ;
|
||||||
binExpr: andExpr | orExpr ;
|
binExpr: andExpr | orExpr ;
|
||||||
|
|
@ -17,7 +17,7 @@ orExpr: expr 'or' expr ;
|
||||||
retrn: 'returns' NAME (',' NAME )* ;
|
retrn: 'returns' NAME (',' NAME )* ;
|
||||||
|
|
||||||
NAME : ('a'..'z'|'A'..'Z'|'-')+ ;
|
NAME : ('a'..'z'|'A'..'Z'|'-')+ ;
|
||||||
CMD_PART : ('/' NAME)+ ;
|
CMD : (('/')(NAME))+ ;
|
||||||
CMD : CMD_PART+ ;
|
QUOTED : ('"')(.*?)('"') ;
|
||||||
NO_SPACE : [\\S]+ ;
|
VALUE : (~[ \t\r\n="])+ ;
|
||||||
QUOTED : ('"' [.]* '"') ;
|
WS: [ \n\t\r]+ -> skip;
|
||||||
|
|
@ -7,7 +7,7 @@ package examples;
|
||||||
public class Config {
|
public class Config {
|
||||||
|
|
||||||
|
|
||||||
public static final String HOST = "10.0.1.3";
|
public static final String HOST = "10.0.1.134";
|
||||||
public static final String USERNAME = "admin";
|
public static final String USERNAME = "admin";
|
||||||
public static final String PASSWORD = "";
|
public static final String PASSWORD = "";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,8 @@ public class Example6 extends Example {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void test() throws MikrotikApiException, InterruptedException {
|
private void test() throws MikrotikApiException, InterruptedException {
|
||||||
con.execute("/interface/gre/add remote-address=10.0.1.1 name=gre1 keepalive=10");
|
// con.execute("/interface/gre/add remote-address=1.2.3.4 name=gre1 keepalive=10 comment='test comment'");
|
||||||
|
con.execute("/ip/firewall/filter/add action=drop chain=forward in-interface=gre1 protocol=udp dst-port=78,80");//,80,32");
|
||||||
Thread.sleep(10000); // 10 seconds for the user to look on the router to see the interface with /interface gre print
|
Thread.sleep(10000); // 10 seconds for the user to look on the router to see the interface with /interface gre print
|
||||||
con.execute("/interface/gre/set remote-address=172.16.1.1 .id=gre1");
|
con.execute("/interface/gre/set remote-address=172.16.1.1 .id=gre1");
|
||||||
// now look again and the IP has changed
|
// now look again and the IP has changed
|
||||||
|
|
|
||||||
|
|
@ -486,7 +486,7 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
throw new ApiConnectionException(ex.getMessage(), ex);
|
throw new ApiConnectionException(ex.getMessage(), ex);
|
||||||
}
|
}
|
||||||
if (err != null) {
|
if (err != null) {
|
||||||
throw err;
|
throw new MikrotikApiException(err.getMessage(), err);
|
||||||
}
|
}
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import java.util.HashMap;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Scanner;
|
||||||
import java.util.StringTokenizer;
|
import java.util.StringTokenizer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -44,8 +45,13 @@ class Parser {
|
||||||
if (token == Token.EQUALS) {
|
if (token == Token.EQUALS) {
|
||||||
next();
|
next();
|
||||||
expect(Token.NAME);
|
expect(Token.NAME);
|
||||||
cmd.addParameter(new Parameter(name, text));
|
String val = text;
|
||||||
next();
|
next();
|
||||||
|
while (token == Token.COMMA) {
|
||||||
|
next();
|
||||||
|
val = val + "," + text;
|
||||||
|
}
|
||||||
|
cmd.addParameter(new Parameter(name, val));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
cmd.addParameter(new Parameter(name));
|
cmd.addParameter(new Parameter(name));
|
||||||
|
|
@ -173,9 +179,10 @@ class Parser {
|
||||||
// System.out.printf("%s: %s\n", token, text);
|
// System.out.printf("%s: %s\n", token, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Parser(String text) {
|
private Parser(String line) {
|
||||||
text = text.trim();
|
line = line.trim();
|
||||||
StringTokenizer st = new StringTokenizer(text, " \t,=", true);
|
StringTokenizer st = new StringTokenizer(line, " \t,=", true);
|
||||||
|
boolean quote = false;
|
||||||
while (st.hasMoreElements()) {
|
while (st.hasMoreElements()) {
|
||||||
String t = st.nextToken().trim();
|
String t = st.nextToken().trim();
|
||||||
if (!t.equals("")) {
|
if (!t.equals("")) {
|
||||||
|
|
@ -183,6 +190,7 @@ class Parser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final List<String> words = new LinkedList<String>();
|
private final List<String> words = new LinkedList<String>();
|
||||||
private String text;
|
private String text;
|
||||||
private Token token;
|
private Token token;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue