Debian Codegraph @

solenv/bin/modules/pre2par/work.pm

raw
1 #
2 # This file is part of the LibreOffice project.
3 #
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 #
8 # This file incorporates work covered by the following license notice:
9 #
10 # Licensed to the Apache Software Foundation (ASF) under one or more
11 # contributor license agreements. See the NOTICE file distributed
12 # with this work for additional information regarding copyright
13 # ownership. The ASF licenses this file to you under the Apache
14 # License, Version 2.0 (the "License"); you may not use this file
15 # except in compliance with the License. You may obtain a copy of
16 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 #
18
19
20 package pre2par::work;
21
22 use pre2par::exiter;
23 use pre2par::remover;
24 use pre2par::pathanalyzer;
25
26 ############################################
27 # pre2par working module
28 ############################################
29
30 ############################################
31 # procedure to split a line, that contains
32 # more than one par file lines
33 ############################################
34
35 sub split_line
36 {
37 my ($line, $parfile) = @_;
38
39 while ( $line =~ /^((?:[^"]|\"(?:[^"\\]|\\.)*\")*?\;\s+)\s*(.*)$/ )
40 {
41 my $oneline = $1;
42 $line = $2;
43 pre2par::remover::remove_leading_and_ending_whitespaces(\$oneline);
44 $oneline = $oneline . "\n";
45 push(@{$parfile}, $oneline);
46
47 if ( $line =~ /^\s*End\s+(\w+.*$)/i )
48 {
49 $line = $1;
50 push(@{$parfile}, "End\n\n");
51 }
52 }
53
54 # the last line
55
56 pre2par::remover::remove_leading_and_ending_whitespaces(\$line);
57 $line = $line . "\n";
58 push(@{$parfile}, $line);
59
60 if ( $line =~ /^\s*End\s*$/i ) { push(@{$parfile}, "\n"); }
61 }
62
63 ###################################################################
64 # Preprocessing the pre file to split all lines with semicolon
65 ###################################################################
66
67 sub preprocess_macros
68 {
69 my ($prefile) = @_;
70
71 my @newprefile = ();
72
73 for ( my $i = 0; $i <= $#{$prefile}; $i++ )
74 {
75 my $oneline = ${$prefile}[$i];
76 if ( $oneline =~ /\;\s*\w+/ )
77 {
78 split_line($oneline, \@newprefile);
79 }
80 else
81 {
82 push(@newprefile, $oneline);
83 }
84 }
85
86 return \@newprefile;
87 }
88
89 ############################################
90 # main working procedure
91 ############################################
92
93 sub convert
94 {
95 my ($prefile) = @_;
96
97 my @parfile = ();
98
99 my $iscodesection = 0;
100 my $ismultiliner = 0;
101 my $globalline = "";
102
103 # Preprocessing the pre file to split all lines with semicolon
104 $prefile = preprocess_macros($prefile);
105
106 for ( my $i = 0; $i <= $#{$prefile}; $i++ )
107 {
108 my $oneline = ${$prefile}[$i];
109
110 if ($iscodesection)
111 {
112 if ( $oneline =~ /^\s*\}\;\s*$/ )
113 {
114 $iscodesection = 0;
115 }
116 else # nothing to do for code inside a code section
117 {
118 push(@parfile, $oneline);
119 next;
120 }
121 }
122
123 if ( $oneline =~ /^\s*$/ ) { next; }
124
125 if ( $oneline =~ /^\s*Code\s+\=\s+\{/ )
126 {
127 $iscodesection = 1;
128 }
129
130 pre2par::remover::remove_leading_and_ending_whitespaces(\$oneline);
131
132 my $insertemptyline = 0;
133
134 if ( $oneline =~ /^\s*End\s*$/i ) { $insertemptyline = 1; }
135
136 # Sometimes the complete file is in one line, then the gid line has to be separated
137
138 if ( $oneline =~ /^\s*(\w+\s+\w+)\s+(\w+\s+\=.*$)/ ) # three words before the equal sign
139 {
140 my $gidline = $1;
141 $oneline = $2;
142 $gidline = $gidline . "\n";
143
144 push(@parfile, $gidline);
145 }
146
147 if ( $oneline =~ /\;\s*\w+/ )
148 {
149 split_line($oneline, \@parfile);
150 next;
151 }
152
153 # searching for lines with brackets, like Customs = { ..., which can be parted above several lines
154
155 if ( $oneline =~ /^\s*\w+\s+\=\s*\(.*\)\s*\;\s*$/ ) # only one line
156 {
157 if (( ! ( $oneline =~ /^\s*Assignment\d+\s*\=/ )) && ( ! ( $oneline =~ /^\s*PatchAssignment\d+\s*\=/ )))
158 {
159 $oneline =~ s/\s//g; # removing whitespaces in lists
160 $oneline =~ s/\=/\ \=\ /; # adding whitespace around equals sign
161 }
162 }
163
164 if ( $oneline =~ /^\s*\w+\s+\=\s*$/ )
165 {
166 $oneline =~ s/\s*$//;
167 pre2par::exiter::exit_program("Error: Illegal syntax, no line break after eqals sign allowed. Line: \"$oneline\"", "convert");
168 }
169
170 if (( $oneline =~ /^\s*\w+\s+\=\s*\(/ ) && (!( $oneline =~ /\)\s*\;\s*$/ ))) # several lines
171 {
172 $ismultiliner = 1;
173 $oneline =~ s/\s//g;
174 $globalline .= $oneline;
175 next; # not including yet
176 }
177
178 if ( $ismultiliner )
179 {
180 $oneline =~ s/\s//g;
181 $globalline .= $oneline;
182
183 if ( $oneline =~ /\)\s*\;\s*$/ ) { $ismultiliner = 0; }
184
185 if (! ( $ismultiliner ))
186 {
187 $globalline =~ s/\=/\ \=\ /; # adding whitespace around equals sign
188 $globalline .= "\n";
189 push(@parfile, $globalline);
190 $globalline = "";
191 }
192
193 next;
194 }
195
196 $oneline = $oneline . "\n";
197
198 $oneline =~ s/\s*\=\s*/ \= /; # nice, to have only one whitespace around equal signs
199
200 # Concatenate adjacent string literals:
201 while ($oneline =~
202 s/^((?:[^"]*
203 \"(?:[^\\"]|\\.)*\"
204 (?:[^"]*[^[:blank:]"][^"]*\"(?:[^\\"]|\\.)*\")*)*
205 [^"]*
206 \"(?:[^\\"]|\\.)*)
207 \"[[:blank:]]*\"
208 ((?:[^\\"]|\\.)*\")
209 /\1\2/x)
210 {}
211
212 push(@parfile, $oneline);
213
214 if ($insertemptyline) { push(@parfile, "\n"); }
215
216 }
217
218 return \@parfile;
219 }
220
221 ############################################
222 # formatting the par file
223 ############################################
224
225 sub formatter
226 {
227 my ($parfile) = @_;
228
229 my $iscodesection = 0;
230
231 my $tabcounter = 0;
232 my $isinsideitem = 0;
233 my $currentitem;
234
235 for ( my $i = 0; $i <= $#{$parfile}; $i++ )
236 {
237 my $oneline = ${$parfile}[$i];
238 my $isitemline = 0;
239
240 if (! $isinsideitem )
241 {
242 for ( my $j = 0; $j <= $#pre2par::globals::allitems; $j++ )
243 {
244 if ( $oneline =~ /^\s*$pre2par::globals::allitems[$j]\s+\w+\s*$/ )
245 {
246 $currentitem = $pre2par::globals::allitems[$j];
247 $isitemline = 1;
248 $isinsideitem = 1;
249 $tabcounter = 0;
250 last;
251 }
252 }
253 }
254
255 if ( $isitemline )
256 {
257 next; # nothing to do
258 }
259
260 if ( $oneline =~ /^\s*end\s*$/i )
261 {
262 $isinsideitem = 0;
263 $tabcounter--;
264 }
265
266 if ( $isinsideitem )
267 {
268 $oneline = "\t" . $oneline;
269 ${$parfile}[$i] = $oneline;
270 }
271 }
272 }
273
274 ############################################
275 # Checking that the pre file has content
276 ############################################
277
278 sub check_content
279 {
280 my ($filecontent, $filename) = @_;
281
282 if ( $#{$filecontent} < 0 ) { pre2par::exiter::exit_program("Error: $filename has no content!", "check_content"); }
283 }
284
285 ############################################
286 # Checking content of par files.
287 # Currently only size.
288 ############################################
289
290 sub diff_content
291 {
292 my ($content1, $content2, $filename) = @_;
293
294 if ( $#{$content1} != $#{$content2} ) { pre2par::exiter::exit_program("Error: $filename was not saved correctly!", "diff_content"); }
295 }
296
297 1;