automotive-ecu 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 432a653cdc31d77adf510d554a8cf9d7b976ee94e951db7fa91b22b20341f82d
4
- data.tar.gz: d49339461986e88fc585f419c06c4a8dce1646db68354088017425938acc4cdb
3
+ metadata.gz: 4ad47ed36e2d54205a5c804187b85e2f2a7b91d73f2fc1bc548f691a87804c88
4
+ data.tar.gz: 2ab14530309248995b479e9dc4010bf61a83f767efd62ddf9b739686791aaae5
5
5
  SHA512:
6
- metadata.gz: fa977d44721a3b9b996b8c09b7cf377f27a5df0c7727b295153036c99c68bef0f882731cf24393d3e2b0f902b50ad915826486549621014aaf8643ecb15e7c42
7
- data.tar.gz: fd88ff7792476744281c71cfef1e0006fd58435598619946b6d1650dd8825c2460ffe9b33fec39ccfaaf5ce28b3df05ef0c975d1860d703285d5c4c653894c6d
6
+ metadata.gz: de3516033d423a95b0c813f3e22eed22a0195fbbf5c4d7b2dc6f3604d897d59bd4b7bc46f0b527651fa5f3c1ffecec4cc3642e3f1482033b0104154088abdb44
7
+ data.tar.gz: ebaf253e55c358547cb63ad4024a8bf0e90c6bb7a529b5ab30a9117593436aacd15d546bd226a5cf7a02156edfe2193c94e67f51e59fc9545c6b9688cd35b45b
@@ -48,25 +48,27 @@ class Ecu
48
48
 
49
49
  return if @scan.eos?
50
50
 
51
- case
52
- when @scan.skip(NEWLINE) then :NEWLINE
53
- when s = @scan.scan(KW_RE) then KW_TABLE[s]
54
- when @scan.skip(QUOTED_TEXT) then :QUOTED_TEXT
55
- when @scan.skip(COMMENT) then :COMMENT
56
- when @scan.skip(DIMENSIONS_SEP) then :DIMENSIONS_SEP
57
- when @scan.skip(HEADER) then :HEADER
58
- when @scan.skip(IDENTIFIER) then :IDENTIFIER
59
- when @scan.skip(FLOAT) then :FLOAT
60
- when @scan.skip(INT) then :INT
61
-
51
+ case @doc.getbyte(@scan.pos)
52
+ when 10, 13 then @scan.skip(NEWLINE) && :NEWLINE # \n \r
53
+ when 34 then @scan.skip(QUOTED_TEXT) && :QUOTED_TEXT # "
54
+ when 42 then @scan.skip(COMMENT) && :COMMENT # *
55
+ when 64 then @scan.skip(DIMENSIONS_SEP) && :DIMENSIONS_SEP # @
56
+ when 43, 45, 46, 48..57 # + - . 0-9
57
+ if @scan.skip(FLOAT) then :FLOAT
58
+ elsif @scan.skip(INT) then :INT
59
+ else @scan.getch; :UNKNOWN_CHAR
60
+ end
62
61
  else
63
- @scan.getch
64
- :UNKNOWN_CHAR
62
+ if s = @scan.scan(KW_RE) then KW_TABLE[s]
63
+ elsif @scan.skip(HEADER) then :HEADER
64
+ elsif @scan.skip(IDENTIFIER) then :IDENTIFIER
65
+ else @scan.getch; :UNKNOWN_CHAR
66
+ end
65
67
  end
66
68
  end
67
69
 
68
70
  def token_value
69
- @doc.byteslice(@scan.pos - @scan.matched_size, @scan.matched_size)
71
+ @scan.matched
70
72
  end
71
73
 
72
74
  def lineno
@@ -35,83 +35,92 @@ class Ecu
35
35
  while tok = lexer.next_token
36
36
  begin
37
37
  case @state
38
- in :PRE_HEADER
38
+ when :PRE_HEADER
39
39
  case tok
40
- in :HEADER then next_state(:POST_HEADER)
41
- in :COMMENT then add_header(lexer.token_value)
42
- in :NEWLINE then # noop
40
+ when :HEADER then next_state(:POST_HEADER)
41
+ when :COMMENT then add_header(lexer.token_value)
42
+ when :NEWLINE then # noop
43
+ else raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{@state})", lexer)
43
44
  end
