Module: Ibex::Runtime::CST::Validator

Defined in:
lib/ibex/runtime/cst/validator.rb,
sig/ibex/runtime/cst/validator.rbs

Overview

Validates schema-v1 CST documents while rebuilding all derived Green data.

Constant Summary collapse

TOP_KEYS =

rubocop:disable Metrics/ModuleLength -- closed schema checks remain auditable in one boundary.

Returns:

  • (Array[String])
%w[
  ibex_ir schema_version grammar_digest table_format state_count production_count trivia_policy kinds root memo
].freeze
KIND_KEYS =

Signature:

  • Array[String]

Returns:

  • (Array[String])
%w[
  names terminal_range nonterminal_range named named_nonterminals trivia synthetic
].freeze
KNOWN_FLAGS =

Signature:

  • Array[String]

Returns:

  • (Integer)
(
  Flags::CONTAINS_ERROR | Flags::CONTAINS_MISSING | Flags::CONTAINS_SKIPPED |
  Flags::HAS_ANNOTATION | Flags::SYNTHETIC | Flags::INCOMPLETE_INPUT
)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.validate(source, grammar_digest: nil, state_count: nil, production_count: nil) ⇒ Object

RBS:

  • (String source, ?grammar_digest: String?, ?state_count: Integer?, ?production_count: Integer?) -> SerializedTree



41
42
43
44
45
46
47
48
49
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
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ibex/runtime/cst/validator.rb', line 41

def validate(source, grammar_digest: nil, state_count: nil, production_count: nil) # rubocop:disable Metrics/MethodLength
  document = JSON.parse(source)
  object!(document, "$")
  exact_keys!(document, TOP_KEYS, "$")
  value!(document, "ibex_ir", "cst", "$.ibex_ir")
  value!(document, "schema_version", 1, "$.schema_version")
  digest = string!(document.fetch("grammar_digest"), "$.grammar_digest")
  unless digest.match?(/\Asha256:[0-9a-f]{64}\z/)
    fail_validation(:invalid_digest, "$.grammar_digest", "expected a SHA-256 digest", actual: digest)
  end
  if grammar_digest && digest != grammar_digest
    fail_validation(
      :grammar_digest_mismatch, "$.grammar_digest",
      "grammar digest does not match", expected: grammar_digest, actual: digest
    )
  end

  kinds = load_kinds(document.fetch("kinds"))
  root = load_element(document.fetch("root"), kinds, "$.root")
  fail_validation(:invalid_root, "$.root", "root must be a node") unless root.is_a?(GreenNode)
  validate_derived!(root, "$.root")
  document_state_count = nonnegative_integer!(document.fetch("state_count"), "$.state_count")
  document_production_count = nonnegative_integer!(
    document.fetch("production_count"), "$.production_count"
  )
  memo = load_memo(
    document.fetch("memo"),
    root,
    digest,
    document_state_count,
    document_production_count,
    expected_state_count: state_count,
    expected_production_count: production_count
  )

  SerializedTree.new(
    grammar_digest: digest,
    table_format: positive_integer!(document.fetch("table_format"), "$.table_format"),
    state_count: document_state_count,
    production_count: document_production_count,
    trivia_policy: trivia_policy!(document.fetch("trivia_policy"), "$.trivia_policy"),
    kinds: kinds, green_root: root, memo: memo
  )
rescue JSON::ParserError => e
  fail_validation(:invalid_json, "$", e.message)
rescue KeyError => e
  fail_validation(:missing_field, "$", e.message)
end

Instance Method Details

#self?.array!Array[untyped]

RBS:

  • (untyped value, String path) -> Array[untyped]

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (Array[untyped])


65
# File 'sig/ibex/runtime/cst/validator.rbs', line 65

def self?.array!: (untyped value, String path) -> Array[untyped]

#self?.decode_textString

RBS:

  • (untyped value, String path) -> String

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (String)


53
# File 'sig/ibex/runtime/cst/validator.rbs', line 53

def self?.decode_text: (untyped value, String path) -> String

#self?.exact_keys!void

This method returns an undefined value.

RBS:

  • (Hash[String, untyped] value, Array[String] expected, String path) -> void

