From c82e3b3b2de0633317ec8fc85925e91422820597 Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Sun, 8 Oct 2017 22:39:59 -0300 Subject: Start splitting into maven modules --- .../bjc/utils/parserutils/TokenTransformer.java | 131 +++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 base/src/main/java/bjc/utils/parserutils/TokenTransformer.java (limited to 'base/src/main/java/bjc/utils/parserutils/TokenTransformer.java') diff --git a/base/src/main/java/bjc/utils/parserutils/TokenTransformer.java b/base/src/main/java/bjc/utils/parserutils/TokenTransformer.java new file mode 100644 index 0000000..30ccc5a --- /dev/null +++ b/base/src/main/java/bjc/utils/parserutils/TokenTransformer.java @@ -0,0 +1,131 @@ +package bjc.utils.parserutils; + +import java.util.Deque; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.UnaryOperator; + +import bjc.utils.data.IHolder; +import bjc.utils.data.ITree; +import bjc.utils.data.Pair; +import bjc.utils.data.Tree; +import bjc.utils.parserutils.TreeConstructor.ConstructorState; +import bjc.utils.parserutils.TreeConstructor.QueueFlattener; + +/* + * Handle creating ASTs from tokens. + */ +final class TokenTransformer implements Consumer { + /* + * Handle operators + */ + private final class OperatorHandler implements UnaryOperator> { + private final TokenType element; + + public OperatorHandler(final TokenType element) { + this.element = element; + } + + @Override + public ConstructorState apply(final ConstructorState pair) { + /* + * Replace the current AST with the result of handling an operator + */ + return new ConstructorState<>(pair.bindLeft(queuedASTs -> { + return handleOperator(queuedASTs); + })); + } + + private ConstructorState handleOperator(final Deque> queuedASTs) { + /* + * The AST we're going to hand back + */ + ITree newAST; + + /* + * Handle special operators + */ + if (isSpecialOperator.test(element)) { + newAST = handleSpecialOperator.apply(element).apply(queuedASTs); + } else { + /* + * Error if we don't have enough for a binary operator + */ + if (queuedASTs.size() < 2) { + final String msg = String.format( + "Attempted to parse binary operator without enough operands\n\tProblem operator is: %s\n\tPossible operand is: %s", + element.toString(), queuedASTs.peek().toString()); + + throw new IllegalStateException(msg); + } + + /* + * Grab the two operands + */ + final ITree right = queuedASTs.pop(); + final ITree left = queuedASTs.pop(); + + /* + * Create a new AST + */ + newAST = new Tree<>(element, left, right); + } + + /* + * Stick it onto the stack + */ + queuedASTs.push(newAST); + + /* + * Hand back the state + */ + return new ConstructorState<>(queuedASTs, newAST); + } + } + + private final IHolder> initialState; + + private final Predicate operatorPredicate; + + private final Predicate isSpecialOperator; + private final Function> handleSpecialOperator; + + /* + * Create a new transformer + */ + public TokenTransformer(final IHolder> initialState, + final Predicate operatorPredicate, final Predicate isSpecialOperator, + final Function> handleSpecialOperator) { + this.initialState = initialState; + this.operatorPredicate = operatorPredicate; + this.isSpecialOperator = isSpecialOperator; + this.handleSpecialOperator = handleSpecialOperator; + } + + @Override + public void accept(final TokenType element) { + /* + * Handle operators + */ + if (operatorPredicate.test(element)) { + initialState.transform(new OperatorHandler(element)); + } else { + final ITree newAST = new Tree<>(element); + + /* + * Insert the new tree into the AST + */ + initialState.transform(pair -> { + /* + * Transform the pair, ignoring the current AST in favor of the one consisting of the current element + */ + return new ConstructorState<>(pair.bindLeft(queue -> { + queue.push(newAST); + + return new Pair<>(queue, newAST); + })); + }); + } + } +} -- cgit v1.2.3