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
Overview
Validates a serialized public IR document before constructing immutable IR objects.
Defined Under Namespace
Classes: AutomatonDocument, Base, GrammarDocument, LexerDocument
Constant Summary collapse
- POSITION =
"(ir):1:1"
Class Method Summary collapse
Class Method Details
.validate(source) ⇒ Object
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.}" rescue Ibex::Error raise rescue KeyError, NoMethodError, TypeError, ArgumentError => e raise Ibex::Error, "#{POSITION}: invalid IR structure: #{e.}" end |
.validate_automaton_digest(automaton) ⇒ Object
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 |