Parameters:

  • value (Hash[String, untyped])
  • expected (Array[String])
  • path (String)


98
# File 'sig/ibex/runtime/cst/validator.rbs', line 98

def self?.exact_keys!: (Hash[String, untyped] value, Array[String] expected, String path) -> void

#self?.fail_validationbot

RBS:

  • (Symbol code, String path, String message, ?expected: untyped, ?actual: untyped) -> bot

Parameters:

  • code (Symbol)
  • path (String)
  • message (String)
  • expected: (Object)
  • actual: (Object)

Returns:

  • (bot)


104
# File 'sig/ibex/runtime/cst/validator.rbs', line 104

def self?.fail_validation: (Symbol code, String path, String message, ?expected: untyped, ?actual: untyped) -> bot

#self?.flags!Integer

RBS:

  • (untyped value, String path) -> Integer

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (Integer)


80
# File 'sig/ibex/runtime/cst/validator.rbs', line 80

def self?.flags!: (untyped value, String path) -> Integer

#self?.integer_integer_map!Hash[Integer, Integer]

RBS:

  • (untyped value, String path) -> Hash[Integer, Integer]

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (Hash[Integer, Integer])


89
# File 'sig/ibex/runtime/cst/validator.rbs', line 89

def self?.integer_integer_map!: (untyped value, String path) -> Hash[Integer, Integer]

#self?.integer_pair!Array[Integer]

RBS:

  • (untyped value, String path) -> Array[Integer]

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (Array[Integer])


83
# File 'sig/ibex/runtime/cst/validator.rbs', line 83

def self?.integer_pair!: (untyped value, String path) -> Array[Integer]

#self?.load_elementGreenNode, GreenToken

RBS:

  • (untyped value, Kind kinds, String path) -> (GreenNode | GreenToken)

Parameters:

  • value (Object)
  • kinds (Kind)
  • path (String)

Returns:



47
# File 'sig/ibex/runtime/cst/validator.rbs', line 47

def self?.load_element: (untyped value, Kind kinds, String path) -> (GreenNode | GreenToken)

#self?.load_kindsKind

RBS:

  • (untyped value) -> Kind

Parameters:

  • value (Object)

Returns:



34
# File 'sig/ibex/runtime/cst/validator.rbs', line 34

def self?.load_kinds: (untyped value) -> Kind

#self?.load_memoParseMemo?

RBS:

  • (untyped value, GreenNode root, String digest, Integer state_count, Integer production_count, expected_state_count: Integer?, expected_production_count: Integer?) -> ParseMemo?

Parameters:

  • value (Object)
  • root (GreenNode)
  • digest (String)
  • state_count (Integer)
  • production_count (Integer)
  • expected_state_count: (Integer, nil)
  • expected_production_count: (Integer, nil)

Returns:



38
# File 'sig/ibex/runtime/cst/validator.rbs', line 38

def self?.load_memo: (untyped value, GreenNode root, String digest, Integer state_count, Integer production_count, expected_state_count: Integer?, expected_production_count: Integer?) -> ParseMemo?

#self?.load_memo_statesArray[Integer?]

RBS:

  • (untyped value, Integer state_count) -> Array[Integer?]

Parameters:

  • value (Object)
  • state_count (Integer)

Returns:

  • (Array[Integer?])


41
# File 'sig/ibex/runtime/cst/validator.rbs', line 41

def self?.load_memo_states: (untyped value, Integer state_count) -> Array[Integer?]

#self?.load_triviaArray[GreenTrivia]

RBS:

  • (untyped value, Kind kinds, String path) -> Array[GreenTrivia]

Parameters:

  • value (Object)
  • kinds (Kind)
  • path (String)

Returns:



50
# File 'sig/ibex/runtime/cst/validator.rbs', line 50

def self?.load_trivia: (untyped value, Kind kinds, String path) -> Array[GreenTrivia]

#self?.nonnegative_integer!Integer

RBS:

  • (untyped value, String path) -> Integer

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (Integer)


71
# File 'sig/ibex/runtime/cst/validator.rbs', line 71

def self?.nonnegative_integer!: (untyped value, String path) -> Integer

