forked from lykhonis/FJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtom.java
More file actions
62 lines (49 loc) · 1.46 KB
/
Copy pathAtom.java
File metadata and controls
62 lines (49 loc) · 1.46 KB
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
56
57
58
59
60
61
62
package com.fjava.translator;
import java.util.ArrayList;
import java.util.List;
abstract class Atom implements Translatable {
private final TranslatorFactory factory;
private final List<String> tokens = new ArrayList<String>();
public Atom(TranslatorFactory factory) {
this.factory = factory;
}
protected boolean test(TokenType... types) {
if (types == null || types.length == 0)
throw new FJavaTranslatingException("Invalid tokens for " + getClass().getSimpleName());
int i = 0;
tokens.clear();
for (TokenType type : types) {
final Token token = factory.tokenizer.getToken(i++);
if (token == null || token.type != type) {
tokens.clear();
return false;
}
tokens.add(token.image);
}
return true;
}
protected String format(String format) {
String result = format;
int i = 1;
for (String token : tokens)
result = result.replaceAll("\\$" + i++, token);
return result;
}
protected String get() {
return factory.translate();
}
protected void skipRequired() {
factory.tokenizer.skip(tokens.size() - 1);
}
protected void require(TokenType type) {
final TokenType foundType = require();
if (!foundType.equals(type))
throw new FJavaTranslatingException("Expecting " + type + " but found " + foundType + ".");
}
protected TokenType require() {
final Token token = factory.tokenizer.nextToken();
if (token == null)
throw new FJavaTranslatingException("Expecting something but found EOF.");
return token.type;
}
}