1 package com.github.mygreen.cellformatter.tokenizer;
2
3
4
5
6
7
8
9
10 public abstract class Token {
11
12
13
14
15 public static final Symbol SYMBOL_SEMI_COLON = new Symbol(";");
16
17
18
19
20 public static final Symbol SYMBOL_COLON = new Symbol(",");
21
22
23
24
25 public static final Symbol SYMBOL_PERCENT = new Symbol("%");
26
27
28
29
30 public static final Symbol SYMBOL_SLASH = new Symbol("/");
31
32
33
34
35 public static final Symbol SYMBOL_DOT = new Symbol(".");
36
37
38
39
40 public static final Symbol SYMBOL_AT_MARK = new Symbol("@");
41
42
43
44
45 public static final String STR_ESCAPE_BACKSPACE = "\\";
46
47
48
49
50 public static final String STR_ESCAPE_QUESTION = "!";
51
52
53
54
55 public static final String[] STR_ESCAPES = {STR_ESCAPE_BACKSPACE, STR_ESCAPE_QUESTION};
56
57
58
59
60 private final String value;
61
62 public Token(final String value) {
63 this.value = value;
64 }
65
66 public static Word word(final String token) {
67 return new Word(token);
68 }
69
70 public static EscapedChar escapedChar(final String token) {
71 return new EscapedChar(token);
72 }
73
74 public static Condition condition(final String token) {
75 return new Condition(token);
76 }
77
78 public static Underscore underscore(final String token) {
79 return new Underscore(token);
80 }
81
82 public static Asterisk asterisk(final String token) {
83 return new Asterisk(token);
84 }
85
86 public static Factor factor(final String token) {
87 return new Factor(token);
88 }
89
90 public static Formatter formatter(final String token) {
91 return new Formatter(token);
92 }
93
94 public static Digits digits(final String token) {
95 return new Digits(token);
96 }
97
98 public Word asWord() {
99 return (Word) this;
100 }
101
102 public Symbol asSymbol() {
103 return (Symbol) this;
104 }
105
106 public EscapedChar asEscapedChar() {
107 return (EscapedChar) this;
108 }
109
110 public Condition asCondition() {
111 return (Condition) this;
112 }
113
114 public Underscore asUnderscore() {
115 return (Underscore) this;
116 }
117
118 public Asterisk asAsterisk() {
119 return (Asterisk) this;
120 }
121
122 public Factor asFactor() {
123 return (Factor) this;
124 }
125
126 public Formatter asFormatter() {
127 return (Formatter) this;
128 }
129
130 public Digits asDigits() {
131 return (Digits) this;
132 }
133
134 @Override
135 public String toString() {
136 return getValue();
137 }
138
139
140
141
142
143 public String getValue() {
144 return value;
145 }
146
147
148
149
150
151 public static class Word extends Token {
152
153 public Word(final String value) {
154 super(value);
155 }
156
157
158
159
160
161 public String getWord() {
162 int length = getValue().length();
163 return getValue().substring(1, length-1);
164
165 }
166
167 }
168
169
170
171
172
173 public static class Symbol extends Token {
174
175 public Symbol(final String value) {
176 super(value);
177 }
178 }
179
180
181
182
183
184 public static class EscapedChar extends Token {
185
186 public EscapedChar(final String value) {
187 super(value);
188 }
189
190
191
192
193
194 public String getChar() {
195 return getValue().substring(1);
196 }
197 }
198
199
200
201
202
203 public static class Condition extends Token {
204
205 public Condition(final String value) {
206 super(value);
207 }
208
209
210
211
212
213 public String getCondition() {
214 int length = getValue().length();
215 return getValue().substring(1, length-1);
216 }
217
218 }
219
220
221
222
223 public static class Underscore extends Token {
224
225 public Underscore(final String value) {
226 super(value);
227 }
228
229
230
231
232
233 public String getAttachedValue() {
234 return getValue().substring(1);
235 }
236
237 }
238
239
240
241
242 public static class Asterisk extends Token {
243
244 public Asterisk(final String value) {
245 super(value);
246 }
247
248
249
250
251
252 public String getAttachedValue() {
253 return getValue().substring(1);
254 }
255
256 }
257
258
259
260
261
262 public static class Factor extends Token {
263
264 public Factor(String value) {
265 super(value);
266 }
267
268 }
269
270
271
272
273
274
275 public static class Formatter extends Token {
276
277 public Formatter(String value) {
278 super(value);
279 }
280
281 }
282
283
284
285
286
287
288 public static class Digits extends Token {
289
290 public Digits(String value) {
291 super(value);
292
293 }
294
295
296
297
298
299 public int intValue() {
300 return Integer.valueOf(getValue());
301 }
302
303 }
304 }