Class: LD::Patch::Parser

Inherits:
Object show all
Includes:
EBNF::LL1::Parser, Meta, Terminals
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/ld-patch-a8edc852261e/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

Attributes included from EBNF::LL1::Parser

#lineno

Instance Method Summary collapse

Methods included from EBNF::LL1::Parser

#add_prod_data, #add_prod_datum, #debug, #depth, #error, #prod_data, #progress, #warn

Constructor Details

#initialize(input = nil, **options) {|parser| ... } ⇒ LD::Patch::Parser

Initializes a new parser instance.

Parameters:

Options Hash (**options):

  • :base_uri (#to_s) — default: nil

    the base URI to use when resolving relative URIs

  • :anon_base (#to_s) — default: "b0"

    Basis for generating anonymous Nodes

  • :resolve_iris (Boolean) — default: false

    Resolve prefix and relative IRIs, otherwise, when serializing the parsed SSE as S-Expressions, use the original prefixed and relative URIs along with base and prefix definitions.

  • :validate (Boolean) — default: false

    whether to validate the parsed statements and values

  • :errors (Array)

    array for placing errors found when parsing

  • :warnings (Array)

    array for placing warnings found when parsing

  • :progress (Boolean)

    Show progress of parser productions

  • :debug (Boolean)

    Detailed debug output

Yields:

  • (parser)

    self

Yield Parameters:



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 'vendor/bundler/ruby/3.3.0/bundler/gems/ld-patch-a8edc852261e/lib/ld/patch/parser.rb', line 326

def initialize(input = nil, **options, &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(options)
  @errors = @options[:errors]
  @options[:debug] ||= case
  when options[:progress] then 2
  when options[: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

#errorsArray<String> (readonly)

Accumulated errors found during processing

Returns:



355
356
357
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ld-patch-a8edc852261e/lib/ld/patch/parser.rb', line 355

def errors
  @errors
end

#inputString

The current input string being processed.

Returns:



26
27
28
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ld-patch-a8edc852261e/lib/ld/patch/parser.rb', line 26

def input
  @input
end

#optionsHash (readonly)

Any additional options for the parser.

Returns:



20
21
22
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ld-patch-a8edc852261e/lib/ld/patch/parser.rb', line 20

def options
  @options
end

#resultArray

The internal representation of the result

Returns:



37
38
39
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ld-patch-a8edc852261e/lib/ld/patch/parser.rb', line 37

def result
  @result
end

#tokensArray<Token> (readonly)

The current input tokens being processed.

Returns:



32
33
34
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ld-patch-a8edc852261e/lib/ld/patch/parser.rb', line 32

def tokens
  @tokens
end

Instance Method Details

#base_uriHRDF::URI

Returns the Base URI defined for the parser, as specified or when parsing a BASE prologue element.

Examples:

base  #=> RDF::URI('http://example.com/')

Returns:

  • (HRDF::URI)


426
427
428
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ld-patch-a8edc852261e/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.

Examples:

base_uri = RDF::URI('http://purl.org/dc/terms/')

Parameters:

Returns:



439
440
441
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ld-patch-a8edc852261e/lib/ld/patch/parser.rb', line 439

def base_uri=(iri)
  @options[:base_uri] = RDF::URI(iri)
end

#ll1_parseObject



357
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ld-patch-a8edc852261e/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 ((: http://example/))

Parameters:

  • prod (Symbol, #to_s) (defaults to: START)

    The starting production for the parser. It may be a URI from the grammar, or a symbol representing the local_name portion of the grammar URI.

Returns:

Raises:



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 'vendor/bundler/ruby/3.3.0/bundler/gems/ld-patch-a8edc852261e/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
      message = args.to_sse
      d_str = depth > 100 ? ' ' * 100 + '+' : ' ' * depth
      str = "[#{lineno}](#{level})#{d_str}#{message}".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.message, 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.

Examples:

Defining a URI prefix

prefix :dc, RDF::URI('http://purl.org/dc/terms/')

Returning a URI prefix

prefix(:dc)    #=> RDF::URI('http://purl.org/dc/terms/')

Overloads:

Returns:



472
473
474
475
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ld-patch-a8edc852261e/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

#prefixesHash{Symbol => RDF::URI}

Returns the URI prefixes currently defined for this parser.

Examples:

prefixes[:dc]  #=> RDF::URI('http://purl.org/dc/terms/')

Returns:

Since:

  • 0.3.0



451
452
453
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ld-patch-a8edc852261e/lib/ld/patch/parser.rb', line 451

def prefixes
  @options[:prefixes] ||= {}
end