This repository has been archived by the owner on Apr 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 290
/
WordLexerRule.java
55 lines (44 loc) · 1.79 KB
/
WordLexerRule.java
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
55
/*******************************************************************************
* Copyright (c) 2015 Bruno Medeiros and other Contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.lang.tooling.parser.lexer;
import melnorme.lang.utils.parse.ICharacterReader;
import melnorme.lang.utils.parse.LexingUtils;
import melnorme.utilbox.collections.HashMap2;
public class WordLexerRule<TOKEN> implements ILexingRule2<TOKEN> {
protected final TOKEN whitespaceToken;
protected final TOKEN defaultWordToken;
protected final HashMap2<String, TOKEN> tokenMap = new HashMap2<>();
protected String lastEvaluatedWord;
public WordLexerRule(TOKEN whitespaceToken, TOKEN defaultWordToken) {
this.whitespaceToken = whitespaceToken;
this.defaultWordToken = defaultWordToken;
}
public void addKeywords(TOKEN token, String[] keywords) {
for(String keyword : keywords) {
addWord(keyword, token);
}
}
public void addWord(String keyword, TOKEN token) {
tokenMap.put(keyword, token);
}
@Override
public TOKEN doEvaluateToken(ICharacterReader reader) {
if(LexingUtils.skipWhitespace(reader) > 0) {
return whitespaceToken;
}
lastEvaluatedWord = LexingUtils.readJavaIdentifier(reader);
if(lastEvaluatedWord.isEmpty()) {
return null;
}
TOKEN keywordToken = tokenMap.get(lastEvaluatedWord);
return (keywordToken == null) ? defaultWordToken : keywordToken;
}
}