Class: ShEx::Parser
- Inherits:
-
Object
- Object
- ShEx::Parser
- 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
-
#input ⇒ String
The current input string being processed.
-
#options ⇒ Hash
readonly
Any additional options for the parser.
-
#result ⇒ Array
The internal representation of the result using hierarchy of RDF objects and ShEx::Operator objects.
-
#tokens ⇒ Array<Token>
readonly
The current input tokens being processed.
Instance Method Summary collapse
-
#initialize(input = nil, options = {}) {|parser| ... } ⇒ ShEx::Parser
constructor
Initializes a new parser instance.
- #ll1_parse ⇒ Object
-
#parse(prod = START) ⇒ Array
Parse query.
- #to_s ⇒ Object
- #to_sxp_bin ⇒ String
Constructor Details
#initialize(input = nil, options = {}) {|parser| ... } ⇒ ShEx::Parser
Initializes a new parser instance.
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 |
# File 'lib/shex/parser.rb', line 587 def initialize(input = nil, = {}, &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!) = {anon_base: "b0", validate: false}.merge() [:debug] ||= case when [:progress] then 2 when [:validate] then 1 end 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
#input ⇒ String
The current input string being processed.
26 27 28 |
# File 'lib/shex/parser.rb', line 26 def input @input end |
#options ⇒ Hash (readonly)
Any additional options for the parser.
20 21 22 |
# File 'lib/shex/parser.rb', line 20 def end |
#result ⇒ Array
The internal representation of the result using hierarchy of RDF objects and ShEx::Operator objects.
39 40 41 |
# File 'lib/shex/parser.rb', line 39 def result @result end |
#tokens ⇒ Array<Token> (readonly)
The current input tokens being processed.
32 33 34 |
# File 'lib/shex/parser.rb', line 32 def tokens @tokens end |
Instance Method Details
#ll1_parse ⇒ Object
619 |
# File 'lib/shex/parser.rb', line 619 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)))))
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 667 668 669 670 671 672 673 674 675 676 677 |
# File 'lib/shex/parser.rb', line 636 def parse(prod = START) ll1_parse(@input, prod.to_sym, .merge(branch: BRANCH, first: FIRST, follow: FOLLOW, whitespace: WS) ) do |context, *data| case context when :trace level, lineno, depth, *args = data = args.to_sse d_str = depth > 100 ? ' ' * 100 + '+' : ' ' * depth str = "[#{lineno}](#{level})#{d_str}#{message}".chop case [:debug] when Array [:debug] << str unless level > 2 when TrueClass $stderr.puts str when Integer $stderr.puts(str) if level <= [: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[: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., lineno: e.lineno, token: e.token) end |
#to_s ⇒ Object
615 616 617 |
# File 'lib/shex/parser.rb', line 615 def to_s @result.to_sxp end |
#to_sxp_bin ⇒ String
611 612 613 |
# File 'lib/shex/parser.rb', line 611 def to_sxp_bin @result end |