Module: Ibex::IR::Migration

Defined in:
lib/ibex/ir/migration.rb

Overview

Meaning-preserving upgrades between published IR schema versions.

Constant Summary collapse

UNAVAILABLE_V1_METADATA =
%w[
  source_provenance
  symbol_docs
  production_docs
  production_expansion
  action_composition
  grammar_tests
  lexer
  cst
  ast_nodes
].freeze

Class Method Summary collapse

Class Method Details

.build_migrated_grammar(grammar, symbols, productions) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ibex/ir/migration.rb', line 76

def build_migrated_grammar(grammar, symbols, productions)
  Grammar.new(
    class_name: grammar.class_name, superclass: grammar.superclass, start: grammar.start,
    mode: grammar.mode, starts: grammar.starts,
    expect: grammar.expect, expect_rr: grammar.expect_rr, options: grammar.options,
    parser_parameters: grammar.parser_parameters,
    value_printers: grammar.value_printers,
    grammar_tests: grammar.grammar_tests,
    recovery: grammar.recovery,
    symbols: symbols, productions: productions,
    user_code: grammar.user_code, user_code_chunks: grammar.user_code_chunks,
    conversions: grammar.conversions, warnings: grammar.warnings, schema_version: 2,
    migration: { from_schema_version: 1, unavailable: UNAVAILABLE_V1_METADATA }
  )
end

.migrate_action(action) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/ibex/ir/migration.rb', line 94

def migrate_action(action)
  return nil unless action

  Action.new(
    code: action.code, location: action.location, named_refs: action.named_refs,
    context_length: action.context_length
  )
end

.migrate_automaton(automaton) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/ibex/ir/migration.rb', line 105

def migrate_automaton(automaton)
  Automaton.new(
    grammar: migrate_grammar(automaton.grammar), states: automaton.states,
    conflict_summary: automaton.conflict_summary, algorithm: automaton.algorithm, schema_version: 2,
    entry_states: automaton.entry_states
  )
end

.migrate_grammar(grammar) ⇒ Object



44
45
46
47
48
# File 'lib/ibex/ir/migration.rb', line 44

def migrate_grammar(grammar)
  symbols = migrate_symbols(grammar)
  productions = migrate_productions(grammar)
  build_migrated_grammar(grammar, symbols, productions)
end

.migrate_productions(grammar) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/ibex/ir/migration.rb', line 64

def migrate_productions(grammar)
  grammar.productions.map do |production|
    Production.new(
      id: production.id, lhs: production.lhs, rhs: production.rhs,
      action: migrate_action(production.action), precedence_override: production.precedence_override,
      origin: production.origin
    )
  end
end

.migrate_symbols(grammar) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/ibex/ir/migration.rb', line 52

def migrate_symbols(grammar)
  grammar.symbols.map do |symbol|
    GrammarSymbol.new(
      id: symbol.id, name: symbol.name, kind: symbol.kind, reserved: symbol.reserved,
      precedence: symbol.precedence, location: symbol.location, display_name: symbol.display_name,
      semantic_type: symbol.semantic_type
    )
  end
end

.to_v2(value) ⇒ Object

Raises:



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ibex/ir/migration.rb', line 30

def to_v2(value)
  return value if value.schema_version == 2
  unless value.schema_version == 1
    raise Ibex::Error, "(ir):1:1: cannot migrate unsupported schema_version #{value.schema_version}"
  end

  return migrate_grammar(value) if value.is_a?(Grammar)
  return migrate_automaton(value) if value.is_a?(Automaton)

  raise Ibex::Error, "(ir):1:1: cannot migrate unsupported IR object #{value.class}"
end

.to_version(value, to: SCHEMA_VERSION) ⇒ Object

Raises:



20
21
22
23
24
25
26
# File 'lib/ibex/ir/migration.rb', line 20

def to_version(value, to: SCHEMA_VERSION)
  return value if value.schema_version == to
  return to_v2(value) if value.schema_version == 1 && to == 2

  raise Ibex::Error,
        "(ir):1:1: cannot migrate schema_version #{value.schema_version} to #{to}; only 1 to 2 is supported"
end