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



165
166
167
168
169
170
171
172
173
174
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 165

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



136
137
138
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 136

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

#add_name(str) ⇒ Object



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

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

  @properties[:name] = str
end

#add_subheader(str) ⇒ Object



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

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

#append_property(value) ⇒ Object



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

def append_property(value)
  # TODO: Check if constructor allows array values
  if ARY_PROPERTIES.include?(@key)
    @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
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 34

def call
  while tok = lexer.next_token
    begin
      case @state
      in :PRE_HEADER
        case tok
        in :HEADER  then next_state(:POST_HEADER)
        in :COMMENT then add_header(lexer.token_value)
        in :NEWLINE then # noop
        end
      in :POST_HEADER
        case tok
        in :FUNKTIONEN              then next_state(:FUNCTIONS_CONTENT)
        in :FESTWERT                then start_label(Festwert)
        in :FESTWERTEBLOCK          then start_label(Festwerteblock)
        in :KENNLINIE               then start_label(Kennlinie)
        in :GRUPPENKENNLINIE        then start_label(Gruppenkennlinie)
        in :FESTKENNLINIE           then start_label(Festkennlinie)
        in :KENNFELD                then start_label(Kennfeld)
        in :GRUPPENKENNFELD         then start_label(Gruppenkennfeld)
        in :FESTKENNFELD            then start_label(Festkennfeld)
        in :STUETZSTELLENVERTEILUNG then start_label(Stuetzstellenverteilung)
        in :COMMENT                 then add_subheader(lexer.token_value)
        in :NEWLINE                 then # noop
        end
      in :FUNCTIONS_CONTENT
        case tok
        in :FKT     then next_state(:FUNCTION_CONTENT)
        in :NEWLINE then # noop
        in :END     then next_state(:POST_HEADER)
        end
      in :FUNCTION_CONTENT
        case tok
        in :IDENTIFIER  then # append that shit?
        in :QUOTED_TEXT then # append that shit as well?
        in :NEWLINE     then next_state(:FUNCTIONS_CONTENT)
        end
      in :LABEL_HEADLINE
        case tok
        in :IDENTIFIER     then add_name(lexer.token_value)
        in :DIMENSIONS_SEP then # noop, order handled by #add_dimension
        in :INT            then add_dimension(lexer.token_value.to_i)
        in :NEWLINE        then next_state(:LABEL_CONTENT)
        end
      in :LABEL_CONTENT
        case tok
        in :WERT        then start_property(:WERT,        :NUMERIC_PROPERTY)
        in :TEXT        then start_property(:TEXT,        :TEXT_PROPERTY)
        in :EINHEIT_X   then start_property(:EINHEIT_X,   :TEXT_PROPERTY)
        in :EINHEIT_Y   then start_property(:EINHEIT_Y,   :TEXT_PROPERTY)
        in :EINHEIT_W   then start_property(:EINHEIT_W,   :TEXT_PROPERTY)
        in :LANGNAME    then start_property(:LANGNAME,    :TEXT_PROPERTY)
        in :FUNKTION    then start_property(:FUNKTION,    :ID_PROPERTY)
        in :DISPLAYNAME then start_property(:DISPLAYNAME, :ID_PROPERTY)
        in :END         then labels << finish_label
        in :"ST/X"      then start_property(:"ST/X",      :NUMERIC_PROPERTY)
        in :"ST/Y"      then start_property(:"ST/Y",      :NUMERIC_PROPERTY)
        in :"ST_TX/X"   then start_property(:"ST_TX/X",   :TEXT_PROPERTY)
        in :"ST_TX/Y"   then start_property(:"ST_TX/Y",   :TEXT_PROPERTY)
        in :NEWLINE     then # noop, DCM badly formatted
        in :COMMENT     then # noop, some programs add SST in comments
        end
      in :NUMERIC_PROPERTY
        case tok
        in :FLOAT       then append_property(lexer.token_value.to_f)
        in :INT         then append_property(lexer.token_value.to_i)
        in :NEWLINE     then finish_property
        end
      in :TEXT_PROPERTY
        case tok
        in :QUOTED_TEXT then append_property(lexer.token_value[1..-2])
        in :NEWLINE     then finish_property
        end
      in :ID_PROPERTY
        case tok
        in :IDENTIFIER  then append_property(lexer.token_value)
        in :NEWLINE     then finish_property
        end
      end
    rescue NoMatchingPatternError => e
      raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{state})", lexer)
    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



122
123
124
125
126
127
128
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 122

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

#finish_labelObject



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

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

#finish_propertyObject



194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 194

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

#next_state(newstate) ⇒ Object



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

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

#reset_label!Object



208
209
210
211
212
213
# File 'lib/ecu/interfaces/dcm/dcm_parser.rb', line 208

def reset_label!
  @constructor = nil
  @properties  = Hash.new
  @key         = nil
  @value       = nil
end

#start_label(constructor) ⇒ Object



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

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

#start_property(key, newstate) ⇒ Object



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

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