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);
}
Aggregations