Search in sources :

Example 1 with Token

use of primal.parser.Lexer.Token in project suite by stupidsing.

the class IterativeParser method parse.

public Node parse(String in0) {
    var in = Preprocess.transform(PreprocessorFactory.create(operators), in0).k;
    var stack = new ArrayDeque<Section>();
    Sink<Operator> addOperator = operator -> {
        var section = stack.peek();
        var tree = section.unwind(operator);
        var tree0 = tree.getRight();
        var tree1 = Tree.of(operator, tree0, Atom.NIL);
        Tree.forceSetRight(tree, tree1);
        section.push(tree1);
    };
    Sink<Node> add = node -> {
        var section = stack.peek();
        if (!section.isDanglingRight)
            addOperator.f(opTuple);
        Tree.forceSetRight(section.list.getLast(), node);
        section.isDanglingRight = false;
    };
    var lex = new Lexer(operators, opTuple, in);
    stack.push(new Section(' '));
    Token token;
    while ((token = lex.lex()) != null) {
        var operator = token.operator;
        var data = token.getData();
        var ch = data.charAt(0);
        if (operator != null) {
            addOperator.f(operator);
            if (operator == TermOp.BRACES)
                stack.push(new Section('{'));
        } else if (ch == '(' || ch == '[' || ch == '{')
            stack.push(new Section(ch));
        else if (ch == ')' || ch == ']' || ch == '}') {
            var section = stack.pop();
            var kind = section.kind;
            if (kind == '(' && ch == ')' || kind == '[' && ch == ']' || kind == '{' && ch == '}') {
                var node = section.unwind(null).getRight();
                if (ch == ']')
                    node = TreeTuple.of(Atom.of("["), node);
                else if (ch == '}')
                    node = TreeTuple.of(Atom.of("{"), node);
                add.f(node);
            } else
                fail("cannot parse " + in);
        } else if (ch == '`')
            if (stack.peek().kind == ch) {
                var node = stack.pop().unwind(null).getRight();
                node = TreeTuple.of(Atom.of("`"), node);
                add.f(node);
            } else
                stack.push(new Section(ch));
        else if (Is.notBlank(data))
            add.f(terminalParser.parseTerminal(data));
    }
    return stack.size() == 1 ? stack.pop().unwind(null).getRight() : fail("cannot parse " + in);
}
Also used : Operator(primal.parser.Operator) Fail.fail(primal.statics.Fail.fail) Operator(primal.parser.Operator) TermOp(suite.node.io.TermOp) Deque(java.util.Deque) Assoc(primal.parser.Operator.Assoc) Sink(primal.fp.Funs.Sink) Is(primal.Verbs.Is) Tree(suite.node.Tree) Lexer(primal.parser.Lexer) Node(suite.node.Node) List(java.util.List) Token(primal.parser.Lexer.Token) Preprocess(suite.text.Preprocess) Atom(suite.node.Atom) ArrayDeque(java.util.ArrayDeque) TreeTuple(suite.node.tree.TreeTuple) Lexer(primal.parser.Lexer) Node(suite.node.Node) Token(primal.parser.Lexer.Token) ArrayDeque(java.util.ArrayDeque)

Aggregations

ArrayDeque (java.util.ArrayDeque)1 Deque (java.util.Deque)1 List (java.util.List)1 Is (primal.Verbs.Is)1 Sink (primal.fp.Funs.Sink)1 Lexer (primal.parser.Lexer)1 Token (primal.parser.Lexer.Token)1 Operator (primal.parser.Operator)1 Assoc (primal.parser.Operator.Assoc)1 Fail.fail (primal.statics.Fail.fail)1 Atom (suite.node.Atom)1 Node (suite.node.Node)1 Tree (suite.node.Tree)1 TermOp (suite.node.io.TermOp)1 TreeTuple (suite.node.tree.TreeTuple)1 Preprocess (suite.text.Preprocess)1