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 # Filters preprocessed C-like header files to remove static code and keep their signatures.
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.
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
29 # cat Pre-Processed/CGGeometry.h | header_static > Pre-Processed/static_header.h
32 # This module can also be used as a library.
33 # The main service is the method `header_static`
37 private fun is_endline
: Bool do return "\};".has
(self)
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
= ""
47 var double_underscore
= 0
50 while not input
.eof
do
51 var line
= input
.read_line
53 if line
.has
("typedef ") and line
.has
(";") then continue
55 if line
.has
("static") then static_target
= true
58 if line
.to_s
.has
("__attribute__") then static_attribute_target
= true
60 if letter
== '{' then bracket_counter
+= 1
61 if letter
== '}' then bracket_counter
-= 1
63 if letter
== '_' and previous_letter
== "_" and bracket_counter
== 0 then
64 double_underscore
+= 1
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)
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
77 static_attribute_target
= false
80 if bracket_counter
== 0 and (letter
== '}' and double_underscore
>= 1 or letter
== ';') then
81 output
.write instruction
+ "\n"
84 if letter
.is_endline
and bracket_counter
== 0 then
90 previous_letter
= letter
.to_s
94 output
.write line
+ "\n"
99 header_static
(sys
.stdin
, sys
.stdout
)