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