Module: Ibex::IR::Validator

Defined in:
lib/ibex/ir/validator.rb,
lib/ibex/ir/validator/base.rb,
lib/ibex/ir/validator/lexer.rb,
lib/ibex/ir/validator/grammar.rb,
lib/ibex/ir/validator/automaton.rb,
sig/ibex/ir/validator.rbs,
sig/ibex/ir/validator/base.rbs,
sig/ibex/ir/validator/lexer.rbs,
sig/ibex/ir/validator/grammar.rbs,
sig/ibex/ir/validator/automaton.rbs

Overview

Validates a serialized public IR document before constructing immutable IR objects.

Defined Under Namespace

Classes: AutomatonDocument, Base, GrammarDocument, LexerDocument

Constant Summary collapse

POSITION =

Returns:

  • (::String)
"(ir):1:1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.validate(source) ⇒ Object

RBS:

  • (String source) -> (Grammar | Automaton | Lexer)



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ibex/ir/validator.rb', line 17

def validate(source)
  data = JSON.parse(source)
  raise Ibex::Error, "#{POSITION}: $ must be an object" unless data.is_a?(Hash)

  type = data.fetch("ibex_ir") { raise Ibex::Error, "#{POSITION}: missing ibex_ir discriminator" }
  version = data["schema_version"]
  supported = type == "lexer" ? SUPPORTED_LEXER_SCHEMA_VERSIONS : SUPPORTED_SCHEMA_VERSIONS
  unless supported.include?(version)
    expected = supported.join(", ")
    raise Ibex::Error, "#{POSITION}: unsupported schema_version #{version.inspect}; expected one of #{expected}"
  end

  case type
  when "grammar" then GrammarDocument.new(data, version: version).validate
  when "automaton" then AutomatonDocument.new(data, version: version).validate
  when "lexer" then LexerDocument.new(data).validate
  else raise Ibex::Error, "#{POSITION}: unsupported IR type #{type.inspect}"
  end
  value = Serialize.load(source)
  validate_automaton_digest(value) if value.is_a?(Automaton)
  value
rescue JSON::ParserError => e
  raise Ibex::Error, "#{POSITION}: invalid JSON: #{e.message}"
rescue Ibex::Error
  raise
rescue KeyError, NoMethodError, TypeError, ArgumentError => e
  raise Ibex::Error, "#{POSITION}: invalid IR structure: #{e.message}"
end

.validate_automaton_digest(automaton) ⇒ Object

RBS:

  • (Automaton automaton) -> void

Raises:



48
49
50
51
52
53
54
# File 'lib/ibex/ir/validator.rb', line 48

def validate_automaton_digest(automaton)
  expected = "sha256:#{Digest::SHA256.hexdigest(Serialize.dump(automaton.grammar))}"
  return if automaton.grammar_digest == expected

  raise Ibex::Error,
        "#{POSITION}: $.grammar_digest does not match the embedded grammar; expected #{expected.inspect}"
end

Instance Method Details

#self?.validateGrammar, ...

RBS:

  • (String source) -> (Grammar | Automaton | Lexer)

Parameters:

  • source (String)

Returns:



10
# File 'sig/ibex/ir/validator.rbs', line 10

def self?.validate: (String source) -> (Grammar | Automaton | Lexer)

#self?.validate_automaton_digestvoid

This method returns an undefined value.

RBS:

  • (Automaton automaton) -> void

Parameters:



13
# File 'sig/ibex/ir/validator.rbs', line 13

def self?.validate_automaton_digest: (Automaton automaton) -> void