Class: SPARQL::Grammar::Parser

Inherits:
Object
  • Object
show all
Includes:
EBNF::PEG::Parser, Meta, Terminals
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb

Overview

A parser for the SPARQL 1.1 grammar.

Constant Summary

Constants included from Terminals

Terminals::ANON, Terminals::BLANK_NODE_LABEL, Terminals::DECIMAL, Terminals::DECIMAL_NEGATIVE, Terminals::DECIMAL_POSITIVE, Terminals::DOUBLE, Terminals::DOUBLE_NEGATIVE, Terminals::DOUBLE_POSITIVE, Terminals::ECHAR, Terminals::EXPONENT, Terminals::INTEGER, Terminals::INTEGER_NEGATIVE, Terminals::INTEGER_POSITIVE, Terminals::IRIREF, Terminals::IRI_RANGE, Terminals::LANGTAG, Terminals::LANG_DIR, Terminals::NIL, Terminals::PERCENT, Terminals::PLX, Terminals::PNAME_LN, Terminals::PNAME_NS, Terminals::PN_CHARS, Terminals::PN_CHARS_BASE, Terminals::PN_CHARS_BODY, Terminals::PN_CHARS_U, Terminals::PN_LOCAL, Terminals::PN_LOCAL_BODY, Terminals::PN_LOCAL_ESC, Terminals::PN_PREFIX, Terminals::STRING_LITERAL1, Terminals::STRING_LITERAL2, Terminals::STRING_LITERAL_LONG1, Terminals::STRING_LITERAL_LONG2, Terminals::STR_EXPR, Terminals::STR_MAP, Terminals::U_CHARS1, Terminals::U_CHARS2, Terminals::VAR1, Terminals::VAR2, Terminals::VARNAME, Terminals::WS

Constants included from Meta

Meta::RULES

Instance Attribute Summary collapse

Attributes included from EBNF::PEG::Parser

#packrat, #scanner, #whitespace

Instance Method Summary collapse

Methods included from EBNF::PEG::Parser

#clear_packrat, #debug, #depth, #error, #find_rule, #onFinish, #onStart, #onTerminal, #prod_data, #progress, #terminal_options, #terminal_regexp, #update_furthest_failure, #warn

Constructor Details

#initialize(input = nil, **options) {|parser| ... } ⇒ SPARQL::Grammar::Parser

Initializes a new parser instance.

Parameters:

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

Yields:

  • (parser)

    self

Yield Parameters:

Yield Returns:

  • (void)

    ignored



2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb', line 2667

def initialize(input = nil, **options, &block)
  @input = case input
  when IO, StringIO then EBNF::Unescape.unescape_codepoints(input.read)
  else EBNF::Unescape.unescape_codepoints(input.to_s)
  end
  @input.encode!(Encoding::UTF_8) if @input.respond_to?(:encode!)
  @options = {anon_base: "b0", validate: false}.merge(options)

  debug("base IRI") {base_uri.inspect}
  debug("validate") {validate?.inspect}

  @vars = {}
  @nd_var_gen = "0"

  if block_given?
    case block.arity
      when 0 then instance_eval(&block)
      else block.call(self)
    end
  end
end

Instance Attribute Details

#inputString

The current input string being processed.

Returns:



26
27
28
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb', line 26

def input
  @input
end

#optionsHash (readonly)

Any additional options for the parser.

Returns:



20
21
22
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb', line 20

def options
  @options
end

#resultArray

The internal representation of the result using hierarchy of RDF objects and SPARQL::Algebra::Operator objects.

Returns:



32
33
34
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb', line 32

def result
  @result
end

Instance Method Details

#parse(prod = :QueryUnit) ⇒ RDF::Queryable

Parse query

The result is a SPARQL Algebra S-List. Productions return an array such as the following:

(prefix ((: http://example/)) (union (bgp (triple ?s ?p ?o)) (graph ?g (bgp (triple ?s ?p ?o)))))

Parameters:

  • prod (Symbol, #to_s) (defaults to: :QueryUnit)

    The starting production for the parser. It may be a URI from the grammar, or a symbol representing the local_name portion of the grammar URI.

Returns:

See Also:



2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb', line 2726

def parse(prod = :QueryUnit)
  res = peg_parse(@input,
    start: prod.to_sym,
    rules: SPARQL::Grammar::Meta::RULES,
    whitespace: WS,
    insensitive_strings: :upper,
    **@options
  )

  # The last thing on the @prod_data stack is the result
  @result = case
  when !res.is_a?(Hash)
    res
  when res.empty?
    nil
  when res[:query]
    res[:query]
  when res[:update]
    res[:update]
  else
    key = res.keys.first
    value = res[key]
    value = [value] unless value.is_a?(Array)
    value.unshift(key)
  end

  # Validate resulting expression
  @result.validate! if @result && validate?
  @result
end

#peg_parseObject



2709
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb', line 2709

alias_method :peg_parse, :parse

#to_sObject



2705
2706
2707
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb', line 2705

def to_s
  @result.to_sxp
end

#to_sxp_binString

Returns:



2701
2702
2703
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb', line 2701

def to_sxp_bin
  @result
end

#valid?Boolean

Returns true if the input string is syntactically valid.

Returns:

  • (Boolean)


2693
2694
2695
2696
2697
2698
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb', line 2693

def valid?
  parse
  true
rescue Error
  false
end