Search in sources :

Example 1 with DeprecationWarning

use of io.github.wysohn.triggerreactor.core.script.warning.DeprecationWarning in project TriggerReactor by wysohn.

the class TestParser method testParseWithDeprecation.

@Test
public void testParseWithDeprecation() throws IOException, LexerException, ParserException {
    Parser.addDeprecationSupervisor((type, value) -> type == Type.ID && "#MODIFYPLAYER".equals(value));
    Charset charset = Charset.forName("UTF-8");
    String text = "#MESSAGE (1+(4/2.0)/3*4-(2/(3*-4)) >= 0)\n" + "#MODIFYPLAYER \"text\"\n";
    Lexer lexer = new Lexer(text, charset);
    Parser parser = new Parser(lexer);
    Node root = parser.parse(true);
    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.OPERATOR_UNARY, "-")), 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, "MODIFYPLAYER")), queue.poll());
    assertEquals(new Node(new Token(Type.ROOT, "<ROOT>")), queue.poll());
    assertEquals(0, queue.size());
    assertEquals(1, parser.getWarnings().size());
    assertEquals(new DeprecationWarning(2, "#MODIFYPLAYER", "#MODIFYPLAYER \"text\""), parser.getWarnings().get(0));
}
Also used : Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Charset(java.nio.charset.Charset) Token(io.github.wysohn.triggerreactor.core.script.Token) DeprecationWarning(io.github.wysohn.triggerreactor.core.script.warning.DeprecationWarning) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 2 with DeprecationWarning

use of io.github.wysohn.triggerreactor.core.script.warning.DeprecationWarning in project TriggerReactor by wysohn.

the class Parser method nextToken.

private void nextToken() throws IOException, ParserException {
    try {
        token = lexer.getToken();
        if (showWarnings && token != null) {
            int row = lexer.getRow();
            Type type = token.type;
            String value = String.valueOf(token.value);
            if (type == null || value == null)
                return;
            if (deprecationSupervisors.stream().anyMatch(deprecationSupervisor -> deprecationSupervisor.isDeprecated(type, value))) {
                this.warnings.add(new DeprecationWarning(row, value, lexer.getScriptLines()[row - 1]));
            }
        }
    } catch (LexerException lex) {
        ParserException pex = new ParserException("Error occured while processing a token after " + token);
        pex.initCause(lex);
        throw pex;
    }
}
Also used : Type(io.github.wysohn.triggerreactor.core.script.Token.Type) DeprecationWarning(io.github.wysohn.triggerreactor.core.script.warning.DeprecationWarning) LexerException(io.github.wysohn.triggerreactor.core.script.lexer.LexerException)

Aggregations

DeprecationWarning (io.github.wysohn.triggerreactor.core.script.warning.DeprecationWarning)2 Token (io.github.wysohn.triggerreactor.core.script.Token)1 Type (io.github.wysohn.triggerreactor.core.script.Token.Type)1 Lexer (io.github.wysohn.triggerreactor.core.script.lexer.Lexer)1 LexerException (io.github.wysohn.triggerreactor.core.script.lexer.LexerException)1 Charset (java.nio.charset.Charset)1 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1