rdf-n3 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,10 @@
1
+ === 0.2.3
2
+ * In Writer, set @base_uri not @base, as :base_uri is an attribute.
3
+ * Relativize URLs without matching as regexp.
4
+ * Allow mixed case literal languages.
5
+ * Improve N3 Unicode support for Ruby 1.9
6
+ * Improve Turtle/N3 Writer to use unescaped and qname'd values
7
+
1
8
  === 0.2.2
2
9
  * Ruby 1.9.2 compatibility
3
10
  * Added script/tc to run test cases
@@ -17,6 +17,9 @@ Install with 'gem install rdf-n3'
17
17
 
18
18
  Based on original parser by Patrick Sinclair (metade)
19
19
 
20
+ == Limitations:
21
+ * Full support of Unicode input requires Ruby version 1.9 or greater.
22
+
20
23
  == Usage:
21
24
  Instantiate a reader from a local file:
22
25
 
data/Rakefile CHANGED
@@ -15,7 +15,7 @@ begin
15
15
  gemspec.authors = ["Gregg Kellogg"]
16
16
  gemspec.add_dependency('rdf', '>= 0.2.1')
17
17
  gemspec.add_dependency('treetop', '>= 1.4.0')
18
- gemspec.add_development_dependency('rspec')
18
+ gemspec.add_development_dependency('rspec', '= 1.3.0')
19
19
  gemspec.add_development_dependency('rdf-spec', '>= 0.2.1')
20
20
  gemspec.add_development_dependency('rdf-rdfxml', '>= 0.2.1')
21
21
  gemspec.add_development_dependency('rdf-isomorphic')
@@ -51,6 +51,12 @@ YARD::Rake::YardocTask.new do |t|
51
51
  t.files = %w(lib/**/*.rb README.rdoc History.txt AUTHORS) # optional
52
52
  end
53
53
 
54
+ desc "Update N3 grammar"
55
+ task :grammar do
56
+ sh "tt -o lib/rdf/n3/reader/n3_grammar.rb lib/rdf/n3/reader/n3_grammar.treetop"
57
+ sh "tt -o lib/rdf/n3/reader/n3_grammar_18.rb lib/rdf/n3/reader/n3_grammar_18.treetop"
58
+ end
59
+
54
60
  desc "Generate test manifest yaml"
55
61
  namespace :spec do
56
62
  task :prepare do
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.3
@@ -31,5 +31,8 @@ module RDF
31
31
  autoload :Reader, 'rdf/n3/reader'
32
32
  autoload :VERSION, 'rdf/n3/version'
33
33
  autoload :Writer, 'rdf/n3/writer'
34
+
35
+ def self.debug?; @debug; end
36
+ def self.debug=(value); @debug = value; end
34
37
  end
35
38
  end
@@ -1,6 +1,12 @@
1
1
  require 'treetop'
2
2
 
3
- Treetop.load(File.join(File.dirname(__FILE__), "reader", "n3_grammar"))
3
+ if defined?(::Encoding)
4
+ # load full grammar
5
+ Treetop.load(File.join(File.dirname(__FILE__), "reader", "n3_grammar"))
6
+ else
7
+ # load 1.8 grammar, doesn't include U00010000-\U000effff
8
+ Treetop.load(File.join(File.dirname(__FILE__), "reader", "n3_grammar_18"))
9
+ end
4
10
 
5
11
  module RDF::N3
6
12
  ##
@@ -69,7 +75,7 @@ module RDF::N3
69
75
  parser = N3GrammerParser.new
70
76
  document = parser.parse(@doc)
71
77
  unless document
72
- puts parser.inspect if $DEBUG
78
+ puts parser.inspect if ::RDF::N3::debug?
73
79
  reason = parser.failure_reason
74
80
  raise RDF::ReaderError, reason
75
81
  end
@@ -104,7 +110,7 @@ module RDF::N3
104
110
  # @param [XML Node, any] node:: XML Node or string for showing context
105
111
  # @param [String] message::
106
112
  def add_debug(node, message)
107
- puts "#{node}: #{message}" if $DEBUG
113
+ puts "#{node}: #{message}" if ::RDF::N3::debug?
108
114
  @debug << "#{node}: #{message}" if @debug.is_a?(Array)
109
115
  end
110
116
 
