line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# RDF::Trine::Parser::TriG |
2
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
=head1 NAME |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
RDF::Trine::Parser::TriG - TriG RDF Parser |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 VERSION |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
This document describes RDF::Trine::Parser::TriG version 1.018 |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 SYNOPSIS |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
use RDF::Trine::Parser; |
15
|
|
|
|
|
|
|
my $parser = RDF::Trine::Parser->new( 'trig' ); |
16
|
|
|
|
|
|
|
$parser->parse_into_model( $base_uri, $data, $model ); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 DESCRIPTION |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
... |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 METHODS |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
Beyond the methods documented below, this class inherits methods from the |
25
|
|
|
|
|
|
|
L<RDF::Trine::Parser> class. |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=over 4 |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=cut |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
package RDF::Trine::Parser::TriG; |
32
|
|
|
|
|
|
|
|
33
|
68
|
|
|
68
|
|
1604
|
use 5.010; |
|
68
|
|
|
|
|
252
|
|
34
|
68
|
|
|
68
|
|
380
|
use strict; |
|
68
|
|
|
|
|
153
|
|
|
68
|
|
|
|
|
1419
|
|
35
|
68
|
|
|
68
|
|
321
|
use warnings; |
|
68
|
|
|
|
|
143
|
|
|
68
|
|
|
|
|
2005
|
|
36
|
68
|
|
|
68
|
|
324
|
no warnings 'redefine'; |
|
68
|
|
|
|
|
149
|
|
|
68
|
|
|
|
|
2190
|
|
37
|
68
|
|
|
68
|
|
337
|
no warnings 'once'; |
|
68
|
|
|
|
|
160
|
|
|
68
|
|
|
|
|
1765
|
|
38
|
68
|
|
|
68
|
|
394
|
use base qw(RDF::Trine::Parser::Turtle); |
|
68
|
|
|
|
|
158
|
|
|
68
|
|
|
|
|
5780
|
|
39
|
68
|
|
|
68
|
|
496
|
use RDF::Trine::Parser::Turtle::Constants; |
|
68
|
|
|
|
|
165
|
|
|
68
|
|
|
|
|
8457
|
|
40
|
68
|
|
|
68
|
|
411
|
use RDF::Trine qw(literal); |
|
68
|
|
|
|
|
159
|
|
|
68
|
|
|
|
|
5785
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
our ($VERSION); |
43
|
|
|
|
|
|
|
BEGIN { |
44
|
68
|
|
|
68
|
|
262
|
$VERSION = '1.018'; |
45
|
68
|
|
|
|
|
190
|
$RDF::Trine::Parser::parser_names{ 'trig' } = __PACKAGE__; |
46
|
68
|
|
|
|
|
185
|
foreach my $ext (qw(trig)) { |
47
|
68
|
|
|
|
|
57110
|
$RDF::Trine::Parser::file_extensions{ $ext } = __PACKAGE__; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
# foreach my $type (qw(application/x-turtle application/turtle text/turtle)) { |
50
|
|
|
|
|
|
|
# $RDF::Trine::Parser::media_types{ $type } = __PACKAGE__; |
51
|
|
|
|
|
|
|
# } |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub _assert_triple { |
55
|
31
|
|
|
31
|
|
44
|
my $self = shift; |
56
|
31
|
|
|
|
|
49
|
my $subj = shift; |
57
|
31
|
|
|
|
|
43
|
my $pred = shift; |
58
|
31
|
|
|
|
|
46
|
my $obj = shift; |
59
|
31
|
|
|
|
|
52
|
my $graph = $self->{graph}; |
60
|
|
|
|
|
|
|
|
61
|
31
|
0
|
33
|
|
|
87
|
if ($self->{canonicalize} and blessed($obj) and $obj->isa('RDF::Trine::Node::Literal')) { |
|
|
|
33
|
|
|
|
|
62
|
0
|
|
|
|
|
0
|
$obj = $obj->canonicalize; |
63
|
|
|
|
|
|
|
} |
64
|
31
|
|
|
|
|
119
|
my $st = RDF::Trine::Statement::Quad->new( $subj, $pred, $obj, $graph ); |
65
|
|
|
|
|
|
|
|
66
|
31
|
50
|
|
|
|
90
|
if (my $code = $self->{handle_triple}) { |
67
|
31
|
|
|
|
|
84
|
$code->( $st ); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
31
|
|
|
|
|
96
|
my $count = ++$self->{triple_count}; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub _statement { |
74
|
23
|
|
|
23
|
|
43
|
my $self = shift; |
75
|
23
|
|
|
|
|
36
|
my $l = shift; |
76
|
23
|
|
|
|
|
32
|
my $t = shift; |
77
|
23
|
|
|
|
|
528
|
my $type = $t->type; |
78
|
|
|
|
|
|
|
# warn '--> ' . decrypt_constant($type); |
79
|
23
|
100
|
33
|
|
|
140
|
if ($type == LBRACE) { return $self->_graph($l, $t); } |
|
2
|
50
|
0
|
|
|
8
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
80
|
0
|
|
|
|
|
0
|
elsif ($type == LBRACKET) { return $self->_graph($l, $t); } |
81
|
0
|
|
|
|
|
0
|
elsif ($type == BNODE) { return $self->_graph($l, $t); } |
82
|
0
|
|
|
|
|
0
|
elsif ($type == EQUALS) { return $self->_graph($l, $t); } |
83
|
3
|
|
|
|
|
9
|
elsif ($type == IRI) { return $self->_graph($l, $t); } |
84
|
4
|
|
|
|
|
14
|
elsif ($type == PREFIXNAME) { return $self->_graph($l, $t); } |
85
|
|
|
|
|
|
|
elsif ($type == WS) {} |
86
|
|
|
|
|
|
|
elsif ($type == PREFIX or $type == SPARQLPREFIX) { |
87
|
14
|
|
|
|
|
47
|
$t = $self->_get_token_type($l, PREFIXNAME); |
88
|
14
|
|
|
|
|
42
|
my $name = $t->value; |
89
|
14
|
|
|
|
|
49
|
$name =~ s/:$//; |
90
|
14
|
|
|
|
|
51
|
$t = $self->_get_token_type($l, IRI); |
91
|
14
|
|
|
|
|
344
|
my $r = RDF::Trine::Node::Resource->new($t->value, $self->{baseURI}); |
92
|
14
|
|
|
|
|
43
|
my $iri = $r->uri_value; |
93
|
14
|
50
|
|
|
|
39
|
if ($type == PREFIX) { |
94
|
14
|
|
|
|
|
48
|
$t = $self->_get_token_type($l, DOT); |
95
|
|
|
|
|
|
|
} |
96
|
14
|
|
|
|
|
361
|
$self->{map}->add_mapping( $name => $iri ); |
97
|
14
|
50
|
|
|
|
191
|
if (my $ns = $self->{namespaces}) { |
98
|
0
|
0
|
|
|
|
0
|
unless ($ns->namespace_uri($name)) { |
99
|
0
|
|
|
|
|
0
|
$ns->add_mapping( $name => $iri ); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
elsif ($type == BASE or $type == SPARQLBASE) { |
104
|
0
|
|
|
|
|
0
|
$t = $self->_get_token_type($l, IRI); |
105
|
0
|
|
|
|
|
0
|
my $r = RDF::Trine::Node::Resource->new($t->value, $self->{baseURI}); |
106
|
0
|
|
|
|
|
0
|
my $iri = $r->uri_value; |
107
|
0
|
0
|
|
|
|
0
|
if ($type == BASE) { |
108
|
0
|
|
|
|
|
0
|
$t = $self->_get_token_type($l, DOT); |
109
|
|
|
|
|
|
|
} |
110
|
0
|
|
|
|
|
0
|
$self->{baseURI} = $iri; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
else { |
113
|
0
|
|
|
|
|
0
|
$self->_throw_error("Expecting statement but got " . decrypt_constant($type), $t, $l); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub _graph { |
118
|
9
|
|
|
9
|
|
12
|
my $self = shift; |
119
|
9
|
|
|
|
|
18
|
my $l = shift; |
120
|
9
|
|
|
|
|
12
|
my $t = shift; |
121
|
9
|
|
|
|
|
206
|
my $type = $t->type; |
122
|
9
|
100
|
100
|
|
|
44
|
if ($type == IRI or $type == PREFIXNAME) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
123
|
7
|
|
|
|
|
27
|
$self->{graph} = $self->_token_to_node($t); |
124
|
7
|
|
|
|
|
16
|
my $old_token = $t; |
125
|
7
|
|
|
|
|
20
|
$t = $self->_next_nonws($l); |
126
|
7
|
50
|
|
|
|
24
|
unless (defined($t)) { |
127
|
0
|
|
|
|
|
0
|
$l->_throw_error("Unexpected EOF after graph"); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
} elsif ($type == BNODE) { |
130
|
0
|
|
|
|
|
0
|
$self->{graph} = $self->_token_to_node($t); |
131
|
0
|
|
|
|
|
0
|
$t = $self->_next_nonws($l); |
132
|
|
|
|
|
|
|
} elsif ($type == LBRACKET) { |
133
|
0
|
|
|
|
|
0
|
$t = $self->_get_token_type($l, RBRACKET); |
134
|
0
|
|
|
|
|
0
|
$t = $self->_next_nonws($l); |
135
|
0
|
|
|
|
|
0
|
$self->{graph} = RDF::Trine::Node::Blank->new(); |
136
|
|
|
|
|
|
|
} else { |
137
|
2
|
|
|
|
|
15
|
$self->{graph} = RDF::Trine::Node::Nil->new(); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
9
|
100
|
|
|
|
171
|
if ($t->type == EQUALS) { |
141
|
1
|
|
|
|
|
4
|
$t = $self->_next_nonws($l); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
9
|
50
|
|
|
|
205
|
if ($t->type != LBRACE) { |
145
|
0
|
|
|
|
|
0
|
$self->_throw_error("Expecting LBRACE but got " . decrypt_constant($type), $t, $l); |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
9
|
|
|
|
|
28
|
$t = $self->_next_nonws($l); |
149
|
9
|
|
|
|
|
179
|
while (1) { |
150
|
32
|
|
|
|
|
758
|
my $type = $t->type; |
151
|
32
|
100
|
33
|
|
|
287
|
unless ($type == LBRACKET or $type == LPAREN or $type == IRI or $type == PREFIXNAME or $type == BNODE) { |
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
152
|
6
|
|
|
|
|
22
|
$self->_unget_token($t); |
153
|
6
|
|
|
|
|
9
|
last; |
154
|
|
|
|
|
|
|
} |
155
|
26
|
|
|
|
|
80
|
$self->_triple($l, $t); |
156
|
26
|
|
|
|
|
74
|
$t = $self->_next_nonws($l); |
157
|
26
|
100
|
|
|
|
667
|
if ($t->type == RBRACE) { |
|
|
50
|
|
|
|
|
|
158
|
3
|
|
|
|
|
9
|
$self->_unget_token($t); |
159
|
3
|
|
|
|
|
5
|
last; |
160
|
|
|
|
|
|
|
} elsif ($t->type == DOT) { |
161
|
23
|
|
|
|
|
69
|
$t = $self->_next_nonws($l); |
162
|
23
|
|
|
|
|
594
|
next; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
9
|
|
|
|
|
30
|
$t = $self->_get_token_type($l, RBRACE); |
167
|
9
|
|
|
|
|
25
|
$t = $self->_next_nonws($l); |
168
|
9
|
100
|
|
|
|
225
|
return unless defined($t); |
169
|
6
|
100
|
|
|
|
136
|
unless ($t->type == DOT) { |
170
|
5
|
|
|
|
|
17
|
$self->_unget_token($t); |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub _triple { |
175
|
26
|
|
|
26
|
|
39
|
my $self = shift; |
176
|
26
|
|
|
|
|
44
|
my $l = shift; |
177
|
26
|
|
|
|
|
35
|
my $t = shift; |
178
|
26
|
|
|
|
|
601
|
my $type = $t->type; |
179
|
|
|
|
|
|
|
# subject |
180
|
26
|
|
|
|
|
44
|
my $subj; |
181
|
26
|
50
|
66
|
|
|
154
|
if ($type == LBRACKET) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
182
|
0
|
|
|
|
|
0
|
$subj = RDF::Trine::Node::Blank->new(); |
183
|
0
|
|
|
|
|
0
|
my $t = $self->_next_nonws($l); |
184
|
0
|
0
|
|
|
|
0
|
if ($t->type != RBRACKET) { |
185
|
0
|
|
|
|
|
0
|
$self->_unget_token($t); |
186
|
0
|
|
|
|
|
0
|
$self->_predicateObjectList( $l, $subj ); |
187
|
0
|
|
|
|
|
0
|
$t = $self->_get_token_type($l, RBRACKET); |
188
|
|
|
|
|
|
|
|
189
|
0
|
|
|
|
|
0
|
$t = $self->_next_nonws($l); |
190
|
0
|
0
|
|
|
|
0
|
return unless defined($t); |
191
|
0
|
|
|
|
|
0
|
$self->_unget_token($t); |
192
|
0
|
0
|
|
|
|
0
|
if ($t->type == DOT) { |
193
|
0
|
|
|
|
|
0
|
return; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
} elsif ($type == LPAREN) { |
197
|
0
|
|
|
|
|
0
|
my $t = $self->_next_nonws($l); |
198
|
0
|
0
|
|
|
|
0
|
if ($t->type == RPAREN) { |
199
|
0
|
|
|
|
|
0
|
$subj = RDF::Trine::Node::Resource->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'); |
200
|
|
|
|
|
|
|
} else { |
201
|
0
|
|
|
|
|
0
|
$subj = RDF::Trine::Node::Blank->new(); |
202
|
0
|
|
|
|
|
0
|
my @objects = $self->_object($l, $t); |
203
|
|
|
|
|
|
|
|
204
|
0
|
|
|
|
|
0
|
while (1) { |
205
|
0
|
|
|
|
|
0
|
my $t = $self->_next_nonws($l); |
206
|
0
|
0
|
|
|
|
0
|
if ($t->type == RPAREN) { |
207
|
0
|
|
|
|
|
0
|
last; |
208
|
|
|
|
|
|
|
} else { |
209
|
0
|
|
|
|
|
0
|
push(@objects, $self->_object($l, $t)); |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
} |
212
|
0
|
|
|
|
|
0
|
$self->_assert_list($subj, @objects); |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
} elsif (not($type==IRI or $type==PREFIXNAME or $type==BNODE)) { |
215
|
0
|
|
|
|
|
0
|
$self->_throw_error("Expecting resource or bnode but got " . decrypt_constant($type), $t, $l); |
216
|
|
|
|
|
|
|
} else { |
217
|
26
|
|
|
|
|
78
|
$subj = $self->_token_to_node($t); |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
# warn "Subject: $subj\n"; |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
#predicateObjectList |
222
|
26
|
|
|
|
|
85
|
$self->_predicateObjectList($l, $subj); |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
1; |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
__END__ |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=back |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head1 BUGS |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
Please report any bugs or feature requests to through the GitHub web interface |
235
|
|
|
|
|
|
|
at L<https://github.com/kasei/perlrdf/issues>. |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head1 SEE ALSO |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
L<http://www4.wiwiss.fu-berlin.de/bizer/TriG/> |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=head1 AUTHOR |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
Gregory Todd Williams C<< <gwilliams@cpan.org> >> |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head1 COPYRIGHT |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
Copyright (c) 2006-2012 Gregory Todd Williams. This |
248
|
|
|
|
|
|
|
program is free software; you can redistribute it and/or modify it under |
249
|
|
|
|
|
|
|
the same terms as Perl itself. |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=cut |