2 # irb/ruby-token.rb - ruby tokens
3 # $Release Version: 0.9.5$
5 # by Keiju ISHITSUKA(keiju@ruby-lang.org)
16 EXPR_FNAME = :EXPR_FNAME
18 EXPR_CLASS = :EXPR_CLASS
26 def initialize(seek, line_no, char_no)
31 attr :seek, :line_no, :char_no
35 def initialize(seek, line_no, char_no)
42 def initialize(seek, line_no, char_no, name)
43 super(seek, line_no, char_no)
50 def initialize(seek, line_no, char_no, value = nil)
51 super(seek, line_no, char_no)
62 def initialize(seek, line_no, char_no, op)
63 super(seek, line_no, char_no)
64 op = TkReading2Token[op][0] unless op.kind_of?(Symbol)
70 class TkUnknownChar < Token
71 def initialize(seek, line_no, char_no, id)
72 super(seek, line_no, char_no)
81 def Token(token, value = nil)
84 if (tk = TkReading2Token[token]).nil?
85 IRB.fail TkReading2TokenNoKey, token
87 tk = Token(tk[0], value)
93 if (tk = TkSymbol2Token[token]).nil?
94 IRB.fail TkSymbol2TokenNoKey, token
96 return Token(tk[0], value)
98 if (token.ancestors & [TkId, TkVal, TkOPASGN, TkUnknownChar]).empty?
99 token.new(@prev_seek, @prev_line_no, @prev_char_no)
101 token.new(@prev_seek, @prev_line_no, @prev_char_no, value)
107 [:TkCLASS, TkId, "class", EXPR_CLASS],
108 [:TkMODULE, TkId, "module", EXPR_BEG],
109 [:TkDEF, TkId, "def", EXPR_FNAME],
110 [:TkUNDEF, TkId, "undef", EXPR_FNAME],
111 [:TkBEGIN, TkId, "begin", EXPR_BEG],
112 [:TkRESCUE, TkId, "rescue", EXPR_MID],
113 [:TkENSURE, TkId, "ensure", EXPR_BEG],
114 [:TkEND, TkId, "end", EXPR_END],
115 [:TkIF, TkId, "if", EXPR_BEG, :TkIF_MOD],
116 [:TkUNLESS, TkId, "unless", EXPR_BEG, :TkUNLESS_MOD],
117 [:TkTHEN, TkId, "then", EXPR_BEG],
118 [:TkELSIF, TkId, "elsif", EXPR_BEG],
119 [:TkELSE, TkId, "else", EXPR_BEG],
120 [:TkCASE, TkId, "case", EXPR_BEG],
121 [:TkWHEN, TkId, "when", EXPR_BEG],
122 [:TkWHILE, TkId, "while", EXPR_BEG, :TkWHILE_MOD],
123 [:TkUNTIL, TkId, "until", EXPR_BEG, :TkUNTIL_MOD],
124 [:TkFOR, TkId, "for", EXPR_BEG],
125 [:TkBREAK, TkId, "break", EXPR_END],
126 [:TkNEXT, TkId, "next", EXPR_END],
127 [:TkREDO, TkId, "redo", EXPR_END],
128 [:TkRETRY, TkId, "retry", EXPR_END],
129 [:TkIN, TkId, "in", EXPR_BEG],
130 [:TkDO, TkId, "do", EXPR_BEG],
131 [:TkRETURN, TkId, "return", EXPR_MID],
132 [:TkYIELD, TkId, "yield", EXPR_END],
133 [:TkSUPER, TkId, "super", EXPR_END],
134 [:TkSELF, TkId, "self", EXPR_END],
135 [:TkNIL, TkId, "nil", EXPR_END],
136 [:TkTRUE, TkId, "true", EXPR_END],
137 [:TkFALSE, TkId, "false", EXPR_END],
138 [:TkAND, TkId, "and", EXPR_BEG],
139 [:TkOR, TkId, "or", EXPR_BEG],
140 [:TkNOT, TkId, "not", EXPR_BEG],
142 [:TkUNLESS_MOD, TkId],
143 [:TkWHILE_MOD, TkId],
144 [:TkUNTIL_MOD, TkId],
145 [:TkALIAS, TkId, "alias", EXPR_FNAME],
146 [:TkDEFINED, TkId, "defined?", EXPR_END],
147 [:TklBEGIN, TkId, "BEGIN", EXPR_END],
148 [:TklEND, TkId, "END", EXPR_END],
149 [:Tk__LINE__, TkId, "__LINE__", EXPR_END],
150 [:Tk__FILE__, TkId, "__FILE__", EXPR_END],
152 [:TkIDENTIFIER, TkId],
166 [:TkDSTRING, TkNode],
167 [:TkDXSTRING, TkNode],
168 [:TkDREGEXP, TkNode],
169 [:TkNTH_REF, TkNode],
170 [:TkBACK_REF, TkNode],
172 [:TkUPLUS, TkOp, "+@"],
173 [:TkUMINUS, TkOp, "-@"],
174 [:TkPOW, TkOp, "**"],
175 [:TkCMP, TkOp, "<=>"],
177 [:TkEQQ, TkOp, "==="],
178 [:TkNEQ, TkOp, "!="],
179 [:TkGEQ, TkOp, ">="],
180 [:TkLEQ, TkOp, "<="],
181 [:TkANDOP, TkOp, "&&"],
182 [:TkOROP, TkOp, "||"],
183 [:TkMATCH, TkOp, "=~"],
184 [:TkNMATCH, TkOp, "!~"],
185 [:TkDOT2, TkOp, ".."],
186 [:TkDOT3, TkOp, "..."],
187 [:TkAREF, TkOp, "[]"],
188 [:TkASET, TkOp, "[]="],
189 [:TkLSHFT, TkOp, "<<"],
190 [:TkRSHFT, TkOp, ">>"],
193 # [:OPASGN, TkOp], # +=, -= etc. #
194 [:TkASSOC, TkOp, "=>"],
195 [:TkQUESTION, TkOp, "?"], #?
196 [:TkCOLON, TkOp, ":"], #:
198 [:TkfLPAREN], # func( #
199 [:TkfLBRACK], # func[ #
200 [:TkfLBRACE], # func{ #
203 [:TkSYMBEG], # :SYMBOL
207 [:TkPLUS, TkOp, "+"],
208 [:TkMINUS, TkOp, "-"],
209 [:TkMULT, TkOp, "*"],
212 [:TkBITOR, TkOp, "|"],
213 [:TkBITXOR, TkOp, "^"],
214 [:TkBITAND, TkOp, "&"],
215 [:TkBITNOT, TkOp, "~"],
216 [:TkNOTOP, TkOp, "!"],
218 [:TkBACKQUOTE, TkOp, "`"],
220 [:TkASSIGN, Token, "="],
221 [:TkDOT, Token, "."],
222 [:TkLPAREN, Token, "("], #(exp)
223 [:TkLBRACK, Token, "["], #[arry]
224 [:TkLBRACE, Token, "{"], #{hash}
225 [:TkRPAREN, Token, ")"],
226 [:TkRBRACK, Token, "]"],
227 [:TkRBRACE, Token, "}"],
228 [:TkCOMMA, Token, ","],
229 [:TkSEMICOLON, Token, ";"],
237 [:TkBACKSLASH, TkUnknownChar, "\\"],
238 [:TkAT, TkUnknownChar, "@"],
239 [:TkDOLLAR, TkUnknownChar, "$"],
242 # {reading => token_class}
243 # {reading => [token_class, *opt]}
247 def RubyToken.def_token(token_n, super_token = Token, reading = nil, *opts)
248 token_n = token_n.id2name if token_n.kind_of?(Symbol)
249 if RubyToken.const_defined?(token_n)
250 IRB.fail AlreadyDefinedToken, token_n
252 token_c = eval("class #{token_n} < #{super_token}; end; #{token_n}")
255 if TkReading2Token[reading]
256 IRB.fail TkReading2TokenDuplicateError, token_n, reading
259 TkReading2Token[reading] = [token_c]
261 TkReading2Token[reading] = [token_c].concat(opts)
264 TkSymbol2Token[token_n.intern] = token_c
267 for defs in TokenDefinitions