diff options
| author | bjculkin <bjculkin@mix.wvu.edu> | 2017-03-16 15:33:09 -0400 |
|---|---|---|
| committer | bjculkin <bjculkin@mix.wvu.edu> | 2017-03-16 15:33:09 -0400 |
| commit | 348e10db258ae0cd0ae61dd99f6359ac4c8c0bd1 (patch) | |
| tree | 96e91ff2c65000612667b98aece9f301f70004ac /dice-lang/src/bjc/dicelang/expr/Lexer.java | |
| parent | ca704c30af8b5bf2695c7128e0b21f505457da7b (diff) | |
Added alternate expression parser.
This adds an alternate expression parser designed solely for arithmetic
operations.
Diffstat (limited to 'dice-lang/src/bjc/dicelang/expr/Lexer.java')
| -rw-r--r-- | dice-lang/src/bjc/dicelang/expr/Lexer.java | 54 |
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]); + } +} |