44
- in :POST_HEADER
45
+ when :POST_HEADER
45
46
  case tok
46
- in :FUNKTIONEN then next_state(:FUNCTIONS_CONTENT)
47
- in :FESTWERT then start_label(Festwert)
48
- in :FESTWERTEBLOCK then start_label(Festwerteblock)
49
- in :KENNLINIE then start_label(Kennlinie)
50
- in :GRUPPENKENNLINIE then start_label(Gruppenkennlinie)
51
- in :FESTKENNLINIE then start_label(Festkennlinie)
52
- in :KENNFELD then start_label(Kennfeld)
53
- in :GRUPPENKENNFELD then start_label(Gruppenkennfeld)
54
- in :FESTKENNFELD then start_label(Festkennfeld)
55
- in :STUETZSTELLENVERTEILUNG then start_label(Stuetzstellenverteilung)
56
- in :COMMENT then add_subheader(lexer.token_value)
57
- in :NEWLINE then # noop
47
+ when :FUNKTIONEN then next_state(:FUNCTIONS_CONTENT)
48
+ when :FESTWERT then start_label(Festwert)
49
+ when :FESTWERTEBLOCK then start_label(Festwerteblock)
50
+ when :KENNLINIE then start_label(Kennlinie)
51
+ when :GRUPPENKENNLINIE then start_label(Gruppenkennlinie)
52
+ when :FESTKENNLINIE then start_label(Festkennlinie)
53
+ when :KENNFELD then start_label(Kennfeld)
54
+ when :GRUPPENKENNFELD then start_label(Gruppenkennfeld)
55
+ when :FESTKENNFELD then start_label(Festkennfeld)
56
+ when :STUETZSTELLENVERTEILUNG then start_label(Stuetzstellenverteilung)
57
+ when :COMMENT then add_subheader(lexer.token_value)
58
+ when :NEWLINE then # noop
59
+ else raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{@state})", lexer)
58
60
  end
59
- in :FUNCTIONS_CONTENT
61
+ when :FUNCTIONS_CONTENT
60
62
  case tok
61
- in :FKT then next_state(:FUNCTION_CONTENT)
62
- in :NEWLINE then # noop
63
- in :END then next_state(:POST_HEADER)
63
+ when :FKT then next_state(:FUNCTION_CONTENT)
64
+ when :NEWLINE then # noop
65
+ when :END then next_state(:POST_HEADER)
66
+ else raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{@state})", lexer)
64
67
  end
65
- in :FUNCTION_CONTENT
68
+ when :FUNCTION_CONTENT
66
69
  case tok
67
- in :IDENTIFIER then # append that shit?
68
- in :QUOTED_TEXT then # append that shit as well?
69
- in :NEWLINE then next_state(:FUNCTIONS_CONTENT)
70
+ when :IDENTIFIER then # append that shit?
71
+ when :QUOTED_TEXT then # append that shit as well?
72
+ when :NEWLINE then next_state(:FUNCTIONS_CONTENT)
73
+ else raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{@state})", lexer)
70
74
  end
71
- in :LABEL_HEADLINE
75
+ when :LABEL_HEADLINE
72
76
  case tok
73
- in :IDENTIFIER then add_name(lexer.token_value)
74
- in :DIMENSIONS_SEP then # noop, order handled by #add_dimension
75
- in :INT then add_dimension(lexer.token_value.to_i)
76
- in :NEWLINE then next_state(:LABEL_CONTENT)
77
+ when :IDENTIFIER then add_name(lexer.token_value)
78
+ when :DIMENSIONS_SEP then # noop, order handled by #add_dimension
79
+ when :INT then add_dimension(lexer.token_value.to_i)
80
+ when :NEWLINE then next_state(:LABEL_CONTENT)
81
+ else raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{@state})", lexer)
77
82
  end
78
- in :LABEL_CONTENT
83
+ when :LABEL_CONTENT
79
84
  case tok
