From 6ca8c6764a8b8769a7a295c0479962a6588580a2 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Fri, 10 Feb 2017 05:39:39 -0500 Subject: Working string literals --- dice-lang/pom.xml | 58 +++++++------ dice-lang/src/bjc/dicelang/v2/DiceLangConsole.java | 4 +- dice-lang/src/bjc/dicelang/v2/DiceLangEngine.java | 99 +++++++++++++++------- 3 files changed, 104 insertions(+), 57 deletions(-) diff --git a/dice-lang/pom.xml b/dice-lang/pom.xml index b98e073..a3a3f6f 100644 --- a/dice-lang/pom.xml +++ b/dice-lang/pom.xml @@ -1,26 +1,34 @@ - 4.0.0 - bjc - dice-lang - 0.0.1-SNAPSHOT - - src - - - maven-compiler-plugin - 3.3 - - 1.8 - 1.8 - - - - - - - bjc - BJC-Utils2 - 0.1.0-SNAPSHOT - - - \ No newline at end of file + 4.0.0 + bjc + dice-lang + 0.0.1-SNAPSHOT + + src + + + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + + + + org.codehaus.mojo + exec-maven-plugin + 1.5.0 + + bjc.dicelang.v2.DiceLangConsole + + + + + + + bjc + BJC-Utils2 + 0.1.0-SNAPSHOT + + + diff --git a/dice-lang/src/bjc/dicelang/v2/DiceLangConsole.java b/dice-lang/src/bjc/dicelang/v2/DiceLangConsole.java index 741d6f5..54bdaec 100644 --- a/dice-lang/src/bjc/dicelang/v2/DiceLangConsole.java +++ b/dice-lang/src/bjc/dicelang/v2/DiceLangConsole.java @@ -1,4 +1,4 @@ -package bjc.dicelang.examples.v2; +package bjc.dicelang.v2; import java.util.Scanner; @@ -36,7 +36,7 @@ public class DiceLangConsole { commandNumber += 1; System.out.printf("(%d) dice-lang> ", commandNumber); - String comm = scn.nextLine(); + comm = scn.nextLine(); } scn.close(); diff --git a/dice-lang/src/bjc/dicelang/v2/DiceLangEngine.java b/dice-lang/src/bjc/dicelang/v2/DiceLangEngine.java index 2bfa53a..51006d7 100644 --- a/dice-lang/src/bjc/dicelang/v2/DiceLangEngine.java +++ b/dice-lang/src/bjc/dicelang/v2/DiceLangEngine.java @@ -1,5 +1,7 @@ -package bjc.utils.dicelang.v2; +package bjc.dicelang.v2; +import bjc.utils.data.IPair; +import bjc.utils.data.Pair; import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.FunctionalMap; import bjc.utils.funcdata.FunctionalStringTokenizer; @@ -7,6 +9,7 @@ import bjc.utils.funcdata.IList; import bjc.utils.funcdata.IMap; import bjc.utils.funcutils.ListUtils; +import java.util.Arrays; import java.util.Deque; import java.util.LinkedList; @@ -47,7 +50,7 @@ public class DiceLangEngine { public boolean runCommand(String command) { // Split the command into tokens IList tokens = FunctionalStringTokenizer - .fromString(currentLine) + .fromString(command) .toList(); // Will hold tokens with string literals removed @@ -63,9 +66,18 @@ public class DiceLangEngine { if(!success) return success; - if(debugMode) - System.out.println("Command after destringing: " + if(debugMode) { + System.out.println("\tCommand after destringing: " + destringed.toString()); + + System.out.println("\tString literals in table"); + stringLiterals.forEach((key, val) -> { + System.out.printf("\t\tName: (%s)\tValue: (%s)\n", + key, val); + }); + } + + return true; } private boolean destringTokens(IList tokens, @@ -76,43 +88,70 @@ public class DiceLangEngine { // The current string literal StringBuilder currentLiteral = new StringBuilder(); + String literalName = "stringLiteral"; for(String token : tokens.toIterable()) { - String[] tokenParts = token.split("^(?!\\\\\")\""); + if(token.startsWith("\"")) { + if(token.endsWith("\"")) { + String litName = literalName + nextLiteral++; + + stringLiterals.put(litName, + token.substring(1, token.length() - 1)); + destringed.add(litName); + + continue; + } - if(tokenParts.length == 1) { - // Insert token into correct place if(stringMode) { - currentLiteral.add(tokenParts[0]); + // @TODO make this not an error + System.out.printf("\tPARSER ERROR: Initial" + +" quotes can only start strings\n"); } else { - destringed.add(tokenParts[0]); - } - } else { - // Handle multiple "'s in a token - for(String stringPart : tokenParts) { - // Insert token into correct place - if(stringMode) { - currentLiteral.add(stringPart); - } else { - destringed.add(stringPart); - } + currentLiteral.append(token.substring(1) + " "); - // We found a quote. Toggle string mode - // and collect the literal - stringMode = !stringMode; + stringMode = true; + } + } else if (token.endsWith("\"")) { + if(!stringMode) { + // @TODO make this not an error + System.out.printf("\tPARSER ERROR: Terminal" + +" quotes can only end strings\n"); + return false; + } else { + currentLiteral.append( + token.substring(0, token.length() - 1)); - if(debugMode) - System.out.printf("DEBUG: Parsed string" - + " literal (" - + currentLiteral.toString() + ")"); + String litName = literalName + nextLiteral++; - stringLiterals.put("stringLiteral" - + nextLiteral, + stringLiterals.put(litName, currentLiteral.toString()); - destringed.add("stringLiteral" + nextLiteral); + destringed.add(litName); - nextLiteral += 1; currentLiteral = new StringBuilder(); + + stringMode = false; + } + } else if (token.contains("\"")) { + if(token.contains("\\\"")) { + if(stringMode) { + currentLiteral.append(token + " "); + } else { + System.out.printf("\tERROR: Escaped quote " + + " outside of string literal\n"); + return false; + } + } else { + // @TODO make this not an error + System.out.printf("\tPARSER ERROR: A string" + + " literal must be delimited by spaces" + + " for now.\n"); + return false; + } + } else { + if(stringMode) { + currentLiteral.append(token + " "); + } else { + destringed.add(token); } } } -- cgit v1.2.3