Search in sources :

Example 1 with Lexer

use of io.github.wysohn.triggerreactor.core.script.lexer.Lexer in project TriggerReactor by wysohn.

the class Parser method main.

public static void main(String[] ar) throws IOException, LexerException, ParserException {
    Charset charset = Charset.forName("UTF-8");
    /*        String text = ""
                + "X = 5\n"
                + "str = \"abc\"\n"
                + "WHILE 1 > 0\n"
                + "    str = str + X\n"
                + "    IF player.in.health > 2 && player.in.health > 0\n"
                + "        #MESSAGE 3*4\n"
                + "    ELSE\n"
                + "        #MESSAGE str\n"
                + "    ENDIF\n"
                + "    #MESSAGE player.getTest().in.getHealth()\n"
                + "    player.getTest().in.health = player.getTest().in.getHealth() + 1.2\n"
                + "    #MESSAGE player.in.hasPermission(\"t\")\n"
                + "    X = X - 1\n"
                + "    IF X < 0\n"
                + "        #STOP\n"
                + "    ENDIF\n"
                + "    #WAIT 1\n"
                + "ENDWHILE";*/
    /*        String text = ""
                + "rand = common.random(3)\n"
                + "IF rand == 0\n"
                + "#MESSAGE 0\n"
                + "ENDIF\n"
                + "IF rand == 1\n"
                + "#MESSAGE 1\n"
                + "ENDIF\n"
                + "IF rand == 2\n"
                + "#MESSAGE 2\n"
                + "ENDIF";*/
    // String text = "#MESSAGE /mw goto ETC";
    // String text = "#MESSAGE args[0]";
    /*        String text = ""
                + "FOR i = 0:10\n"
                + "    #TEST:MESSAGE \"test i=\"+i..i\n"
                + "ENDFOR\n";*/
    /*        String text = "x = 4.0;"
                + "#TEST1 -1;"
                + "#TEST2 -2.0;"
                + "#TEST3 -$test3;"
                + "#TEST4 -x;";*/
    // String text = "#MESSAGE $random:1 == 0";
    String text = "" + "IF i == 0;" + "    #MESSAGE 0;" + "ELSEIF i == 1;" + "    #MESSAGE 1;" + "ELSEIF i == 2;" + "    #MESSAGE 2;" + "ELSE;" + "    #MESSAGE 3;" + "ENDIF;";
    System.out.println("original: \n" + text);
    Lexer lexer = new Lexer(text, charset);
    Parser parser = new Parser(lexer);
    Node root = parser.parse();
    System.out.println(root.toString());
    JFrame frame = new JFrame("Manual Nodes");
    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Root");
    setNode(rootNode, root);
    JTree tree = new JTree(rootNode);
    JScrollPane scrollPane = new JScrollPane(tree);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Also used : JScrollPane(javax.swing.JScrollPane) Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) JTree(javax.swing.JTree) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) JFrame(javax.swing.JFrame) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) Charset(java.nio.charset.Charset)

Example 2 with Lexer

use of io.github.wysohn.triggerreactor.core.script.lexer.Lexer in project TriggerReactor by wysohn.

the class ParserTest method testFor.

@Test
public void testFor() throws Exception {
    Charset charset = Charset.forName("UTF-8");
    String text = "" + "FOR i = 0:10;" + "    #MESSAGE \"test i=\"+i;" + "ENDFOR;";
    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.INTEGER, "10")), queue.poll());
    assertEquals(new Node(new Token(Type.ITERATOR, "<ITERATOR>")), queue.poll());
    assertEquals(new Node(new Token(Type.STRING, "test i=")), 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.OPERATOR_A, "+")), 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, "FOR")), 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 3 with Lexer

use of io.github.wysohn.triggerreactor.core.script.lexer.Lexer in project TriggerReactor by wysohn.

the class TriggerTest method testSimpleIf.

