Module: Ibex::Runtime::CST::Serialize

Defined in:
lib/ibex/runtime/cst/serialize.rb

Overview

Stable JSON serialization for ibex_cst schema version 1.

Constant Summary collapse

SCHEMA_VERSION =

: Integer

1

Class Method Summary collapse

Class Method Details

.dump(value, grammar_digest: nil, table_format: nil, state_count: nil, production_count: nil, memo: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ibex/runtime/cst/serialize.rb', line 17

def dump(value, grammar_digest: nil, table_format: nil, state_count: nil, production_count: nil, memo: nil)
  tree = serialization_tree(
    value, grammar_digest: grammar_digest, table_format: table_format,
           state_count: state_count, production_count: production_count, memo: memo
  )
  document = {
    "ibex_ir" => "cst",
    "schema_version" => SCHEMA_VERSION,
    "grammar_digest" => tree.grammar_digest,
    "table_format" => tree.table_format,
    "state_count" => tree.state_count,
    "production_count" => tree.production_count,
    "trivia_policy" => tree.trivia_policy.to_s,
    "kinds" => kinds_document(tree.kinds),
    "root" => element_document(tree.green_root),
    "memo" => tree.memo&.to_h
  }
  "#{JSON.pretty_generate(document)}\n"
end

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



40
41
42
43
44
45
46
47
# File 'lib/ibex/runtime/cst/serialize.rb', line 40

def load(source, grammar_digest: nil, state_count: nil, production_count: nil)
  Validator.validate(
    source,
    grammar_digest: grammar_digest,
    state_count: state_count,
    production_count: production_count
  )
end