Class: Ibex::IR::Automaton
- Inherits:
-
Object
- Object
- Ibex::IR::Automaton
- Defined in:
- lib/ibex/ir/automaton_ir.rb
Overview
Immutable LALR automaton and its source grammar.
Instance Attribute Summary collapse
-
#algorithm ⇒ Object
readonly
: String.
-
#conflict_summary ⇒ Object
readonly
: conflict_summary.
-
#entry_states ⇒ Object
readonly
: Hash[String, Integer].
-
#grammar ⇒ Object
readonly
: Grammar.
-
#grammar_digest ⇒ Object
readonly
: String.
-
#schema_version ⇒ Object
readonly
: Integer.
-
#states ⇒ Object
readonly
: Array.
Instance Method Summary collapse
-
#initialize(grammar:, states:, conflict_summary:, algorithm: "lalr1", grammar_digest: nil, schema_version: SCHEMA_VERSION, entry_states: nil) ⇒ Automaton
constructor
A new instance of Automaton.
- #to_h ⇒ Object
Constructor Details
#initialize(grammar:, states:, conflict_summary:, algorithm: "lalr1", grammar_digest: nil, schema_version: SCHEMA_VERSION, entry_states: nil) ⇒ Automaton
Returns a new instance of Automaton.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ibex/ir/automaton_ir.rb', line 84 def initialize(grammar:, states:, conflict_summary:, algorithm: "lalr1", grammar_digest: nil, schema_version: SCHEMA_VERSION, entry_states: nil) unless SUPPORTED_SCHEMA_VERSIONS.include?(schema_version) raise Ibex::Error, "unsupported automaton schema_version #{schema_version.inspect}" end grammar = Migration.to_v2(grammar) if schema_version >= 2 && grammar.schema_version != schema_version raise Ibex::Error, "automaton migration did not produce a grammar" unless grammar.is_a?(Grammar) unless grammar.schema_version == schema_version raise Ibex::Error, "automaton schema_version #{schema_version} requires Grammar IR v#{schema_version}, " \ "got v#{grammar.schema_version}" end @algorithm = algorithm.freeze @grammar = grammar @grammar_digest = (grammar_digest || digest_for(grammar)).freeze @states = states.freeze @entry_states = IR.deep_freeze(entry_states || { grammar.start => 0 }) validate_entry_states @conflict_summary = IR.deep_freeze(conflict_summary) @schema_version = schema_version freeze end |
Instance Attribute Details
#algorithm ⇒ Object (readonly)
: String
73 74 75 |
# File 'lib/ibex/ir/automaton_ir.rb', line 73 def algorithm @algorithm end |
#conflict_summary ⇒ Object (readonly)
: conflict_summary
78 79 80 |
# File 'lib/ibex/ir/automaton_ir.rb', line 78 def conflict_summary @conflict_summary end |
#entry_states ⇒ Object (readonly)
: Hash[String, Integer]
77 78 79 |
# File 'lib/ibex/ir/automaton_ir.rb', line 77 def entry_states @entry_states end |
#grammar ⇒ Object (readonly)
: Grammar
75 76 77 |
# File 'lib/ibex/ir/automaton_ir.rb', line 75 def grammar @grammar end |
#grammar_digest ⇒ Object (readonly)
: String
74 75 76 |
# File 'lib/ibex/ir/automaton_ir.rb', line 74 def grammar_digest @grammar_digest end |
#schema_version ⇒ Object (readonly)
: Integer
79 80 81 |
# File 'lib/ibex/ir/automaton_ir.rb', line 79 def schema_version @schema_version end |
#states ⇒ Object (readonly)
: Array
76 77 78 |
# File 'lib/ibex/ir/automaton_ir.rb', line 76 def states @states end |
Instance Method Details
#to_h ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/ibex/ir/automaton_ir.rb', line 110 def to_h value = { ibex_ir: "automaton", schema_version: @schema_version, algorithm: @algorithm, grammar_digest: @grammar_digest, grammar: @grammar.to_h, states: @states.map { |state| state.to_h(@grammar) }, conflict_summary: @conflict_summary } #: Hash[Symbol, untyped] value[:entry_states] = @entry_states unless @entry_states == { @grammar.start => 0 } value end |