@@ -412,7 +418,6 @@ module RDF::N3
412
418
  localname = expression.localname.text_value if expression.respond_to?(:localname)
413
419
  localname ||= (expression.respond_to?(:text_value) ? expression.text_value : expression).to_s.sub(/^:/, "")
414
420
  localname = nil if localname.empty? # In N3/Turtle "_:" is not named
415
- escaped_localname = RDF::NTriples.escape(localname.to_s)
416
421
 
417
422
  if expression.respond_to?(:info)
418
423
  add_debug(*expression.info("build_uri(#{prefix.inspect}, #{localname.inspect})"))
@@ -421,23 +426,23 @@ module RDF::N3
421
426
  end
422
427
 
423
428
  uri = if @uri_mappings[prefix]
424
- add_debug(*expression.info("build_uri: (ns): #{@uri_mappings[prefix]}, #{escaped_localname}")) if expression.respond_to?(:info)
425
- ns(prefix, RDF::NTriples.escape(localname.to_s))
429
+ add_debug(*expression.info("build_uri: (ns): #{@uri_mappings[prefix]}, #{localname}")) if expression.respond_to?(:info)
430
+ ns(prefix, localname.to_s)
426
431
  elsif prefix == '_'
427
432
  add_debug(*expression.info("build_uri: (bnode)")) if expression.respond_to?(:info)
428
433
  bnode(localname)
429
434
  elsif prefix == "rdf"
430
435
  add_debug(*expression.info("build_uri: (rdf)")) if expression.respond_to?(:info)
431
436
  # A special case
432
- RDF::RDF[escaped_localname]
437
+ RDF::RDF[localname.to_s]
433
438
  elsif prefix == "xsd"
434
439
  add_debug(*expression.info("build_uri: (xsd)")) if expression.respond_to?(:info)
435
440
  # A special case
436
- RDF::XSD[escaped_localname]
441
+ RDF::XSD[localname.to_s]
437
442
  else
438
443
  add_debug(*expression.info("build_uri: (default_ns)")) if expression.respond_to?(:info)
439
444
  @default_ns ||= uri("#{@uri}#", nil)
440
- ns(nil, escaped_localname)
445
+ ns(nil, localname.to_s)
441
446
  end
442
447
  add_debug(*expression.info("build_uri: #{uri.inspect}")) if expression.respond_to?(:info)
443
448
  uri
@@ -27,10 +27,17 @@ module N3Grammer
27
27
  r0
28
28
  end
29
29
 
30
- def _nt_alpha
30
+ module Barename0
31
+ def barename_start
32
+ elements[0]
33
+ end
34
+
35
+ end
36
+
37
+ def _nt_barename
31
38
  start_index = index
32
- if node_cache[:alpha].has_key?(index)
33
- cached = node_cache[:alpha][index]
39
+ if node_cache[:barename].has_key?(index)
40
+ cached = node_cache[:barename][index]
34
41
  if cached
35
42
  cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
36
43
  @index = cached.interval.end
@@ -38,22 +45,62 @@ module N3Grammer
38
45
  return cached
39
46
  end
40
47
 
41
- if has_terminal?('\G[A-Z_a-z\\p{Alpha}]', true, index)
48
+ i0, s0 = index, []
49
+ r1 = _nt_barename_start
50
+ s0 << r1
51
+ if r1
52
+ s2, i2 = [], index
53
+ loop do
54
+ r3 = _nt_barename_tail
55
+ if r3
56
+ s2 << r3
57
+ else
58
+ break
59
+ end
60
+ end
61
+ r2 = instantiate_node(SyntaxNode,input, i2...index, s2)
62
+ s0 << r2
63
+ end
64
+ if s0.last
65
+ r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
66
+ r0.extend(Barename0)
67
+ else
68
+ @index = i0
69
+ r0 = nil
70
+ end
71
+
72
+ node_cache[:barename][start_index] = r0
73
+
74
+ r0
75
+ end
76
+
77
+ def _nt_barename_start
78
+ start_index = index
79
+ if node_cache[:barename_start].has_key?(index)
80
+ cached = node_cache[:barename_start][index]
81
+ if cached
82
+ cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
83
+ @index = cached.interval.end
84
+ end
85
+ return cached
86
+ end
87
+
88
+ if has_terminal?('\G[A-Z_a-z\\u00c0-\\u00d6\\u00d8-\\u00f6\\u00f8-\\u02ff\\u0370-\\u037d\\u037f-\\u1fff\\u200c-\\u200d\\u2070-\\u218f\\u2c00-\\u2fef\\u3001-\\ud7ff\\uf900-\\ufdcf\\ufdf0-\\ufffd\\u{10000}-\\u{effff}]', true, index)
42
89
  r0 = instantiate_node(SyntaxNode,input, index...(index + 1))
