Ruby  2.0.0p195(2013-05-14revision40734)
id.def
Go to the documentation of this file.
1 # -*- mode: ruby; coding: us-ascii -*-
2 firstline, predefined = __LINE__+1, %[\
3  intern
4  method_missing MethodMissing
5  length
6  size
7  gets
8  succ
9  each
10  lambda
11  send
12  __send__
14  initialize_copy
15  initialize_clone
16  initialize_dup
17  _ UScore
18  "/*NULL*/" NULL
19  empty?
20  respond_to? Respond_to
21  respond_to_missing? Respond_to_missing
22  <IFUNC>
23  <CFUNC>
24  core#set_method_alias
25  core#set_variable_alias
26  core#undef_method
27  core#define_method
28  core#define_singleton_method
29  core#set_postexe
30  core#hash_from_ary
31  core#hash_merge_ary
32  core#hash_merge_ptr
33  core#hash_merge_kwd
34 ]
35 
36 class KeywordError < RuntimeError
37  def self.raise(mesg, line)
38  super(self, mesg, ["#{__FILE__}:#{line}", *caller])
39  end
40 end
41 
42 predefined_ids = {}
43 preserved_ids = []
44 local_ids = []
45 instance_ids = []
46 global_ids = []
47 const_ids = []
48 class_ids = []
49 names = {}
50 predefined.split(/^/).each_with_index do |line, num|
51  next if /^#/ =~ line or (name, token = line.split; !name)
52  token ||= name
53  if /#/ =~ token
54  token = "_#{token.gsub(/\W+/, '_')}"
55  else
56  token = token.sub(/\?/, 'P').sub(/\A[a-z]/) {$&.upcase}
57  token.sub!(/\A\$/, "_G_")
58  token.sub!(/\A@@/, "_C_")
59  token.sub!(/\A@/, "_I_")
60  token.gsub!(/\W+/, "")
61  end
62  if prev = names[name]
63  KeywordError.raise("#{name} is already registered at line #{prev+firstline}", firstline+num)
64  end
65  if prev = predefined_ids[token]
66  KeywordError.raise("#{token} is already used for #{prev} at line #{names[prev]+firstline}", firstline+num)
67  end
68  names[name] = num
69  case name
70  when /\A[A-Z]\w*\z/; const_ids
71  when /\A(?!\d)\w+\z/; local_ids
72  when /\A\$(?:\d+|(?!\d)\w+)\z/; global_ids
73  when /\A@@(?!\d)\w+\z/; class_ids
74  when /\A@(?!\d)\w+\z/; instance_ids
75  when /\A((?!\d)\w+)=\z/
76  KeywordError.raise("use ID2ATTRSET(#{$1}) instead of ATTRSET #{name}", firstline+num)
77  else preserved_ids
78  end << token
79  predefined_ids[token] = name
80 end
81 {
82  "LOCAL" => local_ids,
83  "INSTANCE" => instance_ids,
84  "GLOBAL" => global_ids,
85  "CONST" => const_ids,
86  "CLASS" => class_ids,
87  :preserved => preserved_ids,
88  :predefined => predefined_ids,
89 }
90