@Test
public void testSimpleIf() throws Exception {
    Charset charset = Charset.forName("UTF-8");
    String text = "x = 4.0;" + "IF x > 0.0;" + "    #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 4 with Lexer

use of io.github.wysohn.triggerreactor.core.script.lexer.Lexer in project TriggerReactor by wysohn.

the class TriggerTest method testShortCircuit.

@Test
public void testShortCircuit() throws Exception {
    Charset charset = Charset.forName("UTF-8");
    String text = "" + "IF player != null && player.health == 0.82;" + "    #TEST1 \"work\";" + "ENDIF;" + "IF player2 == null || player2.health == 0.82;" + "    #TEST2 \"work2\";" + "ENDIF;";
    Lexer lexer = new Lexer(text, charset);
    Parser parser = new Parser(lexer);
    Node root = parser.parse();
    Map<String, Executor> executorMap = new HashMap<String, Executor>() {

        {
            put("TEST1", new Executor() {

                @Override
                protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
                    Assert.assertEquals("work", args[0]);
                    return null;
                }
            });
            put("TEST2", new Executor() {

                @Override
                protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
                    Assert.assertEquals("work2", args[0]);
                    return null;
                }
            });
        }
    };
    Interpreter interpreter = new Interpreter(root, executorMap, new HashMap<>(), new HashMap<String, Object>(), new HashMap<>(), new CommonFunctions(null));
    interpreter.getVars().put("player", new InTest());
    interpreter.getVars().put("player2", new InTest());
    interpreter.startWithContext(null);
    interpreter.getVars().remove("player");
    interpreter.getVars().remove("player2");
    interpreter.startWithContext(null);
}
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 5 with Lexer

use of io.github.wysohn.triggerreactor.core.script.lexer.Lexer in project TriggerReactor by wysohn.

the class TriggerTest method testIteration.

@Test
public void testIteration() throws Exception {
    CommonFunctions mockFunctions = mock(CommonFunctions.class);
    Player[] mockPlayer = new Player[2];
    mockPlayer[0] = mock(Player.class);
    mockPlayer[1] = mock(Player.class);
    when(mockPlayer[0].getName()).thenReturn("Player0");
    when(mockPlayer[1].getName()).thenReturn("Player1");
    Charset charset = Charset.forName("UTF-8");
    String text = "" + "FOR player = getPlayers()\n" + "    #MESSAGE player.getName()\n" + "ENDFOR\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() {

        int index = 0;

        @Override
        public Integer execute(boolean sync, Object context, Object... args) {
            Assert.assertEquals(mockPlayer[index].getName(), args[0]);
            index++;
            return null;
        }
    });
    Collection players = new ArrayList<Player>() {

        {
            add(mockPlayer[0]);
            add(mockPlayer[1]);
        }
    };
    when(mockFunctions.getPlayers()).thenReturn(players);
    Map<String, Object> map = new HashMap<String, Object>();
    Interpreter interpreter = new Interpreter(root, executorMap, new HashMap<>(), map, new HashMap<>(), mockFunctions);
    interpreter.startWithContext(null);
}
Also used : Player(org.bukkit.entity.Player) Interpreter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter) HashMap(java.util.HashMap) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) ArrayList(java.util.ArrayList) 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) Collection(java.util.Collection) CommonFunctions(io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions) Test(org.junit.Test)

Aggregations

Lexer (io.github.wysohn.triggerreactor.core.script.lexer.Lexer)32 Charset (java.nio.charset.Charset)32 Test (org.junit.Test)31 Node (io.github.wysohn.triggerreactor.core.script.parser.Node)27 Parser (io.github.wysohn.triggerreactor.core.script.parser.Parser)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 Token (io.github.wysohn.triggerreactor.core.script.Token)10 Placeholder (io.github.wysohn.triggerreactor.core.script.interpreter.Placeholder)7 LinkedList (java.util.LinkedList)6 Player (org.bukkit.entity.Player)4 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 JFrame (javax.swing.JFrame)1 JScrollPane (javax.swing.JScrollPane)1 JTree (javax.swing.JTree)1 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)1 Location (org.bukkit.Location)1