Class: Ecu::LabParser

Inherits:
Object
  • Object
show all
Defined in:
lib/ecu/interfaces/lab/lab_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ LabParser

Returns a new instance of LabParser.



11
12
13
14
15
16
17
18
19
# File 'lib/ecu/interfaces/lab/lab_parser.rb', line 11

def initialize(doc)
  @lexer      = LabLexer.new(doc)
  @state      = :NO_SECTION
  @signals    = []
  @labels     = []
  @headers    = []
  @subheaders = []
  reset_entry!
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



10
11
12
# File 'lib/ecu/interfaces/lab/lab_parser.rb', line 10

def headers
  @headers
end

#labelsObject (readonly)

Returns the value of attribute labels.



10
11
12
# File 'lib/ecu/interfaces/lab/lab_parser.rb', line 10

def labels
  @labels
end

#lexerObject (readonly)

Returns the value of attribute lexer.



10
11
12
# File 'lib/ecu/interfaces/lab/lab_parser.rb', line 10

def lexer
  @lexer
end

#signalsObject (readonly)

Returns the value of attribute signals.



10
11
12
# File 'lib/ecu/interfaces/lab/lab_parser.rb', line 10

def signals
  @signals
end

#stateObject (readonly)

Returns the value of attribute state.



10
11
12
# File 'lib/ecu/interfaces/lab/lab_parser.rb', line 10

def state
  @state
end

#subheadersObject (readonly)

Returns the value of attribute subheaders.



10
11
12
# File 'lib/ecu/interfaces/lab/lab_parser.rb', line 10

def subheaders
  @subheaders
end

Instance Method Details

#add_header(str) ⇒ Object



91
92
93
# File 'lib/ecu/interfaces/lab/lab_parser.rb', line 91

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

#add_property(str) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/ecu/interfaces/lab/lab_parser.rb', line 125

def add_property(str)
  case @properties
  in { task:, description: } then @properties[:description] << " " << str
  in { task:               } then @properties[:description] = str.strip
  in { **                  } then @properties[:task] = str.split("&").first
  end
end

#add_subheader(str) ⇒ Object



95
96
97
# File 'lib/ecu/interfaces/lab/lab_parser.rb', line 95

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

#callObject



25
26
27
28
29
30
31
32
33
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
# File 'lib/ecu/interfaces/lab/lab_parser.rb', line 25

def call
  while tok = lexer.next_token(state)
    begin
      case state
      in :NO_SECTION
        case tok
        in :SETTINGS_HEADER then next_state(:SETTINGS)
        in :SIGNALS_HEADER  then next_state(:SIGNALS)
        in :LABELS_HEADER   then next_state(:LABELS)
        in :COMMENT         then add_header(lexer.token_value)
        in :NEWLINE         then # noop
        end
      in :SETTINGS
        case tok
        in :SIGNALS_HEADER then next_state(:SIGNALS)
        in :LABELS_HEADER  then next_state(:LABELS)
        in :COMMENT        then add_subheader(lexer.token_value)
        in :STRING         then # save that shit?
        in :SEPARATOR      then # now comes the value
        in :NEWLINE        then # next setting
        end
      in :LABELS
        case tok
        in :SIGNALS_HEADER then next_state(:SIGNALS)
        in :STRING         then start_label(lexer.token_value)
        in :COMMENT        then # noop
        in :NEWLINE        then # noop
        end
      in :SIGNALS
        case tok
        in :LABELS_HEADER  then next_state(:LABELS)
        in :STRING         then start_signal(lexer.token_value)
        in :COMMENT        then # noop
        in :NEWLINE        then # noop
        end
      in :LABEL
        case tok
        in :NEWLINE        then labels << finish_label
        in :SEPARATOR      then # noop, sometimes used as end separator
        in :STRING         then # noop
        end
      in :SIGNAL
        case tok
        in :STRING         then add_property(lexer.token_value)
        in :SEPARATOR      then # noop, next property
        in :NEWLINE        then signals << finish_signal
        end
      end
    rescue NoMatchingPatternError => e
      raise LabParserError.new("Unexpected token #{debug_token(tok)} (state: #{state})", lexer)
    rescue StandardError => e
      raise LabParserError.new("#{e.message} (state: #{@state})", lexer)
    end
  end
  [signals, labels, headers, subheaders]
end

#debug_token(tok) ⇒ Object



82
83
84
# File 'lib/ecu/interfaces/lab/lab_parser.rb', line 82

def debug_token(tok)
  "#{tok}: \"#{lexer.token_value}\" - #{@properties.inspect}"
end

#finish_labelObject



105
106
107
108
109
110
# File 'lib/ecu/interfaces/lab/lab_parser.rb', line 105

def finish_label
  Festwert.new(**@properties, value: nil).tap do
    reset_entry!
    next_state(:LABELS)
  end
end

#finish_signalObject



118
119
120
121
122
123
# File 'lib/ecu/interfaces/lab/lab_parser.rb', line 118

def finish_signal
  Signal.new(**@properties).tap do
    reset_entry!
    next_state(:SIGNALS)
  end
end

#next_state(newstate) ⇒ Object



86
87
88
89
# File 'lib/ecu/interfaces/lab/lab_parser.rb', line 86

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

#reset_entry!Object



21
22
23
# File 'lib/ecu/interfaces/lab/lab_parser.rb', line 21

def reset_entry!
  @properties = {}
end

#start_label(name) ⇒ Object



99
100
101
102
103
# File 'lib/ecu/interfaces/lab/lab_parser.rb', line 99

def start_label(name)
  next_state(:LABEL) do
    @properties[:name] = name
  end
end

#start_signal(name) ⇒ Object



112
113
114
115
116
# File 'lib/ecu/interfaces/lab/lab_parser.rb', line 112

def start_signal(name)
  next_state(:SIGNAL) do
    @properties[:name] = name
  end
end