summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/expr/Lexer.java
blob: 75267ad625d7f8a29d08c8f734e7cb67aeb54c9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package bjc.dicelang.expr;

import bjc.utils.parserutils.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]);
	}
}