Class: SPARQL::Grammar::Parser
- Defined in:
- lib/sparql/grammar/parser11.rb
Overview
A parser for the SPARQL 1.1 grammar.
Constant Summary collapse
- BUILTINS =
Builtin functions
%w{ ABS ADJUST BNODE CEIL COALESCE CONCAT CONTAINS DATATYPE DAY ENCODE_FOR_URI FLOOR HOURS IF IRI LANGMATCHES LANG LCASE MD5 MINUTES MONTH NOW RAND ROUND SECONDS SHA1 SHA224 SHA256 SHA384 SHA512 STRAFTER STRBEFORE STRDT STRENDS STRLANG STRLEN STRSTARTS STRUUID STR TIMEZONE TZ UCASE URI UUID YEAR isBLANK isIRI isURI isLITERAL isNUMERIC sameTerm isTRIPLE TRIPLE SUBJECT PREDICATE OBJECT }.map {|s| s.downcase.to_sym}.freeze
- BUILTIN_RULES =
[:aggregate, :regex, :substr, :replace, :exists, :notexists].freeze
- AGGREGATE_RULES =
[:count, :sum, :min, :max, :avg, :sample, :group_concat]
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::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::UCHAR, Terminals::U_CHARS1, Terminals::U_CHARS2, Terminals::VAR1, Terminals::VAR2, Terminals::VARNAME, Terminals::WS
Constants included from Meta
Meta::BRANCH, Meta::CLEANUP, Meta::FIRST, Meta::FOLLOW, 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 SPARQL::Algebra::Operator objects.
-
#tokens ⇒ Array<Token>
readonly
The current input tokens being processed.
Instance Method Summary collapse
-
#initialize(input = nil, **options) {|parser| ... } ⇒ SPARQL::Grammar::Parser
constructor
Initializes a new parser instance.
- #ll1_parse ⇒ Object
-
#parse(prod = START) ⇒ RDF::Queryable
Parse query.
- #to_s ⇒ Object
- #to_sxp_bin ⇒ String
-
#valid? ⇒ Boolean
Returns
true
if the input string is syntactically valid.
Constructor Details
#initialize(input = nil, **options) {|parser| ... } ⇒ SPARQL::Grammar::Parser
Initializes a new parser instance.
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 |
# File 'lib/sparql/grammar/parser11.rb', line 2073 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} @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.
42 43 44 |
# File 'lib/sparql/grammar/parser11.rb', line 42 def input @input end |
#options ⇒ Hash (readonly)
Any additional options for the parser.
36 37 38 |
# File 'lib/sparql/grammar/parser11.rb', line 36 def @options end |
#result ⇒ Array
The internal representation of the result using hierarchy of RDF objects and SPARQL::Algebra::Operator objects.
54 55 56 |
# File 'lib/sparql/grammar/parser11.rb', line 54 def result @result end |
#tokens ⇒ Array<Token> (readonly)
The current input tokens being processed.
48 49 50 |
# File 'lib/sparql/grammar/parser11.rb', line 48 def tokens @tokens end |
Instance Method Details
#ll1_parse ⇒ Object
2115 |
# File 'lib/sparql/grammar/parser11.rb', line 2115 alias_method :ll1_parse, :parse |
#parse(prod = START) ⇒ RDF::Queryable
Parse query
The result is a SPARQL Algebra S-List. Productions return an array such as the following:
(prefix ((: example/)) (union (bgp (triple ?s ?p ?o)) (graph ?g (bgp (triple ?s ?p ?o)))))
2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 |
# File 'lib/sparql/grammar/parser11.rb', line 2132 def parse(prod = START) ll1_parse(@input, prod.to_sym, branch: BRANCH, first: FIRST, follow: FOLLOW, whitespace: WS, **@options ) # 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[:query] Array(prod_data[:query]).length == 1 ? prod_data[:query].first : prod_data[:query] when prod_data[:update] prod_data[:update] else key = prod_data.keys.first [key] + Array(prod_data[key]) # Creates [:key, [:triple], ...] end # Validate resulting expression @result.validate! if @result && validate? @result end |
#to_s ⇒ Object
2111 2112 2113 |
# File 'lib/sparql/grammar/parser11.rb', line 2111 def to_s @result.to_sxp end |
#to_sxp_bin ⇒ String
2107 2108 2109 |
# File 'lib/sparql/grammar/parser11.rb', line 2107 def to_sxp_bin @result end |
#valid? ⇒ Boolean
Returns true
if the input string is syntactically valid.
2099 2100 2101 2102 2103 2104 |
# File 'lib/sparql/grammar/parser11.rb', line 2099 def valid? parse true rescue Error false end |