summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/expr/Lexer.java
diff options
context:
space:
mode:
Diffstat (limited to 'dice-lang/src/bjc/dicelang/expr/Lexer.java')
-rw-r--r--dice-lang/src/bjc/dicelang/expr/Lexer.java54
1 files changed, 54 insertions, 0 deletions
diff --git a/dice-lang/src/bjc/dicelang/expr/Lexer.java b/dice-lang/src/bjc/dicelang/expr/Lexer.java
new file mode 100644
index 0000000..cff1170
--- /dev/null
+++ b/dice-lang/src/bjc/dicelang/expr/Lexer.java
@@ -0,0 +1,54 @@
+package bjc.dicelang.expr;
+
+import bjc.utils.funcutils.TokenSplitter;
+
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Implements the lexer for simple expression operations.
+ *
+ * @author Ben Culkin
+ */
+public class Lexer {
+ /*
+ * Spliter we use
+ */
+ private TokenSplitter split;
+
+ /**
+ * Create a new expression lexer.
+ */
+ public Lexer() {
+ split = new TokenSplitter();
+
+ split.addDelimiter("(", ")");
+ split.addDelimiter("+", "-", "*", "/");
+ }
+
+ /**
+ * Convert a string from a input command to a series of infix tokens.
+ *
+ * @param inp
+ * The input command.
+ * @param tks
+ * The token state
+ *
+ * @return A series of infix tokens representing the command.
+ */
+ public Token[] lexString(String inp, Tokens tks) {
+ String[] spacedTokens = inp.split("[ \t]");
+
+ List<Token> tokens = new LinkedList<>();
+
+ for(String spacedToken : spacedTokens) {
+ String[] rawTokens = split.split(spacedToken);
+
+ for(String tok : rawTokens) {
+ tokens.add(tks.lexToken(tok, spacedToken));
+ }
+ }
+
+ return tokens.toArray(new Token[0]);
+ }
+}