2 # irb/ruby-token.rb - ruby tokens
3 # $Release Version: 0.9.5$
5 # $Date: 2007-02-12 15:01:19 -0800 (Mon, 12 Feb 2007) $
6 # by Keiju ISHITSUKA(keiju@ruby-lang.org)
17 EXPR_FNAME = :EXPR_FNAME
19 EXPR_CLASS = :EXPR_CLASS
27 def initialize(seek, line_no, char_no)
38 def initialize(seek, line_no, char_no)
45 def initialize(seek, line_no, char_no, name)
46 super(seek, line_no, char_no)
53 def initialize(seek, line_no, char_no, value = nil)
54 super(seek, line_no, char_no)
65 def initialize(seek, line_no, char_no, op)
66 super(seek, line_no, char_no)
67 op = TkReading2Token[op][0] unless op.kind_of?(Symbol)
73 class TkUnknownChar < Token
74 def initialize(seek, line_no, char_no, id)
75 super(seek, line_no, char_no)
84 def Token(token, value = nil)
87 if (tk = TkReading2Token[token]).nil?
88 IRB.fail TkReading2TokenNoKey, token
90 tk = Token(tk[0], value)
96 if (tk = TkSymbol2Token[token]).nil?
97 IRB.fail TkSymbol2TokenNoKey, token
99 return Token(tk[0], value)
101 if (token.ancestors & [TkId, TkVal, TkOPASGN, TkUnknownChar]).empty?
102 token.new(@prev_seek, @prev_line_no, @prev_char_no)
104 token.new(@prev_seek, @prev_line_no, @prev_char_no, value)
110 [:TkCLASS, TkId, "class", EXPR_CLASS],
111 [:TkMODULE, TkId, "module", EXPR_BEG],
112 [:TkDEF, TkId, "def", EXPR_FNAME],
113 [:TkUNDEF, TkId, "undef", EXPR_FNAME],
114 [:TkBEGIN, TkId, "begin", EXPR_BEG],
115 [:TkRESCUE, TkId, "rescue", EXPR_MID],
116 [:TkENSURE, TkId, "ensure", EXPR_BEG],
117 [:TkEND, TkId, "end", EXPR_END],
118 [:TkIF, TkId, "if", EXPR_BEG, :TkIF_MOD],
119 [:TkUNLESS, TkId, "unless", EXPR_BEG, :TkUNLESS_MOD],
120 [:TkTHEN, TkId, "then", EXPR_BEG],
121 [:TkELSIF, TkId, "elsif", EXPR_BEG],
122 [:TkELSE, TkId, "else", EXPR_BEG],
123 [:TkCASE, TkId, "case", EXPR_BEG],
124 [:TkWHEN, TkId, "when", EXPR_BEG],
125 [:TkWHILE, TkId, "while", EXPR_BEG, :TkWHILE_MOD],
126 [:TkUNTIL, TkId, "until", EXPR_BEG, :TkUNTIL_MOD],
127 [:TkFOR, TkId, "for", EXPR_BEG],
128 [:TkBREAK, TkId, "break", EXPR_END],
129 [:TkNEXT, TkId, "next", EXPR_END],
130 [:TkREDO, TkId, "redo", EXPR_END],
131 [:TkRETRY, TkId, "retry", EXPR_END],
132 [:TkIN, TkId, "in", EXPR_BEG],
133 [:TkDO, TkId, "do", EXPR_BEG],
134 [:TkRETURN, TkId, "return", EXPR_MID],
135 [:TkYIELD, TkId, "yield", EXPR_END],
136 [:TkSUPER, TkId, "super", EXPR_END],
137 [:TkSELF, TkId, "self", EXPR_END],
138 [:TkNIL, TkId, "nil", EXPR_END],
139 [:TkTRUE, TkId, "true", EXPR_END],
140 [:TkFALSE, TkId, "false", EXPR_END],
141 [:TkAND, TkId, "and", EXPR_BEG],
142 [:TkOR, TkId, "or", EXPR_BEG],
143 [:TkNOT, TkId, "not", EXPR_BEG],
145 [:TkUNLESS_MOD, TkId],
146 [:TkWHILE_MOD, TkId],
147 [:TkUNTIL_MOD, TkId],
148 [:TkALIAS, TkId, "alias", EXPR_FNAME],
149 [:TkDEFINED, TkId, "defined?", EXPR_END],
150 [:TklBEGIN, TkId, "BEGIN", EXPR_END],
151 [:TklEND, TkId, "END", EXPR_END],
152 [:Tk__LINE__, TkId, "__LINE__", EXPR_END],
153 [:Tk__FILE__, TkId, "__FILE__", EXPR_END],
155 [:TkIDENTIFIER, TkId],
169 [:TkDSTRING, TkNode],
170 [:TkDXSTRING, TkNode],
171 [:TkDREGEXP, TkNode],
172 [:TkNTH_REF, TkNode],
173 [:TkBACK_REF, TkNode],
175 [:TkUPLUS, TkOp, "+@"],
176 [:TkUMINUS, TkOp, "-@"],
177 [:TkPOW, TkOp, "**"],
178 [:TkCMP, TkOp, "<=>"],
180 [:TkEQQ, TkOp, "==="],
181 [:TkNEQ, TkOp, "!="],
182 [:TkGEQ, TkOp, ">="],
183 [:TkLEQ, TkOp, "<="],
184 [:TkANDOP, TkOp, "&&"],
185 [:TkOROP, TkOp, "||"],
186 [:TkMATCH, TkOp, "=~"],
187 [:TkNMATCH, TkOp, "!~"],
188 [:TkDOT2, TkOp, ".."],
189 [:TkDOT3, TkOp, "..."],
190 [:TkAREF, TkOp, "[]"],
191 [:TkASET, TkOp, "[]="],
192 [:TkLSHFT, TkOp, "<<"],
193 [:TkRSHFT, TkOp, ">>"],
196 # [:OPASGN, TkOp], # +=, -= etc. #
197 [:TkASSOC, TkOp, "=>"],
198 [:TkQUESTION, TkOp, "?"], #?
199 [:TkCOLON, TkOp, ":"], #:
201 [:TkfLPAREN], # func( #
202 [:TkfLBRACK], # func[ #
203 [:TkfLBRACE], # func{ #
206 [:TkSYMBEG], # :SYMBOL
210 [:TkPLUS, TkOp, "+"],
211 [:TkMINUS, TkOp, "-"],
212 [:TkMULT, TkOp, "*"],
215 [:TkBITOR, TkOp, "|"],
216 [:TkBITXOR, TkOp, "^"],
217 [:TkBITAND, TkOp, "&"],
218 [:TkBITNOT, TkOp, "~"],
219 [:TkNOTOP, TkOp, "!"],
221 [:TkBACKQUOTE, TkOp, "`"],
223 [:TkASSIGN, Token, "="],
224 [:TkDOT, Token, "."],
225 [:TkLPAREN, Token, "("], #(exp)
226 [:TkLBRACK, Token, "["], #[arry]
227 [:TkLBRACE, Token, "{"], #{hash}
228 [:TkRPAREN, Token, ")"],
229 [:TkRBRACK, Token, "]"],
230 [:TkRBRACE, Token, "}"],
231 [:TkCOMMA, Token, ","],
232 [:TkSEMICOLON, Token, ";"],
240 [:TkBACKSLASH, TkUnknownChar, "\\"],
241 [:TkAT, TkUnknownChar, "@"],
242 [:TkDOLLAR, TkUnknownChar, "$"],
245 # {reading => token_class}
246 # {reading => [token_class, *opt]}
250 def RubyToken.def_token(token_n, super_token = Token, reading = nil, *opts)
251 token_n = token_n.id2name if token_n.kind_of?(Symbol)
252 if RubyToken.const_defined?(token_n)
253 IRB.fail AlreadyDefinedToken, token_n
255 token_c = eval("class #{token_n} < #{super_token}; end; #{token_n}")
258 if TkReading2Token[reading]
259 IRB.fail TkReading2TokenDuplicateError, token_n, reading
262 TkReading2Token[reading] = [token_c]
264 TkReading2Token[reading] = [token_c].concat(opts)
267 TkSymbol2Token[token_n.intern] = token_c
270 for defs in TokenDefinitions