Class: SPARQL::Grammar::Parser

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

Overview

A parser for the SPARQL 1.1 grammar.

Constant Summary collapse

BUILTINS =

Builtin functions

%w{
  ABS  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
}.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

Instance Method Summary collapse

Constructor Details

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

Initializes a new parser instance.

Parameters:

Options Hash (options):

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

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

  • :base_uri (#to_s) — default: nil

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

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

    Basis for generating anonymous Nodes

  • :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

  • :progress (Boolean)

    Show progress of parser productions

  • :debug (Boolean)

    Detailed debug output

Yields:

  • (parser)

    self

Yield Parameters:

Yield Returns:

  • (void)

    ignored



1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/grammar/parser11.rb', line 1469

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)
  @options[:debug] ||= case
  when options[:progress] then 2
  when options[:validate] then 1
  end

  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:



41
42
43
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/grammar/parser11.rb', line 41

def input
  @input
end

#optionsHash (readonly)

Any additional options for the parser.

Returns:



35
36
37
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/grammar/parser11.rb', line 35

def options
  @options
end

#resultArray

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

Returns:



53
54
55
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/grammar/parser11.rb', line 53

def result
  @result
end

#tokensArray<Token> (readonly)

The current input tokens being processed.

Returns:



47
48
49
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/grammar/parser11.rb', line 47

def tokens
  @tokens
end

Instance Method Details

#ll1_parseObject



1515
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/grammar/parser11.rb', line 1515

alias_method :ll1_parse, :parse

#parse(prod = START) ⇒ Array

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:



1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/grammar/parser11.rb', line 1532

def parse(prod = START)
  ll1_parse(@input, prod.to_sym, @options.merge(branch: BRANCH,
                                                first: FIRST,
                                                follow: FOLLOW,
                                                whitespace: WS)
  ) do |context, *data|
    case context
    when :trace
      level, lineno, depth, *args = data
      message = args.to_sse
      d_str = depth > 100 ? ' ' * 100 + '+' : ' ' * depth
      str = "[#{lineno}](#{level})#{d_str}#{message}".chop
      case @options[:debug]
      when Array
        @options[:debug] << str unless level > 2
      when TrueClass
        $stderr.puts str
      when Integer
        $stderr.puts(str) if level <= @options[:debug]
      end
    end
  end

  # 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



1511
1512
1513
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/grammar/parser11.rb', line 1511

def to_s
  @result.to_sxp
end

#to_sxp_binString

Returns:



1507
1508
1509
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/grammar/parser11.rb', line 1507

def to_sxp_bin
  @result
end

#valid?Boolean

Returns true if the input string is syntactically valid.

Returns:

  • (Boolean)


1499
1500
1501
1502
1503
1504
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/grammar/parser11.rb', line 1499

def valid?
  parse
  true
rescue Error
  false
end