summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java
diff options
context:
space:
mode:
authorEVE <EVE@EVE-PC>2017-03-13 16:41:45 -0400
committerEVE <EVE@EVE-PC>2017-03-13 16:41:45 -0400
commit01136c6796e21f023713e026674576d8e623462d (patch)
treee77886fe0e0adaf3c0430fba9ce248ef83f74fe4 /dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java
parent870d769cfc152171d27b2331a7c590d0b307ad48 (diff)
Formatting
Diffstat (limited to 'dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java')
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java221
1 files changed, 105 insertions, 116 deletions
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java
index d536cc3..87f3640 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java
@@ -29,135 +29,124 @@ import bjc.utils.parserutils.TreeConstructor;
*
*/
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.");
- }
- }
-
- if (leafNode.matches("[+-]?\\d*\\.\\d+")) {
- throw new InputMismatchException(
- "Floating point literals are not supported");
- }
-
- return new VariableDiceNode(leafNode);
+ 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 convertOperatorNode(String operatorNode) {
- try {
- return OperatorDiceNode.fromString(operatorNode);
- } catch (IllegalArgumentException iaex) {
- InputMismatchException imex = new InputMismatchException(
- "Attempted to parse invalid operator " + operatorNode);
+ if (leafNode.matches("[+-]?\\d*\\.\\d+")) {
+ throw new InputMismatchException("Floating point literals are not supported");
+ }
+
+ return new VariableDiceNode(leafNode);
+ }
- imex.initCause(iaex);
+ private static IDiceASTNode convertOperatorNode(String operatorNode) {
+ try {
+ return OperatorDiceNode.fromString(operatorNode);
+ } catch (IllegalArgumentException iaex) {
+ InputMismatchException imex = new InputMismatchException(
+ "Attempted to parse invalid operator " + operatorNode);
- throw imex;
- }
+ 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) {
+ // 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;
}
- /**
- * 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 (IllegalArgumentException iaex) {
- // We don't care about details
- return false;
- }
+ if (token.equals("[]")) {
+ // This is a synthetic operator, constructed by [ and ]
+ return true;
}
- private static ITree<String> parseCloseArray(
- Deque<ITree<String>> queuedTrees) {
- IList<ITree<String>> children = new FunctionalList<>();
+ try {
+ OperatorDiceNode.fromString(token);
+ return true;
+ } catch (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<>();
- 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("[");
+ }
}