Class: ShEx::Parser

Inherits:
Object show all
Includes:
EBNF::PEG::Parser, RDF::Util::Logger, Terminals
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex/parser.rb

Overview

A parser for the ShEx grammar.

Constant Summary

Constants included from RDF::Util::Logger

RDF::Util::Logger::IOWrapper

Constants included from Terminals

Terminals::ATPNAME_LN, Terminals::ATPNAME_NS, Terminals::BLANK_NODE_LABEL, Terminals::CODE, Terminals::DECIMAL, Terminals::DOUBLE, Terminals::ECHAR, Terminals::EXPONENT, Terminals::INTEGER, Terminals::IRIREF, Terminals::IRI_RANGE, Terminals::LANGTAG, Terminals::LANG_STRING_LITERAL1, Terminals::LANG_STRING_LITERAL2, Terminals::LANG_STRING_LITERAL_LONG1, Terminals::LANG_STRING_LITERAL_LONG2, 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::RDF_TYPE, Terminals::REGEXP, Terminals::REPEAT_RANGE, Terminals::STRING_LITERAL1, Terminals::STRING_LITERAL2, Terminals::STRING_LITERAL_LONG1, Terminals::STRING_LITERAL_LONG2, Terminals::UCHAR, Terminals::UCHAR4, Terminals::UCHAR8, Terminals::U_CHARS1, Terminals::U_CHARS2, Terminals::WS

Instance Attribute Summary collapse

Attributes included from EBNF::PEG::Parser

#packrat, #scanner, #whitespace

Instance Method Summary collapse

Methods included from RDF::Util::Logger

#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger

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| ... } ⇒ ShEx::Parser

Initializes a new parser instance.

Examples:

parsing a ShExC schema

schema = ShEx::Parser.new(%(
  PREFIX ex: <http://schema.example/> ex:IssueShape {ex:state IRI}
).parse

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

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

Raises:



851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex/parser.rb', line 851

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}

  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:



27
28
29
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex/parser.rb', line 27

def input
  @input
end

#optionsHash (readonly)

Any additional options for the parser.

Returns:



21
22
23
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex/parser.rb', line 21

def options
  @options
end

#resultArray

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



34
35
36
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex/parser.rb', line 34

def result
  @result
end

Instance Method Details

#parse(prod = :shexDoc) ⇒ ShEx::Algebra::Schema

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: :shexDoc)

    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:

Raises:

See Also:



898
899
900
901
902
903
904
905
906
907
908
909
910
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex/parser.rb', line 898

def parse(prod = :shexDoc)
  @result = peg_parse(@input,
    prod.to_sym,
    ShEx::Meta::RULES,
    whitespace: WS,
    **@options)

  # Validate resulting expression
  @result.validate! if @result && validate?
  @result
rescue EBNF::PEG::Parser::Error, EBNF::LL1::Lexer::Error =>  e
  raise ShEx::ParseError, e.message, e.backtrace
end

#peg_parseObject



879
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex/parser.rb', line 879

alias_method :peg_parse, :parse

#to_sObject



875
876
877
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex/parser.rb', line 875

def to_s
  @result.to_sxp
end

#to_sxp_binString

Returns:



871
872
873
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/shex-f64040a493d0/lib/shex/parser.rb', line 871

def to_sxp_bin
  @result
end