Class: Ecu::LabelList::DcmParser

Inherits:
Object
  • Object
show all
Defined in:
lib/ecu/interfaces/dcm/dcm_parser.rb

Constant Summary collapse

ARY_PROPERTIES =
%i( xvalue yvalue value )
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

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ DcmParser

Returns a new instance of DcmParser.



25
26
27
28
29
30
31
32
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 25

def initialize(doc)
  @lexer      = DcmLexer.new(doc)
  @state      = :PRE_HEADER
  @labels     = []
  @headers    = []
  @subheaders = []
  reset_label!
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



24
25
26
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 24

def headers
  @headers
end

#labelsObject (readonly)

Returns the value of attribute labels.



24
25
26
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 24

def labels
  @labels
end

#lexerObject (readonly)

Returns the value of attribute lexer.



24
25
26
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 24

def lexer
  @lexer
end

#stateObject (readonly)

Returns the value of attribute state.



24
25
26
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 24

def state
  @state
end

#subheadersObject (readonly)

Returns the value of attribute subheaders.



24
25
26
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 24

def subheaders
  @subheaders
end

Instance Method Details

#add_dimension(n) ⇒ Object



174
175
176
177
178
179
180
181
182
183
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 174

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



145
146
147
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 145

def add_header(str)
  @headers << str[1..].strip
end

#add_name(str) ⇒ Object



168
169
170
171
172
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 168

def add_name(str)
  fail "Duplicate name" if @properties.key?(:name)

  @properties[:name] = str
end

#add_subheader(str) ⇒ Object



149
150
151
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 149

def add_subheader(str)
  @subheaders << str[1..].strip
end

#append_property(value) ⇒ Object



193
194
195
196
197
198
199
200
201
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 193

def append_property(value)
  if @is_ary
    @value << value
  else
    fail "Multiple definitions for #{@key}" if @value

    @value = value
  end
end

#callObject



34
35
36
37
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
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 34

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.token_value[1..-2])
        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.message} on #{e.backtrace.first} (state: #{@state})", lexer)
    end
  end
  [labels, headers, subheaders]
end

#debug_token(tok) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 131

def debug_token(tok)
  if tok == :UNKNOWN_CHAR
    "UNKNOWN_CHAR: #{lexer.token_value}"
  else
    tok
  end
end

#finish_labelObject



159
160
161
162
163
164
165
166
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 159

def finish_label
  next_state(:POST_HEADER) do
    @properties
      .except(:displayname)
      .then { @constructor.new(**_1) }
      .tap { reset_label! }
  end
end

#finish_propertyObject



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 203

def finish_property
  next_state(:LABEL_CONTENT) do
    if @properties.key?(@key)
      if @is_ary
        @properties[@key].concat(@value)
      else
        fail "Multiple property lines not allowed for #{@key}"
      end
    else
      @properties[@key] = @value
    end
  end
end

#next_state(newstate) ⇒ Object



139
140
141
142
143
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 139

def next_state(newstate)
  yield if block_given?
ensure
  @state = newstate
end

#reset_label!Object



217
218
219
220
221
222
223
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 217

def reset_label!
  @constructor = nil
  @properties  = {}
  @key         = nil
  @is_ary      = false
  @value       = nil
end

#start_label(constructor) ⇒ Object



153
154
155
156
157
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 153

def start_label(constructor)
  next_state(:LABEL_HEADLINE) do
    @constructor = constructor
  end
end

#start_property(key, newstate) ⇒ Object



185
186
187
188
189
190
191
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 185

def start_property(key, newstate)
  next_state(newstate) do
    @key    = KEY_MAPPING[key]
    @is_ary = ARY_PROPERTIES.include?(@key)
    @value  = @is_ary ? [] : nil
  end
end