Class: RDF::TriG::Reader

Inherits:
RDF::Turtle::Reader show all
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trig-ccb51eff7022/lib/rdf/trig/reader.rb

Overview

A parser for the TriG

Leverages the Turtle reader

Constant Summary

Constants included from Util::Logger

Util::Logger::IOWrapper

Constants included from RDF::Turtle::Terminals

RDF::Turtle::Terminals::ANON, RDF::Turtle::Terminals::BASE, RDF::Turtle::Terminals::BLANK_NODE_LABEL, RDF::Turtle::Terminals::DECIMAL, RDF::Turtle::Terminals::DOUBLE, RDF::Turtle::Terminals::ECHAR, RDF::Turtle::Terminals::EXPONENT, RDF::Turtle::Terminals::INTEGER, RDF::Turtle::Terminals::IRIREF, RDF::Turtle::Terminals::IRI_RANGE, RDF::Turtle::Terminals::LANG_DIR, RDF::Turtle::Terminals::PERCENT, RDF::Turtle::Terminals::PLX, RDF::Turtle::Terminals::PNAME_LN, RDF::Turtle::Terminals::PNAME_NS, RDF::Turtle::Terminals::PN_CHARS, RDF::Turtle::Terminals::PN_CHARS_BASE, RDF::Turtle::Terminals::PN_CHARS_BODY, RDF::Turtle::Terminals::PN_CHARS_U, RDF::Turtle::Terminals::PN_LOCAL, RDF::Turtle::Terminals::PN_LOCAL_BODY, RDF::Turtle::Terminals::PN_LOCAL_ESC, RDF::Turtle::Terminals::PN_PREFIX, RDF::Turtle::Terminals::PREFIX, RDF::Turtle::Terminals::STRING_LITERAL_LONG_QUOTE, RDF::Turtle::Terminals::STRING_LITERAL_LONG_SINGLE_QUOTE, RDF::Turtle::Terminals::STRING_LITERAL_QUOTE, RDF::Turtle::Terminals::STRING_LITERAL_SINGLE_QUOTE, RDF::Turtle::Terminals::UCHAR, RDF::Turtle::Terminals::U_CHARS1, RDF::Turtle::Terminals::U_CHARS2, RDF::Turtle::Terminals::WS

Instance Attribute Summary

Attributes included from EBNF::LL1::Parser

#lineno

Attributes inherited from Reader

#options

Attributes included from Enumerable

#existentials, #universals

Instance Method Summary collapse

Methods inherited from RDF::Turtle::Reader

#bnode, #debug, #each_triple, #error, #initialize, #inspect, #literal, options, #pname, #prefix, #process_iri, #prod, #progress, #read_BlankNode, #read_annotation, #read_blankNodePropertyList, #read_collection, #read_directive, #read_iri, #read_literal, #read_object, #read_objectList, #read_predicateObjectList, #read_reifiedTriple, #read_statement, #read_subject, #read_tripleTerm, #read_triples, #read_ttObject, #read_ttSubject, #read_verb, #recover

Methods included from Util::Logger

#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger

Methods included from EBNF::LL1::Parser

#add_prod_data, #add_prod_datum, #debug, #depth, #error, #parse, #prod_data, #progress, #warn

Methods inherited from Reader

#base_uri, #canonicalize?, #close, each, #each_pg_statement, #each_triple, #encoding, #fail_object, #fail_predicate, #fail_subject, for, format, #initialize, #intern?, #lineno, open, options, #prefix, #prefixes, #prefixes=, #read_statement, #read_triple, #rewind, #to_sym, to_sym, #valid?, #validate?

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Enumerable

add_entailment, #canonicalize, #canonicalize!, #dump, #each_graph, #each_object, #each_predicate, #each_subject, #each_term, #each_triple, #entail, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_term, #enum_triple, #graph?, #graph_names, #invalid?, #method_missing, #object?, #objects, #predicate?, #predicates, #project_graph, #quad?, #quads, #respond_to_missing?, #statement?, #statements, #subject?, #subjects, #supports?, #term?, #terms, #to_a, #to_h, #to_set, #triple?, #triples, #valid?, #validate!

