Class: Ecu::LabelList::DcmParser
- Inherits:
-
Object
- Object
- Ecu::LabelList::DcmParser
- Defined in:
- lib/ecu/interfaces/dcm/dcm_parser.rb
Constant Summary collapse
- ARY_PROPERTIES =
Set.new([:xvalue, :yvalue, :value]).freeze
- KEY_MAPPING =
{ "ST/X": :xvalue, "ST/Y": :yvalue, "WERT": :value, "ST_TX/X": :xvalue, "ST_TX/Y": :yvalue, "TEXT": :value, "FUNKTION": :function, "DISPLAYNAME": :displayname, "EINHEIT_X": :xunit, "EINHEIT_Y": :yunit, "EINHEIT_W": :unit, "LANGNAME": :description, }
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#labels ⇒ Object
readonly
Returns the value of attribute labels.
-
#lexer ⇒ Object
readonly
Returns the value of attribute lexer.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#subheaders ⇒ Object
readonly
Returns the value of attribute subheaders.
Instance Method Summary collapse
- #add_dimension(n) ⇒ Object
- #add_header(str) ⇒ Object
- #add_name(str) ⇒ Object
- #add_subheader(str) ⇒ Object
- #append_property(value) ⇒ Object
- #call ⇒ Object
- #debug_token(tok) ⇒ Object
- #finish_label ⇒ Object
- #finish_property ⇒ Object
-
#initialize(doc) ⇒ DcmParser
constructor
A new instance of DcmParser.
- #next_state(newstate) ⇒ Object
- #reset_label! ⇒ Object
- #start_label(constructor) ⇒ Object
- #start_property(key, newstate) ⇒ Object
Constructor Details
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
27 28 29 |
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 27 def headers @headers end |
#labels ⇒ Object (readonly)
Returns the value of attribute labels.
27 28 29 |
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 27 def labels @labels end |
#lexer ⇒ Object (readonly)
Returns the value of attribute lexer.
27 28 29 |
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 27 def lexer @lexer end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
27 28 29 |
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 27 def state @state end |
#subheaders ⇒ Object (readonly)
Returns the value of attribute subheaders.
27 28 29 |
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 27 def subheaders @subheaders end |
Instance Method Details
#add_dimension(n) ⇒ Object
176 177 178 179 180 181 182 183 184 185 |
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 176 def add_dimension(n) fail "Wrong order name/dimensions" unless @properties.key?(:name) fail "Too many dimensions" if @properties.key?(:xdim) && @properties.key?(:ydim) if @properties.key?(:xdim) @properties[:ydim] = n else @properties[:xdim] = n end end |
#add_header(str) ⇒ Object
149 150 151 |
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 149 def add_header(str) @headers << str[1..].strip end |
#add_name(str) ⇒ Object
170 171 172 173 174 |
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 170 def add_name(str) fail "Duplicate name" if @properties.key?(:name) @properties[:name] = str end |
#add_subheader(str) ⇒ Object
153 154 155 |
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 153 def add_subheader(str) @subheaders << str[1..].strip end |
#append_property(value) ⇒ Object
200 201 202 203 204 205 206 207 208 |
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 200 def append_property(value) if @is_ary @value << value else fail "Multiple definitions for #{@key}" if @value @value = value end end |
#call ⇒ Object
38 39 40 41 42 43 44 45 46 47 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 38 def call while tok = lexer.next_token begin case @state when :PRE_HEADER case tok when :HEADER then next_state(:POST_HEADER) when :COMMENT then add_header(lexer.token_value) when :NEWLINE then # noop else raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{@state})", lexer) end when :POST_HEADER case tok when :FUNKTIONEN then next_state(:FUNCTIONS_CONTENT) when :FESTWERT then start_label(Festwert) when :FESTWERTEBLOCK then start_label(Festwerteblock) when :KENNLINIE then start_label(Kennlinie) when :GRUPPENKENNLINIE then start_label(Gruppenkennlinie) when :FESTKENNLINIE then start_label(Festkennlinie) when :KENNFELD then start_label(Kennfeld) when :GRUPPENKENNFELD then start_label(Gruppenkennfeld) when :FESTKENNFELD then start_label(Festkennfeld) when :STUETZSTELLENVERTEILUNG then start_label(Stuetzstellenverteilung) when :COMMENT then add_subheader(lexer.token_value) when :NEWLINE then # noop else raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{@state})", lexer) end when :FUNCTIONS_CONTENT case tok when :FKT then next_state(:FUNCTION_CONTENT) when :NEWLINE then # noop when :END then next_state(:POST_HEADER) else raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{@state})", lexer) end when :FUNCTION_CONTENT case tok when :IDENTIFIER then # append that shit? when :QUOTED_TEXT then # append that shit as well? when :NEWLINE then next_state(:FUNCTIONS_CONTENT) else raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{@state})", lexer) end when :LABEL_HEADLINE case tok when :IDENTIFIER then add_name(lexer.token_value) when :DIMENSIONS_SEP then # noop, order handled by #add_dimension when :INT then add_dimension(lexer.token_value.to_i) when :NEWLINE then next_state(:LABEL_CONTENT) else raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{@state})", lexer) end when :LABEL_CONTENT case tok when :WERT then start_property(:WERT, :NUMERIC_PROPERTY) when :TEXT then start_property(:TEXT, :TEXT_PROPERTY) when :EINHEIT_X then start_property(:EINHEIT_X, :TEXT_PROPERTY) when :EINHEIT_Y then start_property(:EINHEIT_Y, :TEXT_PROPERTY) when :EINHEIT_W then start_property(:EINHEIT_W, :TEXT_PROPERTY) when :LANGNAME then start_property(:LANGNAME, :TEXT_PROPERTY) when :FUNKTION then start_property(:FUNKTION, :ID_PROPERTY) when :DISPLAYNAME then start_property(:DISPLAYNAME, :ID_PROPERTY) when :END then labels << finish_label when :"ST/X" then start_property(:"ST/X", :NUMERIC_PROPERTY) when :"ST/Y" then start_property(:"ST/Y", :NUMERIC_PROPERTY) when :"ST_TX/X" then start_property(:"ST_TX/X", :TEXT_PROPERTY) when :"ST_TX/Y" then start_property(:"ST_TX/Y", :TEXT_PROPERTY) when :NEWLINE then # noop, DCM badly formatted when :COMMENT then # noop, some programs add SST in comments else raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{@state})", lexer) end when :NUMERIC_PROPERTY case tok when :FLOAT then append_property(lexer.token_value.to_f) when :INT then append_property(lexer.token_value.to_i) when :NEWLINE then finish_property else raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{@state})", lexer) end when :TEXT_PROPERTY case tok when :QUOTED_TEXT then append_property(lexer.quoted_value) when :NEWLINE then finish_property else raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{@state})", lexer) end when :ID_PROPERTY case tok when :IDENTIFIER then append_property(lexer.token_value) when :NEWLINE then finish_property else raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{@state})", lexer) end end rescue DcmParserError raise rescue StandardError => e raise DcmParserError.new("#{e.} on #{e.backtrace.first} (state: #{@state})", lexer) end end [labels, headers, subheaders] end |
#debug_token(tok) ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 135 def debug_token(tok) if tok == :UNKNOWN_CHAR "UNKNOWN_CHAR: #{lexer.token_value}" else tok end end |
#finish_label ⇒ Object
163 164 165 166 167 168 |
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 163 def finish_label next_state(:POST_HEADER) do @properties.delete(:displayname) @constructor.new(**@properties).tap { reset_label! } end end |
#finish_property ⇒ Object
210 211 212 213 214 215 216 217 |
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 210 def finish_property next_state(:LABEL_CONTENT) do next if @is_ary # values already in @properties[@key] via @value alias fail "Multiple property lines not allowed for #{@key}" if @properties.key?(@key) @properties[@key] = @value end end |
#next_state(newstate) ⇒ Object
143 144 145 146 147 |
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 143 def next_state(newstate) yield if block_given? ensure @state = newstate end |
#reset_label! ⇒ Object
219 220 221 222 223 224 225 |
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 219 def reset_label! @constructor = nil @properties.clear @key = nil @is_ary = false @value = nil end |
#start_label(constructor) ⇒ Object
157 158 159 160 161 |
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 157 def start_label(constructor) next_state(:LABEL_HEADLINE) do @constructor = constructor end end |
#start_property(key, newstate) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 187 def start_property(key, newstate) next_state(newstate) do @key = KEY_MAPPING[key] @is_ary = ARY_PROPERTIES.include?(@key) if @is_ary @properties[@key] ||= [] @value = @properties[@key] # alias: append_property writes directly to target else @value = nil end end end |