module SPARQL::Grammar

## 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

@see www.w3.org/TR/sparql11-query/#grammar @see rubygems.org/gems/ebnf

Public Class Methods

open(filename, **options, &block) click to toggle source

Parses input from the given file name or URL.

@param [String, to_s] filename @param [Hash{Symbol => Object}] options

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

@option options [Symbol] :format (:ntriples) @yield [reader] @yieldparam [RDF::Reader] reader @yieldreturn [void] ignored @raise [RDF::FormatError] if no reader found for the specified format

# File lib/sparql/grammar.rb, line 268
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) click to toggle source

Parse the given SPARQL `query` string.

@example

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

@param [IO, StringIO, Lexer, Array, String, to_s] query

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

@param [Hash{Symbol => Object}] options @return [Parser] @raise [Parser::Error] on invalid input

# File lib/sparql/grammar.rb, line 253
def self.parse(query, **options, &block)
  Parser.new(query, **options).parse(options[:update] ? :UpdateUnit : :QueryUnit)
end
tokenize(query, **options, &block) click to toggle source

Tokenizes the given SPARQL `query` string.

@example

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

@param [String, to_s] query @param [Hash{Symbol => Object}] options @yield [lexer] @yieldparam [Lexer] lexer @return [Lexer] @raise [Lexer::Error] on invalid input

# File lib/sparql/grammar.rb, line 303
def self.tokenize(query, **options, &block)
  Lexer.tokenize(query, **options, &block)
end
valid?(query, **options) click to toggle source

Returns `true` if the given SPARQL `query` string is valid.

@example

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

@param [String, to_s] query @param [Hash{Symbol => Object}] options @return [Boolean]

# File lib/sparql/grammar.rb, line 284
def self.valid?(query, **options)
  Parser.new(query, **options).valid?
end