forked from SWI-Prolog/packages-semweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turtle.c
3511 lines (2810 loc) · 71.8 KB
/
turtle.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
/* Part of SWI-Prologs
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 2013, VU University Amsterdam
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include <config.h>
#if defined(__sun) || __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070
#undef HAVE_WCSDUP /* there is no prototype */
#undef HAVE_WCSCASECMP /* same problem */
#endif
#ifdef HAVE_VISIBILITY_ATTRIBUTE
#define SO_LOCAL __attribute__((visibility("hidden")))
#else
#define SO_LOCAL
#endif
#define COMMON(type) SO_LOCAL type
#include <SWI-Stream.h>
#include <SWI-Prolog.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <assert.h>
#include "murmur.h"
#include "turtle_chars.c"
#ifdef __WINDOWS__
#define swprintf _snwprintf
#endif
install_t install_turtle(void);
static functor_t FUNCTOR_error2;
static functor_t FUNCTOR_existence_error2;
static functor_t FUNCTOR_syntax_error1;
static functor_t FUNCTOR_stream4;
static functor_t FUNCTOR_node1;
static functor_t FUNCTOR_rdf3;
static functor_t FUNCTOR_rdf4;
static functor_t FUNCTOR_literal1;
static functor_t FUNCTOR_lang2;
static functor_t FUNCTOR_type2;
static functor_t FUNCTOR_pair2;
static functor_t FUNCTOR_colon2;
static atom_t ATOM_parse;
static atom_t ATOM_statement;
static atom_t ATOM_document;
static atom_t ATOM_count;
static atom_t ATOM_error_count;
static atom_t ATOM_anon_prefix;
static atom_t ATOM_base_uri;
static atom_t ATOM_on_error;
static atom_t ATOM_error;
static atom_t ATOM_warning;
static atom_t ATOM_format;
static atom_t ATOM_turtle;
static atom_t ATOM_trig;
static atom_t ATOM_graph;
static atom_t ATOM_auto;
#define RDF_NS L"http://www.w3.org/1999/02/22-rdf-syntax-ns#"
#define XSD_NS L"http://www.w3.org/2001/XMLSchema#"
#define RDF_TYPE (RDF_NS L"type")
#define RDF_FIRST (RDF_NS L"first")
#define RDF_REST (RDF_NS L"rest")
#define RDF_NIL (RDF_NS L"nil")
#define XSD_INTEGER (XSD_NS L"integer")
#define XSD_DECIMAL (XSD_NS L"decimal")
#define XSD_DOUBLE (XSD_NS L"double")
#define XSD_BOOLEAN (XSD_NS L"boolean")
/*******************************
* PORTABILITY *
*******************************/
#ifndef HAVE_WCSDUP
static wchar_t *
my_wcsdup(const wchar_t *in)
{ wchar_t *copy = malloc((wcslen(in)+1)*sizeof(wchar_t));
if ( copy )
return wcscpy(copy, in);
return NULL;
}
#define wcsdup(ws) my_wcsdup(ws)
#endif
#ifndef HAVE_WCSCASECMP
#include <wctype.h>
static int
my_wcscasecmp(const wchar_t *s1, const wchar_t *s2)
{ wint_t n1, n2;
if (s1 == s2)
return 0;
do
{ n1 = towlower(*s1++);
n2 = towlower(*s2++);
if (n1 == L'\0')
break;
} while (n1 == n2);
return n1 - n2;
}
#define wcscasecmp(s1, s2) my_wcscasecmp(s1, s2)
#endif
/*******************************
* DATA STRUCTURES *
*******************************/
#define FAST_URI 128 /* Avoid malloc for shorted ones */
typedef enum resource_type
{ R_BNODE,
R_RESOURCE
} resource_type;
typedef enum object_type
{ O_RESOURCE,
O_LITERAL
} object_type;
typedef enum error_mode
{ E_WARNING = 0, /* default */
E_ERROR
} error_mode;
typedef enum format
{ D_AUTO = 0,
D_TURTLE,
D_TRIG
} format;
typedef struct resource
{ resource_type type;
int constant; /* read-only constant */
union
{ struct
{ wchar_t *name; /* Name of the IRI */
atom_t handle; /* Handle to Prolog atom */
wchar_t fast[FAST_URI]; /* Buffer for short URIs */
} r;
size_t bnode_id;
struct resource *next; /* Next in free list */
} v;
} resource;
typedef struct object
{ object_type type;
union
{ resource *r;
struct
{ size_t len;
wchar_t *string;
wchar_t *lang;
resource *type;
} l;
} value;
} object;
typedef struct hash_cell
{ wchar_t *name; /* Name of the entry */
struct hash_cell *next; /* Hash link */
struct
{ wchar_t *s; /* Value as a string */
size_t bnode_id; /* Blank node id */
} value;
} hash_cell;
typedef struct hash_map
{ size_t count; /* number of entries in map */
size_t size; /* Size of entries array */
hash_cell **entries;
} hash_map;
typedef struct turtle_state
{ wchar_t *base_uri; /* Base URI for <> */
size_t base_uri_len; /* Length of base uri */
size_t base_uri_base_len; /* Length upto last / */
wchar_t *empty_prefix; /* Empty :local */
hash_map prefix_map; /* Prefix --> IRI */
hash_map blank_node_map; /* Name --> resource */
size_t bnode_id; /* Blank node identifiers */
struct
{ wchar_t *prefix;
wchar_t *buffer;
wchar_t *prefix_end;
} bnode;
resource *current_subject;
resource *current_predicate;
resource *current_graph;
resource *default_graph;
resource *free_resources; /* Recycle resources */
IOSTREAM *input; /* Our input */
int current_char; /* Current character */
error_mode on_error; /* E_* */
format format; /* D_AUTO, D_TURTLE or D_TRIG */
size_t error_count; /* Number of syntax errors */
size_t count; /* Counted triples */
term_t head; /* Head of triple list */
term_t tail; /* Tail of triple list */
} turtle_state;
typedef enum
{ NUM_INTEGER,
NUM_DECIMAL,
NUM_DOUBLE
} number_type;
#define RES_CONST(uri) { R_RESOURCE, TRUE, {{ uri, 0, {0} }} }
#define RDF_TYPE_INDEX 0
#define RDF_FIRST_INDEX 1
#define RDF_REST_INDEX 2
#define RDF_NIL_INDEX 3
#define XSD_INTEGER_INDEX 4
#define XSD_DECIMAL_INDEX 5
#define XSD_DOUBLE_INDEX 6
#define XSD_BOOLEAN_INDEX 7
#define RESOURCE(name) (&resource_constants[name ## _INDEX])
static resource resource_constants[] =
{ RES_CONST(RDF_TYPE), /* 0 */
RES_CONST(RDF_FIRST), /* 1 */
RES_CONST(RDF_REST), /* 2 */
RES_CONST(RDF_NIL), /* 3 */
RES_CONST(XSD_INTEGER), /* 4 */
RES_CONST(XSD_DECIMAL), /* 5 */
RES_CONST(XSD_DOUBLE), /* 6 */
RES_CONST(XSD_BOOLEAN) /* 7 */
};
hash_cell *lookup_hash_map(hash_map *hm, const wchar_t *name);
/*******************************
* BUFFER *
*******************************/
#ifdef STRING_BUF_DEBUG
#define FAST_BUF_SIZE 1
#else
#define FAST_BUF_SIZE 512
#endif
typedef struct string_buffer
{ wchar_t fast[FAST_BUF_SIZE];
wchar_t *buf;
wchar_t *in;
wchar_t *end;
#ifdef STRING_BUF_DEBUG
int discarded;
#endif
} string_buffer;
static int
growBuffer(string_buffer *b, int c)
{ if ( b->buf == b->fast )
{ wchar_t *new = malloc((FAST_BUF_SIZE*2)*sizeof(wchar_t));
if ( new )
{ memcpy(new, b->fast, sizeof(b->fast));
b->buf = new;
b->in = b->buf+FAST_BUF_SIZE;
b->end = b->in+FAST_BUF_SIZE;
*b->in++ = c;
return TRUE;
}
} else
{ size_t sz = b->end - b->buf;
wchar_t *new = realloc(b->buf, sz*sizeof(wchar_t)*2);
if ( new )
{ b->buf = new;
b->in = new+sz;
b->end = b->in+sz;
*b->in++ = c;
return TRUE;
}
}
return PL_resource_error("memory");
}
static inline void
initBuf(string_buffer *b)
{ b->buf = b->fast;
b->in = b->buf;
b->end = &b->fast[FAST_BUF_SIZE];
#ifdef STRING_BUF_DEBUG
b->discarded = FALSE;
#endif
}
static inline void
discardBuf(string_buffer *b)
{
#ifdef STRING_BUF_DEBUG
assert(b->discarded == FALSE);
b->discarded = TRUE;
#endif
if ( b->buf != b->fast )
free(b->buf);
}
static inline int
addBuf(string_buffer *b, int c)
{ if ( b->in < b->end )
{ *b->in++ = c;
return TRUE;
}
return growBuffer(b, c);
}
static inline int
bufSize(string_buffer *b)
{ return b->in - b->buf;
}
#define baseBuf(b) ( (b)->buf )
/*******************************
* CHARACTER CLASSES *
*******************************/
#define EOS ((wchar_t)(0))
#define WS 0x01 /* white space */
#define EL 0x02 /* end-of-line */
#define DI 0x04 /* digit */
#define LC 0x08 /* digit */
#define UC 0x10 /* digit */
#define IV 0x20 /* invalid */
#define EF 0x80 /* END-OF-FILE */
#define NI 0x100 /* Not IRI */
#define EC 0x200 /* Local escape */
static const short char_type0[] =
{ /*0 1 2 3 4 5 6 7
8 9 A B C D E F */
EF,
NI, NI, NI, NI, NI, NI, NI, NI, /* 00-07 */
NI, WS|NI, EL|NI, NI, NI, EL|NI, NI, NI, /* 08-0f */
NI, NI, NI, NI, NI, NI, NI, NI, /* 10-17 */
NI, NI, NI, NI, NI, NI, NI, NI, /* 18-1F */
NI|WS, EC, NI, EC, EC, EC, EC, EC, /* 20-27 */
EC, EC, EC, EC, EC, EC, EC, EC, /* 28-2F */
DI, DI, DI, DI, DI, DI, DI, DI, /* 30-37 */
DI, DI, 0, EC, NI, EC, NI, EC, /* 38-3F */
EC, UC, UC, UC, UC, UC, UC, UC, /* 40-47 */
UC, UC, UC, UC, UC, UC, UC, UC, /* 48-4F */
UC, UC, UC, UC, UC, UC, UC, UC, /* 50-57 */
UC, UC, UC, 0, NI, 0, NI, EC, /* 58-5F */
NI, LC, LC, LC, LC, LC, LC, LC, /* 60-67 */
LC, LC, LC, LC, LC, LC, LC, LC, /* 68-6F */
LC, LC, LC, LC, LC, LC, LC, LC, /* 70-77 */
LC, LC, LC, NI, NI, NI, EC, 0 /* 78-7F */
};
static const short* char_type = &char_type0[1];
static inline int
is_ws(int c)
{ return (c < 128 ? (char_type[c] & (WS|EL)) != 0 : FALSE);
}
static inline int
is_eol(int c)
{ return (c < 128 ? (char_type[c] & EL) != 0 : FALSE);
}
static inline int
is_digit(int c)
{ return (c < 128 ? (char_type[c] & DI) != 0 : FALSE);
}
static inline int
is_scheme_char(int c)
{ return (c < 128 ? (char_type[c] & (LC|UC)) != 0 : FALSE);
}
static inline int
is_lang_char1(int c)
{ return (c < 128 ? (char_type[c] & (LC|UC)) != 0 : FALSE);
}
static inline int
is_lang_char(int c)
{ return (c < 128 ? (char_type[c] & (LC|UC|DI)) != 0 : FALSE) || c == '-';
}
static inline int
is_local_escape(int c)
{ return (c < 128 ? (char_type[c] & EC) != 0 : FALSE);
}
static inline int
is_iri_char(int c)
{ return (c < 128 ? (char_type[c] & NI) == 0 : TRUE);
}
static const char hexval0[] =
{/*0 1 2 3 4 5 6 7 8 9 A B C D E F */
-1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00-0f */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10-1F */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20-2F */
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 30-3F */
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 40-4F */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 50-5F */
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 /* 60-6F */
};
static const char* hexval = &hexval0[1];
static inline int
hexd(int c)
{ return (c <= 'f' ? hexval[c] : -1);
}
/*******************************
* HASHES *
*******************************/
#define INIT_PREFIX_SIZE 64
#define INIT_BNODE_SIZE 64
static int
init_hash_map(hash_map *hm, int size)
{ hm->entries = malloc(sizeof(*hm->entries)*size);
if ( hm->entries )
{ memset(hm->entries, 0, sizeof(*hm->entries)*size);
hm->count = 0;
hm->size = size;
return TRUE;
}
return FALSE;
}
static void
free_hash_cell(hash_cell *c)
{ if ( c->name ) free(c->name);
if ( c->value.s ) free(c->value.s);
free(c);
}
static void
clear_hash_table(hash_map *hm)
{ size_t i;
for(i=0; i<hm->size; i++) /* works for uninitialized map */
{ hash_cell *c, *n;
for(c=hm->entries[i]; c; c=n)
{ n = c->next;
free_hash_cell(c);
}
}
free(hm->entries);
}
static int
wcs_hash(const wchar_t *name)
{ size_t len = wcslen(name);
return rdf_murmer_hash(name, len*sizeof(wchar_t), MURMUR_SEED);
}
hash_cell *
lookup_hash_map(hash_map *hm, const wchar_t *name)
{ int key = wcs_hash(name) % hm->size;
hash_cell *c;
for(c = hm->entries[key]; c; c=c->next)
{ if ( wcscmp(name, c->name) == 0 )
return c;
}
return NULL;
}
static int
add_hash_map(hash_map *hm, hash_cell *c)
{ int key = wcs_hash(c->name) % hm->size;
c->next = hm->entries[key];
hm->entries[key] = c;
return TRUE;
}
static int
add_string_hash_map(hash_map *hm,
const wchar_t *name, const wchar_t *value)
{ hash_cell *c;
if ( (c=malloc(sizeof(*c))) )
{ c->name = wcsdup(name);
c->value.s = wcsdup(value);
return add_hash_map(hm, c);
}
return PL_resource_error("memory");
}
/*******************************
* ERROR *
*******************************/
static int next(turtle_state *ts);
static int
print_warning(term_t t)
{ static predicate_t print_message2;
term_t av;
if ( !print_message2 )
print_message2 = PL_predicate("print_message", 2, "system");
if ( (av = PL_new_term_refs(2)) &&
PL_put_atom(av+0, ATOM_warning) &&
PL_put_term(av+1, t) )
return PL_call_predicate(NULL, PL_Q_PASS_EXCEPTION, print_message2, av);
return FALSE;
}
static int
syntax_error(turtle_state *ts, const char *msg)
{ term_t ex;
IOPOS *pos;
if ( PL_exception(0) ) /* pending earlier exception */
return FALSE;
ts->error_count++;
if ( !(ex=PL_new_term_refs(2)) ||
!PL_unify_term(ex+0, PL_FUNCTOR, FUNCTOR_syntax_error1,
PL_CHARS, msg) )
return FALSE;
if ( (pos=ts->input->position) )
{ term_t stream;
int linepos = pos->linepos;
int64_t charno = pos->charno;
if ( linepos > 0 )
{ linepos--;
charno--;
}
if ( !(stream = PL_new_term_ref()) ||
!PL_unify_stream(stream, ts->input) ||
!PL_unify_term(ex+1,
PL_FUNCTOR, FUNCTOR_stream4,
PL_TERM, stream,
PL_INT, (int)pos->lineno,
PL_INT, linepos,
PL_INT64, charno) )
return FALSE;
}
if ( PL_cons_functor_v(ex, FUNCTOR_error2, ex) )
{ for(;;)
{ if ( !next(ts) || ts->current_char == -1 )
break;
if ( ts->current_char == '.' )
{ next(ts);
if ( is_ws(ts->current_char) )
break;
}
}
if ( ts->on_error == E_ERROR )
return PL_raise_exception(ex);
else
print_warning(ex);
}
return FALSE;
}
/*******************************
* SKIPPING *
*******************************/
static int
skip_ws_no_comment(turtle_state *ts)
{ int c = ts->current_char;
while(is_ws(c))
c = Sgetcode(ts->input);
ts->current_char = c;
return !Sferror(ts->input);
}
static int
skip_comment_line(turtle_state *ts)
{ int c;
do
{ c = Sgetcode(ts->input);
} while ( c != -1 && !is_eol(c) );
while(is_eol(c))
c = Sgetcode(ts->input);
ts->current_char = c;
return !Sferror(ts->input);
}
/* skip_ws() skips white zero or more white space and #-comments
*/
static int
skip_ws(turtle_state *ts)
{ for(;;)
{ if ( skip_ws_no_comment(ts) )
{ if ( ts->current_char == '#' )
{ if ( skip_comment_line(ts) )
continue;
return FALSE;
}
return TRUE;
}
return FALSE;
}
}
static int
next(turtle_state *ts)
{ ts->current_char = Sgetcode(ts->input);
return !Sferror(ts->input);
}
/*******************************
* RESOURCES *
*******************************/
static void free_resource(turtle_state *ts, resource *r);
static resource *
alloc_resource(turtle_state *ts)
{ resource *r;
if ( (r=ts->free_resources) )
{ ts->free_resources = r->v.next;
return r;
}
if ( (r=malloc(sizeof(*r))) )
r->constant = FALSE;
else
PL_resource_error("memory");
return r;
}
static resource *
set_uri_resource(turtle_state *ts, resource *r, const wchar_t *iri)
{ size_t len = wcslen(iri);
r->type = R_RESOURCE;
r->v.r.handle = 0;
if ( len < FAST_URI )
{ wcscpy(r->v.r.fast, iri);
r->v.r.name = r->v.r.fast;
} else
{ if ( !(r->v.r.name = wcsdup(iri)) )
{ free_resource(ts, r);
PL_resource_error("memory");
return NULL;
}
}
return r;
}
static resource *
new_resource(turtle_state *ts, const wchar_t *iri)
{ resource *r;
if ( (r=alloc_resource(ts)) )
return set_uri_resource(ts, r, iri);
PL_resource_error("memory");
return NULL;
}
static int
set_atom_resource(resource *r, atom_t handle)
{ if ( r->v.r.handle )
{ if ( r->v.r.handle == handle )
return TRUE;
PL_unregister_atom(r->v.r.handle);
}
r->v.r.handle = handle;
return TRUE;
}
static resource *
atom_resource(turtle_state *ts, atom_t handle)
{ resource *r;
if ( (r=alloc_resource(ts)) )
{ PL_register_atom(handle);
r->v.r.handle = handle;
r->v.r.name = NULL;
return r;
}
return NULL;
}
static resource *
new_bnode_from_id(turtle_state *ts, size_t id)
{ resource *r;
if ( (r=alloc_resource(ts)) )
{ r->type = R_BNODE;
r->v.bnode_id = id;
return r;
}
PL_resource_error("memory");
return NULL;
}
static resource *
new_bnode(turtle_state *ts)
{ return new_bnode_from_id(ts, ++ts->bnode_id);
}
static void
clear_handle_resource(resource *r)
{ if ( !r->constant && r->v.r.handle )
PL_unregister_atom(r->v.r.handle);
}
static void
clear_resource(resource *r)
{ if ( !r->constant && r->type == R_RESOURCE )
{ if ( r->v.r.name && r->v.r.name != r->v.r.fast )
free(r->v.r.name);
clear_handle_resource(r);
}
}
static void
free_resource(turtle_state *ts, resource *r)
{ if ( !r->constant )
{ clear_resource(r);
r->v.next = ts->free_resources;
ts->free_resources = r;
}
}
static void
free_resources(turtle_state *ts)
{ resource *r, *n;
for(r=ts->free_resources; r; r=n)
{ n = r->v.next;
free(r);
}
}
static int
turtle_existence_error(turtle_state *ts, const char *what, term_t t)
{ if ( ts->on_error == E_ERROR )
{ return PL_existence_error(what, t);
} else
{ term_t ex = PL_new_term_ref();
if ( PL_unify_term(ex,
PL_FUNCTOR, FUNCTOR_error2,
PL_FUNCTOR, FUNCTOR_existence_error2,
PL_CHARS, what,
PL_TERM, t,
PL_VARIABLE) )
print_warning(ex);
return FALSE;
}
}
static resource *
resolve_iri(turtle_state *ts, wchar_t *prefix, wchar_t *local)
{ const wchar_t *prefix_iri;
if ( prefix )
{ hash_cell *c;
if ( (c=lookup_hash_map(&ts->prefix_map, prefix)) )
{ prefix_iri = c->value.s;
} else
{ term_t t = PL_new_term_ref();
if ( PL_unify_wchars(t, PL_ATOM, (size_t)-1, prefix) )
turtle_existence_error(ts, "turtle_prefix", t);
return FALSE;
}
} else if ( ts->empty_prefix )
{ prefix_iri = ts->empty_prefix;
} else
{ term_t t = PL_new_term_ref();
if ( PL_unify_wchars(t, PL_ATOM, 0, L"") )
turtle_existence_error(ts, "turtle_prefix", t);
return FALSE;
}
if ( local )
{ size_t plen = wcslen(prefix_iri);
size_t llen = wcslen(local);
resource *r;
if ( (r=alloc_resource(ts)) )
{ wchar_t *name;
if ( plen+llen < FAST_URI )
{ name = r->v.r.fast;
} else
{ if ( !(name = malloc((plen+llen+1)*sizeof(wchar_t))) )
{ free_resource(ts, r);
PL_resource_error("memory");
return NULL;
}
}
wcscpy(name, prefix_iri);
wcscpy(name+plen, local);
r->type = R_RESOURCE;
r->v.r.name = name;
r->v.r.handle = 0;
return r;
}
return NULL;
} else
{ return new_resource(ts, prefix_iri);
}
}
static resource *
make_absolute_resource(turtle_state *ts, const wchar_t *uri)
{ const wchar_t *s;
size_t len, plen;
resource *r;
if ( !uri[0] ) /* <> */
return new_resource(ts, ts->base_uri);
if ( is_scheme_char(uri[0]) ) /* absolute uri */
{ for(s=&uri[1]; *s && is_scheme_char(*s); s++)
;
if ( *s == ':' )
return new_resource(ts, uri);
}
len = wcslen(uri);
if ( uri[0] == '#' ) /* relative to file */
plen = ts->base_uri_len;
else
plen = ts->base_uri_base_len;
if ( (r=alloc_resource(ts)) )
{ wchar_t *name;
if ( plen+len < FAST_URI )
{ name = r->v.r.fast;
} else
{ if ( !(name = malloc((plen+len+1)*sizeof(wchar_t))) )
{ free_resource(ts, r);
PL_resource_error("memory");
return NULL;
}
}
wcsncpy(name, ts->base_uri, plen);
wcscpy(name+plen, uri);
r->type = R_RESOURCE;
r->v.r.name = name;
r->v.r.handle = 0;
return r;
}
return NULL;
}
/*******************************
* PARSER OBJECT *
*******************************/
static void clear_turtle_parser(turtle_state *ts);
static int init_base_uri(turtle_state *ts);
static int read_predicate_object_list(turtle_state *ts, int end);
static int read_object(turtle_state *ts);
static int set_subject(turtle_state *ts, resource *r, resource **old);
static int set_predicate(turtle_state *ts, resource *r, resource **old);
static int set_graph(turtle_state *ts, resource *r, resource **old);
static int set_default_graph(turtle_state *ts, resource *r, resource **old);
static int statement(turtle_state *ts);
static turtle_state *
new_turtle_parser(IOSTREAM *s)
{ turtle_state *ts = malloc(sizeof(*ts));
if ( ts )
{ memset(ts, 0, sizeof(*ts));
ts->input = s;
if ( init_hash_map(&ts->prefix_map, INIT_PREFIX_SIZE) &&
next(ts) ) /* read first character */