Ruby: defs/id.def Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

id.def
1# -*- mode: ruby; coding: us-ascii -*-
2firstline, predefined = __LINE__+1, %[\
3 max
4 min
5 freeze
6 nil?
7 inspect
8 intern
9 object_id
10 const_added
11 const_missing
12 method_missing MethodMissing
13 method_added
14 singleton_method_added
15 method_removed
16 singleton_method_removed
17 method_undefined
18 singleton_method_undefined
19 length
20 size
21 gets
22 succ
23 each
24 proc
25 lambda
26 send
27 __send__
28 __attached__
29 __recursive_key__
30 initialize
31 initialize_copy
32 initialize_clone
33 initialize_dup
34 to_int
35 to_ary
36 to_str
37 to_sym
38 to_hash
39 to_proc
40 to_io
41 to_a
42 to_s
43 to_i
44 to_f
45 to_r
46 bt
47 bt_locations
48 call
49 mesg
50 exception
51 locals
52 not NOT
53 and AND
54 or OR
55 div
56 divmod
57 fdiv
58 quo
59 name
60 nil
61 path
62
63 _ UScore
64
65 # MUST be successive
66 _1 NUMPARAM_1
67 _2 NUMPARAM_2
68 _3 NUMPARAM_3
69 _4 NUMPARAM_4
70 _5 NUMPARAM_5
71 _6 NUMPARAM_6
72 _7 NUMPARAM_7
73 _8 NUMPARAM_8
74 _9 NUMPARAM_9
75
76 "/*NULL*/" NULL
77 empty?
78 eql?
79 default
80 respond_to? Respond_to
81 respond_to_missing? Respond_to_missing
82 <IFUNC>
83 <CFUNC>
84 core#set_method_alias
85 core#set_variable_alias
86 core#undef_method
87 core#define_method
88 core#define_singleton_method
89 core#set_postexe
90 core#hash_merge_ptr
91 core#hash_merge_kwd
92 core#raise
93 core#sprintf
94
95 - debug#created_info
96
97 $_ LASTLINE
98 $~ BACKREF
99 $! ERROR_INFO
100]
101
102# VM ID OP Parser Token
103token_ops = %[\
104 Dot2 .. DOT2
105 Dot3 ... DOT3
106 BDot2 .. BDOT2
107 BDot3 ... BDOT3
108 UPlus +@ UPLUS
109 UMinus -@ UMINUS
110 Pow ** POW
111 Cmp <=> CMP
112 PLUS +
113 MINUS -
114 MULT *
115 DIV /
116 MOD %
117 LTLT << LSHFT
118 GTGT >> RSHFT
119 LT <
120 LE <= LEQ
121 GT >
122 GE >= GEQ
123 Eq == EQ
124 Eqq === EQQ
125 Neq != NEQ
126 Not !
127 And &
128 Or |
129 Backquote `
130 EqTilde =~ MATCH
131 NeqTilde !~ NMATCH
132 AREF []
133 ASET []=
134 COLON2 ::
135 ANDOP &&
136 OROP ||
137 ANDDOT &.
138]
139
140class KeywordError < RuntimeError
141 def self.raise(mesg, line)
142 super(self, mesg, ["#{__FILE__}:#{line}", *caller])
143 end
144end
145
146def id2varname(token, prefix = nil)
147 if /#/ =~ token
148 token = "_#{token.gsub(/\W+/, '_')}"
149 else
150 token = token.sub(/\?/, 'P')
151 token = prefix + token if prefix
152 token.sub!(/\A[a-z]/) {$&.upcase}
153 token.sub!(/\A\$/, "_G_")
154 token.sub!(/\A@@/, "_C_")
155 token.sub!(/\A@/, "_I_")
156 token.gsub!(/\W+/, "")
157 end
158 token
159end
160
161predefined_ids = {}
162preserved_ids = []
163local_ids = []
164instance_ids = []
165global_ids = []
166const_ids = []
167class_ids = []
168attrset_ids = []
169token_op_ids = []
170names = {}
171predefined.split(/^/).each_with_index do |line, num|
172 next if /^#/ =~ line
173 line.sub!(/\s+#.*/, '')
174 name, token = line.split
175 next unless name
176 token = id2varname(token || name)
177 if name == '-'
178 preserved_ids << token
179 next
180 end
181 if prev = names[name]
182 KeywordError.raise("#{name} is already registered at line #{prev+firstline}", firstline+num)
183 end
184 if prev = predefined_ids[token]
185 KeywordError.raise("#{token} is already used for #{prev} at line #{names[prev]+firstline}", firstline+num)
186 end
187 names[name] = num
188 case name
189 when /\A[A-Z]\w*\z/; const_ids
190 when /\A(?!\d)\w+\z/; local_ids
191 when /\A\$(?:\d+|(?!\d)\w+|\W)\z/; global_ids
192 when /\A@@(?!\d)\w+\z/; class_ids
193 when /\A@(?!\d)\w+\z/; instance_ids
194 when /\A((?!\d)\w+)=\z/; attrset_ids
195 else preserved_ids
196 end << token
197 predefined_ids[token] = name
198end
199index = 127
200token_ops.split(/^/).each do |line|
201 next if /^#/ =~ line
202 line.sub!(/\s+#.*/, '')
203 id, op, token = line.split
204 next unless id and op
205 token ||= (id unless /\A\W\z/ =~ op)
206 token_op_ids << [id, op, token, (index += 1 if token)]
207end
208{
209 "LOCAL" => local_ids,
210 "INSTANCE" => instance_ids,
211 "GLOBAL" => global_ids,
212 "CONST" => const_ids,
213 "CLASS" => class_ids,
214 "ATTRSET" => attrset_ids,
215 :preserved => preserved_ids,
216 :predefined => predefined_ids,
217 :token_op => token_op_ids,
218 :last_token => index,
219}