Class: SPARQL::Grammar::Parser
- Includes:
- EBNF::PEG::Parser, Meta, Terminals
- Defined in:
- vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb
Overview
A parser for the SPARQL 1.1 grammar.
Constant Summary
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::LANG_DIR, 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::U_CHARS1, Terminals::U_CHARS2, Terminals::VAR1, Terminals::VAR2, Terminals::VARNAME, Terminals::WS
Constants included from Meta
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 SPARQL::Algebra::Operator objects.
Attributes included from EBNF::PEG::Parser
#packrat, #scanner, #whitespace
Instance Method Summary collapse
-
#initialize(input = nil, **options) {|parser| ... } ⇒ SPARQL::Grammar::Parser
constructor
Initializes a new parser instance.
-
#parse(prod = :QueryUnit) ⇒ RDF::Queryable
Parse query.
- #peg_parse ⇒ Object
- #to_s ⇒ Object
- #to_sxp_bin ⇒ String
-
#valid? ⇒ Boolean
Returns
true
if the input string is syntactically valid.
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| ... } ⇒ SPARQL::Grammar::Parser
Initializes a new parser instance.
2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb', line 2667 def initialize(input = nil, **, &block) @input = case input when IO, StringIO then EBNF::Unescape.unescape_codepoints(input.read) else EBNF::Unescape.unescape_codepoints(input.to_s) 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} @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
#input ⇒ String
The current input string being processed.
26 27 28 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb', line 26 def input @input end |
#options ⇒ Hash (readonly)
Any additional options for the parser.
20 21 22 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb', line 20 def @options end |
#result ⇒ Array
The internal representation of the result using hierarchy of RDF objects and SPARQL::Algebra::Operator objects.
32 33 34 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb', line 32 def result @result end |
Instance Method Details
#parse(prod = :QueryUnit) ⇒ RDF::Queryable
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)))))
2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb', line 2726 def parse(prod = :QueryUnit) res = peg_parse(@input, start: prod.to_sym, rules: SPARQL::Grammar::Meta::RULES, whitespace: WS, insensitive_strings: :upper, **@options ) # The last thing on the @prod_data stack is the result @result = case when !res.is_a?(Hash) res when res.empty? nil when res[:query] res[:query] when res[:update] res[:update] else key = res.keys.first value = res[key] value = [value] unless value.is_a?(Array) value.unshift(key) end # Validate resulting expression @result.validate! if @result && validate? @result end |
#peg_parse ⇒ Object
2709 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb', line 2709 alias_method :peg_parse, :parse |
#to_s ⇒ Object
2705 2706 2707 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb', line 2705 def to_s @result.to_sxp end |
#to_sxp_bin ⇒ String
2701 2702 2703 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb', line 2701 def to_sxp_bin @result end |
#valid? ⇒ Boolean
Returns true
if the input string is syntactically valid.
2693 2694 2695 2696 2697 2698 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/grammar/parser.rb', line 2693 def valid? parse true rescue Error false end |