Class: ShEx::Parser

Inherits:
Object
  • Object
show all
Includes:
EBNF::LL1::Parser, RDF::Util::Logger, Meta, Terminals
Defined in:
lib/shex/parser.rb

Overview

A parser for the ShEx grammar.

Constant Summary

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::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::REPEAT_RANGE, 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::WS

Constants included from Meta

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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:

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

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:



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# File 'lib/shex/parser.rb', line 577

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:

  • (String)


27
28
29
# File 'lib/shex/parser.rb', line 27

def input
  @input
end

#optionsHash (readonly)

Any additional options for the parser.

Returns:

  • (Hash)


21
22
23
# File '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.

Returns:

  • (Array)

See Also:



40
41
42
# File 'lib/shex/parser.rb', line 40

def result
  @result
end

#tokensArray<Token> (readonly)

The current input tokens being processed.

Returns:

  • (Array<Token>)


33
34
35
# File 'lib/shex/parser.rb', line 33

def tokens
  @tokens
end

Instance Method Details

#ll1_parseObject



605
# File 'lib/shex/parser.rb', line 605

alias_method :ll1_parse, :parse

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

Raises:

See Also:



624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
# File 'lib/shex/parser.rb', line 624

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
      if options[:logger]
        level, lineno, depth, *args = data
        case level
        when 0
          log_error(*args, depth: depth, lineno: lineno)
        when 1
          log_warning(*args, depth: depth, lineno: lineno)
        when 2
          log_info(*args, depth: depth, lineno: lineno)
        else
          log_debug(*args, depth: depth, lineno: lineno)
        end
      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[:schema]
    prod_data[:schema]
  else
    key = prod_data.keys.first
    [key] + Array(prod_data[key])  # Creates [:key, [:triple], ...]
  end

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

#to_sObject



601
602
603
# File 'lib/shex/parser.rb', line 601

def to_s
  @result.to_sxp
end

#to_sxp_binString

Returns:

  • (String)


597
598
599
# File 'lib/shex/parser.rb', line 597

def to_sxp_bin
  @result
end