Module: SPARQL::Grammar

Defined in:
vendor/bundler/ruby/2.7.0/bundler/gems/sparql-cd4915d97d78/lib/sparql/grammar.rb,
vendor/bundler/ruby/2.7.0/bundler/gems/sparql-cd4915d97d78/lib/sparql/grammar/parser11.rb,
vendor/bundler/ruby/2.7.0/bundler/gems/sparql-cd4915d97d78/lib/sparql/grammar/terminals11.rb

Overview

Implementation Notes

The parser is driven through a rules table contained in lib/sparql/grammar/meta.rb. This includes branch rules to indicate productions to be taken based on a current production.

The meta.rb file is generated from etc/sparql11.bnf using the ebnf gem.

ebnf --ll1 Query --format rb \
  --mod-name SPARQL::Grammar::Meta \
  --output lib/sparql/grammar/meta.rb \
  etc/sparql11.bnf

Defined Under Namespace

Modules: Meta, Terminals Classes: Parser

Class Method Summary collapse

Class Method Details

.open(filename, **options) {|reader| ... } ⇒ Object

Parses input from the given file name or URL.

Parameters:

  • filename (String, #to_s)
  • options (Hash{Symbol => Object})

    any additional options (see RDF::Reader#initialize and RDF::Format.for)

Options Hash (**options):

  • :format (Symbol) — default: :ntriples

Yields:

  • (reader)

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Raises:



294
295
296
297
298
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/sparql-cd4915d97d78/lib/sparql/grammar.rb', line 294

def self.open(filename, **options, &block)
  RDF::Util::File.open_file(filename, **options) do |file|
    self.parse(file, options, &block)
  end
end

.parse(query, **options, &block) ⇒ Parser

Parse the given SPARQL query string.

Examples:

result = SPARQL::Grammar.parse("SELECT * WHERE { ?s ?p ?o }")

Parameters:

  • query (IO, StringIO, Lexer, Array, String, #to_s)

    Query may be an array of lexed tokens, a lexer, or a string or open file.

  • options (Hash{Symbol => Object})

Returns:

Raises:

  • (Parser::Error)

    on invalid input



279
280
281
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/sparql-cd4915d97d78/lib/sparql/grammar.rb', line 279

def self.parse(query, **options, &block)
  Parser.new(query, **options).parse(options[:update] ? :UpdateUnit : :QueryUnit)
end

.tokenize(query, **options) {|lexer| ... } ⇒ Lexer

Tokenizes the given SPARQL query string.

Examples:

lexer = SPARQL::Grammar.tokenize("SELECT * WHERE { ?s ?p ?o }")
lexer.each_token do |token|
  puts token.inspect
end

Parameters:

Yields:

  • (lexer)

Yield Parameters:

  • lexer (Lexer)

Returns:

  • (Lexer)

Raises:

  • (Lexer::Error)

    on invalid input



329
330
331
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/sparql-cd4915d97d78/lib/sparql/grammar.rb', line 329

def self.tokenize(query, **options, &block)
  Lexer.tokenize(query, options, &block)
end

.valid?(query, **options) ⇒ Boolean

Returns true if the given SPARQL query string is valid.

Examples:

SPARQL::Grammar.valid?("SELECT ?s WHERE { ?s ?p ?o }")  #=> true
SPARQL::Grammar.valid?("SELECT s WHERE { ?s ?p ?o }")   #=> false

Parameters:

Returns:

  • (Boolean)


310
311
312
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/sparql-cd4915d97d78/lib/sparql/grammar.rb', line 310

def self.valid?(query, **options)
  Parser.new(query, **options).valid?
end