Methods included from Isomorphic

#bijection_to, #isomorphic_with?

Methods included from Countable

#count, #empty?

Methods included from Readable

#readable?

Constructor Details

This class inherits a constructor from RDF::Turtle::Reader

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RDF::Enumerable

Instance Method Details

#add_statement(production, statement) ⇒ RDF::Statement

add a statement, object can be literal or URI or bnode

Parameters:

Returns:

Raises:

  • (RDF::ReaderError)

    Checks parameter types and raises if they are incorrect if parsing mode is validate.



91
92
93
94
95
96
97
98
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trig-ccb51eff7022/lib/rdf/trig/reader.rb', line 91

def add_statement(production, statement)
  error("Statement is invalid: #{statement.inspect.inspect}", production: produciton) if validate? && statement.invalid?
  statement.graph_name = @graph_name if @graph_name
  @callback.call(statement) if statement.subject &&
                               statement.predicate &&
                               statement.object &&
                               (validate? ? statement.valid? : true)
end

#each_quad {|subject, predicate, object, graph_name| ... }

This method returns an undefined value.

Iterates the given block for each RDF quad in the input.

Yields:

  • (subject, predicate, object, graph_name)

Yield Parameters:



76
77
78
79
80
81
82
83
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trig-ccb51eff7022/lib/rdf/trig/reader.rb', line 76

def each_quad(&block)
  if block_given?
    each_statement do |statement|
      block.call(*statement.to_quad)
    end
  end
  enum_for(:each_quad)
end

#each_statement {|statement| ... }

This method returns an undefined value.

Iterates the given block for each RDF statement in the input.

Yields:

  • (statement)

Yield Parameters:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trig-ccb51eff7022/lib/rdf/trig/reader.rb', line 47

def each_statement(&block)
  if block_given?
    @recovering = false
    @callback = block

    begin
      while (@lexer.first rescue true)
        read_trigDoc
      end
    rescue EBNF::LL1::Lexer::Error, SyntaxError, EOFError, Recovery
      # Terminate loop if EOF found while recovering
    end

    if validate? && log_statistics[:error]
      raise RDF::ReaderError, "Errors found during processing"
    end
  end
  enum_for(:each_statement)
end

#read_blockObject (protected)

Returns:



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trig-ccb51eff7022/lib/rdf/trig/reader.rb', line 109

def read_block
  prod(:block, %(})) do
    @graph_name = nil
    token = @lexer.first
    case token && (token.type || token.value)
    when :GRAPH
      @lexer.shift
      @graph_name = read_labelOrSubject || error("Expected label or subject", production: :block, token: @lexer.first)
      read_wrappedGraph || error("Expected wrappedGraph", production: :block, token: @lexer.first)
      @graph_name = nil
    when :IRIREF, :BLANK_NODE_LABEL, :ANON, :PNAME_LN, :PNAME_NS
      read_triplesOrGraph || error("Expected triplesOrGraph", production: :block, token: @lexer.first)
    when '{'
      read_wrappedGraph || error("Expected wrappedGraph", production: :block, token: @lexer.first)
    when '(', '[', '<<'
      read_triples2 || error("Expected collection or blankNodePropertyList", production: :block, token: @lexer.first)
    when nil
      # End of input
    else
      error("Unexpected token: #{@lexer.first.inspect}", production: :block, token: @lexer.first)
    end
  end
end

#read_labelOrSubjectRDF::Resource (protected)

Returns:



248
249
250
251
252
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trig-ccb51eff7022/lib/rdf/trig/reader.rb', line 248

def read_labelOrSubject
  prod(:labelOrSubject) do
    read_iri || read_BlankNode
  end
end

#read_trigDocObject (protected)

Returns:



102
103
104
105
106
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trig-ccb51eff7022/lib/rdf/trig/reader.rb', line 102

def read_trigDoc
  prod(:trigDoc, %(} .)) do
    read_directive || read_block
  end
