1 # $Id: module.rb,v 1.17 2012/04/17 02:49:40 machan Exp $
2
3 require 'tmdoc/tmstd'
4 require 'tmdoc/constant'
5 require 'tmdoc/model/module'
6 require 'tmdoc/model/object'
7 require 'tmdoc/transformer/module-into-object/path'
8
9
10 module TmDoc
11
12 module Transformer::ModuleIntoObject
13
14 module Module
15
16 module_function
17
18 def transform_root_module(mm_root_module, mo_files)
19 ASSERT.kind_of mm_root_module, MMLA::GenericModule
20 ASSERT.kind_of mo_files, MOP::Files
21
22 mo_properties = MOLA::SetOfProperty.new(
23 transform_constants(
24 mm_root_module.constants, mo_files, mm_root_module
25 ).to_a + transform_properties(
26 mm_root_module.properties, mo_files, mm_root_module,
27 mm_root_module.map_of_property_to_uniq_num
28 ).to_a
29 )
30
31 args = [
32 Path.transform(mm_root_module.above_path),
33
34 mm_root_module.name,
35
36 mo_properties,
37
38 transform_modules_into_paths(mm_root_module.below_modules),
39
40 Path.transform_paths(mm_root_module.extendee_module_paths),
41
42 Path.transform_paths(mm_root_module.includee_module_paths)
43 ]
44
45 mo_module =
46 case mm_root_module
47 when MMLA::RootModule
48 case mm_root_module
49 when MMLN::ToplevelModule
50 MOLN::ToplevelModule.new(*args)
51 when MMLN::UnknownModule
52 MOLN::UnknownModule.new(*args)
53 when MMLN::BuiltinModule
54 MOLN::BuiltinModule.new(*args)
55 else
56 ASSERT.abort(
57 "Unknown root module: %s", mm_root_module.to_s
58 )
59 end
60 when MMLN::Object
61 above_module_path = Path.transform(
62 mm_root_module.an_above_module.path
63 )
64
65 MOLN::Object.new(*(args << above_module_path))
66 else
67 ASSERT.abort "Unknown root module: %s", mm_root_module.to_s
68 end
69
70 ASSERT.kind_of mo_module, MOLA::GenericModule
71 end
72
73
74 def transform_module(mo_store, mm_module)
75 ASSERT.kind_of mo_store, MO::Store
76 ASSERT.kind_of mm_module, MMLA::ChildModule
77
78 mo_files = mo_store.files
79 mo_properties = MOLA::SetOfProperty.new(
80 transform_constants(
81 mm_module.constants, mo_files, mm_module
82 ).to_a + transform_properties(
83 mm_module.properties, mo_files, mm_module,
84 mm_module.map_of_property_to_uniq_num
85 ).to_a
86 )
87
88 args = [
89 transform_locations(mm_module.locations, mo_files),
90
91 Path.transform(mm_module.above_path),
92
93 mm_module.name,
94
95 mo_store,
96
97 mo_properties,
98
99 transform_modules_into_paths(mm_module.below_modules),
100
101 Path.transform_paths(mm_module.extendee_module_paths),
102
103 Path.transform_paths(mm_module.includee_module_paths),
104
105 Path.transform(mm_module.an_above_module.path)
106 ]
107
108 mo_module =
109 case mm_module
110 when MMLN::Class
111 inherit_path = Path.transform mm_module.inherit_path
112
113 if mm_module.kind_of?(MMLN::UnknownClass)
114 MOLN::UnknownClass.new *(args << inherit_path)
115 else
116 MOLN::Class.new *(args << inherit_path)
117 end
118 when MMLN::Module
119 MOLN::Module.new *args
120 else
121 ASSERT.abort(
122 "Unknwon mm_module: %s(%s)",
123 mm_module.to_s, mm_module.class.to_s
124 )
125 end
126
127 ASSERT.kind_of mo_module, MOLA::ChildModule
128 end
129
130
131 def transform_locations(mm_locations, mo_files)
132 ASSERT.kind_of mm_locations, MML::SetOfLocation
133 ASSERT.kind_of mo_files, MOP::Files
134
135 MOL::SetOfLocation.new(
136 mm_locations.map { |mm_location|
137 ASSERT.kind_of mm_location, MML::Location
138
139 transform_location(mm_location, mo_files)
140 }.compact
141 )
142 end
143
144
145 def transform_location(mm_location, mo_files)
146 ASSERT.opt_kind_of mm_location, MML::Location
147 ASSERT.kind_of mo_files, MOP::Files
148
149 return nil unless mm_location
150
151 mo_file = mo_files.at mm_location.file_name
152 ASSERT.kind_of mo_file, MOP::File
153
154 MOL::Location.new mo_file, mm_location.line_num
155 end
156
157
158 def transform_constants(
159 mm_constants, mo_files, mm_module
160 )
161 ASSERT.kind_of mm_constants, MMLL::SetOfConstant
162 ASSERT.kind_of mo_files, MOP::Files
163 ASSERT.kind_of mm_module, MMLA::GenericModule
164
165 mo_above_path =
166 Path.transform(mm_module.above_path << mm_module.name)
167
168 MOLL::SetOfConstant.new(
169 mm_constants.map { |mm_constant|
170 MOLL::Constant.new(
171 transform_location(mm_constant.location, mo_files),
172 mo_above_path,
173 mm_constant.name,
174 mm_constant.value
175 )
176 }
177 )
178 end
179
180
181 def transform_properties(
182 mm_properties, mo_files, mm_module,
183 mm_map_of_property_to_uniq_num
184 )
185 ASSERT.kind_of mm_properties, MMLA::SetOfProperty
186 ASSERT.kind_of mo_files, MOP::Files
187 ASSERT.kind_of mm_module, MMLA::GenericModule
188 ASSERT.kind_of(
189 mm_map_of_property_to_uniq_num,
190 MMLA::MapOfPropertyToUniqueNumber
191 )
192
193 mo_above_path =
194 Path.transform(mm_module.above_path << mm_module.name)
195
196 MOLA::SetOfProperty.new(
197 mm_properties.map { |mm_property|
198 pair_of_uniq_num =
199 mm_map_of_property_to_uniq_num.at mm_property
200
201 args = [
202 transform_location(mm_property.location, mo_files),
203 mo_above_path,
204 mm_property.name,
205 pair_of_uniq_num.num,
206 pair_of_uniq_num.max_num,
207 mm_property.attach_to_instance?
208 ]
209
210 case mm_property
211 when MMLL::Alias
212 MOLL::Alias.new(*(args << mm_property.orig_name))
213 when MMLL::Attribute
214 opt_args = [mm_property.seq_num, mm_property.accessor]
215
216 MOLL::Attribute.new(*(args + opt_args))
217 when MMLL::Method
218 opt_args = [mm_property.args, mm_property.module_func?]
219
220 MOLL::Method.new(*(args + opt_args))
221 else
222 ASSERT.abort(
223 "Unknown mm_property: %s", mm_property.to_s
224 )
225 end
226 }
227 )
228 end
229
230
231
232
233 def transform_modules_into_paths(mm_modules)
234 ASSERT.kind_of mm_modules, MMLA::SetOfModule
235
236 MOL::SetOfPath.new(
237 mm_modules.map { |mm_module|
238 Path.transform mm_module.path
239 }
240 )
241 end
242 end
243
244 end # TmDoc::Transformer::ModuleIntoObject
245
246 end # TmDoc