tokenizer.c

filter: Statementstmt 0.0% 0 / 349 Branchbran 0.0% 0 / 244 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 593 SCAR 0
CC
0
Cov
0%
CRAP 0
« helper.c ↑ Summary blib/lib/Salvation/TC/Type/Any.pm »
1 #ifndef _TOKENIZER_C_
2 #define _TOKENIZER_C_
3  
4 #include "tokenizer.h"
5 #include <stdlib.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <stdint.h>
9  
10 #define E_STR_INVALID_SIGNATURE "Invalid signature: "
11 #define E_STR_CANT_PARAMETERIZE_TYPE "Can't parameterize type "
12 #define E_STR_INVALID_TYPE_STRING_IN_SIGNATURE "Invalid type string in signature: "
13 #define E_STR_INVALID_PARAMETER_NAME_IN_SIGNATURE "Invalid parameter name in signature: "
14 #define E_STR_TYPE_OR_PARAMETER_NAME_MISSING_IN_SIGNATURE "Type or parameter name missing in signature: "
15 #define E_STR_UNEXPECTED_END_OF_INPUT_IN_SIGNATURE "Unexpected end of input in signature: "
16  
17 0 static inline void push_stack( intptr_t ** stack, int length, intptr_t token ) {
18  
19 0 ▶ if( length > 0 ) *stack = realloc( *stack, ( length + 1 ) * sizeof( intptr_t ) );
Branch: 1 branch
BranchTrueFalseTotal
if( length > 0 ) *stack = realloc( *stack, ( length + 1 ) * sizeof( intptr_t ) ); 0 0 0
20  
21 0 (*stack)[ length ] = token;
22 0 }
23  
24 0 static inline void append( char ** s, char c ) {
25  
26 0 int len = strlen( *s );
27 0 *s = realloc( *s, len + 2 );
28  
29 0 (*s)[ len ] = c;
30 0 (*s)[ len + 1 ] = '\0';
31 0 }
32  
33 0 static inline char * copy_str( char * s ) {
34  
35 0 int len = strlen( s );
36 0 char buf[ len ];
37  
38 0 strcpy( buf, s );
39  
40 0 return strdup( buf );
41 }
42  
43 0 static inline short is_space( char chr ) {
44  
45 0 ▶ return ( ( ( chr == ' ' ) || ( chr == '\n' ) || ( chr == '\r' ) || ( chr == '\t' ) ) ? 1 : 0 );
Branch: 4 branches
BranchTrueFalseTotal
return ( ( ( chr == ' ' ) || ( chr == '\n' ) || ( chr == '\r' ) || ( chr == '\t' ) ) ? 1 : 0 ); 0 0 0
return ( ( ( chr == ' ' ) || ( chr == '\n' ) || ( chr == '\r' ) || ( chr == '\t' ) ) ? 1 : 0 ); 0 0 0
return ( ( ( chr == ' ' ) || ( chr == '\n' ) || ( chr == '\r' ) || ( chr == '\t' ) ) ? 1 : 0 ); 0 0 0
return ( ( ( chr == ' ' ) || ( chr == '\n' ) || ( chr == '\r' ) || ( chr == '\t' ) ) ? 1 : 0 ); 0 0 0
46 }
47  
48 0 static inline short is_delim( char chr ) {
49  
50 0 return ( ( chr == ',' ) ? 1 : 0 );
51 }
52  
53 0 static inline short is_digit( char chr ) {
54  
55 return ( (
56 0 ▶ ( chr == '1' ) || ( chr == '2' ) || ( chr == '3' ) || ( chr == '4' )
Branch: 3 branches
BranchTrueFalseTotal
( chr == '1' ) || ( chr == '2' ) || ( chr == '3' ) || ( chr == '4' ) 0 0 0
( chr == '1' ) || ( chr == '2' ) || ( chr == '3' ) || ( chr == '4' ) 0 0 0
( chr == '1' ) || ( chr == '2' ) || ( chr == '3' ) || ( chr == '4' ) 0 0 0
57 0 ▶ || ( chr == '5' ) || ( chr == '6' ) || ( chr == '7' ) || ( chr == '8' )
Branch: 4 branches
BranchTrueFalseTotal
|| ( chr == '5' ) || ( chr == '6' ) || ( chr == '7' ) || ( chr == '8' ) 0 0 0
|| ( chr == '5' ) || ( chr == '6' ) || ( chr == '7' ) || ( chr == '8' ) 0 0 0
|| ( chr == '5' ) || ( chr == '6' ) || ( chr == '7' ) || ( chr == '8' ) 0 0 0
|| ( chr == '5' ) || ( chr == '6' ) || ( chr == '7' ) || ( chr == '8' ) 0 0 0
58 0 ▶ || ( chr == '9' ) || ( chr == '0' )
Branch: 1 branch
BranchTrueFalseTotal
|| ( chr == '9' ) || ( chr == '0' ) 0 0 0
59 0 ▶ ) ? 1 : 0 );
Branch: 2 branches
BranchTrueFalseTotal
) ? 1 : 0 ); 0 0 0
) ? 1 : 0 ); 0 0 0
60 }
61  
62 static my_stack_t * tokenize_signature_str( char * class, const char * s, tokenizer_options_t * options );
63  
64 0 static inline char * zero_str() {
65  
66 0 char * word = malloc( 1 );
67  
68 0 word[ 0 ] = '\0';
69  
70 0 return word;
71 }
72  
73 0 static my_stack_t * tokenize_type_str( char * class, const char * s, tokenizer_options_t * options ) {
74  
75 0 intptr_t * stack = malloc( sizeof( *stack ) );
76 0 char * word = zero_str();
77 0 char * parameterizable_type = zero_str();
78 0 int stack_size = 0;
79 0 int pos = 0;
80 0 int length = strlen( s );
81  
82 0 ▶ while( pos < length ) {
Branch: 1 branch
BranchTrueFalseTotal
while( pos < length ) { 0 0 0
83  
84 0 char chr = s[ pos ++ ];
85  
86 0 ▶ if( is_space( chr ) ) continue;
Branch: 1 branch
BranchTrueFalseTotal
if( is_space( chr ) ) continue; 0 0 0
87  
88 0 ▶ if( ( chr == '[' ) && ( strcmp( word, "Maybe\0" ) != 0 ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if( ( chr == '[' ) && ( strcmp( word, "Maybe\0" ) != 0 ) ) { 0 0 0
if( ( chr == '[' ) && ( strcmp( word, "Maybe\0" ) != 0 ) ) { 0 0 0
89  
90 0 free( parameterizable_type );
91  
92 0 ▶ if( options -> loose != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( options -> loose != 0 ) { 0 0 0
93  
94 0 parameterizable_type = copy_str( word );
95  
96 } else {
97  
98 0 parameterizable_type = call_load_parameterizable_type_class( class, word );
99  
100 0 ▶ if( strlen( parameterizable_type ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( strlen( parameterizable_type ) == 0 ) { 0 0 0
101  
102 0 free_stack_arr( stack, stack_size );
103 0 char * _s = malloc( strlen( E_STR_CANT_PARAMETERIZE_TYPE ) + strlen( word ) + 1 );
104 0 sprintf( _s, "%s%s", E_STR_CANT_PARAMETERIZE_TYPE, word );
105 0 p_die( _s );
106 }
107 }
108 }
109  
110 0 ▶ if( chr == '|' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( chr == '|' ) { 0 0 0
111  
112 0 ▶ if( strlen( word ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( strlen( word ) == 0 ) { 0 0 0
113  
114 0 ▶ if( stack_size == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( stack_size == 0 ) { 0 0 0
115  
116 0 free_stack_arr( stack, stack_size );
117 0 p_die( "Invalid type string: | can't be a first character of type name\n" );
118  
119 } else {
120  
121 0 abstract_type_t * token = (abstract_type_t*)(stack[ stack_size - 1 ]);
122 0 short token_type = token -> token_type;
123  
124 0 ▶ if( !(
Branch: 2 branches
BranchTrueFalseTotal
if( !( 0 0 0
if( !( 0 0 0
125 ( token_type == TOKEN_TYPE_MAYBE )
126 0 ▶ || ( token_type == TOKEN_TYPE_PARAMETRIZABLE )
Branch: 1 branch
BranchTrueFalseTotal
|| ( token_type == TOKEN_TYPE_PARAMETRIZABLE ) 0 0 0
127 0 ▶ || ( token_type == TOKEN_TYPE_SIGNED )
Branch: 1 branch
BranchTrueFalseTotal
|| ( token_type == TOKEN_TYPE_SIGNED ) 0 0 0
128 || ( token_type == TOKEN_TYPE_LENGTH )
129 ) ) {
130  
131 0 free_stack_arr( stack, stack_size );
132 0 p_die( "Invalid type string: | should follow a type name\n" );
133 }
134 }
135  
136 } else {
137  
138 0 basic_type_t * token = malloc( sizeof( *token ) );
139  
140 0 token -> base.token_type = TOKEN_TYPE_BASIC;
141 0 token -> type = word;
142  
143 0 push_stack( &stack, stack_size, (intptr_t)token );
144 0 ++stack_size;
145  
146 0 word = zero_str();
147 }
148  
149 0 ▶ } else if( chr == '[' ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if( chr == '[' ) { 0 0 0
150  
151 0 int cnt = 1;
152 0 char * substr = zero_str();
153  
154 0 ▶ while( pos < length ) {
Branch: 1 branch
BranchTrueFalseTotal
while( pos < length ) { 0 0 0
155  
156 0 char subchr = s[ pos ++ ];
157  
158 0 ▶ if( subchr == '[' ) ++cnt;
Branch: 1 branch
BranchTrueFalseTotal
if( subchr == '[' ) ++cnt; 0 0 0
159 0 ▶ if( subchr == ']' ) --cnt;
Branch: 1 branch
BranchTrueFalseTotal
if( subchr == ']' ) --cnt; 0 0 0
160  
161 0 ▶ if( cnt == 0 ) break;
Branch: 1 branch
BranchTrueFalseTotal
if( cnt == 0 ) break; 0 0 0
162  
163 0 append( &substr, subchr );
164 }
165  
166 0 ▶ if( ( strlen( substr ) == 0 ) || ( strlen( word ) == 0 ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if( ( strlen( substr ) == 0 ) || ( strlen( word ) == 0 ) ) { 0 0 0
if( ( strlen( substr ) == 0 ) || ( strlen( word ) == 0 ) ) { 0 0 0
167  
168 0 free_stack_arr( stack, stack_size );
169 0 p_die( "Invalid type parameterization: no type name or no parameter name\n" );
170 }
171  
172 0 ▶ if( strlen( parameterizable_type ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( strlen( parameterizable_type ) == 0 ) { 0 0 0
173  
174 0 maybe_type_t * token = malloc( sizeof( *token ) );
175  
176 0 token -> base.token_type = TOKEN_TYPE_MAYBE;
177  
178 0 my_stack_t * _stack = tokenize_type_str( class, substr, options );
179 0 ▶ if( _stack == 0 ) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if( _stack == 0 ) return 0; 0 0 0
180 0 token -> stack = _stack;
181  
182 0 push_stack( &stack, stack_size, (intptr_t)token );
183 0 ++stack_size;
184  
185 } else {
186  
187 0 parameterizable_type_t * token = malloc( sizeof( *token ) );
188  
189 0 token -> base.token_type = TOKEN_TYPE_PARAMETRIZABLE;
190 0 token -> class = parameterizable_type;
191  
192 0 my_stack_t * _param = tokenize_type_str( class, substr, options );;
193 0 ▶ if( _param == 0 ) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if( _param == 0 ) return 0; 0 0 0
194 0 token -> param = _param;
195  
196 0 my_stack_t * _stack = tokenize_type_str( class, word, options );
197 0 ▶ if( _stack == 0 ) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if( _stack == 0 ) return 0; 0 0 0
198 0 token -> stack = _stack;
199  
200 0 push_stack( &stack, stack_size, (intptr_t)token );
201 0 ++stack_size;
202  
203 0 parameterizable_type = zero_str();
204 }
205  
206 0 free( word );
207 0 word = zero_str();
208 0 free( substr );
209  
210 0 ▶ } else if( chr == '(' ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if( chr == '(' ) { 0 0 0
211  
212 0 ▶ if( strlen( word ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( strlen( word ) == 0 ) { 0 0 0
213  
214 0 ▶ if( stack_size == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( stack_size == 0 ) { 0 0 0
215  
216 0 free_stack_arr( stack, stack_size );
217 0 p_die( "Invalid type description: ( can't be a first character of type name\n" );
218  
219 } else {
220  
221 0 abstract_type_t * token = (abstract_type_t*)(stack[ stack_size - 1 ]);
222 0 short token_type = token -> token_type;
223  
224 0 ▶ if( !(
Branch: 1 branch
BranchTrueFalseTotal
if( !( 0 0 0
225 ( token_type == TOKEN_TYPE_PARAMETRIZABLE )
226 ) ) {
227  
228 0 free_stack_arr( stack, stack_size );
229 0 p_die( "Invalid type description: ( should follow a type name\n" );
230 }
231 }
232  
233 } else {
234  
235 0 basic_type_t * token = malloc( sizeof( *token ) );
236  
237 0 token -> base.token_type = TOKEN_TYPE_BASIC;
238 0 token -> type = word;
239  
240 0 push_stack( &stack, stack_size, (intptr_t)token );
241 0 ++stack_size;
242  
243 0 word = zero_str();
244 }
245  
246 0 int cnt = 1;
247 0 char * substr = zero_str();
248  
249 0 append( &substr, chr );
250  
251 0 ▶ while( pos < length ) {
Branch: 1 branch
BranchTrueFalseTotal
while( pos < length ) { 0 0 0
252  
253 0 char subchr = s[ pos ++ ];
254  
255 0 ▶ if( subchr == '(' ) ++cnt;
Branch: 1 branch
BranchTrueFalseTotal
if( subchr == '(' ) ++cnt; 0 0 0
256 0 ▶ if( subchr == ')' ) --cnt;
Branch: 1 branch
BranchTrueFalseTotal
if( subchr == ')' ) --cnt; 0 0 0
257  
258 0 append( &substr, subchr );
259  
260 0 ▶ if( cnt == 0 ) break;
Branch: 1 branch
BranchTrueFalseTotal
if( cnt == 0 ) break; 0 0 0
261 }
262  
263 0 signed_type_t * token = malloc( sizeof( *token ) );
264  
265 0 token -> base.token_type = TOKEN_TYPE_SIGNED;
266  
267 0 my_stack_t * signature = tokenize_signature_str( class, substr, options );
268 0 ▶ if( signature == 0 ) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if( signature == 0 ) return 0; 0 0 0
269 0 token -> signature = signature;
270  
271 0 token -> type = stack[ stack_size - 1 ];
272 0 token -> source = substr;
273  
274 0 stack[ stack_size - 1 ] = (intptr_t)token;
275  
276 0 ▶ } else if( chr == '{' ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if( chr == '{' ) { 0 0 0
277  
278 0 ▶ if( strlen( word ) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( strlen( word ) != 0 ) { 0 0 0
279  
280 0 basic_type_t * token = malloc( sizeof( *token ) );
281  
282 0 token -> base.token_type = TOKEN_TYPE_BASIC;
283 0 token -> type = word;
284  
285 0 push_stack( &stack, stack_size, (intptr_t)token );
286 0 ++stack_size;
287  
288 0 word = zero_str();
289 }
290  
291 0 short has_min = 0;
292 0 short has_max = 0;
293 0 short got_delim = 0;
294  
295 0 char * min = zero_str();
296 0 char * max = zero_str();
297  
298 0 ▶ while( pos < length ) {
Branch: 1 branch
BranchTrueFalseTotal
while( pos < length ) { 0 0 0
299  
300 0 char subchr = s[ pos ++ ];
301  
302 0 ▶ if( subchr == '}' ) break;
Branch: 1 branch
BranchTrueFalseTotal
if( subchr == '}' ) break; 0 0 0
303 0 ▶ if( is_space( subchr ) ) continue;
Branch: 1 branch
BranchTrueFalseTotal
if( is_space( subchr ) ) continue; 0 0 0
304  
305 0 ▶ if( is_delim( subchr ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if( is_delim( subchr ) ) { 0 0 0
306  
307 0 ▶ if( got_delim == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( got_delim == 1 ) { 0 0 0
308  
309 0 free_stack_arr( stack, stack_size );
310 0 p_die( "Invalid length limits: only one delimiter allowed\n" );
311 }
312  
313 0 got_delim = 1;
314 0 continue;
315 }
316  
317 0 ▶ if( is_digit( subchr ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if( is_digit( subchr ) ) { 0 0 0
318  
319 0 ▶ if( got_delim == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( got_delim == 0 ) { 0 0 0
320  
321 0 append( &min, subchr );
322 0 has_min = 1;
323  
324 } else {
325  
326 0 append( &max, subchr );
327 0 has_max = 1;
328 }
329  
330 } else {
331  
332 0 free_stack_arr( stack, stack_size );
333 0 p_die( "Invalid length limits: only digits allowed\n" );
334 }
335 }
336  
337 0 ▶ if( has_min == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( has_min == 0 ) { 0 0 0
338  
339 0 free_stack_arr( stack, stack_size );
340 0 p_die( "Invalid length limits: lower limit is required\n" );
341 }
342  
343 0 ▶ if( ( has_max == 0 ) && ( got_delim == 0 ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if( ( has_max == 0 ) && ( got_delim == 0 ) ) { 0 0 0
if( ( has_max == 0 ) && ( got_delim == 0 ) ) { 0 0 0
344  
345 0 free( max );
346 0 max = copy_str( min );
347 0 has_max = 1;
348 }
349  
350 0 length_type_t * token = malloc( sizeof( *token ) );
351  
352 0 token -> base.token_type = TOKEN_TYPE_LENGTH;
353 0 token -> has_min = has_min;
354 0 token -> has_max = has_max;
355 0 ▶ if( has_min == 1 ) token -> min = min; else free( min );
Branch: 1 branch
BranchTrueFalseTotal
if( has_min == 1 ) token -> min = min; else free( min ); 0 0 0
356 0 ▶ if( has_max == 1 ) token -> max = max; else free( max );
Branch: 1 branch
BranchTrueFalseTotal
if( has_max == 1 ) token -> max = max; else free( max ); 0 0 0
357 0 token -> type = stack[ stack_size - 1 ];
358  
359 0 stack[ stack_size - 1 ] = (intptr_t)token;
360  
361 } else {
362  
363 0 append( &word, chr );
364 }
365 }
366  
367 0 ▶ if( strlen( word ) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( strlen( word ) != 0 ) { 0 0 0
368  
369 0 basic_type_t * token = malloc( sizeof( *token ) );
370  
371 0 token -> base.token_type = TOKEN_TYPE_BASIC;
372 0 token -> type = word;
373  
374 0 push_stack( &stack, stack_size, (intptr_t)token );
375 0 ++stack_size;
376  
377 } else {
378  
379 0 free( word );
380 }
381  
382 0 free( parameterizable_type );
383  
384 0 my_stack_t * out = malloc( sizeof( *out ) );
385  
386 0 out -> size = stack_size;
387 0 out -> data = stack;
388  
389 0 return out;
390 }
391  
392 typedef struct {
393  
394 int circle;
395 int inner_curly;
396 int inner_circle;
397 int inner_square;
398  
399 } brackets_state_t;
400  
401 0 static inline void update_brackets_state( brackets_state_t * bs, char chr ) {
402  
403 0 ▶ if( chr == '(' ) if( ++(bs -> circle) > 1 ) ++(bs -> inner_circle);
Branch: 2 branches
BranchTrueFalseTotal
if( chr == '(' ) if( ++(bs -> circle) > 1 ) ++(bs -> inner_circle); 0 0 0
if( chr == '(' ) if( ++(bs -> circle) > 1 ) ++(bs -> inner_circle); 0 0 0
404 0 ▶ if( chr == '[' ) ++(bs -> inner_square);
Branch: 1 branch
BranchTrueFalseTotal
if( chr == '[' ) ++(bs -> inner_square); 0 0 0
405 0 ▶ if( chr == '{' ) ++(bs -> inner_curly);
Branch: 1 branch
BranchTrueFalseTotal
if( chr == '{' ) ++(bs -> inner_curly); 0 0 0
406  
407 0 ▶ if( chr == ')' ) if( --(bs -> circle) > 0 ) --(bs -> inner_circle);
Branch: 2 branches
BranchTrueFalseTotal
if( chr == ')' ) if( --(bs -> circle) > 0 ) --(bs -> inner_circle); 0 0 0
if( chr == ')' ) if( --(bs -> circle) > 0 ) --(bs -> inner_circle); 0 0 0
408 0 ▶ if( chr == ']' ) --(bs -> inner_square);
Branch: 1 branch
BranchTrueFalseTotal
if( chr == ']' ) --(bs -> inner_square); 0 0 0
409 0 ▶ if( chr == '}' ) --(bs -> inner_curly);
Branch: 1 branch
BranchTrueFalseTotal
if( chr == '}' ) --(bs -> inner_curly); 0 0 0
410 0 }
411  
412 0 static inline short has_open_inner_brackets( brackets_state_t * bs ) {
413  
414 return ( (
415 0 ( bs -> inner_curly == 0 )
416 0 ▶ && ( bs -> inner_circle == 0 )
Branch: 1 branch
BranchTrueFalseTotal
&& ( bs -> inner_circle == 0 ) 0 0 0
417 0 && ( bs -> inner_square == 0 )
418 0 ▶ ) ? 0 : 1 );
Branch: 2 branches
BranchTrueFalseTotal
) ? 0 : 1 ); 0 0 0
) ? 0 : 1 ); 0 0 0
419 }
420  
421 static inline signature_param_t * tokenize_signature_parameter_str( const char * s, tokenizer_options_t * options );
422  
423 0 static my_stack_t * tokenize_signature_str( char * class, const char * s, tokenizer_options_t * options ) {
424  
425 0 intptr_t * stack = malloc( sizeof( *stack ) );
426 0 int stack_size = 0;
427 0 int pos = 0;
428 0 int length = strlen( s );
429  
430 0 char * type = zero_str();
431 0 char * name = zero_str();
432  
433 0 brackets_state_t brackets_state = {
434 .circle = 0,
435 .inner_curly = 0,
436 .inner_circle = 0,
437 .inner_square = 0
438 };
439  
440 0 short seq = SIG_SEQ_MIN;
441 0 short done = 0;
442  
443 0 ▶ while( pos <= length ) {
Branch: 1 branch
BranchTrueFalseTotal
while( pos <= length ) { 0 0 0
444  
445 0 ▶ if( seq == SIG_SEQ_ITEM_TYPE ) {
Branch: 1 branch
BranchTrueFalseTotal
if( seq == SIG_SEQ_ITEM_TYPE ) { 0 0 0
446  
447 0 ▶ while( pos < length ) {
Branch: 1 branch
BranchTrueFalseTotal
while( pos < length ) { 0 0 0
448  
449 0 char chr = s[ pos ++ ];
450  
451 0 ▶ if( brackets_state.circle == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( brackets_state.circle == 0 ) { 0 0 0
452  
453 0 ▶ if( is_space( chr ) ) continue;
Branch: 1 branch
BranchTrueFalseTotal
if( is_space( chr ) ) continue; 0 0 0
454 }
455  
456 0 update_brackets_state( &brackets_state, chr );
457  
458 0 ▶ if( brackets_state.circle == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( brackets_state.circle == 0 ) { 0 0 0
459  
460 0 ▶ if( stack_size > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( stack_size > 0 ) { 0 0 0
461  
462 0 done = 1;
463 0 break;
464  
465 } else {
466  
467 0 free_stack_arr( stack, stack_size );
468 0 char * _s = malloc( strlen( E_STR_INVALID_SIGNATURE ) + strlen( s ) + 1 );
469 0 sprintf( _s, "%s%s", E_STR_INVALID_SIGNATURE, s );
470 0 p_die( _s );
471 }
472 }
473  
474 0 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if( 0 0 0
475 ( chr == '(' )
476 0 ▶ && ( brackets_state.inner_circle == 0 )
Branch: 1 branch
BranchTrueFalseTotal
&& ( brackets_state.inner_circle == 0 ) 0 0 0
477 0 ▶ && ( brackets_state.circle == 1 )
Branch: 1 branch
BranchTrueFalseTotal
&& ( brackets_state.circle == 1 ) 0 0 0
478 ) {
479 0 ▶ while( pos < length ) {
Branch: 1 branch
BranchTrueFalseTotal
while( pos < length ) { 0 0 0
480  
481 0 char subchr = s[ pos ++ ];
482  
483 0 ▶ if( !is_space( subchr ) && !is_delim( subchr ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if( !is_space( subchr ) && !is_delim( subchr ) ) { 0 0 0
if( !is_space( subchr ) && !is_delim( subchr ) ) { 0 0 0
484  
485 0 --pos;
486 0 break;
487 }
488 }
489  
490 0 continue;
491 }
492  
493 0 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if( 0 0 0
494 0 ▶ ( is_space( chr ) || is_delim( chr ) )
Branch: 1 branch
BranchTrueFalseTotal
( is_space( chr ) || is_delim( chr ) ) 0 0 0
495 0 ▶ && ! has_open_inner_brackets( &brackets_state )
Branch: 1 branch
BranchTrueFalseTotal
&& ! has_open_inner_brackets( &brackets_state ) 0 0 0
496 ) {
497 0 ▶ while( pos < length ) {
Branch: 1 branch
BranchTrueFalseTotal
while( pos < length ) { 0 0 0
498  
499 0 char subchr = s[ pos ++ ];
500  
501 0 ▶ if( !is_space( subchr ) && !is_delim( subchr ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if( !is_space( subchr ) && !is_delim( subchr ) ) { 0 0 0
if( !is_space( subchr ) && !is_delim( subchr ) ) { 0 0 0
502  
503 0 --pos;
504 0 break;
505 }
506 }
507  
508 0 break;
509 }
510  
511 0 append( &type, chr );
512 }
513  
514 0 ▶ if( done == 1 ) break;
Branch: 1 branch
BranchTrueFalseTotal
if( done == 1 ) break; 0 0 0
515  
516 0 ▶ if( strlen( type ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( strlen( type ) == 0 ) { 0 0 0
517  
518 0 free_stack_arr( stack, stack_size );
519 0 char * _s = malloc( strlen( E_STR_INVALID_TYPE_STRING_IN_SIGNATURE ) + strlen( s ) + 1 );
520 0 sprintf( _s, "%s%s", E_STR_INVALID_TYPE_STRING_IN_SIGNATURE, s );
521 0 p_die( _s );
522 }
523  
524 0 ▶ } else if( seq == SIG_SEQ_ITEM_NAME ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if( seq == SIG_SEQ_ITEM_NAME ) { 0 0 0
525  
526 0 ▶ while( pos < length ) {
Branch: 1 branch
BranchTrueFalseTotal
while( pos < length ) { 0 0 0
527  
528 0 char chr = s[ pos ++ ];
529  
530 0 update_brackets_state( &brackets_state, chr );
531  
532 0 ▶ if( is_space( chr ) || is_delim( chr ) || ( chr == ')' ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if( is_space( chr ) || is_delim( chr ) || ( chr == ')' ) ) { 0 0 0
if( is_space( chr ) || is_delim( chr ) || ( chr == ')' ) ) { 0 0 0
if( is_space( chr ) || is_delim( chr ) || ( chr == ')' ) ) { 0 0 0
533  
534 0 ▶ while( pos < length ) {
Branch: 1 branch
BranchTrueFalseTotal
while( pos < length ) { 0 0 0
535  
536 0 char subchr = s[ pos ++ ];
537  
538 0 ▶ if( !is_space( subchr ) && !is_delim( subchr ) && ( chr != ')' ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if( !is_space( subchr ) && !is_delim( subchr ) && ( chr != ')' ) ) { 0 0 0
if( !is_space( subchr ) && !is_delim( subchr ) && ( chr != ')' ) ) { 0 0 0
if( !is_space( subchr ) && !is_delim( subchr ) && ( chr != ')' ) ) { 0 0 0
539  
540 0 --pos;
541 0 break;
542 }
543 }
544  
545 0 break;
546 }
547  
548 0 append( &name, chr );
549 }
550  
551 0 ▶ if( strlen( name ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( strlen( name ) == 0 ) { 0 0 0
552  
553 0 free_stack_arr( stack, stack_size );
554 0 char * _s = malloc( strlen( E_STR_INVALID_PARAMETER_NAME_IN_SIGNATURE ) + strlen( s ) + 1 );
555 0 sprintf( _s, "%s%s", E_STR_INVALID_PARAMETER_NAME_IN_SIGNATURE, s );
556 0 p_die( _s );
557 }
558  
559 0 ▶ } else if( seq == SIG_SEQ_ITEM_DELIM ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if( seq == SIG_SEQ_ITEM_DELIM ) { 0 0 0
560  
561 0 ▶ if( ( strlen( type ) == 0 ) || ( strlen( name ) == 0 ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if( ( strlen( type ) == 0 ) || ( strlen( name ) == 0 ) ) { 0 0 0
if( ( strlen( type ) == 0 ) || ( strlen( name ) == 0 ) ) { 0 0 0
562  
563 0 free_stack_arr( stack, stack_size );
564 0 char * _s = malloc( strlen( E_STR_TYPE_OR_PARAMETER_NAME_MISSING_IN_SIGNATURE ) + strlen( s ) + 1 );
565 0 sprintf( _s, "%s%s", E_STR_TYPE_OR_PARAMETER_NAME_MISSING_IN_SIGNATURE, s );
566 0 p_die( _s );
567 }
568  
569 0 signature_item_t * token = malloc( sizeof( *token ) );
570  
571 0 token -> base.token_type = TOKEN_TYPE_SIGNATURE_ITEM;
572  
573 0 my_stack_t * _type = tokenize_type_str( class, type, options );
574 0 ▶ if( _type == 0 ) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if( _type == 0 ) return 0; 0 0 0
575 0 token -> type = _type;
576  
577 0 free( type );
578 0 type = zero_str();
579  
580 0 signature_param_t * _param = tokenize_signature_parameter_str( name, options );
581 0 ▶ if( _param == 0 ) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if( _param == 0 ) return 0; 0 0 0
582 0 token -> param = _param;
583  
584 0 free( name );
585 0 name = zero_str();
586  
587 0 push_stack( &stack, stack_size, (intptr_t)token );
588 0 ++stack_size;
589  
590 0 ▶ if( pos >= length ) break;
Branch: 1 branch
BranchTrueFalseTotal
if( pos >= length ) break; 0 0 0
591 }
592  
593 0 ▶ if( ++seq > SIG_SEQ_MAX ) seq = SIG_SEQ_MIN;
Branch: 1 branch
BranchTrueFalseTotal
if( ++seq > SIG_SEQ_MAX ) seq = SIG_SEQ_MIN; 0 0 0
594 }
595  
596 0 ▶ if( ( brackets_state.circle != 0 ) || has_open_inner_brackets( &brackets_state ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if( ( brackets_state.circle != 0 ) || has_open_inner_brackets( &brackets_state ) ) { 0 0 0
if( ( brackets_state.circle != 0 ) || has_open_inner_brackets( &brackets_state ) ) { 0 0 0
597  
598 0 free_stack_arr( stack, stack_size );
599 0 char * _s = malloc( strlen( E_STR_UNEXPECTED_END_OF_INPUT_IN_SIGNATURE ) + strlen( s ) + 1 );
600 0 sprintf( _s, "%s%s", E_STR_UNEXPECTED_END_OF_INPUT_IN_SIGNATURE, s );
601 0 p_die( _s );
602 }
603  
604 0 free( type );
605 0 free( name );
606  
607 0 my_stack_t * out = malloc( sizeof( *out ) );
608  
609 0 out -> size = stack_size;
610 0 out -> data = stack;
611  
612 0 return out;
613 }
614  
615 0 static inline signature_param_t * tokenize_signature_parameter_str( const char * _s, tokenizer_options_t * options ) {
616  
617 0 char * s = copy_str( (char*)_s );
618 0 signature_param_t * token = malloc( sizeof( *token ) );
619  
620 0 char first_char = s[ 0 ];
621 0 int len = strlen( s );
622  
623 0 ▶ if( first_char == ':' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( first_char == ':' ) { 0 0 0
624  
625 0 token -> named = 1;
626 0 token -> positional = 0;
627  
628 0 --len;
629  
630 0 ▶ for( int i = 0; i < len; ++i ) s[ i ] = s[ i + 1 ];
Branch: 1 branch
BranchTrueFalseTotal
for( int i = 0; i < len; ++i ) s[ i ] = s[ i + 1 ]; 0 0 0
631  
632 0 s[ len ] = '\0';
633 0 s = realloc( s, len + 1 );
634  
635 } else {
636  
637 0 token -> named = 0;
638 0 token -> positional = 1;
639 }
640  
641 0 char last_char = s[ len - 1 ];
642  
643 0 ▶ if( last_char == '!' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( last_char == '!' ) { 0 0 0
644  
645 0 token -> required = 1;
646 0 token -> optional = 0;
647  
648 0 s[ len - 1 ] = '\0';
649 0 s = realloc( s, len );
650  
651 0 ▶ } else if( last_char == '?' ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if( last_char == '?' ) { 0 0 0
652  
653 0 token -> required = 0;
654 0 token -> optional = 1;
655  
656 0 s[ len - 1 ] = '\0';
657 0 s = realloc( s, len );
658  
659 0 ▶ } else if( token -> positional == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if( token -> positional == 1 ) { 0 0 0
660  
661 0 token -> required = 1;
662 0 token -> optional = 0;
663  
664 0 ▶ } else if( token -> named == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if( token -> named == 1 ) { 0 0 0
665  
666 0 token -> required = 0;
667 0 token -> optional = 1;
668 }
669  
670 0 token -> name = s;
671  
672 0 return token;
673 }
674  
675 0 AV * perl_tokenize_type_str( char * class, const char * s, HV * options ) {
676  
677 0 my_stack_t * stack = tokenize_type_str( class, s, perl_to_options( options ) );
678  
679 0 AV * out = mortalize_av( tokens_to_perl( stack ) );
680  
681 0 ▶ if( stack != 0 ) free_my_stack( stack );
Branch: 1 branch
BranchTrueFalseTotal
if( stack != 0 ) free_my_stack( stack ); 0 0 0
682  
683 0 return out;
684 }
685  
686 0 AV * perl_tokenize_signature_str( char * class, const char * s, HV * options ) {
687  
688 0 my_stack_t * stack = tokenize_signature_str( class, s, perl_to_options( options ) );
689  
690 0 AV * out = mortalize_av( tokens_to_perl( stack ) );
691  
692 0 ▶ if( stack != 0 ) free_my_stack( stack );
Branch: 1 branch
BranchTrueFalseTotal
if( stack != 0 ) free_my_stack( stack ); 0 0 0
693  
694 0 return out;
695 }
696  
697 #endif /* end of include guard: _TOKENIZER_C_ */
« helper.c ↑ Summary blib/lib/Salvation/TC/Type/Any.pm »