Class: Ibex::IR::Validator::GrammarDocument

Inherits:
Base
  • Object
show all
Defined in:
lib/ibex/ir/validator/grammar.rb

Overview

Structural and referential validation for a versioned Grammar IR JSON object. rubocop:disable Metrics/ClassLength -- inline type contracts accompany one cohesive document validator.

Constant Summary collapse

ROOT_REQUIRED =
%w[
  ibex_ir schema_version class_name superclass start expect options symbols productions user_code
  conversions warnings
].freeze
ROOT_OPTIONAL =

: Array

%w[user_code_chunks expect_rr].freeze
V2_ROOT_REQUIRED =

: Array

%w[source_provenance migration].freeze
V2_ROOT_OPTIONAL =

: Array

%w[params printers tests recovery lexer mode starts].freeze
SYMBOL_REQUIRED =

: Array

%w[id name kind reserved prec loc].freeze
SYMBOL_OPTIONAL =

: Array

%w[display_name semantic_type].freeze
V2_SYMBOL_REQUIRED =

: Array

%w[doc].freeze
PRODUCTION_REQUIRED =

: Array

%w[id lhs rhs action prec_override origin].freeze
V2_PRODUCTION_REQUIRED =

: Array

%w[doc expansion].freeze
V2_ACTION_REQUIRED =

: Array

%w[composition].freeze
ORIGIN_KINDS =

: Array

%w[
  optional_expansion star_expansion plus_expansion separated_list_expansion group_expansion
].freeze
RUBY_KEYWORDS =

: Array

%w[
  __ENCODING__ __FILE__ __LINE__ alias and begin break case class def defined do else elsif end ensure false for
  if in module next nil not or redo rescue retry return self super then true undef unless until when while yield
].freeze

Constants inherited from Base

Base::POSITION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, path: "$", version: data.fetch("schema_version")) ⇒ GrammarDocument

Returns a new instance of GrammarDocument.



39
40
41
42
43
44
45
46
47
# File 'lib/ibex/ir/validator/grammar.rb', line 39

def initialize(data, path: "$", version: data.fetch("schema_version"))
  super()
  @data = data
  @path = path
  @version = version
  @symbols_by_id = {}
  @symbols_by_name = {}
  @productions_by_id = {}
end

Instance Attribute Details

#productions_by_idObject (readonly)

: Hash[Integer, Hash[String, untyped]]



32
33
34
# File 'lib/ibex/ir/validator/grammar.rb', line 32

def productions_by_id
  @productions_by_id
end

#symbols_by_idObject (readonly)

: Array



30
31
32
# File 'lib/ibex/ir/validator/grammar.rb', line 30

def symbols_by_id
  @symbols_by_id
end

#symbols_by_nameObject (readonly)

: Hash[String, Hash[String, untyped]]



31
32
33
# File 'lib/ibex/ir/validator/grammar.rb', line 31

def symbols_by_name
  @symbols_by_name
end

Instance Method Details

#validateObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ibex/ir/validator/grammar.rb', line 50

def validate
  required = ROOT_REQUIRED + (@version >= 2 ? V2_ROOT_REQUIRED : [])
  optional = ROOT_OPTIONAL + (@version >= 2 ? V2_ROOT_OPTIONAL : [])
  record(@data, @path, required, optional)
  validate_envelope
  validate_header
  validate_options
  validate_parser_parameters if @data.key?("params")
  validate_symbols
  validate_value_printers if @data.key?("printers")
  validate_grammar_tests if @data.key?("tests")
  validate_reserved_symbols
  validate_start
  validate_lexer if @data.key?("lexer")
  validate_recovery if @data.key?("recovery")
  validate_productions
  validate_string_map(@data["user_code"], "#{@path}.user_code")
  validate_string_map(@data["conversions"], "#{@path}.conversions")
  validate_warnings
  validate_user_code_chunks if @data.key?("user_code_chunks")
  if @version >= 2
    validate_source_provenance(@data["source_provenance"], "#{@path}.source_provenance")
    validate_migration
  end
  self
end