PageRenderTime 619ms CodeModel.GetById 161ms app.highlight 104ms RepoModel.GetById 151ms app.codeStats 5ms

/src/org/ooc/frontend/model/tokens/Token.java

http://github.com/nddrylliog/ooc
Java | 298 lines | 234 code | 64 blank | 0 comment | 7 complexity | c414d32e02d6c5f770e9715804d85292 MD5 | raw file
  1package org.ooc.frontend.model.tokens;
  2
  3import org.ubi.Locatable;
  4import org.ubi.SourceReader;
  5
  6
  7public final class Token implements Locatable {
  8
  9	public static final Token defaultToken = new Token(0, 0, (byte) 0);
 10	
 11	public static final class TokenType {
 12		public static final byte CLASS_KW = 1; // class keyword
 13		public static final byte COVER_KW = 2; // cover keyword
 14		public static final byte FUNC_KW = 3; // func keyword
 15		public static final byte ABSTRACT_KW = 4; // abstract keyword
 16		public static final byte EXTENDS_KW = 5; // from keyword
 17		public static final byte FROM_KW = 6; // over keyword
 18		
 19		public static final byte CONST_KW = 10; // const keyword
 20		public static final byte FINAL_KW = 11; // final keyword
 21		public static final byte STATIC_KW = 12; // static keyword
 22		
 23		public static final byte INCLUDE_KW = 13; // include keyword
 24		public static final byte IMPORT_KW = 14; // import keyword
 25		public static final byte USE_KW = 15; // use keyword
 26		public static final byte EXTERN_KW = 16; // extern keyword
 27		public static final byte INLINE_KW = 17; // extern keyword
 28		public static final byte PROTO_KW = 18; // proto keyword
 29		public static final byte UNMANGLED_KW = 19; // unmangled keyword
 30			
 31		public static final byte BREAK_KW = 20; // break keyword
 32		public static final byte CONTINUE_KW = 21; // continue keyword
 33		public static final byte FALLTHR_KW = 22; // fallthrough keyword
 34		
 35		public static final byte OPERATOR_KW = 23; // operator keyword
 36		
 37		public static final byte IF_KW = 24;
 38		public static final byte ELSE_KW = 25;
 39		public static final byte FOR_KW = 26;
 40		public static final byte WHILE_KW = 27;
 41		public static final byte DO_KW = 28;
 42		public static final byte MATCH_KW = 29;
 43		public static final byte CASE_KW = 30;
 44		
 45		public static final byte AS_KW = 31;
 46		public static final byte IN_KW = 32;
 47		
 48		public static final byte VERSION_KW = 33; // version keyword
 49		
 50		public static final byte RETURN_KW = 34;
 51		
 52		public static final byte TRUE = 35;
 53		public static final byte FALSE = 36;
 54		public static final byte NULL = 37;
 55		
 56		public static final byte OOCDOC = 38; // oodoc comment, e.g. /** blah */
 57		
 58		public static final byte NAME = 39; // mostly a Java identifier
 59
 60		public static final byte BACKSLASH = 40; // \
 61		public static final byte DOUBLE_BACKSLASH = 41; // \\
 62		public static final byte AT = 42; // @
 63		public static final byte HASH = 43; // #
 64		public static final byte TILDE = 44; // ~
 65		public static final byte COMMA = 45; // ;
 66		public static final byte DOT = 46; // .
 67		public static final byte DOUBLE_DOT = 47; // ..
 68		public static final byte TRIPLE_DOT = 48; // ...
 69		public static final byte ARROW = 49; // ->
 70		public static final byte COLON = 50; // :
 71		public static final byte LINESEP = 51; // ; or newline
 72		
 73		public static final byte PLUS = 52; // +
 74		public static final byte PLUS_ASSIGN = 53; // +=
 75		public static final byte MINUS = 54; // -
 76		public static final byte MINUS_ASSIGN = 55; // -=
 77		public static final byte STAR = 56; // *
 78		public static final byte STAR_ASSIGN = 57; // *=
 79		public static final byte SLASH = 58; // /
 80		public static final byte SLASH_ASSIGN = 59; // /=
 81		
 82		public static final byte PERCENT = 60; // %
 83		public static final byte BANG = 61; // !
 84		public static final byte NOT_EQUALS = 62; // !=
 85		public static final byte QUEST = 63; // ?
 86		
 87		public static final byte GREATERTHAN = 64; // >
 88		public static final byte LESSTHAN = 65; // <
 89		public static final byte GREATERTHAN_EQUALS = 66; // >=
 90		public static final byte LESSTHAN_EQUALS = 67; // <=
 91		public static final byte ASSIGN = 68; // =
 92		public static final byte DECL_ASSIGN = 69; // :=
 93		public static final byte EQUALS = 70; // ==
 94		
 95		public static final byte DOUBLE_AMPERSAND = 71; // && (logical and)
 96		public static final byte DOUBLE_PIPE = 72; // || (et non pas double pipe..)
 97		
 98		public static final byte AMPERSAND = 73; // & (binary and)
 99		public static final byte PIPE = 74; // | (binary or)
100		
101		public static final byte CHAR_LIT = 75; // 'c'
102		public static final byte STRING_LIT = 76; // "blah\n"
103		
104		public static final byte DEC_INT = 77; // 234
105		public static final byte HEX_INT = 78; // 0xdeadbeef007
106		public static final byte OCT_INT = 79; // 0c777
107		public static final byte BIN_INT = 80; // 0b1011
108		public static final byte DEC_FLOAT = 81; // 3.14
109		
110		public static final byte OPEN_PAREN = 82; // (
111		public static final byte CLOS_PAREN = 83; // )
112		
113		public static final byte OPEN_BRACK = 84; // {
114		public static final byte CLOS_BRACK = 85; // }
115		
116		public static final byte OPEN_SQUAR = 86; // [
117		public static final byte CLOS_SQUAR = 87; // ]
118		
119		public static final byte UNSIGNED = 88;
120		public static final byte SIGNED = 89;
121		public static final byte LONG = 90;
122		public static final byte STRUCT = 91;
123		public static final byte UNION = 92;
124		
125		public static final byte BINARY_AND = 93; //  &
126		public static final byte CARET = 94; // ^
127		
128		public static final byte DOUBLE_ARROW = 95; // =>
129		public static final byte INTO_KW = 96; // into
130	}
131	
132	public static final class TokenString {
133		public static final String[] strings = new String[] {
134			"<notoken>",
135			"class",
136			"cover",
137			"func",
138			"abstract",
139			"extends",
140			"from",
141			"this",
142			"super",
143			"new",
144			
145			"const",
146			"final",
147			"static",
148			
149			"include",
150			"import",
151			"use",
152			"extern",
153			"inline",
154			"proto",
155			"unmangled",
156			
157			"break",
158			"continue",
159			"fallthrough",
160			
161			"operator",
162			
163			"if",
164			"else",
165			"for",
166			"while",
167			"do",
168			"switch",
169			"case",
170			
171			"as",
172			"in",
173			
174			"version",
175			"return",
176			
177			"true",
178			"false",
179			"null",
180			
181			"oocdoc",
182			
183			"name",
184			
185			"\\",
186			"\\\\",
187			"@",
188			"#",
189			"~",
190			",",
191			".",
192			"..",
193			"...",
194			"->",
195			":",
196			"line separator",
197			
198			"+",
199			"+=",
200			"-",
201			"-=",
202			"*",
203			"*=",
204			"/",
205			"/=",
206			
207			"%",
208			"!",
209			"!=",
210			"?",
211			
212			">",
213			"<",
214			">=",
215			"<=",
216			"=",
217			":=",
218			"==",
219			
220			"&&",
221			"||",
222			
223			"&",
224			"|",
225			
226			"CharLiteral",
227			"StringLiteral",
228			
229			"Decimal",
230			"Hexadecimal",
231			"Octal",
232			"Binary",
233			"DecimalFloat",
234			
235			"(",
236			")",
237			"{",
238			"}",
239			"[",
240			"]",
241			
242			"unsigned",
243			"signed",
244			"long",
245			"struct",
246			"union",
247			
248			" &",
249			"^",
250			"=>",
251
252			"into",
253		};
254		
255	}
256	
257	public final int start;
258	public final int length;
259	public byte type;
260	
261	public Token(int start, int length, byte type) {
262		super();
263		this.start = start;
264		this.length = length;
265		this.type = type;
266	}
267	
268	@Override
269	public String toString() {
270		return "'"+TokenString.strings[type]+"'";
271	}
272	
273	public String get(SourceReader sReader) {
274		return sReader.getSlice(start, length);
275	}
276
277	public int getLength() {
278		return length;
279	}
280
281	public int getStart() {
282		return start;
283	}
284	
285	public Token cloneEnclosing(Token end) {
286		Token token = new Token(start, end.getEnd() - start, type);
287		return token;
288	}
289
290	public int getEnd() {
291		return start + length;
292	}
293
294	public boolean isNameToken() {
295		return type == TokenType.NAME || type == TokenType.CLASS_KW || type == TokenType.IN_KW || type == TokenType.STRUCT;
296	}
297	
298}