43
90
  @index += 1
44
91
  else
45
92
  r0 = nil
46
93
  end
47
94
 
48
- node_cache[:alpha][start_index] = r0
95
+ node_cache[:barename_start][start_index] = r0
49
96
 
50
97
  r0
51
98
  end
52
99
 
53
- def _nt_alphanumeric
100
+ def _nt_barename_tail
54
101
  start_index = index
55
- if node_cache[:alphanumeric].has_key?(index)
56
- cached = node_cache[:alphanumeric][index]
102
+ if node_cache[:barename_tail].has_key?(index)
103
+ cached = node_cache[:barename_tail][index]
57
104
  if cached
58
105
  cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
59
106
  @index = cached.interval.end
@@ -62,11 +109,11 @@ module N3Grammer
62
109
  end
63
110
 
64
111
  i0 = index
65
- r1 = _nt_alpha
112
+ r1 = _nt_barename_start
66
113
  if r1
67
114
  r0 = r1
68
115
  else
69
- if has_terminal?('\G[0-9]', true, index)
116
+ if has_terminal?('\G[-0-9\\u00b7\\u0300-\\u036f\\u203f-\\u2040\\\\]', true, index)
70
117
  r2 = true
71
118
  @index += 1
72
119
  else
@@ -80,7 +127,93 @@ module N3Grammer
80
127
  end
81
128
  end
82
129
 
83
- node_cache[:alphanumeric][start_index] = r0
130
+ node_cache[:barename_tail][start_index] = r0
131
+
132
+ r0
133
+ end
134
+
135
+ def _nt_hexdigit
136
+ start_index = index
137
+ if node_cache[:hexdigit].has_key?(index)
138
+ cached = node_cache[:hexdigit][index]
139
+ if cached
140
+ cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
141
+ @index = cached.interval.end
142
+ end
143
+ return cached
144
+ end
145
+
146
+ if has_terminal?('\G[0-9a-fA-F]', true, index)
147
+ r0 = instantiate_node(SyntaxNode,input, index...(index + 1))
148
+ @index += 1
149
+ else
150
+ r0 = nil
151
+ end
152
+
153
+ node_cache[:hexdigit][start_index] = r0
154
+
155
+ r0
156
+ end
157
+
158
+ module Integer0
159
+ end
160
+
161
+ def _nt_integer
162
+ start_index = index
163
+ if node_cache[:integer].has_key?(index)
164
+ cached = node_cache[:integer][index]
165
+ if cached
166
+ cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
167
+ @index = cached.interval.end
168
+ end
169
+ return cached
170
+ end
171
+
172
+ i0, s0 = index, []
173
+ if has_terminal?('\G[+-]', true, index)
174
+ r2 = true
175
+ @index += 1
176
+ else
177
+ r2 = nil
178
+ end
179
+ if r2
180
+ r1 = r2
181
+ else
182
+ r1 = instantiate_node(SyntaxNode,input, index...index)
183
+ end
184
+ s0 << r1
185
+ if r1
186
+ s3, i3 = [], index
187
+ loop do
188
+ if has_terminal?('\G[0-9]', true, index)
189
+ r4 = true
190
+ @index += 1
191
+ else
192
+ r4 = nil
193
+ end
194
+ if r4
195
+ s3 << r4
196
+ else
197
+ break
198
+ end
199
+ end
200
+ if s3.empty?
201
+ @index = i3
202
+ r3 = nil
203
+ else
204
+ r3 = instantiate_node(SyntaxNode,input, i3...index, s3)
205
+ end
206
+ s0 << r3
207
+ end
208
+ if s0.last
209
+ r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
210
+ r0.extend(Integer0)
211
+ else
212
+ @index = i0
213
+ r0 = nil
214
+ end
215
+
216
+ node_cache[:integer][start_index] = r0
84
217
 
85
218
  r0