end

#read_triples2Object (protected)

Returns:



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trig-ccb51eff7022/lib/rdf/trig/reader.rb', line 160

def read_triples2
  token = @lexer.first
  case token && token.value
  when '['
    prod(:triples2) do
      # blankNodePropertyList predicateObjectList? 
      subject = read_blankNodePropertyList || error("Failed to parse blankNodePropertyList", production: :triples2, token: @lexer.first)
      read_predicateObjectList(subject)
      if !@recovering || @lexer.first === '.'
        # If recovering, we will have eaten the closing '.'
        token = @lexer.shift
        unless token && token.value == '.'
          error("Expected '.' following triple", production: :triples2, token: token)
        end
      end
      true
    end
  when '('
    prod(:triples2) do
      subject = read_collection || error("Failed to parse read_collection", production: :triples2, token: @lexer.first)
      token = @lexer.first
      case token && (token.type || token.value)
      when 'a', :IRIREF, :PNAME_LN, :PNAME_NS then read_predicateObjectList(subject)
      else error("Expected predicateObjectList after collection subject", production: :triples2, token: token)
      end
      if !@recovering || @lexer.first === '.'
        # If recovering, we will have eaten the closing '.'
        token = @lexer.shift
        unless token && token.value == '.'
          error("Expected '.' following triple", production: :triples2, token: token)
        end
      end
      true
    end
  when '<<'
    prod(:triples2) do
      subject = read_quotedTriple || error("Failed to parse embedded triple", production: :triples2, token: @lexer.first)
      token = @lexer.first
      case token && (token.type || token.value)
      when 'a', :IRIREF, :PNAME_LN, :PNAME_NS then read_predicateObjectList(subject)
      else error("Expected predicateObjectList after collection subject", production: :triples2, token: token)
      end
      if !@recovering || @lexer.first === '.'
        # If recovering, we will have eaten the closing '.'
        token = @lexer.shift
        unless token && token.value == '.'
          error("Expected '.' following triple", production: :triples2, token: token)
        end
      end
      true
    end
  end
end

#read_triplesBlockObject (protected)

Returns:



236
237
238
239
240
241
242
243
244
245
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trig-ccb51eff7022/lib/rdf/trig/reader.rb', line 236

def read_triplesBlock
  prod(:triplesBlock, %w(.)) do
    while (token = @lexer.first) && token.value != '}' && read_triples
      unless log_recovering?
        break unless @lexer.first === '.'
        @lexer.shift
      end
    end
  end
end

#read_triplesOrGraphObject (protected)

Returns:



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trig-ccb51eff7022/lib/rdf/trig/reader.rb', line 134

def read_triplesOrGraph
  while name = read_labelOrSubject
    prod(:triplesOrGraph, %(} .)) do
      token = @lexer.first
      case token && token.value
      when '{'
        @graph_name = name
        read_wrappedGraph || error("Expected wrappedGraph", production: :triplesOrGraph, token: @lexer.first)
        @graph_name = nil
        true
      else
        read_predicateObjectList(name) || error("Expected predicateObjectList", production: :triplesOrGraph, token: @lexer.first)
        unless @recovering
          # If recovering, we will have eaten the closing '.'
          token = @lexer.shift
          unless token && token.value == '.'
            error("Expected '.' following triple", production: :triplesOrGraph, token: token)
          end
        end
      end
    end
  end
  true
end

#read_wrappedGraphObject (protected)

Returns:



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trig-ccb51eff7022/lib/rdf/trig/reader.rb', line 215

def read_wrappedGraph
  token = @lexer.first
  if token && token.value == '{'
    prod(:wrappedGraph, %w(})) do
      @lexer.shift
      while read_triplesBlock
        # Read until nothing found
      end
      if !@recovering || @lexer.first === '}'
        # If recovering, we will have eaten the closing '}'
        token = @lexer.shift
        unless token && token.value == '}'
          error("Expected '}' following triple", production: :wrappedGraph, token: token)
        end
      end
      true
    end
  end
end