Search in sources :

Example 11 with Parser

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());
}
Also used : Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) Charset(java.nio.charset.Charset) Token(io.github.wysohn.triggerreactor.core.script.Token) LinkedList(java.util.LinkedList) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Test(org.junit.Test)

Example 12 with Parser

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);
}
Also used : Placeholder(io.github.wysohn.triggerreactor.core.script.interpreter.Placeholder) Interpreter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter) HashMap(java.util.HashMap) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) Charset(java.nio.charset.Charset) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Executor(io.github.wysohn.triggerreactor.core.script.interpreter.Executor) CommonFunctions(io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions) Test(org.junit.Test)

Example 13 with Parser

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"));
}
Also used : Interpreter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter) HashMap(java.util.HashMap) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) Charset(java.nio.charset.Charset) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Executor(io.github.wysohn.triggerreactor.core.script.interpreter.Executor) CommonFunctions(io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions) Test(org.junit.Test)

Example 14 with Parser

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());
}
Also used : Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) Charset(java.nio.charset.Charset) Token(io.github.wysohn.triggerreactor.core.script.Token) LinkedList(java.util.LinkedList) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Test(org.junit.Test)

Example 15 with Parser

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());
}
Also used : Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) Charset(java.nio.charset.Charset) Token(io.github.wysohn.triggerreactor.core.script.Token) LinkedList(java.util.LinkedList) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Test(org.junit.Test)

Aggregations

Lexer (io.github.wysohn.triggerreactor.core.script.lexer.Lexer)27 Node (io.github.wysohn.triggerreactor.core.script.parser.Node)27 Parser (io.github.wysohn.triggerreactor.core.script.parser.Parser)27 Charset (java.nio.charset.Charset)27 Test (org.junit.Test)27 CommonFunctions (io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions)21 Executor (io.github.wysohn.triggerreactor.core.script.interpreter.Executor)21 Interpreter (io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter)21 HashMap (java.util.HashMap)21 Placeholder (io.github.wysohn.triggerreactor.core.script.interpreter.Placeholder)7 Token (io.github.wysohn.triggerreactor.core.script.Token)6 LinkedList (java.util.LinkedList)6 Player (org.bukkit.entity.Player)4 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Location (org.bukkit.Location)1 ItemStack (org.bukkit.inventory.ItemStack)1 PlayerInventory (org.bukkit.inventory.PlayerInventory)1