86
219
  end
@@ -1126,157 +1259,6 @@ module N3Grammer
1126
1259
  r0
1127
1260
  end
1128
1261
 
1129
- module Barename0
1130
- def alpha
1131
- elements[0]
1132
- end
1133
-
1134
- end
1135
-
1136
- def _nt_barename
1137
- start_index = index
1138
- if node_cache[:barename].has_key?(index)
1139
- cached = node_cache[:barename][index]
1140
- if cached
1141
- cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
1142
- @index = cached.interval.end
1143
- end
1144
- return cached
1145
- end
1146
-
1147
- i0, s0 = index, []
1148
- r1 = _nt_alpha
1149
- s0 << r1
1150
- if r1
1151
- s2, i2 = [], index
1152
- loop do
1153
- i3 = index
1154
- r4 = _nt_alphanumeric
1155
- if r4
1156
- r3 = r4
1157
- else
1158
- if has_terminal?('-', false, index)
1159
- r5 = instantiate_node(SyntaxNode,input, index...(index + 1))
1160
- @index += 1
1161
- else
1162
- terminal_parse_failure('-')
1163
- r5 = nil
1164
- end
1165
- if r5
1166
- r3 = r5
1167
- else
1168
- @index = i3
1169
- r3 = nil
1170
- end
1171
- end
1172
- if r3
1173
- s2 << r3
1174
- else
1175
- break
1176
- end
1177
- end
1178
- r2 = instantiate_node(SyntaxNode,input, i2...index, s2)
1179
- s0 << r2
1180
- end
1181
- if s0.last
1182
- r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
1183
- r0.extend(Barename0)
1184
- else
1185
- @index = i0
1186
- r0 = nil
1187
- end
1188
-
1189
- node_cache[:barename][start_index] = r0
1190
-
1191
- r0
1192
- end
1193
-
1194
- def _nt_hexdigit
1195
- start_index = index
1196
- if node_cache[:hexdigit].has_key?(index)
1197
- cached = node_cache[:hexdigit][index]
1198
- if cached
1199
- cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
1200
- @index = cached.interval.end
1201
- end
1202
- return cached
1203
- end
1204
-
1205
- if has_terminal?('\G[0-9a-fA-F]', true, index)
1206
- r0 = instantiate_node(SyntaxNode,input, index...(index + 1))
1207
- @index += 1
1208
- else
1209
- r0 = nil
1210
- end
1211
-
1212
- node_cache[:hexdigit][start_index] = r0
1213
-
1214
- r0
1215
- end
1216
-
1217
- module Integer0
1218
- end
1219
-
1220
- def _nt_integer
1221
- start_index = index
1222
- if node_cache[:integer].has_key?(index)
1223
- cached = node_cache[:integer][index]
1224
- if cached
1225
- cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
1226
- @index = cached.interval.end
1227
- end
1228
- return cached
1229
- end
1230
-
1231
- i0, s0 = index, []
1232
- if has_terminal?('\G[+-]', true, index)
1233
- r2 = true
1234
- @index += 1
1235
- else
1236
- r2 = nil
1237
- end
1238
- if r2
1239
- r1 = r2
1240
- else
1241
- r1 = instantiate_node(SyntaxNode,input, index...index)
1242
- end
1243
- s0 << r1
1244
- if r1
1245
- s3, i3 = [], index
1246
- loop do
1247
- if has_terminal?('\G[0-9]', true, index)
1248
- r4 = true
1249
- @index += 1
1250
- else
1251
- r4 = nil
1252
- end
1253
- if r4
1254
- s3 << r4
1255
- else
1256
- break
1257
- end
1258
- end
1259
- if s3.empty?
1260
- @index = i3
1261
- r3 = nil
1262
- else
1263
- r3 = instantiate_node(SyntaxNode,input, i3...index, s3)
1264
- end
1265
- s0 << r3
1266
- end
1267
- if s0.last
1268
- r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
1269
- r0.extend(Integer0)
1270
- else
1271
- @index = i0
1272
- r0 = nil
1273
- end
1274
-
1275
- node_cache[:integer][start_index] = r0
1276
-
1277
- r0
1278
- end
1279
-
1280
1262
  module Language0
1281
1263
  end
1282
1264
 
@@ -1297,7 +1279,7 @@ module N3Grammer
1297
1279
  i0, s0 = index, []
