use of io.github.wysohn.triggerreactor.core.script.parser.Parser in project TriggerReactor by wysohn.
the class ParserTest method testParam.
@Test
public void testParam() throws IOException, LexerException, ParserException {
Charset charset = Charset.forName("UTF-8");
String text = "#SOUND player.getLocation() \"LEVEL_UP\" 1.0 1.0";
Lexer lexer = new Lexer(text, charset);
Parser parser = new Parser(lexer);
Node root = parser.parse();
Queue<Node> queue = new LinkedList<Node>();
serializeNode(queue, root);
assertEquals(new Node(new Token(Type.THIS, "<This>")), queue.poll());
assertEquals(new Node(new Token(Type.ID, "player")), queue.poll());
assertEquals(new Node(new Token(Type.OPERATOR, ".")), queue.poll());
assertEquals(new Node(new Token(Type.CALL, "getLocation")), queue.poll());
assertEquals(new Node(new Token(Type.OPERATOR, ".")), queue.poll());
assertEquals(new Node(new Token(Type.STRING, "LEVEL_UP")), queue.poll());
assertEquals(new Node(new Token(Type.DECIMAL, "1.0")), queue.poll());
assertEquals(new Node(new Token(Type.DECIMAL, "1.0")), queue.poll());
assertEquals(new Node(new Token(Type.EXECUTOR, "SOUND")), queue.poll());
assertEquals(new Node(new Token(Type.ROOT, "<ROOT>")), queue.poll());
assertEquals(0, queue.size());
}
use of io.github.wysohn.triggerreactor.core.script.parser.Parser in project TriggerReactor by wysohn.
the class TriggerTest method testIfWithElse.
@Test
public void testIfWithElse() throws Exception {
Charset charset = Charset.forName("UTF-8");
String text = "x = 4.0;" + "IF x < 0.0;" + " #TEST \"no\";" + "ELSE;" + " #TEST \"pass\";" + "ENDIF;";
Lexer lexer = new Lexer(text, charset);
Parser parser = new Parser(lexer);
Node root = parser.parse();
Map<String, Executor> executorMap = new HashMap<>();
executorMap.put("TEST", new Executor() {
@Override
protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
Assert.assertEquals("pass", args[0]);
return null;
}
});
Map<String, Placeholder> placeholderMap = new HashMap<>();
Interpreter interpreter = new Interpreter(root, executorMap, placeholderMap, new HashMap<String, Object>(), new HashMap<>(), new CommonFunctions(null));
interpreter.startWithContext(null);
}
use of io.github.wysohn.triggerreactor.core.script.parser.Parser in project TriggerReactor by wysohn.
the class TriggerTest method testGlobalVariable.
@Test
public void testGlobalVariable() throws Exception {
Charset charset = Charset.forName("UTF-8");
String text = "" + "{text+\".something\"} = 12.54\n" + "#MESSAGE {text+\".something\"}\n";
Lexer lexer = new Lexer(text, charset);
Parser parser = new Parser(lexer);
Node root = parser.parse();
Map<String, Executor> executorMap = new HashMap<>();
executorMap.put("MESSAGE", new Executor() {
@Override
public Integer execute(boolean sync, Object context, Object... args) {
Assert.assertEquals(12.54, args[0]);
return null;
}
});
Map<String, Object> map = new HashMap<String, Object>();
Interpreter interpreter = new Interpreter(root, executorMap, new HashMap<>(), map, new HashMap<>(), new CommonFunctions(null));
interpreter.getVars().put("text", "someplayername");
interpreter.startWithContext(null);
Assert.assertTrue(map.containsKey("someplayername.something"));
Assert.assertEquals(12.54, map.get("someplayername.something"));
}
use of io.github.wysohn.triggerreactor.core.script.parser.Parser in project TriggerReactor by wysohn.
the class ParserTest method testIf.
@Test
public void testIf() throws Exception {
Charset charset = Charset.forName("UTF-8");
String text = "" + "IF i == 0;" + " #MESSAGE 0;" + "ELSEIF i == 1;" + " #MESSAGE 1;" + "ELSEIF i == 2;" + " #MESSAGE 2;" + "ELSE;" + " #MESSAGE 3;" + "ENDIF;";
Lexer lexer = new Lexer(text, charset);
Parser parser = new Parser(lexer);
Node root = parser.parse();
Queue<Node> queue = new LinkedList<Node>();
serializeNode(queue, root);
assertEquals(new Node(new Token(Type.THIS, "<This>")), queue.poll());
assertEquals(new Node(new Token(Type.ID, "i")), queue.poll());
assertEquals(new Node(new Token(Type.OPERATOR, ".")), queue.poll());
assertEquals(new Node(new Token(Type.INTEGER, "0")), queue.poll());
assertEquals(new Node(new Token(Type.OPERATOR_L, "==")), queue.poll());
assertEquals(new Node(new Token(Type.INTEGER, "0")), queue.poll());
assertEquals(new Node(new Token(Type.EXECUTOR, "MESSAGE")), queue.poll());
assertEquals(new Node(new Token(Type.BODY, "<BODY>")), queue.poll());
assertEquals(new Node(new Token(Type.THIS, "<This>")), queue.poll());
assertEquals(new Node(new Token(Type.ID, "i")), queue.poll());
assertEquals(new Node(new Token(Type.OPERATOR, ".")), queue.poll());
assertEquals(new Node(new Token(Type.INTEGER, "1")), queue.poll());
assertEquals(new Node(new Token(Type.OPERATOR_L, "==")), queue.poll());
assertEquals(new Node(new Token(Type.INTEGER, "1")), queue.poll());
assertEquals(new Node(new Token(Type.EXECUTOR, "MESSAGE")), queue.poll());
assertEquals(new Node(new Token(Type.BODY, "<BODY>")), queue.poll());
assertEquals(new Node(new Token(Type.THIS, "<This>")), queue.poll());
assertEquals(new Node(new Token(Type.ID, "i")), queue.poll());
assertEquals(new Node(new Token(Type.OPERATOR, ".")), queue.poll());
assertEquals(new Node(new Token(Type.INTEGER, "2")), queue.poll());
assertEquals(new Node(new Token(Type.OPERATOR_L, "==")), queue.poll());
assertEquals(new Node(new Token(Type.INTEGER, "2")), queue.poll());
assertEquals(new Node(new Token(Type.EXECUTOR, "MESSAGE")), queue.poll());
assertEquals(new Node(new Token(Type.BODY, "<BODY>")), queue.poll());
assertEquals(new Node(new Token(Type.INTEGER, "3")), queue.poll());
assertEquals(new Node(new Token(Type.EXECUTOR, "MESSAGE")), queue.poll());
assertEquals(new Node(new Token(Type.BODY, "<BODY>")), queue.poll());
assertEquals(new Node(new Token(Type.ID, "ELSEIF")), queue.poll());
assertEquals(new Node(new Token(Type.BODY, "<BODY>")), queue.poll());
assertEquals(new Node(new Token(Type.ID, "ELSEIF")), queue.poll());
assertEquals(new Node(new Token(Type.BODY, "<BODY>")), queue.poll());
assertEquals(new Node(new Token(Type.ID, "IF")), queue.poll());
assertEquals(new Node(new Token(Type.ROOT, "<ROOT>")), queue.poll());
assertEquals(0, queue.size());
}
use of io.github.wysohn.triggerreactor.core.script.parser.Parser in project TriggerReactor by wysohn.
the class ParserTest method testParse.
@Test
public void testParse() throws IOException, LexerException, ParserException {
Charset charset = Charset.forName("UTF-8");
String text = "#MESSAGE (1+(4/2.0)/3*4-(2/(3*-4)) >= 0)\n" + "#MESSAGE \"text\"\n";
Lexer lexer = new Lexer(text, charset);
Parser parser = new Parser(lexer);
Node root = parser.parse();
Queue<Node> queue = new LinkedList<Node>();
serializeNode(queue, root);
assertEquals(new Node(new Token(Type.INTEGER, "1")), queue.poll());
assertEquals(new Node(new Token(Type.INTEGER, "4")), queue.poll());
assertEquals(new Node(new Token(Type.DECIMAL, "2.0")), queue.poll());
assertEquals(new Node(new Token(Type.OPERATOR_A, "/")), queue.poll());
assertEquals(new Node(new Token(Type.INTEGER, "3")), queue.poll());
assertEquals(new Node(new Token(Type.OPERATOR_A, "/")), queue.poll());
assertEquals(new Node(new Token(Type.INTEGER, "4")), queue.poll());
assertEquals(new Node(new Token(Type.OPERATOR_A, "*")), queue.poll());
assertEquals(new Node(new Token(Type.OPERATOR_A, "+")), queue.poll());
assertEquals(new Node(new Token(Type.INTEGER, "2")), queue.poll());
assertEquals(new Node(new Token(Type.INTEGER, "3")), queue.poll());
assertEquals(new Node(new Token(Type.INTEGER, "4")), queue.poll());
assertEquals(new Node(new Token(Type.UNARYMINUS, "<UNARYMINUS>")), queue.poll());
assertEquals(new Node(new Token(Type.OPERATOR_A, "*")), queue.poll());
assertEquals(new Node(new Token(Type.OPERATOR_A, "/")), queue.poll());
assertEquals(new Node(new Token(Type.OPERATOR_A, "-")), queue.poll());
assertEquals(new Node(new Token(Type.INTEGER, "0")), queue.poll());
assertEquals(new Node(new Token(Type.OPERATOR_L, ">=")), queue.poll());
assertEquals(new Node(new Token(Type.EXECUTOR, "MESSAGE")), queue.poll());
assertEquals(new Node(new Token(Type.STRING, "text")), queue.poll());
assertEquals(new Node(new Token(Type.EXECUTOR, "MESSAGE")), queue.poll());
assertEquals(new Node(new Token(Type.ROOT, "<ROOT>")), queue.poll());
assertEquals(0, queue.size());
}
Aggregations