-
Notifications
You must be signed in to change notification settings - Fork 0
/
inedn.c
1923 lines (1727 loc) · 56.2 KB
/
inedn.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
/******************************************************************************
This is IN's implementation of an EDN parser. Highly experimental!!!!
Built by changing the JSON parser. This is based on a "recursive descent"
parsing model.
Information was taken from the proposed RFC document:
[1] https://github.com/edn-format/edn
Information was taken from this source:
[2] https://learnxinyminutes.com/docs/edn/
The Wikipedia of the ASCII set was useful:
[3] https://en.wikipedia.org/wiki/ASCII
2023/12/28 IN <nompelis@nobelware.com> - Initial creation
2024/01/28 IN <nompelis@nobelware.com> - Last modification
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _DEBUG_
#include <unistd.h>
#endif
#include "inedn.h"
// -------------- helper functions --------------
//
// Function to convert from an ASCII "0xXX" representation to the
// "unsigned char" type known as "BYTE"
// (Uses a naive string parsing approach on purpose to be instructive.)
// (I really need to change this...)
//
BYTE inEDN_Convert_ASCIIbyte( const char* string )
{
BYTE b = 0;
unsigned int ip;
BYTE m,l;
char str[2];
sprintf( str, "%c", string[0] );
sscanf( str, "%x", &ip );
m = (BYTE) ip;
m = m << 4;
sprintf( str, "%c", string[1] );
sscanf( str, "%x", &ip );
l = (BYTE) ip;
b = m + l;
return b;
}
//
// Function to print the 8 bits of a "BYTE" type
//
void inEDN_Show_ByteBits( FILE *fp, BYTE b )
{
BYTE mask = 0x01; // (initial) 8-bit mask "0000 0001"
int n=0;
fprintf( fp, "Byte: 0x%.2x ", b );
fprintf( fp, "Bits: " );
for(n=7;n>=0;--n) {
fprintf( fp, "%d", (b >> n) & mask );
}
fprintf( fp, "\n" );
}
//
// Function to display a generic non-container object
//
int inEDN_UnrollList( struct inEDN_state_s* es, // forward declaration
struct inEDN_list_s *list, int depth );
int inEDN_UnrollMap( struct inEDN_state_s* es,
struct inEDN_map_s *map, int depth );
int inEDN_DisplayGeneric( struct inEDN_state_s* es,
union inEDN_type_u* u, int depth )
{
if( es == NULL || u == NULL ) return 1;
FILE *fp = es->fp;
if( u->top.type == inEDN_Type_Undef) {
fprintf( fp, " [Error] Item is of \"Undefined\" type \n" );
} else {
#ifdef _DEBUG_UNROLL_
fprintf( fp, " [INFO] Displaying \"%s\" (%d) at %p \n",
inEDN_Type_strings[ u->top.type ], u->top.type, u );
#endif
switch( u->top.type ) {
case inEDN_Type_Nil:
for(int n=0;n<depth;++n) printf( " " );
fprintf( fp, " nil \n" );
break;
case inEDN_Type_Bool:
for(int n=0;n<depth;++n) printf( " " );
{ struct inEDN_bool_s* p = (struct inEDN_bool_s*) u;
if( p->b ) fprintf( fp, " true \n" );
else fprintf( fp, " false \n" );
}
break;
case inEDN_Type_Int:
for(int n=0;n<depth;++n) printf( " " );
fprintf( fp, " %ld \n", ((struct inEDN_int_s*) u)->l );
break;
case inEDN_Type_Real:
for(int n=0;n<depth;++n) printf( " " );
fprintf( fp, " %19.12e \n", ((struct inEDN_real_s*) u)->d );
break;
case inEDN_Type_String:
for(int n=0;n<depth;++n) printf( " " );
fprintf( fp, " \"%s\" \n", ((struct inEDN_string_s*) u)->string );
break;
case inEDN_Type_Set:
inEDN_UnrollList( es, (struct inEDN_list_s*) u, depth+1 );
break;
case inEDN_Type_List:
inEDN_UnrollList( es, (struct inEDN_list_s*) u, depth+1 );
break;
case inEDN_Type_Vector:
inEDN_UnrollList( es, (struct inEDN_list_s*) u, depth+1 );
break;
case inEDN_Type_Map:
inEDN_UnrollMap( es, (struct inEDN_map_s*) u, depth+1 );
break;
case inEDN_Type_Symbol:
for(int n=0;n<depth;++n) printf( " " );
fprintf( fp, " %s \n", ((struct inEDN_symbol_s*) u)->string );
break;
case inEDN_Type_Keyword:
for(int n=0;n<depth;++n) printf( " " );
fprintf( fp, " %s \n", ((struct inEDN_keyword_s*) u)->string );
break;
default:
fprintf( fp, " This is in error! Unspecified type! \n" );
}
}
return 0;
}
//
// Function to unroll/list the contents a "Map" type (for diagnostics)
//
int inEDN_UnrollMap( struct inEDN_state_s* es,
struct inEDN_map_s *map, int depth )
{
if( es == NULL || map == NULL ) return 1;
FILE *fp = es->fp;
#ifdef _DEBUG_UNROLL_
fprintf( fp, " [INFO] Unrolling map at %p of size %ld \n",
map, map->num );
#endif
for(int n=0;n<depth;++n) printf( " " );
printf( " { \n" );
for(size_t n=0;n<map->num;++n) {
struct inEDN_pair_s *p = &( map->data[n] );
inEDN_DisplayGeneric( es, p->key, depth+1 );
inEDN_DisplayGeneric( es, p->value, depth+1 );
}
for(int n=0;n<depth;++n) printf( " " );
printf( " } \n" );
return 0;
}
//
// Function to unroll/list the contents a the "List/Vector/Set" objects
//
int inEDN_UnrollList( struct inEDN_state_s* es,
struct inEDN_list_s *list, int depth )
{
if( es == NULL || list == NULL ) return 1;
FILE *fp = es->fp;
if( list->type == inEDN_Type_List ) {
#ifdef _DEBUG_UNROLL_
fprintf( fp, " [INFO] Unrolling \"List\" at %p of size %ld \n",
list, list->num );
#endif
for(int n=0;n<depth;++n) printf( " " );
fprintf( fp, " ( \n" );
} else if( list->type == inEDN_Type_Vector ) {
#ifdef _DEBUG_UNROLL_
fprintf( fp, " [INFO] Unrolling \"Vector\" at %p of size %ld \n",
list, list->num );
#endif
for(int n=0;n<depth;++n) printf( " " );
fprintf( fp, " [ \n" );
} else if( list->type == inEDN_Type_Set ) {
#ifdef _DEBUG_UNROLL_
fprintf( fp, " [INFO] Unrolling \"Set\" at %p of size %ld \n",
list, list->num );
#endif
for(int n=0;n<depth;++n) printf( " " );
fprintf( fp, " #{ \n" );
} else {
fprintf( fp, " [Error] Item is not List/Vector/Set \n" );
return 1;
}
for(size_t n=0;n<list->num;++n) {
union inEDN_type_u *u = &( list->data[n] );
if( u->top.type == inEDN_Type_Undef) {
fprintf( fp, " [Error] Item is of \"Undefined\" type \n" );
} else {
// fprintf( fp, " [INFO] Displaying \"%s\" (%d) at %p \n",
// inEDN_Type_strings[ u->top.type ], u->top.type, u );
switch( u->top.type ) {
case inEDN_Type_Nil:
inEDN_DisplayGeneric( es, u, depth+1 );
break;
case inEDN_Type_Bool:
inEDN_DisplayGeneric( es, u, depth+1 );
break;
case inEDN_Type_Int:
inEDN_DisplayGeneric( es, u, depth+1 );
break;
case inEDN_Type_Real:
inEDN_DisplayGeneric( es, u, depth+1 );
break;
case inEDN_Type_String:
inEDN_DisplayGeneric( es, u, depth+1 );
break;
case inEDN_Type_Set:
inEDN_DisplayGeneric( es, u, depth+1 );
break;
case inEDN_Type_List:
inEDN_DisplayGeneric( es, u, depth+1 );
break;
case inEDN_Type_Vector:
inEDN_DisplayGeneric( es, u, depth+1 );
break;
case inEDN_Type_Map:
inEDN_DisplayGeneric( es, u, depth+1 );
break;
case inEDN_Type_Symbol:
inEDN_DisplayGeneric( es, u, depth+1 );
break;
case inEDN_Type_Keyword:
inEDN_DisplayGeneric( es, u, depth+1 );
break;
default:
fprintf( fp, " This is in error! Unspecified type! \n" );
}
}
}
for(int n=0;n<depth;++n) printf( " " );
if( list->type == inEDN_Type_List ) {
fprintf( fp, " ) \n" );
} else if( list->type == inEDN_Type_Vector ) {
fprintf( fp, " ] \n" );
} else if( list->type == inEDN_Type_Set ) {
fprintf( fp, " } \n" );
}
return 0;
}
//
// Function to unroll/list the contents a the "toplevel" object
//
int inEDN_UnrollToplevel( struct inEDN_state_s* es,
struct inEDN_toplevel_s *top )
{
if( es == NULL || top == NULL ) return 1;
FILE *fp = es->fp;
fprintf( fp, " [INFO] Unrolling \"Toplevel\" at %p of size %ld \n",
top, top->num );
for(size_t n=0;n<top->num;++n) {
union inEDN_type_u* u = (union inEDN_type_u*) &( top->array[n] );
inEDN_DisplayGeneric( es, u, 0 );
}
return 0;
}
// -------------- parsing functions --------------
int inEDN_ClassifySpecial( struct inEDN_state_s* es )
{
if( es == NULL ) {
fprintf( stdout, "Error: null EDN pointer \n");
return 1;
}
FILE *fp = es->fp;
#ifdef _DEBUG_
if(fp) fprintf( fp, " [DEBUG] Classifying __Special__ \n" );
#endif
int ierr=0;
const char* ptr = es->ptr;
unsigned char ic=0xff,n=0;
#ifdef _DEBUG_SPECIAL2_
if(fp) fprintf( fp, " [DEBUG] Starting pointer at -->%c<-- \n", ptr[0] );
#endif
while( ierr == 0 ) {
#ifdef _DEBUG_SPECIAL3_
if(fp) fprintf( fp, " [DEBUG] Character: \"%c\" \n", ptr[n] );
#endif
if( ic == 0xff ) {
if( ptr[n] == 'n' || ptr[n] == 'N' ) ic=2;
if( ptr[n] == 'f' || ptr[n] == 'F' ) ic=1;
if( ptr[n] == 't' || ptr[n] == 't' ) ic=0;
if( ic == -1 ) ierr=101; // unexpected character
} else if( ic == 0 ) {
switch( n ) {
case 1:
if( ptr[n] != 'r' && ptr[n] != 'R' ) ierr = 101;
break;
case 2:
if( ptr[n] != 'u' && ptr[n] != 'U' ) ierr = 101;
break;
case 3:
if( ptr[n] != 'e' && ptr[n] != 'E' ) ierr = 101; else ierr=100;
break;
default:
ierr=101;
}
} else if( ic == 1 ) {
switch( n ) {
case 1:
if( ptr[n] != 'a' && ptr[n] != 'A' ) ierr = 101;
break;
case 2:
if( ptr[n] != 'l' && ptr[n] != 'L' ) ierr = 101;
break;
case 3:
if( ptr[n] != 's' && ptr[n] != 'S' ) ierr = 101;
break;
case 4:
if( ptr[n] != 'e' && ptr[n] != 'E' ) ierr = 101; else ierr=100;
break;
default:
ierr=101;
}
} else if( ic == 2 ) {
switch( n ) {
case 1:
if( ptr[n] != 'i' && ptr[n] != 'I' ) ierr = 101;
break;
case 2:
if( ptr[n] != 'l' && ptr[n] != 'L' ) ierr = 101; else ierr=100;
break;
default:
ierr=101;
}
}
// over-write errors in case of truncation
if( ptr[0] == '\0' ) ierr=102; // detect truncated file
++n;
}
if( ierr == 100 ) {
if( ic == 0 ) ierr=5001;
else if( ic == 1 ) ierr=5002;
else if( ic == 2 ) ierr=5003;
}
if( ierr == 101 ) {
if(fp) fprintf( fp, " [Error] Token is garbled \n" );
ierr=9999;
ic=0xff;
}
if( ierr == 102 ) {
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG] Detected truncated stream \n" );
#endif
ierr=1000;
ic=0xff;
}
#ifdef _DEBUG_
if(fp) fprintf( fp, " [DEBUG] Classified __Special__ (%d) \n", ic );
#endif
return ierr;
}
//
// Function to classify a segment based on its initial byte
//
int inEDN_ClassifyToken( struct inEDN_state_s *es, char c )
{
if( es == NULL ) return 1;
FILE *fp = es->fp;
int iret=1;
if( c == '\0' ) {
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG2] Detected nullchar \n" );
#endif
iret = 1000;
} else if( c == ';' ) {
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG2] Detected \"Comment\"token \n" );
#endif
iret = 1001;
} else if( c == ' ' || c == '\t' || c == ',' ) {
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG2] Detected \"Whitespace\" \n" );
#endif
iret = 1002;
} else if( c == '\n' || c == '\r' ) {
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG2] Detected newline \n" );
#endif
iret = 1003;
} else if( c == '(' ) {
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG2] Detected \"List\" token \n" );
#endif
iret = 2001;
} else if( c == ')' ) {
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG2] Detected end of \"List\" token \n" );
#endif
iret = 2011;
} else if( c == '[' ) {
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG2] Detected \"Vector\" token \n" );
#endif
iret = 2002;
} else if( c == ']' ) {
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG2] Detected end of \"Vector\" token \n" );
#endif
iret = 2012;
} else if( c == '{' ) {
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG2] Detected \"Map\" token \n" );
#endif
iret = 2003;
} else if( c == '}' ) {
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG2] Detected end of \"Map/Set\" token \n" );
#endif
iret = 2013;
} else if( c == '\"' ) {
// a string is necessarily enclosed in double quotes (good!)
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG2] Detected \"String\" token \n" );
#endif
iret = 3001;
} else if( c == '#' ) {
// dispatch character
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG2] Detected \"Set\" token \n" );
#endif
iret = 4000;
} else if( c == 't' || c == 'f' || c == 'n') {
// potentially a boolean or nil; we must invoke a parser to find out
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG2] Potentially a \"Boolean/Nil\" token \n" );
#endif
switch( inEDN_ClassifySpecial( es ) ) {
case 5001:
#ifdef _DEBUG_CLASSIFY3_
if(fp) fprintf( fp, " [DEBUG3] Detected \"true\" \n" );
#endif
iret = 5001; // true
break;
case 5002:
#ifdef _DEBUG_CLASSIFY3_
if(fp) fprintf( fp, " [DEBUG3] Detected \"false\" \n" );
#endif
iret = 5002; // false
break;
case 5003:
#ifdef _DEBUG_CLASSIFY3_
if(fp) fprintf( fp, " [DEBUG3] Detected \"nil\" \n" );
#endif
iret = 5003; // nil
break;
default:
#ifdef _DEBUG_CLASSIFY3_
if(fp) fprintf( fp, " [DEBUG3] Detected \"Symbol\" \n" );
#endif
iret = 6000; // must be a "symbol"
}
} else if( c == '.' || c == '*' || c == '+' || c == '!' || c == '-' ||
c == '_' || c == '?' || c == '$' || c == '%' || c == '&' ||
c == '=' || c == '<' || c == '>' ||
c == '/' || // this is special, but can be a symbol in itself
(( 65 <= c && c <= 90 ) || ( 97 <= c && c <= 122 )) // ascii
) {
// Expect ". * + ! - _ ? $ % & = < >". If -, + or . are leading, the next
// character must be non-numeric. (This can overlap signed integers and
// reals; classification is preliminary and it can be parsed as a symbol
// with subsequent re-parsing and re-assignment of the type.)
///// I have a feeling I should make a 256-byte array to map directlly to
///// the outcome instead of using conditionals. This is a subsequent commit!
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG2] Detected \"Symbol/Numeric\" token \n" );
#endif
iret = 6000; // preliminary classifcation as "symbol"
} else if( c == ':' ) {
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG2] Detected \"Keyword\" token \n" );
#endif
iret = 7000;
} else if( 48 <= c && c <= 57 ) {
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG2] Detected \"Numeric\" token \n" );
#endif
iret = 8000; // numeric / may be integer or real
} else {
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG2] Unclassified token during detection \n" );
#endif
iret = 9999;
}
return iret;
}
//
// Function to classify a segment following the initial dispatch character
// (Presently, this is "set" "discard" or something else that is "special"
//
int inEDN_ClassifyPound( struct inEDN_state_s *es, char c )
{
if( es == NULL ) return 1;
FILE *fp = es->fp;
int iret=4000;
if( c == '{' ) {
iret = 4001;
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG] Detected dispatch to set \n" );
#endif
} else if( c == '_' ) {
iret = 4002;
#ifdef _DEBUG_CLASSIFY2_
if(fp) fprintf( fp, " [DEBUG] Detected dispatch to discard \n" );
#endif
} else {
printf("Unclassified dispatch ancountered while dev.: \"%c\" \n", c );
exit(1);
}
return iret;
}
//
// Function to allocate a standard type
//
int inEDN_Allocate( struct inEDN_state_s *es,
union inEDN_type_u** p )
{
if( es == NULL || p == NULL ) return 1;
FILE *fp = es->fp;
#ifdef _DEBUG_
if(fp) fprintf( fp, " [DEBUG] Allocating generic type \n" );
#endif
size_t size = sizeof( union inEDN_type_u );
*p = (union inEDN_type_u*) malloc( size );
if( *p == NULL ) {
sprintf( es->msg, "Error: could not allocate item " );
if(fp) fprintf( fp, "%s\n", es->msg );
return -1;
}
INEDN_TYPE_INIT( *(*p) )
return 0;
}
//
// Function to extend a "Toplevel" type's reference array
//
int inEDN_Extend( struct inEDN_state_s *es,
struct inEDN_toplevel_s *t )
{
if( es == NULL || t == NULL ) return 1;
FILE *fp = es->fp;
#ifdef _DEBUG_
if(fp) fprintf( fp, " [DEBUG] Extending Toplevel at %p, items: %ld\n",
t, t->num );
#endif
if( t->num < 0 ) return 2;
size_t isize = (t->num + 1) * sizeof( union inEDN_type_u );
union inEDN_type_u* p = (union inEDN_type_u*) realloc( t->array, isize );
if( p == NULL ) {
sprintf( es->msg, "Error: could not reallocate array of toplevel" );
if(fp) fprintf( fp, "%s\n", es->msg );
return -1;
}
INEDN_TYPE_INIT( p[ t->num ] )
t->array = p;
t->num += 1;
return 0;
}
//
// Function to parse a comment line
// It should be called when a ";" is encountered, and it is expected to keep
// parsing until a newline or line-feed character is reached, and return 0.
// If a null character is reached, it will return 2. It returns 1 or error,
// which should only happen in weird circumstances. It advances the pointer
// of the string being parsed to the next character, unless it reached nullchar.
//
int inEDN_ParseComment( struct inEDN_state_s* es )
{
if( es == NULL ) {
fprintf( stdout, "Error: null EDN pointer \n");
return 1;
}
FILE *fp = es->fp;
#ifdef _DEBUG_
if(fp) fprintf( fp, " [DEBUG] Parsing __Comment__ \n" );
#endif
int ierr=0;
const char* ptr = es->ptr;
#ifdef _DEBUG_COMMENT2_
if(fp) fprintf( fp, " [DEBUG2] Starting pointer at -->%c<-- \n", ptr[0] );
#endif
while( ierr == 0 ) {
// seek a newline
if( ptr[0] == '\n' || ptr[0] == '\r') {
#ifdef _DEBUG_COMMENT2_
if(fp) fprintf( fp, "\n" );
#endif
++ptr;
ierr = 100;
} else if( ptr[0] == '\0' ) {
#ifdef _DEBUG_COMMENT2_
if(fp) fprintf( fp, "\n" );
#endif
ierr = 102; // truncated; not necessarily a problem/error
} else {
#ifdef _DEBUG_COMMENT2_
if(fp) fprintf( fp, "%c", ptr[0] );
#endif
++ptr;
}
}
es->ptr = ptr;
#ifdef _DEBUG_
if(fp) fprintf( fp, " [DEBUG] Ending parsing of __Comment__ \n" );
#endif
if( ierr == 100 ) return 0;
if( ierr == 102 ) return 2;
}
//
// Function to parse "Whitespace" (exhausts whitespace or fails at truncation)
//
int inEDN_ParseWhitespace( struct inEDN_state_s* es )
{
if( es == NULL ) {
fprintf( stdout, "Error: null EDN pointer \n");
return 1;
}
FILE *fp = es->fp;
#ifdef _DEBUG_
if(fp) fprintf( fp, " [DEBUG] Parsing __Whitespace__ \n" );
#endif
int ierr=0;
const char* ptr = es->ptr;
#ifdef _DEBUG_WHITE2_
if(fp) fprintf( fp, " [DEBUG2] Starting pointer at -->%c<-- \n", ptr[0] );
#endif
while( ierr == 0 ) {
// spaces, tabs, and newlines prior to the colon
if( ptr[0] == ' ' || ptr[0] == '\t' ) {
++ptr;
} else if( ptr[0] == ',' ) { // lingering comma is a whitespace (what?)
++ptr;
} else if( ptr[0] == '\0' ) {
ierr = 101; // truncated; not necessarily a problem/error
} else {
ierr = 100; // located something
}
}
es->ptr = ptr;
if( ierr == 101 ) {
#ifdef _DEBUG_WHITE2_
if(fp) fprintf( fp, " [DEBUG2] Went past whitespace with truncation\n" );
#endif
return 2;
}
if( ierr == 100 ) {
#ifdef _DEBUG_WHITE2_
if(fp) fprintf( fp, " [DEBUG2] Went past whitespace -->%c<--\n", ptr[0]);
#endif
return 0;
}
}
//
// function to parse a numeric item (int or real)
//
int inEDN_ParseNumeric( struct inEDN_state_s* es,
union inEDN_type_u* u )
{
if( es == NULL || u == NULL ) {
fprintf( stdout, "Error: null EDN/u pointer \n");
return 1;
}
FILE *fp = es->fp;
#ifdef _DEBUG_
if(fp) fprintf( fp, " [DEBUG] Parsing __Numeric__ \n" );
#endif
int ierr=0;
const char* ptr = es->ptr;
#ifdef _DEBUG_NUMERIC2_
if(fp) fprintf( fp, " [DEBUG2] Starting pointer at -->%c<-- \n", ptr[0] );
#endif
unsigned char iperiod=0, itype=0, isuffix=0;
// size the numeric item (to allocate a string)
// determine if it is a real or integer
// determine if it has offensive characters or spaces, etc
int n=0;
while( ierr == 0 ) {
if( ptr[n] == '+' || ptr[n] == '-' ) {
// allow signs
} else if( ptr[n] == '.' ) {
++iperiod; // counting periods
if( iperiod == 2 ) ierr=103; // error: too many periods
// check for sanity (can have "N" prior to "." in error)
if( itype == 1 ) {
ierr=104; // mixing integers and reals
} else {
itype=2;
}
} else if( ptr[n] == 'e' || ptr[n] == 'E' ) {
// allow for exponent-mantissa reals
// check for sanity (can "e"/"E" trail "N" in error)
if( itype == 1 ) {
ierr=104; // mixing integers and reals
} else {
itype=2;
}
} else if ( ptr[n] == 'N' ) {
// allow trailing "N" for ints, but check for characters of a real
++isuffix;
if( isuffix == 2 ) ierr=103; // error: too many Ns
if( itype == 2 ) {
ierr=104; // mixing integers and reals
} else {
itype=1;
}
} else if( 48 <= ptr[n] && ptr[n] <= 57 ) {
// allow decimal digits
} else if ( ptr[n] == '\n' || ptr[n] == '\r' ) {
ierr=102; // truncated by newline; may not be an error
--n; // retract a byte
} else if ( ptr[n] == '\0' ) {
ierr=102; // truncated by nullchar; may not be an error
--n; // retract a byte
} else if ( ptr[n] == ' ' || ptr[n] == '\t' || ptr[n] == ',' ||
ptr[n] == ')' || ptr[n] == ']' || ptr[n] == '}' ||
ptr[n] == '(' || ptr[n] == '[' || ptr[n] == '{' ) {
ierr=100; // terminated in a reasonable fashion
--n; // retract a byte
} else {
#ifdef _DEBUG_NUMERIC2_
if(fp) fprintf( fp, " [DEBUG2] Alpha character in error \n" );
#endif
ierr=101; // error: all other alpha characters is in error
}
++n;
}
#ifdef _DEBUG_NUMERIC2_
if(fp) fprintf( fp, " [DEBUG2] Counted %d bytes; state: %d \n", n, ierr );
#endif
if( ierr == 100 || ierr == 102 ) {
if( itype == 0 ) itype=1;
char* str = (char*) malloc( (size_t) n+1 );
if( str == NULL ) {
if(fp) fprintf( fp, " [Error] memory allocation problem \n" );
return -1;
}
memcpy( str, ptr, (size_t) n );
str[n] = '\0';
#ifdef _DEBUG_NUMERIC2_
if(fp) fprintf( fp, " [DEBUG2] Payload \"%s\"\n", str );
#endif
if( isuffix ) {
str[n-1] = '\0';
#ifdef _DEBUG_NUMERIC2_
if(fp) fprintf( fp, " [DEBUG2] Has suffix N; clean \"%s\" \n", str );
#endif
}
// parse and re-assign if no error
if( itype == 1 ) {
long l = atol( str );
struct inEDN_int_s* p = (struct inEDN_int_s*) u;
p->type = inEDN_Type_Int;
p->l = l;
#ifdef _DEBUG_NUMERIC2_
if(fp) fprintf( fp, " [DEBUG2] Changed to \"int\" %ld \n", p->l );
#endif
} else if( itype == 2 ) {
double d = strtod( str, NULL );
struct inEDN_real_s* p = (struct inEDN_real_s*) u;
p->type = inEDN_Type_Real;
p->d = d;
#ifdef _DEBUG_NUMERIC2_
if(fp) fprintf( fp, " [DEBUG2] Changed to \"real\" %16.9e \n", p->d );
#endif
} else {
// should errors occur (not possible now), we revert everything
ierr=999;
}
free( str );
#ifdef _DEBUG_NUMERIC2_
if(fp) fprintf( fp, " [DEBUG2] Advancing by %d bytes \n", n );
#endif
#ifdef _DEBUG_NUMERIC3_
if(fp) fprintf( fp, " [DEBUG2] Start pointer at \"%c\" \n", es->ptr[0] );
#endif
es->ptr += (size_t) (n-0);
#ifdef _DEBUG_NUMERIC3_
if( es->ptr[0] == '\n' || es->ptr[0] == '\r' )
if(fp) fprintf( fp, " [DEBUG3] End pointer at \"\\n / \\r\" \n" );
else
if(fp) fprintf( fp, " [DEBUG3] End pointer at \"%c\"\n", es->ptr[0] );
#endif
}
if( ierr == 100 ) ierr=0;
if( ierr == 102 ) ierr=2;
if( ierr == 101 || ierr == 103 || ierr == 104 ) ierr=3;
#ifdef _DEBUG_
if(fp) fprintf( fp, " [DEBUG] Ending parsing of __Numeric__ (%d)\n", ierr );
#endif
return ierr;
}
//
// function to parse the string stored under a "symbol" type, expecting that
// it is a numeric value (real or int)
//
int inEDN_ParseNumericFromSymbol( struct inEDN_state_s* es,
struct inEDN_symbol_s* sp )
{
if( es == NULL || sp == NULL ) {
fprintf( stdout, "Error: null EDN/sp pointer \n");
return 1;
}
if( sp->size <= 0 ) {
fprintf( stdout, "Error: numeric string of zero size \n");
return 1;
}
FILE *fp = es->fp;
#ifdef _DEBUG_
if(fp) fprintf( fp, " [DEBUG] Parsing __NumericSymbol__ \n" );
if(fp) fprintf( fp, " [DEBUG] Payload of symbol -->%s<---\n", sp->string );
#endif
int ierr=0;
char* ptr = sp->string;
unsigned char itype=0, isuffix=0;
// determine if it has a suffix "N" to indicate precision (and ignore it)
if( ptr[ sp->size-1 ] == 'N' ) {
#ifdef _DEBUG_NUMERIC2_
if(fp) fprintf( fp, " [DEBUG2] Has suffix N \n" );
#endif
isuffix=1; // to reverse the following change
ptr[ sp->size-1 ] = '\0';
itype=1; // it must be an integer by convention
}
// determine if it is a fraction or integer, and...
// determine if it has offensive characters or spaces, etc
if( itype == 0 ) itype=1; // assume it is an integer
int n=0;
while( ptr[n] != '\0' && ierr == 0 ) {
if( ptr[n] == '.' ) itype=2;
// check for errors; set "itype=0" and "ierr!=0" on error
if( ptr[n] == '+' || ptr[n] == '-' ) {
// signs
} else if( ptr[n] == 'e' || ptr[n] == 'E' ) { // fine
itype=2;
} else if( 48 <= ptr[n] && ptr[n] <= 57 ) {
// digits
} else {
itype=0;
ierr=1;
}
++n;
}
// parse and re-assign if no error
if( itype == 1 ) {
long l = atol( ptr );
free( sp-> string );
struct inEDN_int_s* p = (struct inEDN_int_s*) sp;
p->type = inEDN_Type_Int;
p->l = l;
#ifdef _DEBUG_NUMERIC2_
if(fp) fprintf( fp, " [DEBUG2] Changed to \"int\" %ld \n", p->l );
#endif
} else if( itype == 2 ) {
double d = strtod( ptr, NULL );
free( sp-> string );
struct inEDN_real_s* p = (struct inEDN_real_s*) sp;
p->type = inEDN_Type_Real;
p->d = d;
#ifdef _DEBUG_NUMERIC2_
if(fp) fprintf( fp, " [DEBUG3] Changed to \"real\" %16.9e \n", p->d );
#endif
} else {
// should errors occur (not possible now), we revert everything
ierr=100;
if( isuffix ) ptr[ sp->size-1 ] = 'N';
}
#ifdef _DEBUG_
if(fp) fprintf( fp, " [DEBUG] Ending parsing of __NumericSymbol__ \n" );
#endif
return ierr;
}
//
// function to parse a string
// (A string is enclosed in double quotes.)
//
int inEDN_ParseString( struct inEDN_state_s* es,
struct inEDN_string_s* sp )
{
if( es == NULL || sp == NULL ) {
fprintf( stdout, "Error: null EDN/sp pointer \n");
return 1;
}
FILE *fp = es->fp;