Module: SPARQL::Grammar

Defined in:
lib/sparql/grammar.rb,
lib/sparql/grammar/parser11.rb,
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
  • :all_vars (Boolean) — default: false

    If true, emits on empty project operator when parsing SELECT *, which will emit all in-scope variables, rather than just those used in solutions. In the next minor release, the default for this option will change to true.

  • :anon_base (#to_s) — default: "b0"

    Basis for generating anonymous Nodes

  • :base_uri (#to_s) — default: nil

    the base URI to use when resolving relative URIs (for acessing intermediate parser productions)

  • :logger (Logger, #write, #<<)

    Record error/info/debug output

  • :prefixes (Hash) — default: Hash.new

    the prefix mappings to use (for acessing intermediate parser productions)

  • :resolve_iris (Boolean) — default: false

    Resolve prefix and relative IRIs, otherwise, when serializing the parsed SSE as S-Expressions, use the original prefixed and relative URIs along with base and prefix definitions.

  • :validate (Boolean) — default: false

    whether to validate the parsed statements and values

Yields:

  • (reader)

Yield Parameters:

  • reader (RDF::Reader)

Yield Returns:

  • (void)

    ignored

Raises:

  • (RDF::FormatError)

    if no reader found for the specified format



270
271
272
273
274
# File 'lib/sparql/grammar.rb', line 270

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})

Options Hash (**options):

  • :all_vars (Boolean) — default: false

    If true, emits on empty project operator when parsing SELECT *, which will emit all in-scope variables, rather than just those used in solutions. In the next minor release, the default for this option will change to true.

  • :anon_base (#to_s) — default: "b0"

    Basis for generating anonymous Nodes

  • :base_uri (#to_s) — default: nil

    the base URI to use when resolving relative URIs (for acessing intermediate parser productions)

  • :logger (Logger, #write, #<<)

    Record error/info/debug output

  • :prefixes (Hash) — default: Hash.new

    the prefix mappings to use (for acessing intermediate parser productions)

  • :resolve_iris (Boolean) — default: false

    Resolve prefix and relative IRIs, otherwise, when serializing the parsed SSE as S-Expressions, use the original prefixed and relative URIs along with base and prefix definitions.

  • :validate (Boolean) — default: false

    whether to validate the parsed statements and values

Returns:

Raises:

  • (Parser::Error)

    on invalid input



254
255
256
# File 'lib/sparql/grammar.rb', line 254

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:

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

Yields:

  • (lexer)

Yield Parameters:

  • lexer (Lexer)

Returns:

  • (Lexer)

Raises:

  • (Lexer::Error)

    on invalid input



305
306
307
# File 'lib/sparql/grammar.rb', line 305

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:

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

Returns:

  • (Boolean)


286
287
288
# File 'lib/sparql/grammar.rb', line 286

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