lib/irb/ruby-token.rb
DEFINITIONS
This source file includes following functions.
1 #
2 # irb/ruby-token.rb - ruby tokens
3 # $Release Version: 0.9$
4 # $Revision: 1.4 $
5 # $Date: 2002/07/09 11:17:16 $
6 # by Keiju ISHITSUKA(keiju@ishitsuka.com)
7 #
8 # --
9 #
10 #
11 #
12 module RubyToken
13 EXPR_BEG = :EXPR_BEG
14 EXPR_MID = :EXPR_MID
15 EXPR_END = :EXPR_END
16 EXPR_ARG = :EXPR_ARG
17 EXPR_FNAME = :EXPR_FNAME
18 EXPR_DOT = :EXPR_DOT
19 EXPR_CLASS = :EXPR_CLASS
20
21 # for ruby 1.4X
22 if !defined?(Symbol)
23 Symbol = Integer
24 end
25
26 class Token
27 def initialize(seek, line_no, char_no)
28 @seek = seek
29 @line_no = line_no
30 @char_no = char_no
31 end
32 attr :seek
33 attr :line_no
34 attr :char_no
35 end
36
37 class TkNode < Token
38 def initialize(seek, line_no, char_no)
39 super
40 end
41 attr :node
42 end
43
44 class TkId < Token
45 def initialize(seek, line_no, char_no, name)
46 super(seek, line_no, char_no)
47 @name = name
48 end
49 attr :name
50 end
51
52 class TkVal < Token
53 def initialize(seek, line_no, char_no, value = nil)
54 super(seek, line_no, char_no)
55 @value = value
56 end
57 attr :value
58 end
59
60 class TkOp < Token
61 attr :name, true
62 end
63
64 class TkOPASGN < TkOp
65 def initialize(seek, line_no, char_no, op)
66 super(seek, line_no, char_no)
67 op = TkReading2Token[op] unless op.kind_of?(Symbol)
68 @op = op
69 end
70 attr :op
71 end
72
73 class TkUnknownChar < Token
74 def initialize(seek, line_no, char_no, id)
75 super(seek, line_no, char_no)
76 @name = name
77 end
78 attr :name
79 end
80
81 class TkError < Token
82 end
83
84 def Token(token, value = nil)
85 case token
86 when String
87 if (tk = TkReading2Token[token]).nil?
88 IRB.fail TkReading2TokenNoKey, token
89 end
90 tk = Token(tk[0], value)
91 if tk.kind_of?(TkOp)
92 tk.name = token
93 end
94 return tk
95 when Symbol
96 if (tk = TkSymbol2Token[token]).nil?
97 IRB.fail TkSymbol2TokenNoKey, token
98 end
99 return Token(tk[0], value)
100 else
101 if (token.ancestors & [TkId, TkVal, TkOPASGN, TkUnknownChar]).empty?
102 token.new(@prev_seek, @prev_line_no, @prev_char_no)
103 else
104 token.new(@prev_seek, @prev_line_no, @prev_char_no, value)
105 end
106 end
107 end
108
109 TokenDefinitions = [
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],
144 [:TkIF_MOD, TkId],
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],
154
155 [:TkIDENTIFIER, TkId],
156 [:TkFID, TkId],
157 [:TkGVAR, TkId],
158 [:TkCVAR, TkId],
159 [:TkIVAR, TkId],
160 [:TkCONSTANT, TkId],
161
162 [:TkINTEGER, TkVal],
163 [:TkFLOAT, TkVal],
164 [:TkSTRING, TkVal],
165 [:TkXSTRING, TkVal],
166 [:TkREGEXP, TkVal],
167
168 [:TkDSTRING, TkNode],
169 [:TkDXSTRING, TkNode],
170 [:TkDREGEXP, TkNode],
171 [:TkNTH_REF, TkNode],
172 [:TkBACK_REF, TkNode],
173
174 [:TkUPLUS, TkOp, "+@"],
175 [:TkUMINUS, TkOp, "-@"],
176 [:TkPOW, TkOp, "**"],
177 [:TkCMP, TkOp, "<=>"],
178 [:TkEQ, TkOp, "=="],
179 [:TkEQQ, TkOp, "==="],
180 [:TkNEQ, TkOp, "!="],
181 [:TkGEQ, TkOp, ">="],
182 [:TkLEQ, TkOp, "<="],
183 [:TkANDOP, TkOp, "&&"],
184 [:TkOROP, TkOp, "||"],
185 [:TkMATCH, TkOp, "=~"],
186 [:TkNMATCH, TkOp, "!~"],
187 [:TkDOT2, TkOp, ".."],
188 [:TkDOT3, TkOp, "..."],
189 [:TkAREF, TkOp, "[]"],
190 [:TkASET, TkOp, "[]="],
191 [:TkLSHFT, TkOp, "<<"],
192 [:TkRSHFT, TkOp, ">>"],
193 [:TkCOLON2, TkOp],
194 [:TkCOLON3, TkOp],
195 # [:OPASGN, TkOp], # +=, -= etc. #
196 [:TkASSOC, TkOp, "=>"],
197 [:TkQUESTION, TkOp, "?"], #?
198 [:TkCOLON, TkOp, ":"], #:
199
200 [:TkfLPAREN], # func( #
201 [:TkfLBRACK], # func[ #
202 [:TkfLBRACE], # func{ #
203 [:TkSTAR], # *arg
204 [:TkAMPER], # &arg #
205 [:TkSYMBEG], # :SYMBOL
206
207 [:TkGT, TkOp, ">"],
208 [:TkLT, TkOp, "<"],
209 [:TkPLUS, TkOp, "+"],
210 [:TkMINUS, TkOp, "-"],
211 [:TkMULT, TkOp, "*"],
212 [:TkDIV, TkOp, "/"],
213 [:TkMOD, TkOp, "%"],
214 [:TkBITOR, TkOp, "|"],
215 [:TkBITXOR, TkOp, "^"],
216 [:TkBITAND, TkOp, "&"],
217 [:TkBITNOT, TkOp, "~"],
218 [:TkNOTOP, TkOp, "!"],
219
220 [:TkBACKQUOTE, TkOp, "`"],
221
222 [:TkASSIGN, Token, "="],
223 [:TkDOT, Token, "."],
224 [:TkLPAREN, Token, "("], #(exp)
225 [:TkLBRACK, Token, "["], #[arry]
226 [:TkLBRACE, Token, "{"], #{hash}
227 [:TkRPAREN, Token, ")"],
228 [:TkRBRACK, Token, "]"],
229 [:TkRBRACE, Token, "}"],
230 [:TkCOMMA, Token, ","],
231 [:TkSEMICOLON, Token, ";"],
232
233 [:TkCOMMENT],
234 [:TkRD_COMMENT],
235 [:TkSPACE],
236 [:TkNL],
237 [:TkEND_OF_SCRIPT],
238
239 [:TkBACKSLASH, TkUnknownChar, "\\"],
240 [:TkAT, TkUnknownChar, "@"],
241 [:TkDOLLAR, TkUnknownChar, "$"],
242 ]
243
244 # {reading => token_class}
245 # {reading => [token_class, *opt]}
246 TkReading2Token = {}
247 TkSymbol2Token = {}
248
249 def RubyToken.def_token(token_n, super_token = Token, reading = nil, *opts)
250 token_n = token_n.id2name if token_n.kind_of?(Symbol)
251 if RubyToken.const_defined?(token_n)
252 IRB.fail AlreadyDefinedToken, token_n
253 end
254 token_c = eval("class #{token_n} < #{super_token}; end; #{token_n}")
255
256 if reading
257 if TkReading2Token[reading]
258 IRB.fail TkReading2TokenDuplicateError, token_n, reading
259 end
260 if opts.empty?
261 TkReading2Token[reading] = [token_c]
262 else
263 TkReading2Token[reading] = [token_c].concat(opts)
264 end
265 end
266 TkSymbol2Token[token_n.intern] = token_c
267 end
268
269 for defs in TokenDefinitions
270 def_token(*defs)
271 end
272 end