f76df4dde627fb412276634e36d6d13f46f3ee14
1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Nodes for modules and files.
16 module model
::module_compound
23 # Creates one modules by inner namespace. The full name of the modules begin
24 # with the namespace’s full name, and end with the unqualified name of the file,
25 # without the extension.
30 # Mapping between inner namespace’s names and corresponding modules.
31 private var inner_namespaces
: Map[String, Module] = new HashMap[String, Module]
33 # The last component of the path, without the extension.
35 # Used as the unqualified name of the modules.
36 private var basename
: String = ""
42 redef fun name_separator
: String do return "/"
44 redef fun location
=(location
: nullable Location) do
46 if location
!= null and location
.path
!= null then
47 full_name
= location
.path
.as(not null)
49 for m
in inner_namespaces
.values
do m
.location
= location
52 redef fun name
=(name
: String) do
53 # Example: `MyClass.java`
55 var match
= name
.search_last
(".")
60 basename
= name
.substring
(0, match
.from
)
62 # Update the modules’ name.
63 for ns
, m
in inner_namespaces
do
65 m
.full_name
= basename
67 m
.full_name
= "{ns}{ns_separator}{basename}"
72 redef fun declare_namespace
(id
: String, full_name
: String) do
75 assert not full_name
.is_empty
or id
.is_empty
else
76 sys
.stderr
.write
"Inner mamespace declarations without name are not yet supported (except for the root namespace).\n"
78 if inner_namespaces
.keys
.has
(full_name
) then
79 m
= inner_namespaces
[full_name
]
80 if id
!= "" then m
.parent
= id
83 if full_name
.is_empty
then
84 m
.full_name
= basename
86 m
.full_name
= "{full_name}{ns_separator}{basename}"
89 m
.location
= self["location"].as(nullable Location)
90 inner_namespaces
[full_name
] = m
94 redef fun declare_class
(id
: String, full_name
: String) do
95 assert not id
.is_empty
else
96 sys
.stderr
.write
"Inner class declarations without ID are not yet supported.\n"
98 assert not full_name
.is_empty
else
99 sys
.stderr
.write
"Inner class declarations without name are not yet supported.\n"
101 var match
= full_name
.search_last
(ns_separator
)
105 if match
== null then
108 ns_name
= full_name
.substring
(0, match
.from
)
110 if inner_namespaces
.keys
.has
(ns_name
) then
111 m
= inner_namespaces
[ns_name
]
113 declare_namespace
("", ns_name
)
114 m
= inner_namespaces
[ns_name
]
116 m
.declare_class
(id
, full_name
)
119 redef fun put_in_graph
do
120 # Do not add `self` to the Neo4j graph...
121 # ... but add its modules...
122 for m
in inner_namespaces
.values
do m
.put_in_graph
123 # ... and add `self` to the index.
124 if model_id
!= "" then graph
.by_id
[model_id
] = self
133 # The `model_id` of the parent namespace.
134 var parent
: String = "" is writable
136 # The classes defined in the module.
137 var inner_classes
: SimpleCollection[String] = new Array[String]
141 self.labels
.add
("MModule")
144 redef fun declare_class
(id
: String, full_name
: String) do
145 assert not id
.is_empty
else
146 sys
.stderr
.write
"Inner class declarations without ID not supported yet.\n"
148 inner_classes
.add
(id
)
151 redef fun put_edges
do
152 graph
.add_edge
(graph
.by_id
[parent
], "DECLARES", self)
153 for c
in inner_classes
do
154 var node
= graph
.by_id
[c
].as(ClassCompound)
155 graph
.add_edge
(self, "INTRODUCES", node
)
156 graph
.add_edge
(self, "DEFINES", node
.class_def
)