/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

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