Module: Flexr::Generated
- Defined in:
- lib/flexr/generated.rb
Constant Summary collapse
- ARTIFACT_SCHEMA_VERSION =
1- RUNTIME_ABI_VERSION =
1
Class Method Summary collapse
- .artifact_metadata ⇒ Object
- .decode_array(encoded, nil_value: nil) ⇒ Object
- .decode_packed(packed) ⇒ Object
- .incompatible_artifact!(detail) ⇒ Object
- .install!(klass, payload) ⇒ Object
- .install_compiled!(klass, payload) ⇒ Object
- .validate_artifact!(artifact) ⇒ Object
Class Method Details
.artifact_metadata ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/flexr/generated.rb', line 10 def { schema_version: ARTIFACT_SCHEMA_VERSION, compiler_version: VERSION, runtime_abi_version: RUNTIME_ABI_VERSION, unicode_version: Unicode::VERSION } end |
.decode_array(encoded, nil_value: nil) ⇒ Object
122 123 124 125 126 127 |
# File 'lib/flexr/generated.rb', line 122 def decode_array(encoded, nil_value: nil) values = encoded.unpack1("m0").unpack("l<*") return values unless nil_value values.map { |value| value == nil_value ? nil : value } end |
.decode_packed(packed) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/flexr/generated.rb', line 110 def decode_packed(packed) return packed unless packed.is_a?(Hash) && packed[:encoding]&.to_sym == :base64 { base: decode_array(packed.fetch(:base)), default: decode_array(packed.fetch(:default), nil_value: -1), next: decode_array(packed.fetch(:next), nil_value: -1), check: decode_array(packed.fetch(:check), nil_value: -1), fallback: packed.key?(:fallback) ? decode_array(packed.fetch(:fallback), nil_value: -1) : nil } end |
.incompatible_artifact!(detail) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/flexr/generated.rb', line 102 def incompatible_artifact!(detail) diagnostic = Diagnostics.error( "FLEXR-E020", "generated artifact is incompatible with this runtime", help: "regenerate the lexer with the installed flexr version", note: detail ) raise CompileError.new(diagnostic., diagnostic: diagnostic) end |
.install!(klass, payload) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/flexr/generated.rb', line 19 def install!(klass, payload) rules = payload.fetch(:rules) config = payload klass.__flexr_reset! klass.backend(config.fetch(:backend, :table)) klass.token_kind(config.fetch(:token_kind, :array)) klass.encoding(config.fetch(:encoding, Encoding::UTF_8)) Array(config[:declared_tokens]).each { |token| klass.emits(token) } config.fetch(:options, {}).each do |option, value| if option == :accel klass.accel(value) elsif value == true klass.option(option) else klass.__flexr_config.[option] = value end end Array(config[:states]).each do |state| klass.state(state, inclusive: config.fetch(:inclusive_states, {}).fetch(state.to_sym, false)) { nil } end rules.each do |definition| klass.__flexr_add_generated_rule(definition) end config.fetch(:eof_rules, {}).each do |state, action| klass.__flexr_add_generated_eof(state, action) end klass end |
.install_compiled!(klass, payload) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/flexr/generated.rb', line 48 def install_compiled!(klass, payload) validate_artifact!(payload[:artifact]) install!(klass, payload) machines = payload.fetch(:compiled).fetch(:machines).transform_values do |machine| dfa_data = machine.fetch(:dfa) packed = decode_packed(dfa_data[:packed]) dfa = Automaton::DFA.new( transitions: dfa_data[:transitions], accepts: dfa_data.fetch(:accepts), ec: dfa_data.fetch(:ec), class_count: dfa_data.fetch(:class_count), start: dfa_data.fetch(:start), rule_ids: dfa_data.fetch(:rule_ids), packed: packed, direct: dfa_data[:direct], state_count: dfa_data.fetch(:state_count) ) Automaton::Machine.new(dfa: dfa, state_name: machine.fetch(:state_name).to_sym) end compiled = Automaton::CompiledSpec.new( machines: machines, rules: klass.__flexr_rules, states: payload.fetch(:compiled).fetch(:states).map(&:to_sym), stats: payload.fetch(:compiled).fetch(:stats), diagnostics: payload.fetch(:compiled).fetch(:diagnostics, []).map do |diagnostic| Diagnostic.new(**diagnostic.transform_keys(&:to_sym)) end ) klass.__flexr_set_compiled!(compiled) klass.__flexr_mark_generated! klass end |
.validate_artifact!(artifact) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/flexr/generated.rb', line 81 def validate_artifact!(artifact) return incompatible_artifact!("artifact metadata is missing") unless artifact.is_a?(Hash) expected = { schema_version: ARTIFACT_SCHEMA_VERSION, runtime_abi_version: RUNTIME_ABI_VERSION, unicode_version: Unicode::VERSION } mismatch = expected.find { |name, value| artifact[name] != value } compiler_version = artifact[:compiler_version] return if mismatch.nil? && compiler_version.is_a?(String) && !compiler_version.empty? detail = if mismatch name, value = mismatch "#{name}=#{artifact[name].inspect} (expected #{value.inspect})" else "compiler_version=#{compiler_version.inspect}" end incompatible_artifact!(detail) end |