diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-11-10 19:59:43 -0500 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-11-10 19:59:43 -0500 |
| commit | 038378ca7d33de42da5e6ff6bd12ae1a41fae418 (patch) | |
| tree | 86fa2011f9668d1e23a7563fe3407e2ce77ddfce /dice-lang/src/bjc/dicelang/ast/DiceASTParser.java | |
| parent | 60b097696a707e6389b68023a2c1937f71130035 (diff) | |
Clarity tweaks
Diffstat (limited to 'dice-lang/src/bjc/dicelang/ast/DiceASTParser.java')
| -rw-r--r-- | dice-lang/src/bjc/dicelang/ast/DiceASTParser.java | 228 |
1 files changed, 116 insertions, 112 deletions
diff --git a/dice-lang/src/bjc/dicelang/ast/DiceASTParser.java b/dice-lang/src/bjc/dicelang/ast/DiceASTParser.java index 7f0fc36..259b855 100644 --- a/dice-lang/src/bjc/dicelang/ast/DiceASTParser.java +++ b/dice-lang/src/bjc/dicelang/ast/DiceASTParser.java @@ -30,131 +30,135 @@ import bjc.dicelang.ast.nodes.VariableDiceNode; * */ public class DiceASTParser { - private static IDiceASTNode convertLeafNode(String leafNode) { - DiceLiteralType literalType = ILiteralDiceNode - .getLiteralType(leafNode); - - if (literalType != null) { - switch (literalType) { - case DICE: - return new DiceLiteralNode( - IDiceExpression.toExpression(leafNode)); - case INTEGER: - return new IntegerLiteralNode( - Integer.parseInt(leafNode)); - default: - throw new InputMismatchException( - "Cannot convert string '" + leafNode - + "' into a literal."); - } + private static IDiceASTNode convertLeafNode(String leafNode) { + DiceLiteralType literalType = ILiteralDiceNode + .getLiteralType(leafNode); + + if (literalType != null) { + switch (literalType) { + case DICE: + return new DiceLiteralNode( + IDiceExpression.toExpression(leafNode)); + case INTEGER: + return new IntegerLiteralNode( + Integer.parseInt(leafNode)); + default: + throw new InputMismatchException( + "Cannot convert string '" + leafNode + + "' into a literal."); + } + } + + if (leafNode.matches("[+-]?\\d*\\.\\d+")) { + throw new InputMismatchException( + "Floating point literals are not supported"); + } + + return new VariableDiceNode(leafNode); } - if (leafNode.matches("[+-]?\\d*\\.\\d+")) { - throw new InputMismatchException( - "Floating point literals are not supported"); - } - - return new VariableDiceNode(leafNode); - } + private static IDiceASTNode convertOperatorNode(String operatorNode) { + try { + return OperatorDiceNode.fromString(operatorNode); + } catch (IllegalArgumentException iaex) { + InputMismatchException imex = new InputMismatchException( + "Attempted to parse invalid operator " + operatorNode); - private static IDiceASTNode convertOperatorNode(String operatorNode) { - try { - return OperatorDiceNode.fromString(operatorNode); - } catch (IllegalArgumentException iaex) { - InputMismatchException imex = new InputMismatchException( - "Attempted to parse invalid operator " + operatorNode); + imex.initCause(iaex); - imex.initCause(iaex); - - throw imex; - } - } - - /** - * Create an AST from a list of tokens - * - * @param tokens - * The list of tokens to convert - * @return An AST built from the tokens - */ - public static ITree<IDiceASTNode> createFromString( - IList<String> tokens) { - Predicate<String> specialPicker = (operator) -> { - if (StringUtils.containsOnly(operator, "\\[")) { - return true; - } else if (StringUtils.containsOnly(operator, "\\]")) { - return true; - } - - return false; - }; - - IMap<String, Function<Deque<ITree<String>>, ITree<String>>> operators = new FunctionalMap<>(); - - operators.put("[", (queuedTrees) -> { - Tree<String> openArray = new Tree<>("["); - - return openArray; - }); - - operators.put("]", (queuedTrees) -> { - return parseCloseArray(queuedTrees); - }); - - ITree<String> rawTokens = TreeConstructor.constructTree(tokens, - (token) -> { - return isOperatorNode(token); - }, specialPicker, operators::get); - - ITree<IDiceASTNode> tokenizedTree = rawTokens.rebuildTree( - DiceASTParser::convertLeafNode, - DiceASTParser::convertOperatorNode); - - return tokenizedTree; - } - - private static boolean isOperatorNode(String token) { - if (StringUtils.containsOnly(token, "\\[")) { - return true; - } else if (StringUtils.containsOnly(token, "\\]")) { - return true; + throw imex; + } } - if (token.equals("[]")) { - // This is a synthetic operator, constructed by [ and ] - return true; + /** + * Create an AST from a list of tokens + * + * @param tokens + * The list of tokens to convert + * @return An AST built from the tokens + */ + public static ITree<IDiceASTNode> createFromString( + IList<String> tokens) { + // Mark arrays as special operators + Predicate<String> specialPicker = (operator) -> { + if (StringUtils.containsOnly(operator, "\\[") || + StringUtils.containsOnly(operator, "\\]")) { + return true; + } + + return false; + }; + + // Here is the map for holding special operators + IMap<String, Function<Deque<ITree<String>>, ITree<String>>> operators = new FunctionalMap<>(); + + // Handle open [ + operators.put("[", (queuedTrees) -> { + // Just put in a [ + Tree<String> openArray = new Tree<>("["); + + return openArray; + }); + + operators.put("]", (queuedTrees) -> { + // Parse closing an array + return parseCloseArray(queuedTrees); + }); + + ITree<String> rawTokens = TreeConstructor.constructTree(tokens, + (token) -> { + return isOperatorNode(token); + }, specialPicker, operators::get); + + ITree<IDiceASTNode> tokenizedTree = rawTokens.rebuildTree( + DiceASTParser::convertLeafNode, + DiceASTParser::convertOperatorNode); + + return tokenizedTree; + } + + private static boolean isOperatorNode(String token) { + if (StringUtils.containsOnly(token, "\\[")) { + return true; + } else if (StringUtils.containsOnly(token, "\\]")) { + return true; + } + + if (token.equals("[]")) { + // This is a synthetic operator, constructed by [ and ] + return true; + } + + try { + OperatorDiceNode.fromString(token); + return true; + } catch (@SuppressWarnings("unused") IllegalArgumentException iaex) { + // We don't care about details + return false; + } } - try { - OperatorDiceNode.fromString(token); - return true; - } catch (@SuppressWarnings("unused") IllegalArgumentException iaex) { - // We don't care about details - return false; - } - } - - private static ITree<String> parseCloseArray( - Deque<ITree<String>> queuedTrees) { - IList<ITree<String>> children = new FunctionalList<>(); + private static ITree<String> parseCloseArray( + Deque<ITree<String>> queuedTrees) { + IList<ITree<String>> children = new FunctionalList<>(); - while (shouldContinuePopping(queuedTrees)) { - children.add(queuedTrees.pop()); - } + while (shouldContinuePopping(queuedTrees)) { + children.add(queuedTrees.pop()); + } - queuedTrees.pop(); + queuedTrees.pop(); - children.reverse(); + children.reverse(); - ITree<String> arrayTree = new Tree<>("[]", children); + ITree<String> arrayTree = new Tree<>("[]", children); - return arrayTree; - } + return arrayTree; + } - private static boolean shouldContinuePopping( - Deque<ITree<String>> queuedTrees) { - String peekToken = queuedTrees.peek().getHead(); + private static boolean shouldContinuePopping( + Deque<ITree<String>> queuedTrees) { + String peekToken = queuedTrees.peek().getHead(); - return !peekToken.equals("["); - } + return !peekToken.equals("["); + } } |
