Class: SPARQL::Grammar::Parser

Inherits:
Object
  • Object
show all
Includes:
EBNF::LL1::Parser, Meta, Terminals
Defined in:
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
  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

Instance Method Summary collapse

Constructor Details

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

Initializes a new parser instance.

Parameters:

  • input (String, IO, StringIO, #to_s) (defaults to: nil)
  • options (Hash{Symbol => Object})

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

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

    Record error/info/debug output

Yields:

  • (parser)

    self

Yield Parameters:

Yield Returns:

  • (void)

    ignored


1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
# File 'lib/sparql/grammar/parser11.rb', line 1562

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:

  • (String)

42
43
44
# File 'lib/sparql/grammar/parser11.rb', line 42

def input
  @input
end

#optionsHash (readonly)

Any additional options for the parser.

Returns:


36
37
38
# File '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 '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 'lib/sparql/grammar/parser11.rb', line 48

def tokens
  @tokens
end

Instance Method Details

#ll1_parseObject


1604
# File 'lib/sparql/grammar/parser11.rb', line 1604

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:


1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
# File 'lib/sparql/grammar/parser11.rb', line 1621

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


1600
1601
1602
# File 'lib/sparql/grammar/parser11.rb', line 1600

def to_s
  @result.to_sxp
end

#to_sxp_binString

Returns:

  • (String)

1596
1597
1598
# File 'lib/sparql/grammar/parser11.rb', line 1596

def to_sxp_bin
  @result
end

#valid?Boolean

Returns true if the input string is syntactically valid.

Returns:

  • (Boolean)

1588
1589
1590
1591
1592
1593
# File 'lib/sparql/grammar/parser11.rb', line 1588

def valid?
  parse
  true
rescue Error
  false
end