Class: LD::Patch::Parser
- Inherits:
-
Object
- Object
- LD::Patch::Parser
- Defined in:
- lib/ld/patch/parser.rb
Overview
A parser for the LD Patch grammar.
Constant Summary
Constants included from Terminals
Terminals::ANON, Terminals::BLANK_NODE_LABEL, 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::STRING_LITERAL_LONG_QUOTE, Terminals::STRING_LITERAL_LONG_SINGLE_QUOTE, Terminals::STRING_LITERAL_QUOTE, Terminals::STRING_LITERAL_SINGLE_QUOTE, Terminals::STR_EXPR, Terminals::UCHAR, Terminals::U_CHARS1, Terminals::U_CHARS2, Terminals::VAR1, Terminals::VARNAME, Terminals::WS
Constants included from Meta
Meta::BRANCH, Meta::FIRST, Meta::FOLLOW, Meta::START, Meta::TERMINALS
Instance Attribute Summary collapse
-
#errors ⇒ Array<String>
readonly
Accumulated errors found during processing.
-
#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.
-
#tokens ⇒ Array<Token>
readonly
The current input tokens being processed.
Instance Method Summary collapse
-
#base_uri ⇒ HRDF::URI
Returns the Base URI defined for the parser, as specified or when parsing a BASE prologue element.
-
#base_uri=(iri) ⇒ RDF::URI
Set the Base URI to use for this parser.
-
#initialize(input = nil, options = {}) {|parser| ... } ⇒ LD::Patch::Parser
constructor
Initializes a new parser instance.
- #ll1_parse ⇒ Object
-
#parse(prod = START) ⇒ SPARQL::Algebra::Operator, Object
Parse patch.
-
#prefix(name = nil, iri = nil) ⇒ RDF::URI
Defines the given named URI prefix for this parser.
-
#prefixes ⇒ Hash{Symbol => RDF::URI}
Returns the URI prefixes currently defined for this parser.
- #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| ... } ⇒ LD::Patch::Parser
Initializes a new parser instance.
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/ld/patch/parser.rb', line 326 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() @errors = [:errors] [:debug] ||= case when [:progress] then 2 when [:validate] then (@errors ? nil : 1) end debug("base IRI") {base_uri.inspect} debug("validate") {validate?.inspect} @vars = {} if block_given? case block.arity when 0 then instance_eval(&block) else block.call(self) end end end |
Instance Attribute Details
#errors ⇒ Array<String> (readonly)
Accumulated errors found during processing
375 376 377 |
# File 'lib/ld/patch/parser.rb', line 375 def errors @errors end |
#input ⇒ String
The current input string being processed.
26 27 28 |
# File 'lib/ld/patch/parser.rb', line 26 def input @input end |
#options ⇒ Hash (readonly)
Any additional options for the parser.
20 21 22 |
# File 'lib/ld/patch/parser.rb', line 20 def end |
#result ⇒ Array
The internal representation of the result
37 38 39 |
# File 'lib/ld/patch/parser.rb', line 37 def result @result end |
#tokens ⇒ Array<Token> (readonly)
The current input tokens being processed.
32 33 34 |
# File 'lib/ld/patch/parser.rb', line 32 def tokens @tokens end |
Instance Method Details
#base_uri ⇒ HRDF::URI
Returns the Base URI defined for the parser, as specified or when parsing a BASE prologue element.
443 444 445 |
# File 'lib/ld/patch/parser.rb', line 443 def base_uri RDF::URI([:base_uri]) end |
#base_uri=(iri) ⇒ RDF::URI
Set the Base URI to use for this parser.
456 457 458 |
# File 'lib/ld/patch/parser.rb', line 456 def base_uri=(iri) [:base_uri] = RDF::URI(iri) end |
#ll1_parse ⇒ Object
377 |
# File 'lib/ld/patch/parser.rb', line 377 alias_method :ll1_parse, :parse |
#parse(prod = START) ⇒ SPARQL::Algebra::Operator, Object
Parse patch
The result is an S-List. Productions return an array such as the following:
(prefix ((: <http://example/>))
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/ld/patch/parser.rb', line 388 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 if @errors && level == 0 @errors << str else case [:debug] when Array [:debug] << str when TrueClass $stderr.puts str when Integer $stderr.puts(str) if level <= [:debug] 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[:ldpatch] prod_data[:ldpatch] 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 LD::Patch::ParseError.new(e., lineno: e.lineno, token: e.token) end |
#prefix(name, uri) ⇒ RDF::URI #prefix(name) ⇒ RDF::URI
Defines the given named URI prefix for this parser.
489 490 491 492 |
# File 'lib/ld/patch/parser.rb', line 489 def prefix(name = nil, iri = nil) name = name.to_s.empty? ? nil : (name.respond_to?(:to_sym) ? name.to_sym : name.to_s.to_sym) iri.nil? ? prefixes[name] : prefixes[name] = iri end |
#prefixes ⇒ Hash{Symbol => RDF::URI}
Returns the URI prefixes currently defined for this parser.
468 469 470 |
# File 'lib/ld/patch/parser.rb', line 468 def prefixes [:prefixes] ||= {} end |
#to_s ⇒ Object
368 369 370 |
# File 'lib/ld/patch/parser.rb', line 368 def to_s @result.to_sxp end |
#to_sxp_bin ⇒ String
364 365 366 |
# File 'lib/ld/patch/parser.rb', line 364 def to_sxp_bin @result end |
#valid? ⇒ Boolean
Returns ‘true` if the input string is syntactically valid.
356 357 358 359 360 361 |
# File 'lib/ld/patch/parser.rb', line 356 def valid? parse true rescue ParseError false end |