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::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::STR_EXPR, Terminals::STR_MAP, Terminals::UCHAR, Terminals::UCHAR4, Terminals::UCHAR8, 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) ⇒ ShEx::Algebra::Schema
Parse query.
- #to_s ⇒ Object
- #to_sxp_bin ⇒ String
Constructor Details
#initialize(input = nil, options = {}) {|parser| ... } ⇒ ShEx::Parser
Initializes a new parser instance.
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 |
# File 'lib/shex/parser.rb', line 733 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!) @options = {anon_base: "b0", validate: false}.merge() 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.
28 29 30 |
# File 'lib/shex/parser.rb', line 28 def input @input end |
#options ⇒ Hash (readonly)
Any additional options for the parser.
22 23 24 |
# File 'lib/shex/parser.rb', line 22 def @options end |
#result ⇒ Array
The internal representation of the result using hierarchy of RDF objects and ShEx::Operator objects.
41 42 43 |
# File 'lib/shex/parser.rb', line 41 def result @result end |
#tokens ⇒ Array<Token> (readonly)
The current input tokens being processed.
34 35 36 |
# File 'lib/shex/parser.rb', line 34 def tokens @tokens end |
Instance Method Details
#ll1_parse ⇒ Object
761 |
# File 'lib/shex/parser.rb', line 761 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)))))
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 |
# File 'lib/shex/parser.rb', line 780 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 [:logger] level, lineno, depth, *args = data case level when 0 log_error(*args, depth: depth, lineno: lineno) when 1 log_warn(*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, e., e.backtrace end |
#to_s ⇒ Object
757 758 759 |
# File 'lib/shex/parser.rb', line 757 def to_s @result.to_sxp end |
#to_sxp_bin ⇒ String
753 754 755 |
# File 'lib/shex/parser.rb', line 753 def to_sxp_bin @result end |