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::CLEANUP, 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.
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!) @options = {anon_base: "b0", validate: false}.merge() @errors = @options[:errors] @options[: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
355 356 357 |
# File 'lib/ld/patch/parser.rb', line 355 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 @options 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.
426 427 428 |
# File 'lib/ld/patch/parser.rb', line 426 def base_uri RDF::URI(@options[:base_uri]) end |
#base_uri=(iri) ⇒ RDF::URI
Set the Base URI to use for this parser.
439 440 441 |
# File 'lib/ld/patch/parser.rb', line 439 def base_uri=(iri) @options[:base_uri] = RDF::URI(iri) end |
#ll1_parse ⇒ Object
357 |
# File 'lib/ld/patch/parser.rb', line 357 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 ((: example/))
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 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 |
# File 'lib/ld/patch/parser.rb', line 368 def parse(prod = START) ll1_parse(@input, prod.to_sym, branch: BRANCH, first: FIRST, follow: FOLLOW, whitespace: WS, **@options ) 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}#{}".chop if @errors && level == 0 @errors << str else case @options[:debug] when Array @options[:debug] << str when TrueClass $stderr.puts str when Integer $stderr.puts(str) if level <= @options[: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.
472 473 474 475 |
# File 'lib/ld/patch/parser.rb', line 472 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.
451 452 453 |
# File 'lib/ld/patch/parser.rb', line 451 def prefixes @options[:prefixes] ||= {} end |