Class: SPARQL::Grammar::Parser

Inherits:
Object
  • Object
show all
Includes:
EBNF::LL1::Parser, Meta, Terminals
Defined in:
vendor/bundler/ruby/3.1.0/bundler/gems/sparql-111cba30fecb/lib/sparql/grammar/parser11.rb

Overview

A parser for the SPARQL 1.1 grammar.

Constant Summary collapse

BUILTINS =

Builtin functions

%w{
  ABS ADJUST BNODE CEIL COALESCE CONCAT
  CONTAINS DATATYPE DAY ENCODE_FOR_URI
  FLOOR HOURS IF IRI LANGMATCHES LANG LCASE
  MD5 MINUTES MONTH NOW RAND ROUND SECONDS
  SHA1 SHA224 SHA256 SHA384 SHA512
  STRAFTER STRBEFORE STRDT STRENDS STRLANG STRLEN STRSTARTS STRUUID STR
  TIMEZONE TZ UCASE URI UUID YEAR
  isBLANK isIRI isURI isLITERAL isNUMERIC sameTerm
  isTRIPLE TRIPLE SUBJECT PREDICATE OBJECT
}.map {|s| s.downcase.to_sym}.freeze
BUILTIN_RULES =
[:aggregate, :regex, :substr, :replace, :exists, :notexists].freeze
AGGREGATE_RULES =
[:count, :sum, :min, :max, :avg, :sample, :group_concat]

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::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::UCHAR, Terminals::U_CHARS1, Terminals::U_CHARS2, Terminals::VAR1, Terminals::VAR2, Terminals::VARNAME, Terminals::WS

Constants included from Meta

Meta::BRANCH, Meta::CLEANUP, Meta::FIRST, Meta::FOLLOW, Meta::START, Meta::TERMINALS

Instance Attribute Summary collapse

Attributes included from EBNF::LL1::Parser

#lineno

Instance Method Summary collapse

Methods included from EBNF::LL1::Parser

#add_prod_data, #add_prod_datum, #debug, #depth, #error, #prod_data, #progress, #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



2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
# File 'vendor/bundler/ruby/3.1.0/bundler/gems/sparql-111cba30fecb/lib/sparql/grammar/parser11.rb', line 2068

def initialize(input = nil, **options, &block)
  @input = case input
  when IO, StringIO then input.read
  else input.to_s.dup
  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:



42
43
44
# File 'vendor/bundler/ruby/3.1.0/bundler/gems/sparql-111cba30fecb/lib/sparql/grammar/parser11.rb', line 42

def input
  @input
end

#optionsHash (readonly)

Any additional options for the parser.

Returns:



36
37
38
# File 'vendor/bundler/ruby/3.1.0/bundler/gems/sparql-111cba30fecb/lib/sparql/grammar/parser11.rb', line 36

def options
  @options
end

#resultArray

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

Returns:



54
55
56
# File 'vendor/bundler/ruby/3.1.0/bundler/gems/sparql-111cba30fecb/lib/sparql/grammar/parser11.rb', line 54

def result
  @result
end

#tokensArray<Token> (readonly)

The current input tokens being processed.

Returns:



48
49
50
# File 'vendor/bundler/ruby/3.1.0/bundler/gems/sparql-111cba30fecb/lib/sparql/grammar/parser11.rb', line 48

def tokens
  @tokens
end

Instance Method Details

#ll1_parseObject



2110
# File 'vendor/bundler/ruby/3.1.0/bundler/gems/sparql-111cba30fecb/lib/sparql/grammar/parser11.rb', line 2110

alias_method :ll1_parse, :parse

#parse(prod = START) ⇒ 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: START)

    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:



2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
# File 'vendor/bundler/ruby/3.1.0/bundler/gems/sparql-111cba30fecb/lib/sparql/grammar/parser11.rb', line 2127

def parse(prod = START)
  ll1_parse(@input,
    prod.to_sym,
    branch: BRANCH,
    first: FIRST,
    follow: FOLLOW,
    whitespace: WS,
    **@options
  )

  # The last thing on the @prod_data stack is the result
  @result = case
  when !prod_data.is_a?(Hash)
    prod_data
  when prod_data.empty?
    nil
  when prod_data[:query]
    Array(prod_data[:query]).length == 1 ? prod_data[:query].first : prod_data[:query]
  when prod_data[:update]
    prod_data[:update]
  else
    key = prod_data.keys.first
    [key] + Array(prod_data[key])  # Creates [:key, [:triple], ...]
  end

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

#to_sObject



2106
2107
2108
# File 'vendor/bundler/ruby/3.1.0/bundler/gems/sparql-111cba30fecb/lib/sparql/grammar/parser11.rb', line 2106

def to_s
  @result.to_sxp
end

#to_sxp_binString

Returns:



2102
2103
2104
# File 'vendor/bundler/ruby/3.1.0/bundler/gems/sparql-111cba30fecb/lib/sparql/grammar/parser11.rb', line 2102

def to_sxp_bin
  @result
end

#valid?Boolean

Returns true if the input string is syntactically valid.

Returns:

  • (Boolean)


2094
2095
2096
2097
2098
2099
# File 'vendor/bundler/ruby/3.1.0/bundler/gems/sparql-111cba30fecb/lib/sparql/grammar/parser11.rb', line 2094

def valid?
  parse
  true
rescue Error
  false
end