contrib/objcwrapper: filter out only typedefs over one line
[nit.git] / contrib / objcwrapper / src / header_static.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 # Filters preprocessed C-like header files to remove static code and keep their signatures.
16 #
17 # This tool is used in the process of parsing header files to extract
18 # information on the declared services (the functions and structures).
19 # This information is then used to generate bindings for Nit code
20 # to access these services.
21 #
22 # The C header sometimes contains static code. It deletes static code of
23 # headers, but keep their signatures. This tool is an extension of
24 # header_keeper. It searches the keyword static to identify
25 # the static code, and ignores the code into their brackets. The result is
26 # printed to sdtout.
27 #
28 # ~~~sh
29 # cat Pre-Processed/CGGeometry.h | header_static > Pre-Processed/static_header.h
30 # ~~~
31 #
32 # This module can also be used as a library.
33 # The main service is the method `header_static`
34 module header_static
35
36 redef class Char
37 private fun is_endline: Bool do return "\};".has(self)
38 end
39
40 # Filters the preprocessed `input` to keep signatures for static code and write to the `output`
41 fun header_static(input: Reader, output: Writer) do
42 var static_target = false
43 var static_attribute_target = false
44 var bracket_counter = 0
45 var previous_letter = ""
46 var instruction = ""
47 var double_underscore = 0
48 var position = 0
49
50 while not input.eof do
51 var line = input.read_line
52
53 if line.has("typedef ") and line.has(";") then continue
54
55 if line.has("static") then static_target = true
56
57 if static_target then
58 if line.to_s.has("__attribute__") then static_attribute_target = true
59 for letter in line do
60 if letter == '{' then bracket_counter += 1
61 if letter == '}' then bracket_counter -= 1
62
63 if letter == '_' and previous_letter == "_" and bracket_counter == 0 then
64 double_underscore += 1
65 end
66
67 # Sometimes we lost space between return type and signature name,
68 # because he has a return line between both.
69 # We add a space before signature name for safety.
70 if bracket_counter == 0 and letter == '_' and double_underscore >= 1 and not static_attribute_target then
71 instruction = instruction.insert_at(" ", position - 2)
72 end
73 if bracket_counter == 0 and not letter.is_endline then instruction += letter.to_s
74 if bracket_counter == 0 and letter.is_endline then
75 instruction += ";"
76 static_target = false
77 static_attribute_target = false
78 end
79
80 if bracket_counter == 0 and (letter == '}' and double_underscore >= 1 or letter == ';') then
81 output.write instruction + "\n"
82 end
83
84 if letter.is_endline and bracket_counter == 0 then
85 double_underscore = 0
86 position = 0
87 instruction = ""
88 end
89
90 previous_letter = letter.to_s
91 position += 1
92 end
93 else
94 output.write line + "\n"
95 end
96 end
97 end
98
99 header_static(sys.stdin, sys.stdout)