This repository was archived by the owner on Dec 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathss.c
1724 lines (1429 loc) · 46.9 KB
/
ss.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
#include "ss.h"
#include <stdbool.h>
#include <stdint.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
/********************************* Core Types *********************************/
typedef struct ss_Map ss_Map;
typedef struct ss_List ss_List;
typedef struct ss_Iter ss_Iter;
typedef struct ss_Stream ss_Stream;
typedef struct ss_Buffer ss_Buffer;
typedef struct ss_Object ss_Object;
typedef enum ss_Type ss_Type;
typedef struct ss_Compiler ss_Compiler;
typedef ss_Match* (*ss_Matcher)( ss_Context* ctx, ss_Pattern* pat, ss_Map* scope, ss_Stream* stream );
typedef void (*ss_Cleaner)( ss_Context* ctx, ss_Pattern* pat );
struct ss_Pattern {
ss_Matcher match;
ss_Cleaner clean;
char* binding;
};
struct ss_Stream {
ss_Context* ctx;
char const* loc;
char const* end;
long (*read)( ss_Context* ctx, ss_Stream* stream );
};
struct ss_Context {
ss_Map* patterns;
ss_Error errnum;
char const* errmsg;
size_t tmpcap;
size_t tmptop;
char const* tmpbuf;
};
struct ss_Scanner {
ss_Pattern* pat;
ss_Stream stream;
};
struct ss_Match {
ss_Map* scope;
ss_Match* next;
char const* loc;
char const* end;
};
struct ss_Compiler {
ss_List* patterns;
ss_Stream stream;
long ch1;
long ch2;
};
enum ss_Type {
TYPE_PATTERN,
TYPE_SCANNER,
TYPE_CONTEXT,
TYPE_MATCH,
TYPE_MAP,
TYPE_LIST,
TYPE_BUFFER,
TYPE_COMPILER,
TYPE_ITER,
TYPE_LAST
};
struct ss_Object {
ss_Type type;
unsigned refc;
char data[];
};
/********************************* Prototypes *********************************/
static void* ss_alloc( size_t sz, ss_Type type );
static void* ss_refer( void* ptr );
static void ss_free( void* ptr );
static void ss_prelude( ss_Context* ctx );
static void ss_error( ss_Context* ctx, ss_Error err, char const* fmt, ... );
static ss_Map* ss_mapNew( ss_Context* ctx );
static int ss_mapPut( ss_Context* ctx, ss_Map* map, char const* key, void* val );
static void* ss_mapGet( ss_Context* ctx, ss_Map* map, char const* key );
static int ss_mapCommit( ss_Context* ctx, ss_Map* map );
static void ss_mapCancel( ss_Context* ctx, ss_Map* map );
static ss_List* ss_listNew( ss_Context* ctx );
static int ss_listAdd( ss_Context* ctx, ss_List* list, void* val );
static ss_Iter* ss_listIter( ss_Context* ctx, ss_List* list );
static void* ss_iterNext( ss_Context* ctx, ss_Iter* iter );
static ss_Buffer* ss_bufferNew( ss_Context* ctx );
static int ss_bufferPut( ss_Context* ctx, ss_Buffer* buf, long ch );
static long const* ss_bufferBuf( ss_Context* ctx, ss_Buffer* buf );
static size_t ss_bufferLen( ss_Context* ctx, ss_Buffer* buf );
static ss_Pattern* ss_allOfPattern( ss_Context* ctx, ss_List* patterns );
static ss_Pattern* ss_oneOfPattern( ss_Context* ctx, ss_List* patterns );
static ss_Pattern* ss_hasNextPattern( ss_Context* ctx, ss_Pattern* pattern );
static ss_Pattern* ss_notNextPattern( ss_Context* ctx, ss_Pattern* pattern );
static ss_Pattern* ss_zeroOrOnePattern( ss_Context* ctx, ss_Pattern* pattern );
static ss_Pattern* ss_zeroOrMorePattern( ss_Context* ctx, ss_Pattern* pattern );
static ss_Pattern* ss_justOnePattern( ss_Context* ctx, ss_Pattern* pattern );
static ss_Pattern* ss_oneOrMorePattern( ss_Context* ctx, ss_Pattern* pattern );
static ss_Pattern* ss_literalPattern( ss_Context* ctx, long const* str, size_t len );
/****************************** Context Creation ******************************/
ss_Context* ss_init( void ) {
ss_Context* ctx = ss_alloc( sizeof(ss_Context), TYPE_CONTEXT );
if( !ctx )
return NULL;
ctx->patterns = ss_mapNew();
ctx->errnum = ss_ERR_NONE;
ctx->errmsg = NULL;
ctx->tmpcap = 64;
ctx->tmptop = 0;
ctx->tmpbuf = malloc( 64 );
if( !ctx->tmpbuf ) {
ss_release( ctx );
ss_error( ctx, ss_ERR_ALLOC, NULL );
return NULL;
}
ss_prelude( ctx );
return ctx;
}
static void freeContext( void* ptr ) {
ss_Context* ctx = ptr;
if( ctx->pattern )
ss_release( ctx->patterns );
if( ctx->tmpbuf )
free( tmpbuf );
ss_free( ctx );
}
/***************************** String Decoding ********************************/
#define ss_STREAM_END (-1)
#define ss_STREAM_ERR (-2)
static long readByte( ss_Context* ctx, ss_Stream* stream ) {
if( stream->loc == stream->end )
return ss_STREAM_END;
else
return *(stream->loc++);
}
#define isSingleChr( c ) ( (unsigned char)(c) >> 7 == 0 )
#define isDoubleChr( c ) ( (unsigned char)(c) >> 5 == 6 )
#define isTripleChr( c ) ( (unsigned char)(c) >> 4 == 14 )
#define isQuadChr( c ) ( (unsigned char)(c) >> 3 == 30 )
#define isAfterChr( c ) ( (unsigned char)(c) >> 6 == 2 )
static long readChar( ss_Context* ctx, ss_Stream* stream ) {
if( stream->loc == stream->end )
return ss_STREAM_END;
long code = 0;
int byte = *( stream->loc++ );
int size = 0;
if( isSingleChr( byte ) ) {
size = 1;
code = byte;
}
else
if( isDoubleChr( byte ) ) {
size = 2;
code = byte & 0x1F;
}
else
if( isTripleChr( byte ) ) {
size = 3;
code = byte & 0xF;
}
else
if( isQuadChr( byte ) ) {
size = 4;
code = byte & 0x7;
}
else {
ss_error( ctx, "Input is corrupted or not formated as UTF-8" );
return ss_STREAM_ERROR;
}
for( int i = 1 ; i < size ; i++ ) {
if( stream->loc == stream->end )
return ss_STREAM_END;
byte = *( stream->loc++ );
code = ( code << 6 ) | ( byte & 0x3F );
}
return code;
}
static ss_Stream ss_makeStream( ss_Format fmt, char const* loc, char const* end ) {
ss_Stream stream = { .loc = loc, .end = end };
switch( fmt ) {
case ss_BYTES:
stream.read = readByte;
break;
case ss_CHARS:
stream.read = readChar;
break;
default:
assert( false );
}
return stream;
}
/********************************* Compilation ********************************/
static int ss_advance( ss_Context* ctx, ss_Compiler* compiler ) {
compiler->ch1 = compiler->ch2;
compiler->ch2 = compiler->stream.read( &compiler->stream );
if( compiler->ch2 == ss_STREAM_ERR )
return ss_STREAM_ERR;
else
return 0;
}
static ss_Compiler* ss_compiler( ss_Context* ctx, ss_Format fmt, char const* str, size_t len ) {
ss_Compiler* compiler = ss_alloc( sizeof(ss_Compiler), TYPE_COMPILER );
if( !compiler ) {
ss_error( ctx, ss_ERR_ALLOC, NULL );
return NULL;
}
compiler->stream = ss_makeStream( fmt, str, str + len );
compiler->patterns = ss_listNew( ctx );
if( !compiler->patterns ) {
ss_release( compiler );
return NULL;
}
if( ss_advance( ctx, compiler ) || ss_advance( ctx, compiler ) ) {
ss_release( compiler );
return NULL;
}
return compiler;
}
static void freeCompiler( void* ptr ) {
ss_Compiler* compiler = ptr;
if( compiler->patterns )
ss_release( compiler->patterns );
ss_free( compiler );
}
static void ss_whitespace( ss_Context* ctx, ss_Compiler* compiler ) {
while( isspace( compiler->ch1 ) && !ss_advance( ctx, compiler ) )
;
}
static bool isopening( long ch ) {
return ch == '(' ||
ch == '{' ||
ch == '[' ||
ch == '<';
}
static bool isclosing( long ch ) {
return ch == ')' ||
ch == '}' ||
ch == ']' ||
ch == '>';
}
static bool isbreak( long ch1, long ch2 ) {
if( ch1 == '^' ) {
if( isopening( ch2 ) )
return true;
if( ch2 == '*' || ch2 == '?' )
return true;
}
if( ch1 == '~' ) {
if( isopening( ch2 ) )
return true;
if( ch2 == '*' || ch2 == '?' )
return true;
}
if( isopening( ch1 ) )
return true;
if( ch1 == '*' || ch1 == '?' || ch1 == '\\' )
return true;
return false;
}
static bool isend( long ch ) {
return ch < 0;
}
static ss_Pattern* ss_compileText( ss_Context* ctx, ss_Compiler* compiler ) {
if( compiler->ch1 == ss_STREAM_END )
return NULL;
if( isbreak( compiler->ch1, compiler->ch2 ) )
return NULL;
ss_Buffer* buf = ss_bufferNew( ctx );
if( !buf )
return NULL;
while( !isbreak( compiler->ch1, compiler->ch2 ) && !isend( compiler->ch1 ) ) {
if( ss_bufferPut( buf, compiler->ch1 ) ) {
ss_release( buf );
return NULL;
}
if( ss_advance( ctx, compiler ) {
ss_release( buf );
return NULL;
}
}
long const* str = ss_bufferBuf( buf );
size_t len = ss_bufferLen( buf );
ss_Pattern* pat = ss_literalPattern( str, len );
ss_release( buf );
return pat;
}
static ss_Pattern* ss_compileString( ss_Context* ctx, ss_Compiler* compiler ) {
long quote;
if( compiler->ch1 == '"' || compiler->ch1 == '`' || compiler->ch1 == '\'' )
quote = compiler->ch1;
else
return NULL;
if( ss_advance( ctx, compiler ) )
return NULL;
ss_Buffer* buf = ss_bufferNew( ctx );
if( !buf )
return NULL;
while( compiler->ch1 != quote ) {
if( isend( compiler->ch1 ) ) {
ss_error( ctx, "Unterminated string" );
ss_release( buf );
return NULL;
}
if( ss_bufferPut( buf, compiler->ch1 ) ) {
ss_release( buf );
return NULL;
}
if( ss_advance( ctx, compiler ) ) {
ss_release( buf );
return NULL;
}
}
if( ss_advance( ctx, compiler ) ) {
ss_release( buf );
return NULL;
}
long const* str = ss_bufferBuf( buf );
size_t len = ss_bufferLen( buf );
ss_Pattern* pat = ss_literalPattern( str, len );
ss_release( buf );
return pat;
}
static ss_Pattern* ss_compileChar( ss_Context* ctx, ss_Compiler* compiler ) {
if( compiler->ch1 != '\\' )
return NULL;
if( ss_advance( ctx, compiler ) )
return NULL;
long chr = compiler->ch1;
if( ss_advance( ctx, compiler ) )
return NULL;
return ss_literalPattern( &chr, 1 );
}
static ss_Pattern* ss_compileCode( ss_Context* ctx, ss_Compiler* compiler ) {
if( !isdigit( compiler->ch1 ) )
return NULL;
long code = 0;
while( isalnum( compiler->ch1 ) ) {
if( !isdigit( compiler->ch1 ) ) {
ss_error( ctx, ss_ERR_SYNTAX, "Non-digit at end of character code" );
return NULL;
}
code = code*10 + (compiler->ch1 - '0');
if( ss_advance( ctx, compiler ) )
return NULL;
}
return ss_literalPattern( &code, 1 );
}
static char const* parseName( ss_Context* ctx, ss_Compiler* compiler ) {
ctx->tmptop = 0;
while( compiler->ch1 == '_' || isalnum( compiler->ch1 ) ) {
if( ctx->tmptop >= ctx->tmpcap - 1 ) {
void* rep = realloc( ctx->tmpbuf );
if( !rep ) {
ss_error( ctx, ss_ERR_ALLOC, NULL );
return NULL;
}
ctx->tmpbuf = rep;
}
ctx->tmpbuf[ctx->tmptop++] = (char)compiler->ch1;
if( ss_advance( ctx, compiler ) )
return NULL;
}
ctx->tmpbuf[ctx->tmptop++] = '\0';
return buf;
}
static ss_Pattern* ss_compileNamed( ss_Context* ctx, ss_Compiler* compiler ) {
if( !isalpha( compiler->ch1 ) && compiler->ch1 != '_' && compiler->ch1 != '*' && compiler->ch1 != '?' )
return NULL;
char const* name = NULL;
if( compiler->ch1 == '*' ) {
name = "splat";
if( ss_advance( ctx, compiler ) )
return NULL;
}
else
if( compiler->ch1 == '?' ) {
name = "quark";
if( ss_advance( ctx, compiler ) )
return NULL;
}
else {
name = parseName( compiler );
if( !name )
return NULL;
}
ss_Pattern* pat = ss_mapGet( compiler->ctx->patterns, name );
if( !pat ) {
ss_error( ctx, ss_ERR_ALLOC, NULL );
return NULL;
}
return ss_refer( pat );
}
static ss_Pattern* ss_compilePattern( ss_Compiler* compiler );
static bool arematching( long open, long close ) {
return (open == '(' && close == ')') ||
(open == '{' && close == '}') ||
(open == '[' && close == ']') ||
(open == '<' && close == '>');
}
static ss_Pattern* ss_compileCompound( ss_Compiler* compiler ) {
long open;
if( isopening( compiler->ch1 ) )
open = compiler->ch1;
else
return NULL;
if( ss_advance( compiler ) || ss_whitespace( compiler ) )
return NULL;
ss_List* oneOfList = ss_listNew();
while( !isclosing( compiler->ch1 ) ) {
if( isend( compiler->ch1 ) ) {
ss_error( ctx, ss_ERR_SYNTAX, "Unterminated pattern" );
ss_release( oneOfList );
return NULL;
}
ss_List* allOfList = ss_listNew();
do {
ss_Pattern* pat = ss_compilePattern( compiler );
if( !pat ) {
if( !ctx->errnum )
ss_error( ctx, ss_ERR_SYNTAX, "Expected sub-pattern" );
ss_release( oneOfList );
ss_release( allOfList );
return NULL;
}
ss_listAdd( allOfList, pat );
ss_whitespace( compiler );
ss_release( pat );
} while( compiler->ch1 != '|' && !isclosing( compiler->ch1 ) );
if( compiler->ch1 == '|' )
ss_advance( compiler );
ss_Pattern* allOfPat = ss_allOfPattern( allOfList );
ss_listAdd( oneOfList, allOfPat );
ss_whitespace( compiler );
ss_release( allOfList );
ss_release( allOfPat );
}
if( !arematching( open, compiler->ch1 ) ) {
compiler->ctx->error = "Mismatched brackets";
ss_release( oneOfList );
return NULL;
}
ss_advance( compiler );
ss_Pattern* oneOfPat = ss_oneOfPattern( oneOfList );
ss_release( oneOfList );
ss_Pattern* compPat = NULL;
switch( open ) {
case '(':
compPat = ss_justOnePattern( oneOfPat );
ss_release( oneOfPat );
break;
case '{':
compPat = ss_zeroOrMorePattern( oneOfPat );
ss_release( oneOfPat );
break;
case '[':
compPat = ss_zeroOrOnePattern( oneOfPat );
ss_release( oneOfPat );
break;
case '<':
compPat = ss_oneOrMorePattern( oneOfPat );
ss_release( oneOfPat );
break;
default:
assert( false );
break;
}
return compPat;
}
static ss_Pattern* ss_compilePrimitive( ss_Compiler* compiler ) {
ss_Pattern* pat = NULL;
pat = ss_compileString( compiler );
if( pat )
goto parsed;
pat = ss_compileChar( compiler );
if( pat )
goto parsed;
pat = ss_compileCode( compiler );
if( pat )
goto parsed;
pat = ss_compileCompound( compiler );
if( pat )
goto parsed;
return NULL;
parsed:
if( compiler->ch1 != ':' )
return pat;
ss_advance( compiler );
char const* binding = parseName( compiler );
if( !binding ) {
ss_release( pat );
compiler->ctx->error = "Invalid binding name";
return NULL;
}
size_t len = strlen( binding );
char* cpy = xmalloc( len + 1 );
strcpy( cpy, binding );
pat->binding = cpy;
return pat;
}
static ss_Pattern* ss_compileNotNext( ss_Compiler* compiler ) {
if( compiler->ch1 != '~' )
return NULL;
ss_advance( compiler );
ss_whitespace( compiler );
ss_Pattern* pat = ss_compilePrimitive( compiler );
if( !pat ) {
if( !compiler->ctx->error )
compiler->ctx->error = "Expected sub-pattern";
return NULL;
}
ss_Pattern* notNextPat = ss_notNextPattern( pat );
ss_release( pat );
return notNextPat;
}
static ss_Pattern* ss_compileHasNext( ss_Compiler* compiler ) {
if( compiler->ch1 != '^' )
return NULL;
ss_advance( compiler );
ss_whitespace( compiler );
ss_Pattern* pat = ss_compilePrimitive( compiler );
if( !pat ) {
if( !compiler->ctx->error )
compiler->ctx->error = "Expected sub-pattern";
return NULL;
}
ss_Pattern* hasNextPat = ss_hasNextPattern( pat );
ss_release( pat );
return hasNextPat;
}
static ss_Pattern* ss_compilePattern( ss_Compiler* compiler ) {
ss_Pattern* pat = ss_compilePrimitive( compiler );
if( pat || compiler->ctx->error )
return pat;
pat = ss_compileNotNext( compiler );
if( pat || compiler->ctx->error )
return pat;
pat = ss_compileHasNext( compiler );
if( pat || compiler->ctx->error )
return pat;
pat = ss_compileNamed( compiler );
if( pat || compiler->ctx->error )
return pat;
return NULL;
}
static ss_Pattern* ss_compileFull( ss_Compiler* compiler ) {
ss_List* allOfList = ss_listNew();
while( true ) {
ss_Pattern* pat = NULL;
pat = ss_compileText( compiler );
if( !pat )
pat = ss_compilePattern( compiler );
if( !pat )
break;
ss_listAdd( allOfList, pat );
ss_release( pat );
}
ss_Pattern* allOfPat = ss_allOfPattern( allOfList );
ss_release( allOfList );
return allOfPat;
}
ss_Pattern* ss_compile( ss_Context* ctx, ss_Format fmt, char const* pat, size_t len ) {
ss_Compiler* compiler = ss_compiler( ctx, fmt, pat, len );
ss_Pattern* pattern = ss_compileFull( compiler );
ss_release( compiler );
return pattern;
}
void ss_define( ss_Context* ctx, char const* name, ss_Pattern* pat ) {
ss_mapPut( ctx->patterns, name, pat );
ss_mapCommit( ctx->patterns );
}
/********************************** Matching **********************************/
ss_Scanner* ss_start( ss_Context* ctx, ss_Format fmt, ss_Pattern* pat, char const* str, size_t len ) {
ss_Scanner* scanner = ss_alloc( sizeof(ss_Scanner), TYPE_SCANNER );
scanner->pat = ss_refer( pat );
scanner->stream = ss_makeStream( fmt, str, str + len );
return scanner;
}
ss_Match* ss_match( ss_Scanner* scanner ) {
ss_Stream stream = scanner->stream;
ss_Pattern* pat = scanner->pat;
ss_Map* scope = ss_mapNew();
ss_Match* match = pat->match( pat, scope, &stream );
ss_mapCommit( scope );
ss_release( scope );
if( stream.end == scanner->stream.end )
return match;
ss_release( match );
return NULL;
}
ss_Match* ss_find( ss_Scanner* scanner ) {
ss_Match* m = NULL;
while( !m && scanner->stream.loc != scanner->stream.end ) {
ss_Stream stream = scanner->stream;
ss_Pattern* pat = scanner->pat;
ss_Map* scope = ss_mapNew();
m = pat->match( pat, scope, &stream );
ss_mapCommit( scope );
ss_release( scope );
scanner->stream.read( &scanner->stream );
}
if( !m )
return NULL;
scanner->stream.loc = m->end;
return m;
}
ss_Match* ss_next( ss_Match* match ) {
return ss_refer( match->next );
}
char const* ss_loc( ss_Match* match ) {
return match->loc;
}
char const* ss_end( ss_Match* match ) {
return match->end;
}
ss_Match* ss_get( ss_Match* match, char const* binding ) {
ss_Match* m = ss_mapGet( match->scope, binding );
if( m )
return ss_refer( m );
else
return NULL;
}
static void freeScanner( void* ptr ) {
ss_Scanner* scanner = ptr;
ss_release( scanner->pat );
ss_free( scanner );
}
/****************************** Object Allocation *****************************/
static void* xmalloc( size_t sz ) {
void* ptr = malloc( sz );
if( !ptr ) {
fprintf( stderr, "Allocation error\n" );
abort();
}
return ptr;
}
static void* xcalloc( size_t nmem, size_t size ) {
void* ptr = calloc( nmem, size );
if( !ptr ) {
fprintf( stderr, "Allocation error\n" );
abort();
}
return ptr;
}
static void* xrealloc( void* ptr, size_t sz ) {
void* nptr = realloc( ptr, sz );
if( !nptr ) {
free( nptr );
fprintf( stderr, "Allocation error\n" );
abort();
}
return nptr;
}
#define ss_obj( PTR ) ((void*)(PTR) - sizeof(ss_Object))
static void* ss_alloc( size_t sz, ss_Type type ) {
ss_Object* obj = xmalloc( sizeof(ss_Object) + sz );
obj->type = type;
obj->refc = 1;
return obj->data;
}
static void* ss_refer( void* ptr ) {
ss_Object* obj = ss_obj( ptr );
obj->refc++;
return ptr;
}
static void ss_free( void* ptr ) {
ss_Object* obj = ss_obj( ptr );
free( obj );
}
static void freePattern( void* ptr );
static void freeScanner( void* ptr );
static void freeContext( void* ptr );
static void freeMatch( void* ptr );
static void freeMap( void* ptr );
static void freeList( void* ptr );
static void freeBuffer( void* ptr );
static void freeCompiler( void* ptr );
static void freeIter( void* ptr );
static void (*freeFuns[])( void* ptr ) = {
freePattern,
freeScanner,
freeContext,
freeMatch,
freeMap,
freeList,
freeBuffer,
freeCompiler,
freeIter
};
void ss_release( void* ptr ) {
ss_Object* obj = ss_obj( ptr );
assert( obj->type < TYPE_LAST );
if( --obj->refc == 0 )
freeFuns[obj->type]( ptr );
}
/***************************** Map Implementation *****************************/
typedef struct ss_MapNode ss_MapNode;
struct ss_MapNode {
ss_MapNode* next;
unsigned hash;
void* value;
char key[];
};
struct ss_Map {
ss_MapNode** buf;
unsigned cnt;
unsigned cap;
ss_MapNode* staged;
};
static ss_Map* ss_mapNew( void ) {
ss_Map* map = ss_alloc( sizeof(ss_Map), TYPE_MAP );
map->cap = 21;
map->cnt = 0;
map->buf = xcalloc( map->cap, sizeof(ss_MapNode*) );
map->staged = NULL;
return map;
}
static unsigned hash( char const* key ) {
unsigned h = 0;
for( size_t i = 0 ; key[i] != '\0' ; i++ )
h = h*37 + key[i];
return h;
}
static void growMap( ss_Map* map, unsigned cap ) {
size_t ocap = map->cap;
ss_MapNode** obuf = map->buf;
map->cap = cap;
map->buf = xcalloc( map->cap, sizeof(ss_MapNode*) );
for( unsigned i = 0 ; i < ocap ; i++ ) {
ss_MapNode* it = obuf[i];
while( it ) {
ss_MapNode* node = it;
it = it->next;
unsigned j = node->hash % map->cap;
node->next = map->buf[j];
map->buf[j] = node;
}
}
free( obuf );
}
static void ss_mapPut( ss_Map* map, char const* key, void* val ) {
unsigned h = hash( key );
size_t keyLen = strlen( key );
ss_MapNode* node = xmalloc( sizeof(ss_MapNode) + keyLen + 1 );
node->next = map->staged;
node->hash = h;
node->value = ss_refer( val );
strcpy( node->key, key );
map->staged = node;
}
static void* ss_mapGet( ss_Map* map, char const* key ) {
unsigned h = hash( key );
unsigned i = h % map->cap;
ss_MapNode* it = map->buf[i];
while( it ) {
if( it->hash == h && !strcmp( key, it->key ) ) {
return it->value;
}
it = it->next;
}
return NULL;
}
static void ss_mapCommit( ss_Map* map ) {
for( ss_MapNode* it = map->staged ; it ; it = it->next )
map->cnt++;
if( map->cnt*3 > map->cap )
growMap( map, map->cnt*3 );
ss_MapNode* it = map->staged;
while( it ) {
ss_MapNode* node = it;
it = node->next;
unsigned i = node->hash % map->cap;
node->next = map->buf[i];
map->buf[i] = node;
}
map->staged = NULL;
}
static void ss_mapCancel( ss_Map* map ) {
ss_MapNode* it = map->staged;
while( it ) {
ss_MapNode* node = it;
it = node->next;
ss_release( node->value );
free( node );
}
map->staged = NULL;
}
static void freeMap( void* ptr ) {
ss_Map* map = ptr;
ss_mapCancel( map );
for( unsigned i = 0 ; i < map->cap ; i++ ) {
ss_MapNode* it = map->buf[i];
while( it ) {
ss_MapNode* node = it;
it = it->next;
ss_release( node->value );
free( node );
}
}
free( map->buf );
ss_free( map );
}
/**************************** List Implementation *****************************/
typedef struct ss_ListNode ss_ListNode;
struct ss_ListNode {
ss_ListNode* next;
void* value;
};
struct ss_List {
ss_ListNode* first;
ss_ListNode* last;
};
static ss_List* ss_listNew( void ) {
ss_List* list = ss_alloc( sizeof(ss_List), TYPE_LIST );
list->first = NULL;
list->last = NULL;
return list;
}
static void ss_listAdd( ss_List* list, void* val ) {
ss_ListNode* node = xmalloc( sizeof(ss_ListNode) );
node->value = ss_refer( val );
node->next = NULL;
if( list->first ) {
list->last->next = node;
list->last = node;
}
else {
list->first = node;
list->last = node;
}
}
struct ss_Iter {
ss_List* list;