ci: do not error when nothing with nitunit_some
[nit.git] / contrib / neo_doxygen / src / doxml / language_specific.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Handle language-specific parts of the importation.
16 module doxml::language_specific
17
18 import model
19
20 # Various importation logics that depend on the project’s language.
21 abstract class SourceLanguage
22
23 # Apply the information deduced from `type_text` to `member`.
24 #
25 # `type_text` is the content of the `<type>` element.
26 fun apply_member_type(member: Member, type_text: RawType) do
27 if type_text["text"] != null then
28 member.static_type = type_text
29 end
30 end
31
32 # Apply the information deduced from `type_text` to `parameter`.
33 #
34 # `type_text` is the content of the `<type>` element.
35 fun apply_parameter_type(parameter: Parameter, type_text: RawType) do
36 if type_text["text"] != null then
37 parameter.static_type = type_text
38 end
39 end
40
41 # Extract the specified keyword at the beginning of the specified text.
42 #
43 # If the keyword is at the beginning of the specified text, return `true`
44 # and remove the keyword. Else, return false.
45 #
46 # Used to extract some keywords that Doxygen puts in the type.
47 #
48 # class DummySource
49 # super JavaSource
50 # #
51 # fun test(text: LinkedText, keyword: String): Bool do
52 # return extract_keyword(text, keyword)
53 # end
54 # end
55 # #
56 # var text = new RawType(new ProjectGraph(""))
57 # var dummy = new DummySource
58 # var res: Bool
59 # #
60 # text.add_part("abstract final", "")
61 # res = dummy.test(text, "static")
62 # assert not res
63 # res = dummy.test(text, "abstract")
64 # assert res
65 # assert "final" == text["text"].as(JsonArray).first
66 # res = dummy.test(text, "final")
67 # assert res
68 # assert text["text"] == null
69 # res = dummy.test(text, "abstract")
70 # assert not res
71 protected fun extract_keyword(text: LinkedText, keyword: String): Bool do
72 var text_array = text["text"]
73 if text_array == null then return false
74 assert text_array isa JsonArray
75 if text_array.is_empty then return false
76
77 var content = text_array.first.as(String).l_trim
78 var link = text.links.first
79 var found = false
80
81 if link == null and content.has_prefix(keyword) then
82 if keyword.length == content.length then
83 content = ""
84 found = true
85 else if content.chars[keyword.length] <= ' ' then
86 content = content.substring_from(keyword.length).l_trim
87 found = true
88 end
89 if "" == content then
90 text.shift_part
91 else if found then
92 text.set_part(0, content, "")
93 end
94 end
95 return found
96 end
97
98 # Extract the specified suffix in the specified text.
99 #
100 # If the suffix is at the end of the specified text, return `true`
101 # and remove the suffix. Else, return false.
102 #
103 # Used to extract stuff like `...` that Doxygen puts in the type.
104 #
105 # class DummySource
106 # super JavaSource
107 # #
108 # fun test(text: LinkedText, s: String): Bool do
109 # return extract_suffix(text, s)
110 # end
111 # end
112 # #
113 # var text = new RawType(new ProjectGraph(""))
114 # var dummy = new DummySource
115 # var res: Bool
116 # #
117 # text.add_part("Object...+++", "")
118 # res = dummy.test(text, "...")
119 # assert not res
120 # res = dummy.test(text, "+++")
121 # assert res
122 # assert "Object..." == text["text"].as(JsonArray).first
123 # res = dummy.test(text, "...")
124 # assert res
125 # assert "Object" == text["text"].as(JsonArray).first
126 protected fun extract_suffix(text: LinkedText, suffix: String): Bool do
127 var text_array = text["text"]
128 if text_array == null then return false
129 assert text_array isa JsonArray
130 if text_array.is_empty then return false
131
132 var content = text_array.last.as(String).r_trim
133 var link = text.links.first
134
135 if link == null and content.has_suffix(suffix) then
136 content = content.substring(0, content.length - suffix.length).r_trim
137 if "" == content then
138 text.pop_part
139 else
140 text.set_part(0, content, "")
141 end
142 return true
143 else
144 return false
145 end
146 end
147 end
148
149 # The default importation logics.
150 #
151 # Do nothing special.
152 class DefaultSource
153 super SourceLanguage
154 end
155
156 # Importation logics for Java.
157 class JavaSource
158 super SourceLanguage
159
160 redef fun apply_member_type(member, type_text) do
161 # For abstract members, Doxygen put `abstract` at the beginning of the type.
162 # We assume that Doxygen do not put annotations in the type (it seems to
163 # be the case).
164 if extract_keyword(type_text, "abstract") then
165 member.is_abstract = true
166 end
167 # TODO final
168 # TODO void
169 # TODO Avoid using `RawType` when possible. Only use `RawType` as a fallback.
170 super
171 end
172
173 redef fun apply_parameter_type(parmeter, type_text) do
174 # We assume that Doxygen do not put annotations in the type (it seems to
175 # be the case).
176 # TODO final
177 # TODO Avoid using `RawType` when possible. Only use `RawType` as a fallback.
178 parmeter.is_vararg = extract_suffix(type_text, "...")
179 super
180 end
181 end
182
183 # Importation logics for Python.
184 class PythonSource
185 super SourceLanguage
186
187 redef fun apply_member_type(member, type_text) do
188 # Doxygen may forgot to remove the `def` keyword on methods.
189 extract_keyword(type_text, "def")
190 super
191 end
192 end