Module: Ibex::IR::Migration

Defined in:
lib/ibex/ir/migration.rb,
sig/ibex/ir/migration.rbs

Overview

Meaning-preserving upgrades between published IR schema versions.

Constant Summary collapse

UNAVAILABLE_V1_METADATA =

Returns:

  • (Array[String])
%w[
  source_provenance
  symbol_docs
  production_docs
  production_expansion
  action_composition
  grammar_tests
  lexer
  cst
  ast_nodes
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_migrated_grammar(grammar, symbols, productions) ⇒ Object

RBS:

  • (Grammar grammar, Array[GrammarSymbol] symbols, Array[Production] productions) -> Grammar



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

RBS:

  • (Action? action) -> Action?



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

RBS:

  • (Automaton automaton) -> Automaton



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

RBS:

  • (Grammar grammar) -> Grammar



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

RBS:

  • (Grammar grammar) -> Array[Production]



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

RBS:

  • (Grammar grammar) -> Array[GrammarSymbol]



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

RBS:

  • (Grammar | Automaton value) -> (Grammar | Automaton)

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

RBS:

  • (Grammar | Automaton value, ?to: Integer) -> (Grammar | Automaton)

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

Instance Method Details

#self?.build_migrated_grammarGrammar

RBS:

  • (Grammar grammar, Array[GrammarSymbol] symbols, Array[Production] productions) -> Grammar

Parameters:

Returns:



25
# File 'sig/ibex/ir/migration.rbs', line 25

def self?.build_migrated_grammar: (Grammar grammar, Array[GrammarSymbol] symbols, Array[Production] productions) -> Grammar

#self?.migrate_actionAction?

RBS:

  • (Action? action) -> Action?

Parameters:

Returns:



28
# File 'sig/ibex/ir/migration.rbs', line 28

def self?.migrate_action: (Action? action) -> Action?

#self?.migrate_automatonAutomaton

RBS:

  • (Automaton automaton) -> Automaton

Parameters:

Returns:



31
# File 'sig/ibex/ir/migration.rbs', line 31

def self?.migrate_automaton: (Automaton automaton) -> Automaton

#self?.migrate_grammarGrammar

RBS:

  • (Grammar grammar) -> Grammar

Parameters:

Returns:



16
# File 'sig/ibex/ir/migration.rbs', line 16

def self?.migrate_grammar: (Grammar grammar) -> Grammar

#self?.migrate_productionsArray[Production]

RBS:

  • (Grammar grammar) -> Array[Production]

Parameters:

Returns:



22
# File 'sig/ibex/ir/migration.rbs', line 22

def self?.migrate_productions: (Grammar grammar) -> Array[Production]

#self?.migrate_symbolsArray[GrammarSymbol]

RBS:

  • (Grammar grammar) -> Array[GrammarSymbol]

Parameters:

Returns:



19
# File 'sig/ibex/ir/migration.rbs', line 19

def self?.migrate_symbols: (Grammar grammar) -> Array[GrammarSymbol]

#self?.to_v2Grammar, Automaton

RBS:

  • (Grammar | Automaton value) -> (Grammar | Automaton)

Parameters:

Returns:



13
# File 'sig/ibex/ir/migration.rbs', line 13

def self?.to_v2: (Grammar | Automaton value) -> (Grammar | Automaton)

#self?.to_versionGrammar, Automaton

RBS:

  • (Grammar | Automaton value, ?to: Integer) -> (Grammar | Automaton)

Parameters:

Returns:



10
# File 'sig/ibex/ir/migration.rbs', line 10

def self?.to_version: (Grammar | Automaton value, ?to: Integer) -> (Grammar | Automaton)