lib/rdf/trig/reader.rb in rdf-trig-1.0.0 vs lib/rdf/trig/reader.rb in rdf-trig-1.0.1

- old
+ new

@@ -65,46 +65,46 @@ terminal(:STRING_LITERAL_SINGLE_QUOTE, STRING_LITERAL_SINGLE_QUOTE, :unescape => true) do |prod, token, input| input[:string_value] = token.value[1..-2] end # String terminals - terminal(nil, %r([\{\}\(\),.;\[\]a]|\^\^|@base|@prefix|true|false)) do |prod, token, input| + terminal(nil, %r([\{\}\(\),.;\[\]a]|\^\^|true|false)) do |prod, token, input| case token.value - when 'a' then input[:resource] = RDF.type + when 'A', 'a' then input[:resource] = RDF.type when 'true', 'false' then input[:resource] = RDF::Literal::Boolean.new(token.value) when '@base', '@prefix' then input[:lang] = token.value[1..-1] else input[:string] = token.value end end - terminal(:LANGTAG, LANGTAG) do |prod, token, input| - input[:lang] = token.value[1..-1] - end - - terminal(:SPARQL_PREFIX, SPARQL_PREFIX) do |prod, token, input| + terminal(:PREFIX, PREFIX) do |prod, token, input| input[:string_value] = token.value.downcase end - terminal(:SPARQL_BASE, SPARQL_BASE) do |prod, token, input| + terminal(:BASE, BASE) do |prod, token, input| input[:string_value] = token.value.downcase end + terminal(:LANGTAG, LANGTAG) do |prod, token, input| + input[:lang] = token.value[1..-1] + end + # Productions # [3g] graph defines the basic creation of context start_production(:graph) do |input, current, callback| callback.call(:context, "graph", nil) end production(:graph) do |input, current, callback| callback.call(:context, "graph", nil) end - # [4g] graphIri - # Normally, just returns the IRIref, but if called from [3g], also + # [4g] graphName + # Normally, just returns the resource, but if called from [3g], also # sets the context for triples defined within that graph - production(:graphIri) do |input, current, callback| + production(:graphName) do |input, current, callback| # If input contains set_graph_iri, use the returned value to set @context - debug("graphIri") {"Set graph context to #{current[:resource]}"} - callback.call(:context, "graphIri", current[:resource]) + debug("graphName") {"Set graph context to #{current[:resource]}"} + callback.call(:context, "graphName", current[:resource]) end # Productions # [4] prefixID defines a prefix mapping @@ -120,25 +120,10 @@ iri = current[:resource] debug("base") {"Defined base as #{iri}"} options[:base_uri] = iri end - # [28s] sparqlPrefix ::= [Pp][Rr][Ee][Ff][Ii][Xx] PNAME_NS IRIREF - production(:sparqlPrefix) do |input, current, callback| - prefix = current[:prefix] - iri = current[:resource] - debug("sparqlPrefix") {"Defined prefix #{prefix.inspect} mapping to #{iri.inspect}"} - prefix(prefix, iri) - end - - # [29s] sparqlBase ::= [Bb][Aa][Ss][Ee] IRIREF - production(:sparqlBase) do |input, current, callback| - iri = current[:resource] - debug("base") {"Defined base as #{iri}"} - options[:base_uri] = iri - end - # [6] triples start_production(:triples) do |input, current, callback| # Note production as triples for blankNodePropertyList # to set :subject instead of :resource current[:triples] = true @@ -193,24 +178,19 @@ current[:object_list] = [] end production(:collection) do |input, current, callback| # Create an RDF list - bnode = self.bnode objects = current[:object_list] - list = RDF::List.new(bnode, nil, objects) + list = RDF::List[*objects] list.each_statement do |statement| - # Spec Confusion, referenced section "Collection" is missing from the spec. - # Anicdodal evidence indicates that some expect each node to be of type rdf:list, - # but existing Notation3 and Turtle tests (http://www.w3.org/2001/sw/DataAccess/df1/tests/manifest.ttl) do not. next if statement.predicate == RDF.type && statement.object == RDF.List callback.call(:statement, "collection", statement.subject, statement.predicate, statement.object) end - bnode = RDF.nil if list.empty? # Return bnode as resource - input[:resource] = bnode + input[:resource] = list.subject end # [16] RDFLiteral ::= String ( LanguageTag | ( "^^" IRIref ) )? production(:RDFLiteral) do |input, current, callback| opts = {} @@ -238,11 +218,12 @@ @context = data[1] when :statement data << @context if @context debug("each_statement") {"data: #{data.inspect}, context: #{@context.inspect}"} loc = data.shift - add_statement(loc, RDF::Statement.from(data)) + s = RDF::Statement.from(data, :lineno => lineno) + add_statement(loc, s) unless !s.valid? && validate? when :trace level, lineno, depth, *args = data message = "#{args.join(': ')}" d_str = depth > 100 ? ' ' * 100 + '+' : ' ' * depth str = "[#{lineno}](#{level})#{d_str}#{message}" @@ -255,10 +236,10 @@ $stderr.puts(str) if level <= @options[:debug] end end end rescue EBNF::LL1::Parser::Error => e - debug("Parsing completed with errors:\n\t#{e.message}") + progress("Parsing completed with errors:\n\t#{e.message}") raise RDF::ReaderError, e.message if validate? end ## # Iterates the given block for each RDF quad in the input.