#self?.object!Hash[String, untyped]

RBS:

  • (untyped value, String path) -> Hash[String, untyped]

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (Hash[String, untyped])


62
# File 'sig/ibex/runtime/cst/validator.rbs', line 62

def self?.object!: (untyped value, String path) -> Hash[String, untyped]

#self?.positive_integer!Integer

RBS:

  • (untyped value, String path) -> Integer

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (Integer)


74
# File 'sig/ibex/runtime/cst/validator.rbs', line 74

def self?.positive_integer!: (untyped value, String path) -> Integer

#self?.string!String

RBS:

  • (untyped value, String path) -> String

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (String)


68
# File 'sig/ibex/runtime/cst/validator.rbs', line 68

def self?.string!: (untyped value, String path) -> String

#self?.string_integer_map!Hash[String, Integer]

RBS:

  • (untyped value, String path) -> Hash[String, Integer]

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (Hash[String, Integer])


86
# File 'sig/ibex/runtime/cst/validator.rbs', line 86

def self?.string_integer_map!: (untyped value, String path) -> Hash[String, Integer]

#self?.trivia_policy!Symbol

RBS:

  • (untyped value, String path) -> Symbol

Parameters:

  • value (Object)
  • path (String)

Returns:

  • (Symbol)


77
# File 'sig/ibex/runtime/cst/validator.rbs', line 77

def self?.trivia_policy!: (untyped value, String path) -> Symbol

#self?.validateSerializedTree

RBS:

  • (String source, ?grammar_digest: String?, ?state_count: Integer?, ?production_count: Integer?) -> SerializedTree

Parameters:

  • source (String)
  • grammar_digest: (String, nil)
  • state_count: (Integer, nil)
  • production_count: (Integer, nil)

Returns:



31
# File 'sig/ibex/runtime/cst/validator.rbs', line 31

def self?.validate: (String source, ?grammar_digest: String?, ?state_count: Integer?, ?production_count: Integer?) -> SerializedTree

#self?.validate_derived!void

This method returns an undefined value.

RBS:

  • (GreenNode | GreenToken element, String path) -> void

Parameters:



56
# File 'sig/ibex/runtime/cst/validator.rbs', line 56

def self?.validate_derived!: (GreenNode | GreenToken element, String path) -> void

#self?.validate_kind!void

This method returns an undefined value.

RBS:

  • (Integer kind, Kind kinds, String path) -> void

Parameters:

  • kind (Integer)
  • kinds (Kind)
  • path (String)


59
# File 'sig/ibex/runtime/cst/validator.rbs', line 59

def self?.validate_kind!: (Integer kind, Kind kinds, String path) -> void

#self?.validate_kind_metadata!void

This method returns an undefined value.

RBS:

  • (Kind::metadata metadata) -> void

Parameters:

  • metadata (Kind::metadata)


92
# File 'sig/ibex/runtime/cst/validator.rbs', line 92

def self?.validate_kind_metadata!: (Kind::metadata metadata) -> void

#self?.validate_kind_range!void

This method returns an undefined value.

RBS:

  • (Array[Integer] range, Integer limit, String path) -> void

Parameters:

  • range (Array[Integer])
  • limit (Integer)
  • path (String)


95
# File 'sig/ibex/runtime/cst/validator.rbs', line 95

def self?.validate_kind_range!: (Array[Integer] range, Integer limit, String path) -> void

#self?.validate_memo_length!void

This method returns an undefined value.

RBS:

  • (Array[Integer?] states, GreenNode root) -> void

Parameters:



44
# File 'sig/ibex/runtime/cst/validator.rbs', line 44

def self?.validate_memo_length!: (Array[Integer?] states, GreenNode root) -> void

#self?.value!void

This method returns an undefined value.

RBS:

  • (Hash[String, untyped] value, String key, untyped expected, String path) -> void

Parameters:

  • value (Hash[String, untyped])
  • key (String)
  • expected (Object)
  • path (String)


101
# File 'sig/ibex/runtime/cst/validator.rbs', line 101

def self?.value!: (Hash[String, untyped] value, String key, untyped expected, String path) -> void