rdf-n3 0.2.3.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.yardopts +4 -3
- data/{History.txt → History.md} +30 -6
- data/{README.rdoc → README.md} +56 -19
- data/Rakefile +15 -29
- data/VERSION +1 -1
- data/example-files/sp2b.n3 +50177 -0
- data/lib/rdf/n3.rb +2 -2
- data/lib/rdf/n3/reader.rb +560 -367
- data/lib/rdf/n3/reader/meta.rb +640 -0
- data/lib/rdf/n3/reader/n3-selectors.n3 +0 -0
- data/lib/rdf/n3/reader/parser.rb +229 -0
- data/lib/rdf/n3/vocab.rb +1 -0
- data/lib/rdf/n3/writer.rb +324 -265
- data/rdf-n3.gemspec +24 -26
- data/script/build_meta +242 -0
- data/script/parse +62 -13
- data/script/tc +4 -4
- data/spec/cwm_spec.rb +11 -3
- data/spec/n3reader_spec.rb +233 -63
- data/spec/rdf_helper.rb +15 -15
- data/spec/spec_helper.rb +10 -4
- data/spec/swap_spec.rb +11 -35
- data/spec/swap_test/n3parser.tests +14 -14
- data/spec/swap_test/n3parser.yml +0 -19
- data/spec/swap_test/nodeID/classes.ref.rdf +1 -1
- data/spec/swap_test/ref/contexts-1.n3 +12 -0
- data/spec/swap_test/ref/prefix2.rdf +33 -0
- data/spec/swap_test/ref/strquot.n3 +0 -1
- data/spec/swap_test/ref/xml-syntax-basic-serialization.rdf +1 -1
- data/spec/swap_test/regression.n3 +5 -5
- data/spec/swap_test/regression.yml +53 -23
- data/spec/turtle/manifest-bad.yml +91 -0
- data/spec/turtle/manifest.yml +187 -0
- data/spec/turtle_spec.rb +12 -20
- data/spec/writer_spec.rb +39 -37
- metadata +43 -48
- data/lib/rdf/n3/patches/qname_hacks.rb +0 -57
- data/lib/rdf/n3/patches/seq.rb +0 -34
- data/lib/rdf/n3/reader/n3_grammar.rb +0 -3764
- data/lib/rdf/n3/reader/n3_grammar.treetop +0 -227
- data/lib/rdf/n3/reader/n3_grammar_18.rb +0 -3764
- data/lib/rdf/n3/reader/n3_grammar_18.treetop +0 -227
- data/spec/literal_spec.rb +0 -245
data/rdf-n3.gemspec
CHANGED
@@ -5,49 +5,47 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{rdf-n3}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
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-
|
13
|
-
s.description = %q{
|
14
|
-
}
|
12
|
+
s.date = %q{2010-12-27}
|
13
|
+
s.description = %q{RDF::N3 is an Notation-3 (n3-rdf) parser for Ruby using the RDF.rb library suite.}
|
15
14
|
s.email = %q{gregg@kellogg-assoc.com}
|
16
15
|
s.extra_rdoc_files = [
|
17
16
|
"AUTHORS",
|
18
|
-
"History.
|
19
|
-
"README.
|
17
|
+
"History.md",
|
18
|
+
"README.md",
|
19
|
+
"VERSION"
|
20
20
|
]
|
21
21
|
s.files = [
|
22
22
|
".gitignore",
|
23
23
|
".yardopts",
|
24
24
|
"AUTHORS",
|
25
|
-
"History.
|
26
|
-
"README.
|
25
|
+
"History.md",
|
26
|
+
"README.md",
|
27
27
|
"Rakefile",
|
28
28
|
"VERSION",
|
29
|
+
"example-files/sp2b.n3",
|
29
30
|
"example.rb",
|
30
31
|
"lib/rdf/n3.rb",
|
31
32
|
"lib/rdf/n3/format.rb",
|
32
33
|
"lib/rdf/n3/patches/array_hacks.rb",
|
33
34
|
"lib/rdf/n3/patches/graph_properties.rb",
|
34
|
-
"lib/rdf/n3/patches/qname_hacks.rb",
|
35
|
-
"lib/rdf/n3/patches/seq.rb",
|
36
35
|
"lib/rdf/n3/reader.rb",
|
37
|
-
"lib/rdf/n3/reader/
|
38
|
-
"lib/rdf/n3/reader/
|
39
|
-
"lib/rdf/n3/reader/
|
40
|
-
"lib/rdf/n3/reader/n3_grammar_18.treetop",
|
36
|
+
"lib/rdf/n3/reader/meta.rb",
|
37
|
+
"lib/rdf/n3/reader/n3-selectors.n3",
|
38
|
+
"lib/rdf/n3/reader/parser.rb",
|
41
39
|
"lib/rdf/n3/version.rb",
|
42
40
|
"lib/rdf/n3/vocab.rb",
|
43
41
|
"lib/rdf/n3/writer.rb",
|
44
42
|
"rdf-n3.gemspec",
|
43
|
+
"script/build_meta",
|
45
44
|
"script/console",
|
46
45
|
"script/parse",
|
47
46
|
"script/tc",
|
48
47
|
"spec/cwm_spec.rb",
|
49
48
|
"spec/format_spec.rb",
|
50
|
-
"spec/literal_spec.rb",
|
51
49
|
"spec/matchers.rb",
|
52
50
|
"spec/n3reader_spec.rb",
|
53
51
|
"spec/rdf_helper.rb",
|
@@ -451,6 +449,7 @@ Gem::Specification.new do |s|
|
|
451
449
|
"spec/swap_test/ref/bnode.n3",
|
452
450
|
"spec/swap_test/ref/bnode.rdf",
|
453
451
|
"spec/swap_test/ref/colon-in-uri.n3",
|
452
|
+
"spec/swap_test/ref/contexts-1.n3",
|
454
453
|
"spec/swap_test/ref/daml-ex.n3",
|
455
454
|
"spec/swap_test/ref/daml-ont.n3",
|
456
455
|
"spec/swap_test/ref/djb1a-out.n3",
|
@@ -468,6 +467,7 @@ Gem::Specification.new do |s|
|
|
468
467
|
"spec/swap_test/ref/path1.n3",
|
469
468
|
"spec/swap_test/ref/path2.n3",
|
470
469
|
"spec/swap_test/ref/prefix1.rdf",
|
470
|
+
"spec/swap_test/ref/prefix2.rdf",
|
471
471
|
"spec/swap_test/ref/prefix3.rdf",
|
472
472
|
"spec/swap_test/ref/rdf-redefine.rdf",
|
473
473
|
"spec/swap_test/ref/reluri-1.rdf",
|
@@ -533,7 +533,9 @@ Gem::Specification.new do |s|
|
|
533
533
|
"spec/turtle/bad-13.ttl",
|
534
534
|
"spec/turtle/bad-14.ttl",
|
535
535
|
"spec/turtle/manifest-bad.ttl",
|
536
|
+
"spec/turtle/manifest-bad.yml",
|
536
537
|
"spec/turtle/manifest.ttl",
|
538
|
+
"spec/turtle/manifest.yml",
|
537
539
|
"spec/turtle/rdf-schema.out",
|
538
540
|
"spec/turtle/rdf-schema.ttl",
|
539
541
|
"spec/turtle/rdfq-results.out",
|
@@ -606,7 +608,7 @@ Gem::Specification.new do |s|
|
|
606
608
|
"spec/turtle_spec.rb",
|
607
609
|
"spec/writer_spec.rb"
|
608
610
|
]
|
609
|
-
s.homepage = %q{http://github.com/gkellogg/rdf-
|
611
|
+
s.homepage = %q{http://github.com/gkellogg/rdf-n3}
|
610
612
|
s.rdoc_options = ["--charset=UTF-8"]
|
611
613
|
s.require_paths = ["lib"]
|
612
614
|
s.rubygems_version = %q{1.3.7}
|
@@ -614,7 +616,6 @@ Gem::Specification.new do |s|
|
|
614
616
|
s.test_files = [
|
615
617
|
"spec/cwm_spec.rb",
|
616
618
|
"spec/format_spec.rb",
|
617
|
-
"spec/literal_spec.rb",
|
618
619
|
"spec/matchers.rb",
|
619
620
|
"spec/n3reader_spec.rb",
|
620
621
|
"spec/rdf_helper.rb",
|
@@ -629,26 +630,23 @@ Gem::Specification.new do |s|
|
|
629
630
|
s.specification_version = 3
|
630
631
|
|
631
632
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
632
|
-
s.add_runtime_dependency(%q<rdf>, [">= 0.
|
633
|
-
s.
|
634
|
-
s.add_development_dependency(%q<rspec>, ["= 1.3.0"])
|
633
|
+
s.add_runtime_dependency(%q<rdf>, [">= 0.3.0"])
|
634
|
+
s.add_development_dependency(%q<rspec>, [">= 2.1.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"])
|
638
638
|
s.add_development_dependency(%q<yard>, [">= 0"])
|
639
639
|
else
|
640
|
-
s.add_dependency(%q<rdf>, [">= 0.
|
641
|
-
s.add_dependency(%q<
|
642
|
-
s.add_dependency(%q<rspec>, ["= 1.3.0"])
|
640
|
+
s.add_dependency(%q<rdf>, [">= 0.3.0"])
|
641
|
+
s.add_dependency(%q<rspec>, [">= 2.1.0"])
|
643
642
|
s.add_dependency(%q<rdf-spec>, [">= 0.2.1"])
|
644
643
|
s.add_dependency(%q<rdf-rdfxml>, [">= 0.2.1"])
|
645
644
|
s.add_dependency(%q<rdf-isomorphic>, [">= 0"])
|
646
645
|
s.add_dependency(%q<yard>, [">= 0"])
|
647
646
|
end
|
648
647
|
else
|
649
|
-
s.add_dependency(%q<rdf>, [">= 0.
|
650
|
-
s.add_dependency(%q<
|
651
|
-
s.add_dependency(%q<rspec>, ["= 1.3.0"])
|
648
|
+
s.add_dependency(%q<rdf>, [">= 0.3.0"])
|
649
|
+
s.add_dependency(%q<rspec>, [">= 2.1.0"])
|
652
650
|
s.add_dependency(%q<rdf-spec>, [">= 0.2.1"])
|
653
651
|
s.add_dependency(%q<rdf-rdfxml>, [">= 0.2.1"])
|
654
652
|
s.add_dependency(%q<rdf-isomorphic>, [">= 0"])
|
data/script/build_meta
ADDED
@@ -0,0 +1,242 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", 'lib')))
|
4
|
+
require 'rdf/n3'
|
5
|
+
require 'getoptlong'
|
6
|
+
|
7
|
+
# Build rdf/n3/parser/meta.rb from http://www.w3.org/2000/10/swap/grammar/n3-selectors.n3
|
8
|
+
# Based on code from http://www.w3.org/2000/10/swap/grammar/predictiveParser.py
|
9
|
+
|
10
|
+
class BNF < RDF::Vocabulary("http://www.w3.org/2000/10/swap/grammar/bnf#"); end
|
11
|
+
class REGEX < RDF::Vocabulary("http://www.w3.org/2000/10/swap/grammar/regex#"); end
|
12
|
+
class N3 < RDF::Vocabulary("http://www.w3.org/2000/10/swap/grammar/n3#"); end
|
13
|
+
|
14
|
+
class PredictiveParser
|
15
|
+
attr_accessor :already, :agenda, :errors, :literalTerminals, :branchTable, :tokenRegexps
|
16
|
+
attr_accessor :graph
|
17
|
+
|
18
|
+
def initialize
|
19
|
+
@already = []
|
20
|
+
@agenda = []
|
21
|
+
@errors = []
|
22
|
+
@literalTerminals = {}
|
23
|
+
@branchTable = {}
|
24
|
+
@tokenRegexps = {}
|
25
|
+
end
|
26
|
+
|
27
|
+
def parse(file)
|
28
|
+
progress("Loading " + file)
|
29
|
+
File.open(file) do |f|
|
30
|
+
@graph = RDF::Graph.new do |g|
|
31
|
+
RDF::N3::Reader.new(f).each do |statement|
|
32
|
+
g << statement
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
progress("Loaded #{@graph.count} statements.")
|
37
|
+
end
|
38
|
+
|
39
|
+
def recordError(str)
|
40
|
+
errors << str
|
41
|
+
"##### ERROR: #{str}"
|
42
|
+
end
|
43
|
+
|
44
|
+
def progress(str); puts(str); end
|
45
|
+
def chatty(str); progress(str) if $verbose; end
|
46
|
+
|
47
|
+
def runProduction(lhs)
|
48
|
+
doProduction(lhs)
|
49
|
+
while !@agenda.empty?
|
50
|
+
x = @agenda.shift
|
51
|
+
@already << x
|
52
|
+
doProduction(x)
|
53
|
+
end
|
54
|
+
|
55
|
+
if !@errors.empty?
|
56
|
+
progress("###### FAILED with #{errors.length} errors.")
|
57
|
+
@errors.each {|s| progress(" #{s}")}
|
58
|
+
exit(-2)
|
59
|
+
else
|
60
|
+
progress("Ok for predictive parsing")
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Generate branch tables for one production
|
65
|
+
def doProduction(lhs)
|
66
|
+
if lhs == BNF.void
|
67
|
+
progress("\nvoid")
|
68
|
+
return
|
69
|
+
end
|
70
|
+
if lhs == BNF.eof
|
71
|
+
progress( "\nEOF")
|
72
|
+
return
|
73
|
+
end
|
74
|
+
if lhs.is_a?(RDF::Literal)
|
75
|
+
literalTerminals[lhs.value()] = 1
|
76
|
+
return
|
77
|
+
end
|
78
|
+
|
79
|
+
branchDict = {}
|
80
|
+
|
81
|
+
rhs = graph.first_object(:subject => lhs, :predicate => BNF.matches)
|
82
|
+
if rhs
|
83
|
+
chatty("\nToken #{lhs} matches regexp #{rhs}")
|
84
|
+
tokenRegexps[lhs] = rhs.value
|
85
|
+
|
86
|
+
cc = graph.query(:subject => lhs, :predicate => BNF.canStartWith)
|
87
|
+
progress(recordError("No record of what token #{lhs} can start with")) if cc.empty?
|
88
|
+
cc.each {|statement| chatty(" Can start with: #{statement.object}")}
|
89
|
+
return
|
90
|
+
end
|
91
|
+
|
92
|
+
rhs = graph.first_object(:subject => lhs, :predicate => BNF.mustBeOneSequence)
|
93
|
+
unless rhs
|
94
|
+
progress(recordError("No definition of #{lhs}"))
|
95
|
+
# raise RuntimeError("No definition of %s in\n %s" %(`lhs`, `g`))
|
96
|
+
return
|
97
|
+
end
|
98
|
+
|
99
|
+
options = rhs
|
100
|
+
progress("\nProduction #{lhs} :: #{options}")
|
101
|
+
graph.query(:subject => lhs, :predicate => BNF.canPrecede) do |statement|
|
102
|
+
chatty(" Can precede '#{statement.object}'")
|
103
|
+
end
|
104
|
+
|
105
|
+
graph.query(:subject => lhs, :predicate => BNF.branch) do |statement|
|
106
|
+
branch = statement.object
|
107
|
+
sequence = graph.first_object(:subject => statement.object, :predicate => BNF.sequence)
|
108
|
+
option = RDF::List.new(sequence, graph).to_a
|
109
|
+
progress(" option: #{option}")
|
110
|
+
|
111
|
+
option.each do |part|
|
112
|
+
agenda << part unless already.include?(part) || agenda.include?(part)
|
113
|
+
end
|
114
|
+
|
115
|
+
conditions = graph.query(:subject => branch, :predicate => BNF.condition).map(&:object)
|
116
|
+
if conditions.empty?
|
117
|
+
progress(recordError("NO SELECTOR for #{lhs} option #{option}"))
|
118
|
+
if option.empty?
|
119
|
+
# Void case - the tricky one
|
120
|
+
graph.pattern(:subject => lhs, :predicate => BNF.canPrecede) do |st|
|
121
|
+
progress(" Can precede #{st.object}")
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
progress(" Conditions: #{conditions.to_a.map(&:to_s)}")
|
127
|
+
conditions.each do |str1|
|
128
|
+
if branchDict.has_key?(str1)
|
129
|
+
progress(
|
130
|
+
"Conflict: #{str1} is also the condition for #{branchDict[str1]}")
|
131
|
+
end
|
132
|
+
branchDict[str1] = option
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
branchDict.keys.each do |str1|
|
137
|
+
branchDict.keys.each do |str2|
|
138
|
+
s1, s2 = str1.to_s, str2.to_s
|
139
|
+
if (s1.index(s2) == 0 || s2.index(s1) == 0) && branchDict[str1] != branchDict[str2]
|
140
|
+
progress("WARNING: for #{lhs}, #{str1} indicates #{branchDict[str1]}, but #{str2} indicates #{branchDict[str2]}")
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
branchTable[lhs] = branchDict
|
146
|
+
end
|
147
|
+
|
148
|
+
def litOrNot(value)
|
149
|
+
(value.is_a?(RDF::Literal) ? "" : ":") + value.to_s.dump
|
150
|
+
end
|
151
|
+
|
152
|
+
def outputBranchTable(io, indent = 0)
|
153
|
+
ind0 = ' ' * indent
|
154
|
+
ind1 = ind0 + ' '
|
155
|
+
ind2 = ind1 + ' '
|
156
|
+
ind3 = ind2 + ' '
|
157
|
+
io.puts "#{ind0}BRANCHES = {"
|
158
|
+
branchTable.keys.sort_by(&:to_s).each do |prod|
|
159
|
+
# Special case double, integer, and decimal to output just a numericliteral, due to a parser conflict
|
160
|
+
next if prod.to_s =~ /numericliteral/
|
161
|
+
io.puts "#{ind1}#{litOrNot(prod)} => {"
|
162
|
+
branchTable[prod].keys.sort_by(&:to_s).each do |term|
|
163
|
+
list = branchTable[prod][term].map {|t2| litOrNot(t2)}.join(",\n#{ind3}")
|
164
|
+
io.puts "#{ind2}#{litOrNot(term)} => [#{list}],"
|
165
|
+
end
|
166
|
+
io.puts "#{ind1}},"
|
167
|
+
end
|
168
|
+
io.puts "#{ind0}}\n"
|
169
|
+
end
|
170
|
+
|
171
|
+
def outputRegexpTable(io, indent = 0)
|
172
|
+
ind0 = ' ' * indent
|
173
|
+
ind1 = ind0 + ' '
|
174
|
+
io.puts "#{ind0}REGEXPS = {"
|
175
|
+
tokenRegexps.keys.sort_by(&:to_s).each do |prod|
|
176
|
+
# Special case double, integer, and decimal to output just a numericliteral, due to a parser conflict
|
177
|
+
next if prod.to_s =~ /(integer|double|decimal)/
|
178
|
+
io.puts "#{ind1}#{litOrNot(prod)} => Regexp.compile(" +
|
179
|
+
case prod.to_s
|
180
|
+
when /barename/ then %q(%(^[#{BARENAME_START}][#{BARENAME_TAIL}]*))
|
181
|
+
when /explicituri/ then %q("^<[^>]*>")
|
182
|
+
when /langcode/ then %q("^[a-zA-Z]+(-[a-zA-Z0-9]+)*")
|
183
|
+
when /prefix/ then %q(%(^([#{BARENAME_START}][#{BARENAME_TAIL}]*)?:))
|
184
|
+
when /qname/ then %q(%(^(([#{BARENAME_START}][#{BARENAME_TAIL}]*)?:)?([#{BARENAME_START}][#{BARENAME_TAIL}]*)?))
|
185
|
+
when /variable/ then %q(%(^\\\\?[#{BARENAME_START}][#{BARENAME_TAIL}]*))
|
186
|
+
else tokenRegexps[prod].dump
|
187
|
+
end + "),"
|
188
|
+
end
|
189
|
+
|
190
|
+
io.puts "\n#{ind1}# Hack to replace integer|double|decimal with numericliteral"
|
191
|
+
io.puts "#{ind1}#{litOrNot(N3.numericliteral)} => Regexp.compile(" + %q(%(^[-+]?[0-9]+(\\\\.[0-9]+)?(e[-+]?[0-9]+)?)) + ")"
|
192
|
+
io.puts "#{ind0}}\n"
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
$verbose = false
|
197
|
+
$debug = false
|
198
|
+
grammarFile = File.expand_path(File.join(File.dirname(__FILE__), "../lib/rdf/n3/reader/n3-selectors.n3"))
|
199
|
+
parseAs = "document"
|
200
|
+
output = STDOUT
|
201
|
+
|
202
|
+
opts = GetoptLong.new(
|
203
|
+
["--debug", GetoptLong::NO_ARGUMENT],
|
204
|
+
["--verbose", GetoptLong::NO_ARGUMENT],
|
205
|
+
["--grammar", GetoptLong::REQUIRED_ARGUMENT],
|
206
|
+
["--as", GetoptLong::REQUIRED_ARGUMENT],
|
207
|
+
["--output", "-o", GetoptLong::REQUIRED_ARGUMENT]
|
208
|
+
)
|
209
|
+
opts.each do |opt, arg|
|
210
|
+
case opt
|
211
|
+
when '--verbose' then $verbose = true
|
212
|
+
when '--debug' then $debug = true
|
213
|
+
when '--grammar' then grammarFile = arg
|
214
|
+
when '--as' then parseAs = arg
|
215
|
+
when '--output' then output = File.open(arg, "w")
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
pp = PredictiveParser.new
|
220
|
+
|
221
|
+
pp.parse(grammarFile)
|
222
|
+
pp.runProduction(N3[parseAs])
|
223
|
+
|
224
|
+
unless output == STDOUT
|
225
|
+
output.puts "# This file is automatically generated by #{__FILE__}"
|
226
|
+
output.puts "# Branch and Regexp tables derived from http://www.w3.org/2000/10/swap/grammar/n3-selectors.n3"
|
227
|
+
output.puts "module RDF::N3::Meta"
|
228
|
+
end
|
229
|
+
pp.outputBranchTable(output, 1)
|
230
|
+
output.puts %q(
|
231
|
+
if RUBY_VERSION >= "1.9.0"
|
232
|
+
BARENAME_START = "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}"
|
233
|
+
BARENAME_TAIL = "0-9#{BARENAME_START}\u00b7\u0300-\u036f\u203f-\u2040\\\\-"
|
234
|
+
else
|
235
|
+
BARENAME_START = "A-Z_a-z\xc0-\xd6\xd8-\xf6\xf8-\xff"
|
236
|
+
BARENAME_TAIL = "0-9#{BARENAME_START}\xb7\\\\-"
|
237
|
+
end
|
238
|
+
)
|
239
|
+
pp.outputRegexpTable(output, 1)
|
240
|
+
unless output == STDOUT
|
241
|
+
output.puts "end"
|
242
|
+
end
|
data/script/parse
CHANGED
@@ -2,21 +2,65 @@
|
|
2
2
|
require 'rubygems'
|
3
3
|
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", 'lib')))
|
4
4
|
require 'rdf/n3'
|
5
|
+
require 'rdf/ntriples'
|
5
6
|
require 'getoptlong'
|
7
|
+
require 'open-uri'
|
6
8
|
|
7
|
-
def
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
def run(input, base)
|
10
|
+
reader_class = RDF::Reader.for($input_format.to_sym)
|
11
|
+
writer_class = RDF::Writer.for($output_format.to_sym)
|
12
|
+
raise "Reader not found for #{$input_format}" unless reader_class
|
13
|
+
raise "Writer not found for #{$output_format}" unless writer_class
|
14
|
+
|
15
|
+
start = Time.new
|
16
|
+
num = 0
|
17
|
+
if $parse_only
|
18
|
+
include RDF::N3::Meta
|
19
|
+
include RDF::N3::Parser
|
20
|
+
puts "\nparse #{input.read}---\n\n" unless $quiet
|
21
|
+
input.rewind
|
22
|
+
if $quiet
|
23
|
+
$stdout = StringIO.new
|
24
|
+
end
|
25
|
+
test(input, BRANCHES, REGEXPS)
|
26
|
+
if $quiet
|
27
|
+
$stdout = STDOUT
|
28
|
+
print "."
|
29
|
+
end
|
30
|
+
elsif $output_format == :ntriples || $quiet
|
31
|
+
reader_class.new(input, :base_uri => base, :strict => true).each do |statement|
|
32
|
+
num += 1
|
33
|
+
if $quiet
|
34
|
+
#print "."
|
35
|
+
else
|
36
|
+
puts statement.to_ntriples
|
37
|
+
end
|
38
|
+
end
|
39
|
+
elsif $output_format == :inspect
|
40
|
+
reader_class.new(input, :base_uri => base, :strict => true).each do |statement|
|
41
|
+
num += 1
|
42
|
+
puts statement.inspect
|
11
43
|
end
|
12
|
-
|
44
|
+
else
|
45
|
+
puts writer_class.buffer(:base_uri => base, :standard_prefixes => true) { |writer|
|
46
|
+
reader_class.new(input, :base_uri => base, :strict => true).each do |statement|
|
47
|
+
num += 1
|
48
|
+
writer << statement
|
49
|
+
end
|
50
|
+
}
|
51
|
+
end
|
52
|
+
puts
|
53
|
+
secs = Time.new - start
|
54
|
+
puts "Parsed #{num} statements in #{secs} seconds @ #{num/secs} statements/second."
|
55
|
+
rescue
|
56
|
+
fname = input.respond_to?(:path) ? input.path : "-stdin-"
|
57
|
+
STDERR.puts("Error in #{fname}")
|
58
|
+
raise
|
13
59
|
end
|
14
60
|
|
15
|
-
mode = ARGV.shift
|
16
|
-
raise "Mode must be one of 'parse'" unless mode == "parse"
|
17
|
-
|
18
61
|
$verbose = false
|
19
|
-
$
|
62
|
+
$output_format = :ntriples
|
63
|
+
$input_format = :n3
|
20
64
|
base_uri = "http://example.com"
|
21
65
|
input = nil
|
22
66
|
|
@@ -25,7 +69,9 @@ opts = GetoptLong.new(
|
|
25
69
|
["--verbose", GetoptLong::NO_ARGUMENT],
|
26
70
|
["--quiet", GetoptLong::NO_ARGUMENT],
|
27
71
|
["--format", GetoptLong::REQUIRED_ARGUMENT],
|
72
|
+
["--input-format", GetoptLong::REQUIRED_ARGUMENT],
|
28
73
|
["--execute", "-e", GetoptLong::REQUIRED_ARGUMENT],
|
74
|
+
["--parse-only", GetoptLong::NO_ARGUMENT],
|
29
75
|
["--uri", GetoptLong::REQUIRED_ARGUMENT]
|
30
76
|
)
|
31
77
|
opts.each do |opt, arg|
|
@@ -34,16 +80,19 @@ opts.each do |opt, arg|
|
|
34
80
|
when '--quiet' then $quiet = true
|
35
81
|
when '--debug' then ::RDF::N3::debug = true
|
36
82
|
when '--execute' then input = arg
|
37
|
-
when '--format' then $
|
83
|
+
when '--format' then $output_format = arg.to_sym
|
84
|
+
when '--input-format' then $input_format = arg.to_sym
|
38
85
|
when '--uri' then base_uri = arg
|
86
|
+
when '--parse-only' then $parse_only = true
|
39
87
|
end
|
40
88
|
end
|
41
89
|
|
42
90
|
if ARGV.empty?
|
43
91
|
s = input ? input : $stdin.read
|
44
|
-
|
92
|
+
run(StringIO.new(s), base_uri)
|
45
93
|
else
|
46
94
|
ARGV.each do |test_file|
|
47
|
-
|
95
|
+
run(Kernel.open(test_file), base_uri)
|
48
96
|
end
|
49
|
-
end
|
97
|
+
end
|
98
|
+
puts
|