1298
1280
  s1, i1 = [], index
1299
1281
  loop do
1300
- if has_terminal?('\G[a-z]', true, index)
1282
+ if has_terminal?('\G[a-zA-Z]', true, index)
1301
1283
  r2 = true
1302
1284
  @index += 1
1303
1285
  else
@@ -1331,7 +1313,7 @@ module N3Grammer
1331
1313
  if r5
1332
1314
  s6, i6 = [], index
1333
1315
  loop do
1334
- if has_terminal?('\G[a-z0-9]', true, index)
1316
+ if has_terminal?('\G[a-zA-Z0-9]', true, index)
1335
1317
  r7 = true
1336
1318
  @index += 1
1337
1319
  else
@@ -1493,13 +1475,6 @@ module N3Grammer
1493
1475
  r0
1494
1476
  end
1495
1477
 
1496
- module Localname0
1497
- def alpha
1498
- elements[0]
1499
- end
1500
-
1501
- end
1502
-
1503
1478
  def _nt_localname
1504
1479
  start_index = index
1505
1480
  if node_cache[:localname].has_key?(index)
@@ -1511,60 +1486,13 @@ module N3Grammer
1511
1486
  return cached
1512
1487
  end
1513
1488
 
1514
- i0, s0 = index, []
1515
- r1 = _nt_alpha
1516
- s0 << r1
1517
- if r1
1518
- s2, i2 = [], index
1519
- loop do
1520
- i3 = index
1521
- r4 = _nt_alphanumeric
1522
- if r4
1523
- r3 = r4
1524
- else
1525
- if has_terminal?('-', false, index)
1526
- r5 = instantiate_node(SyntaxNode,input, index...(index + 1))
1527
- @index += 1
1528
- else
1529
- terminal_parse_failure('-')
1530
- r5 = nil
1531
- end
1532
- if r5
1533
- r3 = r5
1534
- else
1535
- @index = i3
1536
- r3 = nil
1537
- end
1538
- end
1539
- if r3
1540
- s2 << r3
1541
- else
1542
- break
1543
- end
1544
- end
1545
- r2 = instantiate_node(SyntaxNode,input, i2...index, s2)
1546
- s0 << r2
1547
- end
1548
- if s0.last
1549
- r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
1550
- r0.extend(Localname0)
1551
- else
1552
- @index = i0
1553
- r0 = nil
1554
- end
1489
+ r0 = _nt_barename
1555
1490
 
1556
1491
  node_cache[:localname][start_index] = r0
1557
1492
 
1558
1493
  r0
1559
1494
  end
1560
1495
 
1561
- module Nprefix0
1562
- def alpha
1563
- elements[0]
1564
- end
1565
-
1566
- end
1567
-
1568
1496
  def _nt_nprefix
1569
1497
  start_index = index
1570
1498
  if node_cache[:nprefix].has_key?(index)
@@ -1576,47 +1504,7 @@ module N3Grammer
1576
1504
  return cached
1577
1505
  end
1578
1506
 
1579
- i0, s0 = index, []
1580
- r1 = _nt_alpha
1581
- s0 << r1
1582
- if r1
1583
- s2, i2 = [], index
1584
- loop do
1585
- i3 = index
1586
- r4 = _nt_alphanumeric
1587
- if r4
1588
- r3 = r4
1589
- else
1590
- if has_terminal?('-', false, index)
1591
- r5 = instantiate_node(SyntaxNode,input, index...(index + 1))
1592
- @index += 1
1593
- else
1594
- terminal_parse_failure('-')
1595
- r5 = nil
1596
- end
1597
- if r5
1598
- r3 = r5
1599
- else
1600
- @index = i3
1601
- r3 = nil
1602
- end
1603
- end
1604
- if r3
1605
- s2 << r3
1606
- else
1607
- break
1608
- end
1609
- end
1610
- r2 = instantiate_node(SyntaxNode,input, i2...index, s2)
1611
- s0 << r2
1612
- end
1613
- if s0.last
1614
- r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
1615
- r0.extend(Nprefix0)
1616
- else
1617
- @index = i0
1618
- r0 = nil
1619
- end
1507
+ r0 = _nt_barename
1620
1508
 
1621
1509
  node_cache[:nprefix][start_index] = r0
1622
1510