Ruby  2.4.2p198(2017-09-14revision59899)
id.def
Go to the documentation of this file.
1 # -*- mode: ruby; coding: us-ascii -*-
2 firstline, predefined = __LINE__+1, %[\
3  max
4  min
5  freeze
6  inspect
7  intern
8  object_id
9  const_missing
10  method_missing MethodMissing
11  method_added
12  singleton_method_added
13  method_removed
14  singleton_method_removed
15  method_undefined
16  singleton_method_undefined
17  length
18  size
19  gets
20  succ
21  each
22  proc
23  lambda
24  send
25  __send__
26  __attached__
27  initialize
28  initialize_copy
29  initialize_clone
30  initialize_dup
31  to_int
32  to_ary
33  to_str
34  to_sym
35  to_hash
36  to_proc
37  to_io
38  to_a
39  to_s
40  to_i
41  bt
42  bt_locations
43  call
44  mesg
45  exception
46 
47  _ UScore
48  "/*NULL*/" NULL
49  empty?
50  eql?
51  respond_to? Respond_to
52  respond_to_missing? Respond_to_missing
53  <IFUNC>
54  <CFUNC>
55  core#set_method_alias
56  core#set_variable_alias
57  core#undef_method
58  core#define_method
59  core#define_singleton_method
60  core#set_postexe
61  core#hash_from_ary
62  core#hash_merge_ary
63  core#hash_merge_ptr
64  core#hash_merge_kwd
65 
66  - debug#created_info
67 
68  $_ LASTLINE
69  $~ BACKREF
70 ]
71 
72 # VM ID OP Parser Token
73 token_ops = %[\
74  Dot2 .. DOT2
75  Dot3 ... DOT3
76  UPlus +@ UPLUS
77  UMinus -@ UMINUS
78  Pow ** POW
79  DSTAR **
80  Cmp <=> CMP
81  PLUS +
82  MINUS -
83  MULT *
84  DIV /
85  MOD %
86  LTLT << LSHFT
87  GTGT >> RSHFT
88  LT <
89  LE <= LEQ
90  GT >
91  GE >= GEQ
92  Eq == EQ
93  Eqq === EQQ
94  Neq != NEQ
95  Not !
96  Backquote `
97  EqTilde =~ MATCH
98  NeqTilde !~ NMATCH
99  AREF []
100  ASET []=
101  COLON2 ::
102  COLON3 ::
103  ANDOP &&
104  OROP ||
105  ANDDOT &.
106 ]
107 
108 class KeywordError < RuntimeError
109  def self.raise(mesg, line)
110  super(self, mesg, ["#{__FILE__}:#{line}", *caller])
111  end
112 end
113 
114 predefined_ids = {}
115 preserved_ids = []
116 local_ids = []
117 instance_ids = []
118 global_ids = []
119 const_ids = []
120 class_ids = []
121 attrset_ids = []
122 token_op_ids = []
123 names = {}
124 predefined.split(/^/).each_with_index do |line, num|
125  next if /^#/ =~ line
126  line.sub!(/\s+#.*/, '')
127  name, token = line.split
128  next unless name
129  token ||= name
130  if /#/ =~ token
131  token = "_#{token.gsub(/\W+/, '_')}"
132  else
133  token = token.sub(/\?/, 'P').sub(/\A[a-z]/) {$&.upcase}
134  token.sub!(/\A\$/, "_G_")
135  token.sub!(/\A@@/, "_C_")
136  token.sub!(/\A@/, "_I_")
137  token.gsub!(/\W+/, "")
138  end
139  if name == '-'
140  preserved_ids << token
141  next
142  end
143  if prev = names[name]
144  KeywordError.raise("#{name} is already registered at line #{prev+firstline}", firstline+num)
145  end
146  if prev = predefined_ids[token]
147  KeywordError.raise("#{token} is already used for #{prev} at line #{names[prev]+firstline}", firstline+num)
148  end
149  names[name] = num
150  case name
151  when /\A[A-Z]\w*\z/; const_ids
152  when /\A(?!\d)\w+\z/; local_ids
153  when /\A\$(?:\d+|(?!\d)\w+|\W)\z/; global_ids
154  when /\A@@(?!\d)\w+\z/; class_ids
155  when /\A@(?!\d)\w+\z/; instance_ids
156  when /\A((?!\d)\w+)=\z/; attrset_ids
157  else preserved_ids
158  end << token
159  predefined_ids[token] = name
160 end
161 token_ops.split(/^/).each do |line|
162  next if /^#/ =~ line
163  line.sub!(/\s+#.*/, '')
164  id, op, token = line.split
165  next unless id and op
166  token ||= (id unless /\A\W\z/ =~ op)
167  token_op_ids << [id, op, token]
168 end
169 {
170  "LOCAL" => local_ids,
171  "INSTANCE" => instance_ids,
172  "GLOBAL" => global_ids,
173  "CONST" => const_ids,
174  "CLASS" => class_ids,
175  "ATTRSET" => attrset_ids,
176  :preserved => preserved_ids,
177  :predefined => predefined_ids,
178  :token_op => token_op_ids,
179 }