80
- in :WERT then start_property(:WERT, :NUMERIC_PROPERTY)
81
- in :TEXT then start_property(:TEXT, :TEXT_PROPERTY)
82
- in :EINHEIT_X then start_property(:EINHEIT_X, :TEXT_PROPERTY)
83
- in :EINHEIT_Y then start_property(:EINHEIT_Y, :TEXT_PROPERTY)
84
- in :EINHEIT_W then start_property(:EINHEIT_W, :TEXT_PROPERTY)
85
- in :LANGNAME then start_property(:LANGNAME, :TEXT_PROPERTY)
86
- in :FUNKTION then start_property(:FUNKTION, :ID_PROPERTY)
87
- in :DISPLAYNAME then start_property(:DISPLAYNAME, :ID_PROPERTY)
88
- in :END then labels << finish_label
89
- in :"ST/X" then start_property(:"ST/X", :NUMERIC_PROPERTY)
90
- in :"ST/Y" then start_property(:"ST/Y", :NUMERIC_PROPERTY)
91
- in :"ST_TX/X" then start_property(:"ST_TX/X", :TEXT_PROPERTY)
92
- in :"ST_TX/Y" then start_property(:"ST_TX/Y", :TEXT_PROPERTY)
93
- in :NEWLINE then # noop, DCM badly formatted
94
- in :COMMENT then # noop, some programs add SST in comments
85
+ when :WERT then start_property(:WERT, :NUMERIC_PROPERTY)
86
+ when :TEXT then start_property(:TEXT, :TEXT_PROPERTY)
87
+ when :EINHEIT_X then start_property(:EINHEIT_X, :TEXT_PROPERTY)
88
+ when :EINHEIT_Y then start_property(:EINHEIT_Y, :TEXT_PROPERTY)
89
+ when :EINHEIT_W then start_property(:EINHEIT_W, :TEXT_PROPERTY)
90
+ when :LANGNAME then start_property(:LANGNAME, :TEXT_PROPERTY)
91
+ when :FUNKTION then start_property(:FUNKTION, :ID_PROPERTY)
92
+ when :DISPLAYNAME then start_property(:DISPLAYNAME, :ID_PROPERTY)
93
+ when :END then labels << finish_label
94
+ when :"ST/X" then start_property(:"ST/X", :NUMERIC_PROPERTY)
95
+ when :"ST/Y" then start_property(:"ST/Y", :NUMERIC_PROPERTY)
96
+ when :"ST_TX/X" then start_property(:"ST_TX/X", :TEXT_PROPERTY)
97
+ when :"ST_TX/Y" then start_property(:"ST_TX/Y", :TEXT_PROPERTY)
98
+ when :NEWLINE then # noop, DCM badly formatted
99
+ when :COMMENT then # noop, some programs add SST in comments
100
+ else raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{@state})", lexer)
95
101
  end
96
- in :NUMERIC_PROPERTY
102
+ when :NUMERIC_PROPERTY
97
103
  case tok
98
- in :FLOAT then append_property(lexer.token_value.to_f)
99
- in :INT then append_property(lexer.token_value.to_i)
100
- in :NEWLINE then finish_property
104
+ when :FLOAT then append_property(lexer.token_value.to_f)
105
+ when :INT then append_property(lexer.token_value.to_i)
106
+ when :NEWLINE then finish_property
107
+ else raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{@state})", lexer)
101
108
  end
102
- in :TEXT_PROPERTY
109
+ when :TEXT_PROPERTY
103
110
  case tok
104
- in :QUOTED_TEXT then append_property(lexer.token_value[1..-2])
105
- in :NEWLINE then finish_property
111
+ when :QUOTED_TEXT then append_property(lexer.token_value[1..-2])
112
+ when :NEWLINE then finish_property
113
+ else raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{@state})", lexer)
106
114
  end
107
- in :ID_PROPERTY
115
+ when :ID_PROPERTY
108
116
  case tok
109
- in :IDENTIFIER then append_property(lexer.token_value)
110
- in :NEWLINE then finish_property
117
+ when :IDENTIFIER then append_property(lexer.token_value)
118
+ when :NEWLINE then finish_property
119
+ else raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{@state})", lexer)
111
120
  end
112
121
  end
113
- rescue NoMatchingPatternError => e
114
- raise DcmParserError.new("Unexpected token #{debug_token(tok)} (state: #{state})", lexer)
122
+ rescue DcmParserError
123
+ raise
115
124
  rescue StandardError => e
