-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
1625 lines (1428 loc) · 70.8 KB
/
parser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// SprintTrace: element, property and statement parser
// Copyright 2022, Laminoid.com (Muessig & Muessig GbR).
// Licensed under the terms and conditions of the GPLv3.
//
#include "parser.h"
#include "primitives.h"
#include "elements.h"
#include "list.h"
#include "stringbuilder.h"
#include "token.h"
#include "errors.h"
#include <string.h>
#include <stdlib.h>
char* sprint_parser_statement_name(sprint_statement* statement)
{
return statement != NULL ? statement->name : NULL;
}
bool sprint_parser_statement_flags(sprint_statement* statement, bool any, sprint_statement_flags flags)
{
if (statement == NULL || flags < 1) return false;
return any ? (statement->flags & flags) > 0 : (statement->flags & flags) == flags;
}
int sprint_parser_statement_index(sprint_statement* statement)
{
return statement != NULL ? statement->index : 0;
}
sprint_error sprint_parser_statement_destroy(sprint_statement* statement)
{
if (statement == NULL) return SPRINT_ERROR_ARGUMENT_NULL;
// Free the name
if (statement->name != NULL) {
free(statement->name);
statement->name = NULL;
}
return SPRINT_ERROR_NONE;
}
sprint_parser* sprint_parser_create(sprint_tokenizer* tokenizer)
{
if (tokenizer == NULL || tokenizer->read == NULL) return NULL;
// Create the builder
sprint_stringbuilder* builder = sprint_stringbuilder_create(15);
if (builder == NULL)
return NULL;
// Create the parser
sprint_parser* parser = calloc(1, sizeof(*tokenizer));
if (parser == NULL) {
sprint_check(sprint_stringbuilder_destroy(builder));
return NULL;
}
// Assign tokenizer and builder
parser->tokenizer = tokenizer;
parser->builder = builder;
return parser;
}
sprint_error sprint_parser_token(sprint_parser* parser, sprint_token* token)
{
if (parser == NULL || token == NULL) return SPRINT_ERROR_ARGUMENT_NULL;
*token = parser->token;
return SPRINT_ERROR_NONE;
}
sprint_error sprint_parser_origin(sprint_parser* parser, sprint_source_origin* origin)
{
if (parser == NULL || origin == NULL) return SPRINT_ERROR_ARGUMENT_NULL;
*origin = parser->token.origin;
return SPRINT_ERROR_NONE;
}
static sprint_error sprint_token_unexpected_internal(sprint_parser* parser, bool warning)
{
sprint_token* token = &parser->token;
bool invalid = token->type == SPRINT_TOKEN_TYPE_INVALID;
sprint_stringbuilder* builder = sprint_stringbuilder_of(invalid ? "invalid" : "unexpected");
if (builder == NULL)
return SPRINT_ERROR_MEMORY;
// Assemble the string further
sprint_error error = SPRINT_ERROR_NONE;
sprint_chain(error, sprint_stringbuilder_put_str(builder, " token "));
sprint_chain(error, sprint_stringbuilder_put_str(builder, warning ? "skipped" : "hit"));
sprint_chain(error, sprint_stringbuilder_put_str(builder, " at "));
if (token->origin.source != NULL)
sprint_chain(error, sprint_stringbuilder_format(builder, "%s:", token->origin.source));
sprint_chain(error, sprint_stringbuilder_format(builder, "%d:%d", token->origin.line, token->origin.pos));
// Append the contents
char* contents = NULL;
if (sprint_check(sprint_token_contents(token, parser->builder, &contents)) && contents != NULL)
sprint_chain(error, sprint_stringbuilder_format(builder, ": %s", contents));
// Complete the builder
contents = sprint_stringbuilder_complete(builder);
if (error != SPRINT_ERROR_NONE)
return sprint_rethrow(error);
if (contents == NULL)
return SPRINT_ERROR_INTERNAL;
// Emit the warning or error
if (warning)
sprint_warning(contents);
else
sprint_throw(false, contents);
return SPRINT_ERROR_NONE;
}
sprint_error sprint_parser_next_statement(sprint_parser* parser, sprint_statement* statement, bool sync)
{
if (parser == NULL || statement == NULL) return SPRINT_ERROR_ARGUMENT_NULL;
// Keep seeking until a token is found
sprint_error error = SPRINT_ERROR_NONE;
bool skipped = false;
while (true) {
// Get the first or next token
sprint_token* token = &parser->token;
if (error == SPRINT_ERROR_NONE)
error = sprint_tokenizer_next(parser->tokenizer, token, parser->builder);
if (error == SPRINT_ERROR_EOF || !sprint_check(error))
return sprint_rethrow(error);
// Raise the subsequent flag, if not a word
parser->subsequent |= token->type != SPRINT_TOKEN_TYPE_WORD;
// Backup and update the value flag
bool last_value = parser->value;
parser->value = token->type == SPRINT_TOKEN_TYPE_VALUE_SEPARATOR;
// Check the token type
switch (token->type) {
case SPRINT_TOKEN_TYPE_WORD:
// If the word is part of a value, it is not valid, so decide whether to throw or skip
if (last_value)
break;
// Clear the statement
memset(statement, 0, sizeof(*statement));
// Now that a word was found, set the first flag if applicable and store the name
if (!parser->subsequent)
statement->flags |= SPRINT_STATEMENT_FLAG_FIRST;
if (!sprint_chain(error, sprint_token_word(token, parser->builder, &statement->name)))
return sprint_rethrow(error);
// Set the name flag
statement->flags |= SPRINT_STATEMENT_FLAG_NAME;
// Get the next token, determine its type and reset the subsequent flag
if (error == SPRINT_ERROR_NONE)
error = sprint_tokenizer_next(parser->tokenizer, token, parser->builder);
if (error == SPRINT_ERROR_EOF || !sprint_check(error)) {
sprint_check(sprint_parser_statement_destroy(statement));
return sprint_rethrow(error);
}
parser->subsequent = true;
switch (token->type) {
case SPRINT_TOKEN_TYPE_VALUE_SEPARATOR:
// Set the value flags and return success
parser->value = true;
statement->flags |= SPRINT_STATEMENT_FLAG_VALUE;
return SPRINT_ERROR_NONE;
case SPRINT_TOKEN_TYPE_STATEMENT_TERMINATOR:
// Clear the subsequent flag for the next word and set the last flag
parser->subsequent = false;
statement->flags |= SPRINT_STATEMENT_FLAG_LAST;
// fallthrough
case SPRINT_TOKEN_TYPE_STATEMENT_SEPARATOR:
// Clear the value flag
parser->value = false;
// At this point, a valid statement was found
return SPRINT_ERROR_NONE;
case SPRINT_TOKEN_TYPE_NUMBER:
// Store the index, set the flag and continue reading
if (!sprint_chain(error, sprint_token_int(token, parser->builder, &statement->index))) {
sprint_check(sprint_parser_statement_destroy(statement));
return sprint_rethrow(error);
}
statement->flags |= SPRINT_STATEMENT_FLAG_INDEX;
break;
default:
// The token is unexpected or invalid
sprint_check(sprint_parser_statement_destroy(statement));
sprint_check(sprint_token_unexpected_internal(parser, sync));
if (sync)
continue;
return SPRINT_ERROR_SYNTAX;
}
// Get the next token, make sure it is a value separator and reset the subsequent flag
if (error == SPRINT_ERROR_NONE)
error = sprint_tokenizer_next(parser->tokenizer, token, parser->builder);
if (error == SPRINT_ERROR_EOF || !sprint_check(error)) {
sprint_check(sprint_parser_statement_destroy(statement));
return sprint_rethrow(error);
}
parser->subsequent = true;
switch (token->type) {
case SPRINT_TOKEN_TYPE_VALUE_SEPARATOR:
// If there was a value separator already, fail
if (sprint_parser_statement_flags(statement, false, SPRINT_STATEMENT_FLAG_VALUE))
break;
// Set the value flags and return that a valid statement was found
parser->value = true;
statement->flags |= SPRINT_STATEMENT_FLAG_VALUE;
return SPRINT_ERROR_NONE;
case SPRINT_TOKEN_TYPE_STATEMENT_TERMINATOR:
// Clear the subsequent flag for the next word and set the last flag
parser->subsequent = false;
statement->flags |= SPRINT_STATEMENT_FLAG_LAST;
// fallthrough
case SPRINT_TOKEN_TYPE_STATEMENT_SEPARATOR:
// Clear the value flag
parser->value = false;
// fallthrough
default:
// Fall through to an error
break;
}
// The token is unexpected or invalid
sprint_check(sprint_parser_statement_destroy(statement));
sprint_check(sprint_token_unexpected_internal(parser, sync));
if (token->type == SPRINT_TOKEN_TYPE_STATEMENT_TERMINATOR)
return SPRINT_ERROR_EOS;
if (sync)
continue;
return SPRINT_ERROR_SYNTAX;
case SPRINT_TOKEN_TYPE_STATEMENT_TERMINATOR:
// Clear the subsequent flag for the next word and return an EOS error
parser->subsequent = false;
return SPRINT_ERROR_EOS;
case SPRINT_TOKEN_TYPE_STATEMENT_SEPARATOR:
// If there initially is a separator or terminator, skip it in any mode
skipped = !skipped;
if (skipped)
continue;
// fallthrough
case SPRINT_TOKEN_TYPE_NUMBER:
case SPRINT_TOKEN_TYPE_STRING:
case SPRINT_TOKEN_TYPE_VALUE_SEPARATOR:
case SPRINT_TOKEN_TYPE_TUPLE_SEPARATOR:
// Go to the end of the switch block and decide whether to throw or just skip
break;
case SPRINT_TOKEN_TYPE_NONE:
case SPRINT_TOKEN_TYPE_INVALID:
default:
// The token is unexpected or invalid, so throw a syntax error
sprint_check(sprint_token_unexpected_internal(parser, sync));
// If syncing, skip over the error
if (sync)
continue;
// Otherwise, return with a syntax error
return SPRINT_ERROR_SYNTAX;
}
// If syncing, just skip silently
if (sync)
continue;
// Otherwise, throw and return a syntax error
sprint_check(sprint_token_unexpected_internal(parser, false));
return SPRINT_ERROR_SYNTAX;
}
}
static sprint_error sprint_parser_next_internal(sprint_parser* parser, sprint_token_type type)
{
// Get the next token and reset the subsequent flag
sprint_token* token = &parser->token;
sprint_error error = sprint_tokenizer_next(parser->tokenizer, token, parser->builder);
if (error == SPRINT_ERROR_EOF || !sprint_check(error))
return sprint_rethrow(error);
parser->subsequent = true;
// Handle the special conditions
switch (token->type) {
case SPRINT_TOKEN_TYPE_VALUE_SEPARATOR:
// Set the value flag
parser->value = true;
break;
case SPRINT_TOKEN_TYPE_STATEMENT_TERMINATOR:
// Clear the subsequent flag for the next word
parser->subsequent = false;
// fallthrough
case SPRINT_TOKEN_TYPE_STATEMENT_SEPARATOR:
// Clear the value flag
parser->value = false;
break;
default:
// If the token type matches, it succeeded
if (token->type == type)
return SPRINT_ERROR_NONE;
// Otherwise, fall through to a syntax error
break;
}
// Throw the syntax error
sprint_token_unexpected_internal(parser, false);
return SPRINT_ERROR_SYNTAX;
}
sprint_error sprint_parser_next_bool(sprint_parser* parser, bool* val)
{
if (parser == NULL || val == NULL) return SPRINT_ERROR_ARGUMENT_NULL;
sprint_error error = SPRINT_ERROR_NONE;
sprint_chain(error, sprint_parser_next_internal(parser, SPRINT_TOKEN_TYPE_WORD));
sprint_chain(error, sprint_token_bool(&parser->token, parser->builder, val));
return sprint_rethrow(error);
}
sprint_error sprint_parser_next_int(sprint_parser* parser, int* val)
{
if (parser == NULL || val == NULL) return SPRINT_ERROR_ARGUMENT_NULL;
sprint_error error = SPRINT_ERROR_NONE;
sprint_chain(error, sprint_parser_next_internal(parser, SPRINT_TOKEN_TYPE_NUMBER));
sprint_chain(error, sprint_token_int(&parser->token, parser->builder, val));
return sprint_rethrow(error);
}
sprint_error sprint_parser_next_dist(sprint_parser* parser, sprint_dist* dist)
{
sprint_error error = SPRINT_ERROR_NONE;
if (sprint_chain(error, sprint_parser_next_int(parser, dist)) && !sprint_dist_valid(*dist))
return SPRINT_ERROR_SYNTAX;
return error;
}
sprint_error sprint_parser_next_size(sprint_parser* parser, sprint_dist* size)
{
sprint_error error = SPRINT_ERROR_NONE;
if (sprint_chain(error, sprint_parser_next_int(parser, size)) && !sprint_size_valid(*size))
return SPRINT_ERROR_SYNTAX;
return error;
}
sprint_error sprint_parser_next_angle(sprint_parser* parser, sprint_angle* angle, sprint_prim_format format)
{
sprint_error error = SPRINT_ERROR_NONE;
if (!sprint_chain(error, sprint_parser_next_int(parser, angle)))
return error;
// Handle the different raw precisions
sprint_angle factor = sprint_angle_factor(format);
if (factor < 1)
return SPRINT_ERROR_ARGUMENT_RANGE;
// Scale the angle
*angle *= factor;
// And make sure that it is valid
return sprint_angle_valid(*angle) ? SPRINT_ERROR_NONE : SPRINT_ERROR_SYNTAX;
}
sprint_error sprint_parser_next_tuple(sprint_parser* parser, sprint_tuple* tuple)
{
if (parser == NULL || tuple == NULL) return SPRINT_ERROR_ARGUMENT_NULL;
// Read the x-component
sprint_error error = SPRINT_ERROR_NONE;
sprint_dist dist_x = 0;
sprint_chain(error, sprint_parser_next_dist(parser, &dist_x));
// Match the tuple separator
sprint_chain(error, sprint_parser_next_internal(parser, SPRINT_TOKEN_TYPE_TUPLE_SEPARATOR));
// Read the y-component
sprint_dist dist_y = 0;
sprint_chain(error, sprint_parser_next_dist(parser, &dist_y));
// Create the tuple and return
*tuple = sprint_tuple_of(dist_x, dist_y);
return sprint_rethrow(error);
}
sprint_error sprint_parser_next_str(sprint_parser* parser, char** str)
{
if (parser == NULL || str == NULL) return SPRINT_ERROR_ARGUMENT_NULL;
sprint_error error = SPRINT_ERROR_NONE;
sprint_chain(error, sprint_parser_next_internal(parser, SPRINT_TOKEN_TYPE_STRING));
sprint_chain(error, sprint_token_str(&parser->token, parser->builder, str));
return sprint_rethrow(error);
}
static sprint_error sprint_parser_next_uint(sprint_parser* parser, int* val)
{
sprint_error error = SPRINT_ERROR_NONE;
if (sprint_chain(error, sprint_parser_next_int(parser, val)) && *val < 0)
return SPRINT_ERROR_SYNTAX;
return error;
}
static sprint_error sprint_parser_next_layer(sprint_parser* parser, sprint_layer* layer)
{
sprint_error error = SPRINT_ERROR_NONE;
if (sprint_chain(error, sprint_parser_next_int(parser, (int*) layer)) && !sprint_layer_valid(*layer))
return SPRINT_ERROR_SYNTAX;
return error;
}
static sprint_error sprint_parser_next_tht_form(sprint_parser* parser, sprint_pad_tht_form* form)
{
sprint_error error = SPRINT_ERROR_NONE;
if (sprint_chain(error, sprint_parser_next_int(parser, (int*) form)) && !sprint_pad_tht_form_valid(*form))
return SPRINT_ERROR_SYNTAX;
return error;
}
static sprint_error sprint_parser_next_text_style(sprint_parser* parser, sprint_text_style* style)
{
sprint_error error = SPRINT_ERROR_NONE;
if (sprint_chain(error, sprint_parser_next_int(parser, (int*) style)) && !sprint_text_style_valid(*style))
return SPRINT_ERROR_SYNTAX;
return error;
}
static sprint_error sprint_parser_next_text_thickness(sprint_parser* parser, sprint_text_thickness* thickness)
{
sprint_error error = SPRINT_ERROR_NONE;
if (sprint_chain(error, sprint_parser_next_int(parser, (int*) thickness)) &&
!sprint_text_thickness_valid(*thickness))
return SPRINT_ERROR_SYNTAX;
return error;
}
static sprint_error sprint_parser_next_value_internal(sprint_parser* parser, sprint_statement* statement, bool* salvaged)
{
// Keep reading until a valid statement or a terminator is found
while (parser->subsequent) {
// Read the next statement
sprint_error error = sprint_parser_next_statement(parser, statement, *salvaged);
if (error == SPRINT_ERROR_SYNTAX) {
// Destroy the statement
sprint_check(sprint_parser_statement_destroy(statement));
// Set the salvaged flag, emit a warning and skip this statement
*salvaged = true;
sprint_check(sprint_token_unexpected_internal(parser, true));
continue;
} else if (error == SPRINT_ERROR_EOS || !sprint_check(error))
return sprint_rethrow(error);
// Make sure the flags are valid
const sprint_statement_flags required_flags = SPRINT_STATEMENT_FLAG_NAME | SPRINT_STATEMENT_FLAG_VALUE;
if (sprint_parser_statement_flags(statement, false, required_flags))
return SPRINT_ERROR_NONE;
// Destroy the statement
sprint_check(sprint_parser_statement_destroy(statement));
// If the last flag is present, stop processing
if (sprint_parser_statement_flags(statement, true, SPRINT_STATEMENT_FLAG_LAST))
break;
// Set the salvaged flag, emit a warning and skip this statement
*salvaged = true;
sprint_check(sprint_token_unexpected_internal(parser, true));
}
// Return an end of statement error
return SPRINT_ERROR_EOS;
}
static sprint_error sprint_parser_next_track_internal(sprint_parser* parser, sprint_element* element, bool* salvaged)
{
// Initialize the element
sprint_error error = SPRINT_ERROR_NONE;
if (!sprint_chain(error, sprint_track_default(element, true)))
return sprint_rethrow(error);
element->parsed = true;
// Keep track of found properties
bool found_layer = false, found_width = false, found_clear = false, found_cutout = false, found_soldermask = false,
found_flat_start = false, found_flat_end = false, found_name = false;
// Keep a list of points
sprint_list* list = sprint_list_create(sizeof(*element->track.points), 16);
if (list == NULL)
return SPRINT_ERROR_MEMORY;
// Read all element properties
sprint_statement statement;
while (parser->subsequent) {
// Read the next value statement
error = sprint_parser_next_value_internal(parser, &statement, salvaged);
if (error == SPRINT_ERROR_EOS) {
error = SPRINT_ERROR_NONE;
break;
}
if (!sprint_check(error)) {
sprint_check(sprint_list_destroy(list));
return sprint_rethrow(error);
}
// Determine the statement name
bool already_found = false;
if (strcasecmp(statement.name, "P") == 0) {
sprint_tuple tuple;
if (sprint_parser_statement_flags(&statement, true, SPRINT_STATEMENT_FLAG_INDEX) &&
sprint_parser_statement_index(&statement) == sprint_list_count(list) &&
sprint_chain(error, sprint_parser_next_tuple(parser, &tuple)))
sprint_chain(error, sprint_list_add(list, &tuple));
} else if (sprint_parser_statement_flags(&statement, true, SPRINT_STATEMENT_FLAG_INDEX)) {
error = SPRINT_ERROR_SYNTAX;
sprint_throw_format(false, "unexpected property index: %s%d", statement.name, statement.index);
} else if (strcasecmp(statement.name, "LAYER") == 0) {
if (found_layer)
already_found = true;
found_layer |= sprint_chain(error, sprint_parser_next_layer(parser, &element->track.layer));
} else if (strcasecmp(statement.name, "WIDTH") == 0) {
if (found_width)
already_found = true;
found_width |= sprint_chain(error, sprint_parser_next_size(parser, &element->track.width));
} else if (strcasecmp(statement.name, "CLEAR") == 0) {
if (found_clear)
already_found = true;
found_clear |= sprint_chain(error, sprint_parser_next_size(parser, &element->track.clear));
} else if (strcasecmp(statement.name, "CUTOUT") == 0) {
if (found_cutout)
already_found = true;
found_cutout |= sprint_chain(error, sprint_parser_next_bool(parser, &element->track.cutout));
} else if (strcasecmp(statement.name, "SOLDERMASK") == 0) {
if (found_soldermask)
already_found = true;
found_soldermask |= sprint_chain(error, sprint_parser_next_bool(parser, &element->track.soldermask));
} else if (strcasecmp(statement.name, "FLATSTART") == 0) {
if (found_flat_start)
already_found = true;
found_flat_start |= sprint_chain(error, sprint_parser_next_bool(parser, &element->track.flat_start));
} else if (strcasecmp(statement.name, "FLATEND") == 0) {
if (found_flat_end)
already_found = true;
found_flat_end |= sprint_chain(error, sprint_parser_next_bool(parser, &element->track.flat_end));
} else if (strcasecmp(statement.name, "NAME") == 0) {
if (found_name)
already_found = true;
found_name |= sprint_chain(error, sprint_parser_next_str(parser, &element->track.name));
} else {
error = SPRINT_ERROR_SYNTAX;
sprint_throw_format(false, "unknown property: %s", statement.name);
}
// Handle already found properties
if (already_found)
sprint_warning_format("overwriting duplicate property: %s", statement.name);
// Destroy the statement
sprint_check(sprint_parser_statement_destroy(&statement));
// Handle syntax errors by enabling salvaged mode and ignoring the property
if (error == SPRINT_ERROR_SYNTAX) {
sprint_check(sprint_token_unexpected_internal(parser, false));
*salvaged = true;
continue;
}
// All other errors stop processing
if (error != SPRINT_ERROR_NONE) {
sprint_check(sprint_list_destroy(list));
return sprint_rethrow(error);
}
}
// Make sure that there are at least two points and all properties
if (!found_layer | !found_width | sprint_list_count(list) < 2) {
sprint_throw_format(false, "incomplete element: %s", sprint_element_type_to_keyword(SPRINT_ELEMENT_TRACK, false));
error = SPRINT_ERROR_SYNTAX;
}
// Complete the list and check verify full validity
if (sprint_chain(error, sprint_list_complete(list, &element->track.num_points, (void*) &element->track.points)) &&
!sprint_assert(false, sprint_track_valid(&element->track)))
error = SPRINT_ERROR_ASSERTION;
return sprint_rethrow(error);
}
static sprint_error sprint_parser_next_pad_tht_internal(sprint_parser* parser, sprint_element* element, bool* salvaged)
{
// Initialize the element
sprint_error error = SPRINT_ERROR_NONE;
if (!sprint_chain(error, sprint_pad_tht_default(element, true)))
return sprint_rethrow(error);
element->parsed = true;
// Keep track of found properties
bool found_layer = false, found_position = false, found_size = false, found_drill = false, found_form = false,
found_id = false, found_clear = false, found_soldermask = false, found_rotation = false, found_via = false,
found_thermal = false, found_thermal_tracks = false, found_thermal_tracks_width = false,
found_thermal_tracks_individual = false, found_name = false;
// Keep a list of connections
sprint_list* list = sprint_list_create(sizeof(*element->pad_tht.link.connections), 8);
if (list == NULL)
return SPRINT_ERROR_MEMORY;
// Read all element properties
sprint_statement statement;
while (parser->subsequent) {
// Read the next value statement
error = sprint_parser_next_value_internal(parser, &statement, salvaged);
if (error == SPRINT_ERROR_EOS) {
error = SPRINT_ERROR_NONE;
break;
}
if (!sprint_check(error)) {
sprint_check(sprint_list_destroy(list));
return sprint_rethrow(error);
}
// Determine the statement name
bool already_found = false;
if (strcasecmp(statement.name, "CON") == 0) {
int id = 0;
if (sprint_parser_statement_flags(&statement, true, SPRINT_STATEMENT_FLAG_INDEX) &&
sprint_parser_statement_index(&statement) == sprint_list_count(list) &&
sprint_chain(error, sprint_parser_next_uint(parser, &id)))
sprint_chain(error, sprint_list_add(list, &id));
} else if (sprint_parser_statement_flags(&statement, true, SPRINT_STATEMENT_FLAG_INDEX)) {
error = SPRINT_ERROR_SYNTAX;
sprint_throw_format(false, "unexpected property index: %s%d", statement.name, statement.index);
} else if (strcasecmp(statement.name, "LAYER") == 0) {
if (found_layer)
already_found = true;
found_layer |= sprint_chain(error, sprint_parser_next_layer(parser, &element->pad_tht.layer));
} else if (strcasecmp(statement.name, "POS") == 0) {
if (found_position)
already_found = true;
found_position |= sprint_chain(error, sprint_parser_next_tuple(parser, &element->pad_tht.position));
} else if (strcasecmp(statement.name, "SIZE") == 0) {
if (found_size)
already_found = true;
found_size |= sprint_chain(error, sprint_parser_next_size(parser, &element->pad_tht.size));
} else if (strcasecmp(statement.name, "DRILL") == 0) {
if (found_drill)
already_found = true;
found_drill |= sprint_chain(error, sprint_parser_next_size(parser, &element->pad_tht.drill));
} else if (strcasecmp(statement.name, "FORM") == 0) {
if (found_form)
already_found = true;
found_form |= sprint_chain(error, sprint_parser_next_tht_form(parser, &element->pad_tht.form));
} else if (strcasecmp(statement.name, "PAD_ID") == 0) {
if (found_id)
already_found = true;
found_id |= sprint_chain(error, sprint_parser_next_uint(parser, &element->pad_tht.link.id));
element->pad_tht.link.has_id = found_id;
} else if (strcasecmp(statement.name, "CLEAR") == 0) {
if (found_clear)
already_found = true;
found_clear |= sprint_chain(error, sprint_parser_next_size(parser, &element->pad_tht.clear));
} else if (strcasecmp(statement.name, "SOLDERMASK") == 0) {
if (found_soldermask)
already_found = true;
found_soldermask |= sprint_chain(error, sprint_parser_next_bool(parser, &element->pad_tht.soldermask));
} else if (strcasecmp(statement.name, "ROTATION") == 0) {
if (found_rotation)
already_found = true;
found_rotation |= sprint_chain(error, sprint_parser_next_angle(parser, &element->pad_tht.rotation, SPRINT_PRIM_FORMAT_ANGLE_COARSE));
} else if (strcasecmp(statement.name, "VIA") == 0) {
if (found_via)
already_found = true;
found_via |= sprint_chain(error, sprint_parser_next_bool(parser, &element->pad_tht.via));
} else if (strcasecmp(statement.name, "THERMAL") == 0) {
if (found_thermal)
already_found = true;
found_thermal |= sprint_chain(error, sprint_parser_next_bool(parser, &element->pad_tht.thermal));
} else if (strcasecmp(statement.name, "THERMAL_TRACKS") == 0) {
if (found_thermal_tracks)
already_found = true;
found_thermal_tracks |= sprint_chain(error, sprint_parser_next_int(parser, &element->pad_tht.thermal_tracks));
} else if (strcasecmp(statement.name, "THERMAL_TRACKS_WIDTH") == 0) {
if (found_thermal_tracks_width)
already_found = true;
found_thermal_tracks_width |= sprint_chain(error, sprint_parser_next_uint(parser, &element->pad_tht.thermal_tracks_width));
} else if (strcasecmp(statement.name, "THERMAL_TRACKS_INDIVIDUAL") == 0) {
if (found_thermal_tracks_individual)
already_found = true;
found_thermal_tracks_individual |= sprint_chain(error, sprint_parser_next_bool(parser, &element->pad_tht.thermal_tracks_individual));
} else if (strcasecmp(statement.name, "NAME") == 0) {
if (found_name)
already_found = true;
found_name |= sprint_chain(error, sprint_parser_next_str(parser, &element->pad_tht.name));
} else {
error = SPRINT_ERROR_SYNTAX;
sprint_throw_format(false, "unknown property: %s", statement.name);
}
// Handle already found properties
if (already_found)
sprint_warning_format("overwriting duplicate property: %s", statement.name);
// Destroy the statement
sprint_check(sprint_parser_statement_destroy(&statement));
// Handle syntax errors by enabling salvaged mode and ignoring the property
if (error == SPRINT_ERROR_SYNTAX) {
sprint_check(sprint_token_unexpected_internal(parser, false));
*salvaged = true;
continue;
}
// All other errors stop processing
if (error != SPRINT_ERROR_NONE) {
sprint_check(sprint_list_destroy(list));
return sprint_rethrow(error);
}
}
// Make sure that there are all properties
if (!found_layer | !found_position | !found_size | !found_drill | !found_form |
(element->pad_tht.thermal & (!found_thermal_tracks | !found_thermal_tracks_width | !found_thermal_tracks_individual))) {
sprint_throw_format(false, "incomplete element: %s", sprint_element_type_to_keyword(SPRINT_ELEMENT_PAD_THT, false));
error = SPRINT_ERROR_SYNTAX;
}
// Complete the list and check verify full validity
if (sprint_chain(error, sprint_list_complete(list, &element->pad_tht.link.num_connections,
(void*) &element->pad_tht.link.connections)) &&
!sprint_assert(false, sprint_pad_tht_valid(&element->pad_tht)))
error = SPRINT_ERROR_ASSERTION;
return sprint_rethrow(error);
}
static sprint_error sprint_parser_next_pad_smt_internal(sprint_parser* parser, sprint_element* element, bool* salvaged)
{
// Initialize the element
sprint_error error = SPRINT_ERROR_NONE;
if (!sprint_chain(error, sprint_pad_smt_default(element, true)))
return sprint_rethrow(error);
element->parsed = true;
// Keep track of found properties
bool found_layer = false, found_position = false, found_width = false, found_height = false, found_id = false,
found_clear = false, found_soldermask = false, found_rotation = false, found_thermal = false,
found_thermal_tracks = false, found_thermal_tracks_width = false, found_name = false;
// Keep a list of connections
sprint_list* list = sprint_list_create(sizeof(*element->pad_smt.link.connections), 8);
if (list == NULL)
return SPRINT_ERROR_MEMORY;
// Read all element properties
sprint_statement statement;
while (parser->subsequent) {
// Read the next value statement
error = sprint_parser_next_value_internal(parser, &statement, salvaged);
if (error == SPRINT_ERROR_EOS) {
error = SPRINT_ERROR_NONE;
break;
}
if (!sprint_check(error)) {
sprint_check(sprint_list_destroy(list));
return sprint_rethrow(error);
}
// Determine the statement name
bool already_found = false;
if (strcasecmp(statement.name, "CON") == 0) {
int id = 0;
if (sprint_parser_statement_flags(&statement, true, SPRINT_STATEMENT_FLAG_INDEX) &&
sprint_parser_statement_index(&statement) == sprint_list_count(list) &&
sprint_chain(error, sprint_parser_next_uint(parser, &id)))
sprint_chain(error, sprint_list_add(list, &id));
} else if (sprint_parser_statement_flags(&statement, true, SPRINT_STATEMENT_FLAG_INDEX)) {
error = SPRINT_ERROR_SYNTAX;
sprint_throw_format(false, "unexpected property index: %s%d", statement.name, statement.index);
} else if (strcasecmp(statement.name, "LAYER") == 0) {
if (found_layer)
already_found = true;
found_layer |= sprint_chain(error, sprint_parser_next_layer(parser, &element->pad_smt.layer));
} else if (strcasecmp(statement.name, "POS") == 0) {
if (found_position)
already_found = true;
found_position |= sprint_chain(error, sprint_parser_next_tuple(parser, &element->pad_smt.position));
} else if (strcasecmp(statement.name, "SIZE_X") == 0) {
if (found_width)
already_found = true;
found_width |= sprint_chain(error, sprint_parser_next_size(parser, &element->pad_smt.width));
} else if (strcasecmp(statement.name, "SIZE_Y") == 0) {
if (found_height)
already_found = true;
found_height |= sprint_chain(error, sprint_parser_next_size(parser, &element->pad_smt.height));
} else if (strcasecmp(statement.name, "PAD_ID") == 0) {
if (found_id)
already_found = true;
found_id |= sprint_chain(error, sprint_parser_next_uint(parser, &element->pad_smt.link.id));
element->pad_smt.link.has_id = found_id;
} else if (strcasecmp(statement.name, "CLEAR") == 0) {
if (found_clear)
already_found = true;
found_clear |= sprint_chain(error, sprint_parser_next_size(parser, &element->pad_smt.clear));
} else if (strcasecmp(statement.name, "SOLDERMASK") == 0) {
if (found_soldermask)
already_found = true;
found_soldermask |= sprint_chain(error, sprint_parser_next_bool(parser, &element->pad_smt.soldermask));
} else if (strcasecmp(statement.name, "ROTATION") == 0) {
if (found_rotation)
already_found = true;
found_rotation |= sprint_chain(error, sprint_parser_next_angle(parser, &element->pad_smt.rotation, SPRINT_PRIM_FORMAT_ANGLE_COARSE));
} else if (strcasecmp(statement.name, "THERMAL") == 0) {
if (found_thermal)
already_found = true;
found_thermal |= sprint_chain(error, sprint_parser_next_bool(parser, &element->pad_smt.thermal));
} else if (strcasecmp(statement.name, "THERMAL_TRACKS") == 0) {
if (found_thermal_tracks)
already_found = true;
found_thermal_tracks |= sprint_chain(error, sprint_parser_next_int(parser, &element->pad_smt.thermal_tracks));
} else if (strcasecmp(statement.name, "THERMAL_TRACKS_WIDTH") == 0) {
if (found_thermal_tracks_width)
already_found = true;
found_thermal_tracks_width |= sprint_chain(error, sprint_parser_next_uint(parser, &element->pad_smt.thermal_tracks_width));
} else if (strcasecmp(statement.name, "NAME") == 0) {
if (found_name)
already_found = true;
found_name |= sprint_chain(error, sprint_parser_next_str(parser, &element->pad_smt.name));
} else {
error = SPRINT_ERROR_SYNTAX;
sprint_throw_format(false, "unknown property: %s", statement.name);
}
// Handle already found properties
if (already_found)
sprint_warning_format("overwriting duplicate property: %s", statement.name);
// Destroy the statement
sprint_check(sprint_parser_statement_destroy(&statement));
// Handle syntax errors by enabling salvaged mode and ignoring the property
if (error == SPRINT_ERROR_SYNTAX) {
sprint_check(sprint_token_unexpected_internal(parser, false));
*salvaged = true;
continue;
}
// All other errors stop processing
if (error != SPRINT_ERROR_NONE) {
sprint_check(sprint_list_destroy(list));
return sprint_rethrow(error);
}
}
// Make sure that there are all properties
if (!found_layer | !found_position | !found_width | !found_height | (element->pad_smt.thermal & (!found_thermal_tracks | !found_thermal_tracks_width))) {
sprint_throw_format(false, "incomplete element: %s", sprint_element_type_to_keyword(SPRINT_ELEMENT_PAD_SMT, false));
error = SPRINT_ERROR_SYNTAX;
}
// Complete the list and check verify full validity
if (sprint_chain(error, sprint_list_complete(list, &element->pad_smt.link.num_connections,
(void*) &element->pad_smt.link.connections)) &&
!sprint_assert(false, sprint_pad_smt_valid(&element->pad_smt)))
error = SPRINT_ERROR_ASSERTION;
return sprint_rethrow(error);
}
static sprint_error sprint_parser_next_zone_internal(sprint_parser* parser, sprint_element* element, bool* salvaged)
{
// Initialize the element
sprint_error error = SPRINT_ERROR_NONE;
if (!sprint_chain(error, sprint_zone_default(element, true)))
return sprint_rethrow(error);
element->parsed = true;
// Keep track of found properties
bool found_layer = false, found_width = false, found_clear = false, found_cutout = false, found_soldermask = false,
found_soldermask_cutout = false, found_hatch = false, found_hatch_auto = false, found_hatch_width = false, found_name = false;
// Keep a list of points
sprint_list* list = sprint_list_create(sizeof(*element->zone.points), 16);
if (list == NULL)
return SPRINT_ERROR_MEMORY;
// Read all element properties
sprint_statement statement;
while (parser->subsequent) {
// Read the next value statement
error = sprint_parser_next_value_internal(parser, &statement, salvaged);
if (error == SPRINT_ERROR_EOS) {
error = SPRINT_ERROR_NONE;
break;
}
if (!sprint_check(error)) {
sprint_check(sprint_list_destroy(list));
return sprint_rethrow(error);
}
// Determine the statement name
bool already_found = false;
if (strcasecmp(statement.name, "P") == 0) {
sprint_tuple tuple;
if (sprint_parser_statement_flags(&statement, true, SPRINT_STATEMENT_FLAG_INDEX) &&
sprint_parser_statement_index(&statement) == sprint_list_count(list) &&
sprint_chain(error, sprint_parser_next_tuple(parser, &tuple)))
sprint_chain(error, sprint_list_add(list, &tuple));
} else if (sprint_parser_statement_flags(&statement, true, SPRINT_STATEMENT_FLAG_INDEX)) {
error = SPRINT_ERROR_SYNTAX;
sprint_throw_format(false, "unexpected property index: %s%d", statement.name, statement.index);
} else if (strcasecmp(statement.name, "LAYER") == 0) {
if (found_layer)
already_found = true;
found_layer |= sprint_chain(error, sprint_parser_next_layer(parser, &element->zone.layer));
} else if (strcasecmp(statement.name, "WIDTH") == 0) {
if (found_width)
already_found = true;
found_width |= sprint_chain(error, sprint_parser_next_size(parser, &element->zone.width));
} else if (strcasecmp(statement.name, "CLEAR") == 0) {
if (found_clear)
already_found = true;
found_clear |= sprint_chain(error, sprint_parser_next_size(parser, &element->zone.clear));
} else if (strcasecmp(statement.name, "CUTOUT") == 0) {
if (found_cutout)
already_found = true;
found_cutout |= sprint_chain(error, sprint_parser_next_bool(parser, &element->zone.cutout));
} else if (strcasecmp(statement.name, "SOLDERMASK") == 0) {
if (found_soldermask)
already_found = true;
found_soldermask |= sprint_chain(error, sprint_parser_next_bool(parser, &element->zone.soldermask));
} else if (strcasecmp(statement.name, "SOLDERMASK_CUTOUT") == 0) {
if (found_soldermask_cutout)
already_found = true;
found_soldermask_cutout |= sprint_chain(error, sprint_parser_next_bool(parser, &element->zone.soldermask_cutout));
} else if (strcasecmp(statement.name, "HATCH") == 0) {
if (found_hatch)
already_found = true;
found_hatch |= sprint_chain(error, sprint_parser_next_bool(parser, &element->zone.hatch));
} else if (strcasecmp(statement.name, "HATCH_AUTO") == 0) {
if (found_hatch_auto)
already_found = true;
found_hatch_auto |= sprint_chain(error, sprint_parser_next_bool(parser, &element->zone.hatch_auto));
} else if (strcasecmp(statement.name, "HATCH_WIDTH") == 0) {
if (found_hatch_width)
already_found = true;
found_hatch_width |= sprint_chain(error, sprint_parser_next_size(parser, &element->zone.hatch_width));
} else if (strcasecmp(statement.name, "NAME") == 0) {
if (found_name)
already_found = true;
found_name |= sprint_chain(error, sprint_parser_next_str(parser, &element->zone.name));
} else {
error = SPRINT_ERROR_SYNTAX;
sprint_throw_format(false, "unknown property: %s", statement.name);
}
// Handle already found properties
if (already_found)
sprint_warning_format("overwriting duplicate property: %s", statement.name);
// Destroy the statement
sprint_check(sprint_parser_statement_destroy(&statement));
// Handle syntax errors by enabling salvaged mode and ignoring the property
if (error == SPRINT_ERROR_SYNTAX) {
sprint_check(sprint_token_unexpected_internal(parser, false));
*salvaged = true;
continue;
}
// All other errors stop processing
if (error != SPRINT_ERROR_NONE) {
sprint_check(sprint_list_destroy(list));
return sprint_rethrow(error);
}
}
// Make sure that there are at least two points and all properties
if (!found_layer | !found_width | sprint_list_count(list) < 2 | element->zone.hatch & !element->zone.hatch_auto & !found_hatch_width) {
sprint_throw_format(false, "incomplete element: %s", sprint_element_type_to_keyword(SPRINT_ELEMENT_ZONE, false));
error = SPRINT_ERROR_SYNTAX;
}
// Complete the list and check verify full validity
if (sprint_chain(error, sprint_list_complete(list, &element->zone.num_points, (void*) &element->zone.points)) &&
!sprint_assert(false, sprint_zone_valid(&element->zone)))
error = SPRINT_ERROR_ASSERTION;
return sprint_rethrow(error);
}