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

Defined in:
lib/json5/generated_parser.rb

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.

%w[
  ibex_ir schema_version grammar_digest table_format state_count production_count trivia_policy kinds root memo
].freeze
KIND_KEYS =

Signature:

  • Array[String]

%w[
  names terminal_range nonterminal_range named named_nonterminals trivia synthetic
].freeze
KNOWN_FLAGS =

Signature:

  • Array[String]

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

Class 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



2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
# File 'lib/json5/generated_parser.rb', line 2198

def validate(source, grammar_digest: nil, state_count: nil, production_count: nil) # rubocop:disable Metrics/MethodLength
  document = JSON.parse(source) #: json_value
  document = 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