116
125
  raise DcmParserError.new("#{e.message} on #{e.backtrace.first} (state: #{@state})", lexer)
117
126
  end
@@ -149,10 +158,8 @@ class Ecu
149
158
 
150
159
  def finish_label
151
160
  next_state(:POST_HEADER) do
152
- @properties
153
- .except(:displayname)
154
- .then { @constructor.new(**_1) }
155
- .tap { reset_label! }
161
+ @properties.delete(:displayname)
162
+ @constructor.new(**@properties).tap { reset_label! }
156
163
  end
157
164
  end
158
165
 
@@ -175,14 +182,19 @@ class Ecu
175
182
 
176
183
  def start_property(key, newstate)
177
184
  next_state(newstate) do
178
- @key = KEY_MAPPING[key]
179
- @value = ARY_PROPERTIES.include?(@key) ? [] : nil
185
+ @key = KEY_MAPPING[key]
186
+ @is_ary = ARY_PROPERTIES.include?(@key)
187
+ if @is_ary
188
+ @properties[@key] ||= []
189
+ @value = @properties[@key] # alias: append_property writes directly to target
190
+ else
191
+ @value = nil
192
+ end
180
193
  end
181
194
  end
182
195
 
183
196
  def append_property(value)
184
- # TODO: Check if constructor allows array values
185
- if ARY_PROPERTIES.include?(@key)
197
+ if @is_ary
186
198
  @value << value
187
199
  else
188
200
  fail "Multiple definitions for #{@key}" if @value
@@ -193,22 +205,18 @@ class Ecu
193
205
 
194
206
  def finish_property
195
207
  next_state(:LABEL_CONTENT) do
196
- if @properties.key?(@key)
197
- if ARY_PROPERTIES.include?(@key)
198
- @properties[@key] += @value
199
- else
200
- fail "Multiple property lines not allowed for #{key}"
201
- end
202
- else
203
- @properties[@key] = @value
204
- end
208
+ next if @is_ary # values already in @properties[@key] via @value alias
209
+
210
+ fail "Multiple property lines not allowed for #{@key}" if @properties.key?(@key)
211
+ @properties[@key] = @value
205
212
  end
206
213
  end
207
214
 
208
215
  def reset_label!
209
216
  @constructor = nil
210
- @properties = Hash.new
217
+ @properties = {}
211
218
  @key = nil
219
+ @is_ary = false
212
220
  @value = nil
213
221
  end
214
222
 
@@ -7,10 +7,11 @@ class Ecu
7
7
  str << " LANGNAME #{description.enquote}\n" if description
8
8
  str << " FUNKTION #{function}\n" if function
9
9
  str << " EINHEIT_W #{unit.enquote}\n" if unit
10
- str << case value
11
- when Numeric then " WERT #{value}\n"
12
- when String then " TEXT #{value.enquote}\n"
13
- end
10
+ if value.is_a?(Numeric)
11
+ str << " WERT " << value.to_s << "\n"
12
+ else
13
+ str << " TEXT " << value.enquote << "\n"
14
+ end
14
15
  str << "END\n"
15
16
  end
16
17
  end
@@ -29,21 +29,23 @@ class Ecu
29
29
  end
30
30
 
31
31
  def to_dcm(indented=false)
32
- out = []
32
+ buf = String.new
33
33
 
34
34
  unless headers.empty?
35
- out += headers.map { "* " + _1 }.push("")
35
+ headers.each { buf << "* " << _1 << "\n" }
36
+ buf << "\n"
36
37
  end
37
38
 
38
- out << DCM_HEADER << ""
39
+ buf << DCM_HEADER << "\n"
39
40
 
40
41
  unless subheaders.empty?
41
- out += subheaders.map { "* " + _1 }.push("")
42
+ buf << "\n"
43
+ subheaders.each { buf << "* " << _1 << "\n" }
42
44
  end
43
45
 
44
- out += map { _1.to_dcm(indented) }
46
+ each { buf << "\n" << _1.to_dcm(indented) }
45
47
 
46
- out.join("\n")
48
+ buf
47
49
  end
48
50
  end
49
51
  end
data/lib/ecu/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Ecu
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: automotive-ecu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Mueller