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.
@@ -0,0 +1,227 @@
1
+ #encoding: utf-8
2
+ grammar N3Grammer
3
+ # Entry point to grammar
4
+ rule document
5
+ statements
6
+ end
7
+
8
+ rule barename
9
+ barename_start barename_tail*
10
+ end
11
+
12
+ rule barename_start
13
+ [A-Z_a-z\p{Alpha}]
14
+ end
15
+
16
+ rule barename_tail
17
+ barename_start / [-0-9\\]
18
+ end
19
+
20
+ rule hexdigit
21
+ [0-9a-fA-F]
22
+ end
23
+
24
+ rule integer
25
+ [+-]? [0-9]+
26
+ end
27
+
28
+ rule barename_csl
29
+ space+ barename barename_csl_tail
30
+ / ""
31
+ end
32
+
33
+ rule barename_csl_tail
34
+ space* "," space* barename space* barename_csl_tail
35
+ / ""
36
+ end
37
+
38
+ rule boolean
39
+ "@"* ("true" / "false")
40
+ end
41
+
42
+ rule comment
43
+ '#' (![\n\r] .)*
44
+ end
45
+
46
+ rule declaration
47
+ "@"? 'keywords' barename_csl {
48
+ def declaration; true; end
49
+ def keywords; true;end
50
+ }
51
+ / "@"? 'base' space+ explicituri:explicituri {
52
+ def declaration; true; end
53
+ def base; true; end
54
+ }
55
+ / "@"? 'prefix' space+ nprefix:nprefix? ':' space* explicituri:explicituri {
56
+ def declaration; true; end
57
+ }
58
+ end
59
+
60
+ rule decimal
61
+ integer '.' [0-9]+
62
+ end
63
+
64
+ rule double
65
+ decimal [eE] integer
66
+ end
67
+
68
+ rule existential
69
+ "@"? "forSome" space+ symbol_csl
70
+ end
71
+
72
+ rule explicituri
73
+ "<" uri:URI_Reference ">"
74
+ end
75
+
76
+ rule expression
77
+ pathitem space* '.' expression
78
+ / pathitem space* "!" space* expression
79
+ / pathitem space* "^" space* expression { def reverse; true; end }
80
+ / pathitem
81
+ end
82
+
83
+ rule language
84
+ [a-zA-Z]+ ( "-" [a-zA-Z0-9]+ )*
85
+ end
86
+
87
+ rule literal
88
+ (string_single / string_multi) ("^^" symbol / "@" language )?
89
+ end
90
+
91
+ rule localname
92
+ barename
93
+ end
94
+
95
+ rule nprefix
96
+ barename
97
+ end
98
+
99
+ rule numericliteral
100
+ double { def numericliteral; "double"; end}
101
+ / decimal { def numericliteral; "decimal"; end}
102
+ / integer { def numericliteral; "integer"; end}
103
+ end
104
+
105
+ rule object
106
+ expression
107
+ end
108
+
109
+ rule object_list
110
+ object space* "," space* object_list
111
+ / object
112
+ end
113
+
114
+ rule pathitem
115
+ boolean { def boolean; true; end }
116
+ / literal { def literal; true; end }
117
+ / numericliteral
118
+ #/ quickvariable
119
+ / symbol
120
+ / "[" space* "]" { def anonnode; true; end }
121
+ / "[" space* property_list space* "]" { def anonnode; true; end }
122
+ / "{" space* statements space* "}" { def anonnode; true; end }
123
+ / "(" space* path_list space* ")" { def anonnode; true; end }
124
+ end
125
+
126
+ rule path_list
127
+ expression space* path_list
128
+ / ""
129
+ end
130
+
131
+ rule prop
132
+ expression
133
+ end
134
+
135
+ rule property_list
136
+ verb space* object_list space* ";"+ space* property_list
137
+ / verb space* object_list space* ";"*
138
+ / verb space* ";"* {def object_missing; true; end}
139
+ / ""
140
+ end
141
+
142
+ rule qname
143
+ nprefix ":" localname
144
+ / nprefix ':' { def text_value; ""; end }
145
+ / ':' localname*
146
+ / localname { def barename; true; end }
147
+ end
148
+
149
+ rule simpleStatement
150
+ subject space+ property_list
151
+ / subject # For [] and a.b
152
+ end
153
+
154
+ rule space
155
+ [ \t\n\r]+ / comment
156
+ end
157
+
158
+ rule statement
159
+ declaration
160
+ / existential
161
+ / simpleStatement
162
+ / universal
163
+ end
164
+
165
+ rule statements
166
+ (space / statement space* ('.' space*)? )*
167
+ end
168
+
169
+ # " constant-value-with-escaping "
170
+ rule string_single
171
+ '""' !["] / '"' string_single_char+ '"'
172
+ end
173
+
174
+ rule string_single_char
175
+ !["\n\r] (
176
+ ("\\"
177
+ [\\\"bfnrt]
178
+ / ( "u" hexdigit hexdigit hexdigit hexdigit )
179
+ / ( "U" "00" hexdigit hexdigit hexdigit hexdigit hexdigit hexdigit)
180
+ )
181
+ / .)
182
+ end
183
+
184
+ # """ constant value with escaping including single or double occurrences of quotes and/or newlines """
185
+ rule string_multi
186
+ '"""' string_multi_single_char* '"""'
187
+ end
188
+
189
+ rule string_multi_single_char
190
+ "\\\""
191
+ / !('"""') .
192
+ end
193
+
194
+ rule subject
195
+ expression
196
+ end
197
+
198
+ rule symbol
199
+ qname / explicituri
200
+ end
201
+
202
+ rule symbol_csl
203
+ symbol space* "," space* symbol_csl
204
+ / symbol
205
+ end
206
+
207
+ rule verb
208
+ "@"? "has" space+ prop # has xxx
209
+ / "@"? "is" space+ prop space+ "@"? "of" { # is xxx of
210
+ def invert; true; end
211
+ }
212
+ / "@"? "a" !":" # has rdf:type
213
+ / "=>" # has log:implies
214
+ / "<=" { def invert; true; end } # is log:implies of
215
+ / "=" # has owl:sameAs
216
+ / prop # has xxx of -- shorthand
217
+ end
218
+
219
+ rule universal
220
+ "@"? "forAll" space+ symbol_csl
221
+ end
222
+
223
+ rule URI_Reference
224
+ [^{}<>]*
225
+ end
226
+
227
+ end
@@ -1,11 +1,8 @@
1
1
  module RDF::N3::VERSION
2
- MAJOR = 0
3
- MINOR = 2
4
- TINY = 2
5
- EXTRA = nil
2
+ VERSION_FILE = File.join(File.expand_path(File.dirname(__FILE__)), "..", "..", "..", "VERSION")
3
+ MAJOR, MINOR, TINY, EXTRA = File.read(VERSION_FILE).chop.split(".")
6
4
 
7
- STRING = [MAJOR, MINOR, TINY].join('.')
8
- STRING << "-#{EXTRA}" if EXTRA
5
+ STRING = [MAJOR, MINOR, TINY, EXTRA].compact.join('.')
9
6
 
10
7
  ##
11
8
  # @return [String]
@@ -17,5 +14,5 @@ module RDF::N3::VERSION
17
14
 
18
15
  ##
19
16
  # @return [Array(Integer, Integer, Integer)]
20
- def self.to_a() [MAJOR, MINOR, TINY] end
17
+ def self.to_a() STRING.split(".") end
21
18
  end
@@ -104,7 +104,7 @@ module RDF::N3
104
104
  # @see #write_triple
105
105
  def write_epilogue
106
106
  @max_depth = @options[:max_depth] || 3
107
- @base = @options[:base_uri]
107
+ @base_uri = @options[:base_uri]
108
108
  @debug = @options[:debug]
109
109
  @default_namespace = @options[:default_namespace]
110
110
 
@@ -129,7 +129,7 @@ module RDF::N3
129
129
  def start_document
130
130
  @started = true
131
131
 
132
- write("#{indent}@base <#{@base}> .\n") if @base
132
+ write("#{indent}@base <#{@base_uri}> .\n") if @base_uri
133
133
 
134
134
  add_debug("start_document: #{@namespaces.inspect}")
135
135
  @namespaces.keys.sort.each do |prefix|
@@ -137,28 +137,30 @@ module RDF::N3
137
137
  end
138
138
  end
139
139
 
140
- def end_document; end
140
+ def end_document
141
+ write("\n")
142
+ end
141
143
 
142
144
  # Checks if l is a valid RDF list, i.e. no nodes have other properties.
143
145
  def is_valid_list(l)
144
146
  props = @graph.properties(l)
145
- #puts "is_valid_list: #{props.inspect}" if $DEBUG
147
+ #puts "is_valid_list: #{props.inspect}" if ::RDF::N3::debug?
146
148
  return false unless props.has_key?(RDF.first.to_s) || l == RDF.nil
147
149
  while l && l != RDF.nil do
148
- #puts "is_valid_list(length): #{props.length}" if $DEBUG
150
+ #puts "is_valid_list(length): #{props.length}" if ::RDF::N3::debug?
149
151
  return false unless props.has_key?(RDF.first.to_s) && props.has_key?(RDF.rest.to_s)
150
152
  n = props[RDF.rest.to_s]
151
- #puts "is_valid_list(n): #{n.inspect}" if $DEBUG
153
+ #puts "is_valid_list(n): #{n.inspect}" if ::RDF::N3::debug?
152
154
  return false unless n.is_a?(Array) && n.length == 1
153
155
  l = n.first
154
156
  props = @graph.properties(l)
155
157
  end
156
- #puts "is_valid_list: valid" if $DEBUG
158
+ #puts "is_valid_list: valid" if ::RDF::N3::debug?
157
159
  true
158
160
  end
159
161
 
160
162
  def do_list(l)
161
- puts "do_list: #{l.inspect}" if $DEBUG
163
+ puts "do_list: #{l.inspect}" if ::RDF::N3::debug?
162
164
  position = SUBJECT
163
165
  while l do
164
166
  p = @graph.properties(l)
@@ -174,7 +176,7 @@ module RDF::N3
174
176
 
175
177
  def p_list(node, position)
176
178
  return false if !is_valid_list(node)
177
- #puts "p_list: #{node.inspect}, #{position}" if $DEBUG
179
+ #puts "p_list: #{node.inspect}, #{position}" if ::RDF::N3::debug?
178
180
 
179
181
  write(position == SUBJECT ? "(" : " (")
180
182
  @depth += 2
@@ -192,7 +194,7 @@ module RDF::N3
192
194
  def p_squared(node, position)
193
195
  return false unless p_squared?(node, position)
194
196
 
195
- #puts "p_squared: #{node.inspect}, #{position}" if $DEBUG
197
+ #puts "p_squared: #{node.inspect}, #{position}" if ::RDF::N3::debug?
196
198
  subject_done(node)
197
199
  write(position == SUBJECT ? '[' : ' [')
198
200
  @depth += 2
@@ -204,18 +206,18 @@ module RDF::N3
204
206
  end
205
207
 
206
208
  def p_default(node, position)
207
- #puts "p_default: #{node.inspect}, #{position}" if $DEBUG
208
- l = (position == SUBJECT ? "" : " ") + label(node)
209
+ #puts "p_default: #{node.inspect}, #{position}" if ::RDF::N3::debug?
210
+ l = (position == SUBJECT ? "" : " ") + format_value(node)
209
211
  write(l)
210
212
  end
211
213
 
212
214
  def path(node, position)
213
- puts "path: #{node.inspect}, pos: #{position}, []: #{is_valid_list(node)}, p2?: #{p_squared?(node, position)}, rc: #{ref_count(node)}" if $DEBUG
215
+ puts "path: #{node.inspect}, pos: #{position}, []: #{is_valid_list(node)}, p2?: #{p_squared?(node, position)}, rc: #{ref_count(node)}" if ::RDF::N3::debug?
214
216
  raise RDF::WriterError, "Cannot serialize node '#{node}'" unless p_list(node, position) || p_squared(node, position) || p_default(node, position)
215
217
  end
216
218
 
217
219
  def verb(node)
218
- puts "verb: #{node.inspect}" if $DEBUG
220
+ puts "verb: #{node.inspect}" if ::RDF::N3::debug?
219
221
  if node == RDF.type
220
222
  write(" a")
221
223
  else
@@ -224,7 +226,7 @@ module RDF::N3
224
226
  end
225
227
 
226
228
  def object_list(objects)
227
- puts "object_list: #{objects.inspect}" if $DEBUG
229
+ puts "object_list: #{objects.inspect}" if ::RDF::N3::debug?
228
230
  return if objects.empty?
229
231
 
230
232
  objects.each_with_index do |obj, i|
@@ -236,7 +238,7 @@ module RDF::N3
236
238
  def predicate_list(subject)
237
239
  properties = @graph.properties(subject)
238
240
  prop_list = sort_properties(properties) - [RDF.first.to_s, RDF.rest.to_s]
239
- puts "predicate_list: #{prop_list.inspect}" if $DEBUG
241
+ puts "predicate_list: #{prop_list.inspect}" if ::RDF::N3::debug?
240
242
  return if prop_list.empty?
241
243
 
242
244
  prop_list.each_with_index do |prop, i|
@@ -271,11 +273,11 @@ module RDF::N3
271
273
 
272
274
  def relativize(uri)
273
275
  uri = uri.to_s
274
- @base ? uri.sub(/^#{@base}/, "") : uri
276
+ @base_uri ? uri.sub(@base_uri.to_s, "") : uri
275
277
  end
276
278
 
277
279
  def statement(subject)
278
- puts "statement: #{subject.inspect}, s2?: #{s_squared(subject)}" if $DEBUG
280
+ puts "statement: #{subject.inspect}, s2?: #{s_squared(subject)}" if ::RDF::N3::debug?
279
281
  subject_done(subject)
280
282
  s_squared(subject) || s_default(subject)
281
283
  end
@@ -342,8 +344,8 @@ module RDF::N3
342
344
  # Return a QName for the URI, or nil. Adds namespace of QName to defined namespaces
343
345
  def get_qname(uri)
344
346
  if uri.is_a?(RDF::URI)
345
- md = uri.to_s.match(/^#{@base}(.*)$/) if @base
346
- return "<#{md[1]}>" if md
347
+ md = relativize(uri)
348
+ return "<#{md}>" unless md == uri.to_s
347
349
 
348
350
  # Duplicate logic from URI#qname to remember namespace assigned
349
351
 
@@ -373,10 +375,6 @@ module RDF::N3
373
375
  end
374
376
  end
375
377
 
376
- def label(node)
377
- get_qname(node) || node.to_ntriples
378
- end
379
-
380
378
  def add_namespace(prefix, ns)
381
379
  return if @namespaces.has_key?(prefix.to_s)
382
380
  uri = (ns.respond_to?(:to_uri) ? ns.to_uri : ns).to_s
@@ -429,7 +427,7 @@ module RDF::N3
429
427
  #
430
428
  # @param [String] message::
431
429
  def add_debug(message)
432
- STDERR.puts message if $DEBUG
430
+ STDERR.puts message if ::RDF::N3::debug?
433
431
  @debug << message if @debug.is_a?(Array)
434
432
  end
435
433
 
@@ -442,5 +440,43 @@ module RDF::N3
442
440
  def write(text)
443
441
  @stream.write(text)
444
442
  end
443
+
444
+ ##
445
+ # Returns the N-Triples representation of a literal.
446
+ #
447
+ # @param [RDF::Literal, String, #to_s] literal
448
+ # @param [Hash{Symbol => Object}] options
449
+ # @return [String]
450
+ def format_literal(literal, options = {})
451
+ case literal
452
+ when RDF::Literal
453
+ text = quoted(literal.value)
454
+ text << "@#{literal.language}" if literal.has_language?
455
+ text << "^^#{format_uri(literal.datatype)}" if literal.has_datatype?
456
+ text
457
+ else
458
+ quoted(literal.to_s)
459
+ end
460
+ end
461
+
462
+ ##
463
+ # Returns the Turtle/N3 representation of a URI reference.
464
+ #
465
+ # @param [RDF::URI] literal
466
+ # @param [Hash{Symbol => Object}] options
467
+ # @return [String]
468
+ def format_uri(uri, options = {})
469
+ get_qname(uri) || "<%s>" % uri_for(uri)
470
+ end
471
+
472
+ ##
473
+ # Returns the Turtle/N3 representation of a blank node.
474
+ #
475
+ # @param [RDF::Node] node
476
+ # @param [Hash{Symbol => Object}] options
477
+ # @return [String]
478
+ def format_node(node, options = {})
479
+ "_:%s" % node.id
480
+ end
445
481
  end
446
482
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rdf-n3}
8
- s.version = "0.2.2"
8
+ s.version = "0.2.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Gregg Kellogg"]
12
- s.date = %q{2010-08-11}
12
+ s.date = %q{2010-11-10}
13
13
  s.description = %q{ RDF::N3 is an Notation-3 (n3-rdf) parser for Ruby using the RDF.rb library suite.
14
14
  }
15
15
  s.email = %q{gregg@kellogg-assoc.com}
@@ -31,13 +31,13 @@ Gem::Specification.new do |s|
31
31
  "lib/rdf/n3/format.rb",
32
32
  "lib/rdf/n3/patches/array_hacks.rb",
33
33
  "lib/rdf/n3/patches/graph_properties.rb",
34
- "lib/rdf/n3/patches/literal_normalization.rb",
35
34
  "lib/rdf/n3/patches/qname_hacks.rb",
36
35
  "lib/rdf/n3/patches/seq.rb",
37
- "lib/rdf/n3/patches/uri_hacks.rb",
38
36
  "lib/rdf/n3/reader.rb",
39
37
  "lib/rdf/n3/reader/n3_grammar.rb",
40
38
  "lib/rdf/n3/reader/n3_grammar.treetop",
39
+ "lib/rdf/n3/reader/n3_grammar_18.rb",
40
+ "lib/rdf/n3/reader/n3_grammar_18.treetop",
41
41
  "lib/rdf/n3/version.rb",
42
42
  "lib/rdf/n3/vocab.rb",
43
43
  "lib/rdf/n3/writer.rb",
@@ -631,7 +631,7 @@ Gem::Specification.new do |s|
631
631
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
632
632
  s.add_runtime_dependency(%q<rdf>, [">= 0.2.1"])
633
633
  s.add_runtime_dependency(%q<treetop>, [">= 1.4.0"])
634
- s.add_development_dependency(%q<rspec>, [">= 0"])
634
+ s.add_development_dependency(%q<rspec>, ["= 1.3.0"])
635
635
  s.add_development_dependency(%q<rdf-spec>, [">= 0.2.1"])
636
636
  s.add_development_dependency(%q<rdf-rdfxml>, [">= 0.2.1"])
637
637
  s.add_development_dependency(%q<rdf-isomorphic>, [">= 0"])
@@ -639,7 +639,7 @@ Gem::Specification.new do |s|
639
639
  else
640
640
  s.add_dependency(%q<rdf>, [">= 0.2.1"])
641
641
  s.add_dependency(%q<treetop>, [">= 1.4.0"])
642
- s.add_dependency(%q<rspec>, [">= 0"])
642
+ s.add_dependency(%q<rspec>, ["= 1.3.0"])
643
643
  s.add_dependency(%q<rdf-spec>, [">= 0.2.1"])
644
644
  s.add_dependency(%q<rdf-rdfxml>, [">= 0.2.1"])
645
645
  s.add_dependency(%q<rdf-isomorphic>, [">= 0"])
@@ -648,7 +648,7 @@ Gem::Specification.new do |s|
648
648
  else
649
649
  s.add_dependency(%q<rdf>, [">= 0.2.1"])
650
650
  s.add_dependency(%q<treetop>, [">= 1.4.0"])
651
- s.add_dependency(%q<rspec>, [">= 0"])
651
+ s.add_dependency(%q<rspec>, ["= 1.3.0"])
652
652
  s.add_dependency(%q<rdf-spec>, [">= 0.2.1"])
653
653
  s.add_dependency(%q<rdf-rdfxml>, [">= 0.2.1"])
654
654
  s.add_dependency(%q<rdf-isomorphic>, [">= 0"])