Class: Ecu::LabLexer

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

Constant Summary collapse

KEYWORDS =
[
  "[SETTINGS]",
  "[RAMCELL]",
  "[Label]",
].freeze
NEWLINE =
%r{ \r\n|\n }x
SETTINGS_HEADER =
%r{ \[SETTINGS\]\s*#{NEWLINE} }x
LABELS_HEADER =
%r{ \[Label\]\s*#{NEWLINE}    }x
SIGNALS_HEADER =
%r{ \[RAMCELL\]\s*#{NEWLINE}  }x
STRING =
%r{ [^\r\n;]+ }x
WHITESPACE =
%r{ [ \t]+ }x
SEPARATOR =
%r{ [;] }x
COMMENT =
%r{ ^[;].*$ }x

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ LabLexer

Returns a new instance of LabLexer.



6
7
8
9
# File 'lib/ecu/interfaces/lab/lab_lexer.rb', line 6

def initialize(doc)
  @doc  = doc
  @scan = StringScanner.new(doc)
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



5
6
7
# File 'lib/ecu/interfaces/lab/lab_lexer.rb', line 5

def doc
  @doc
end

Instance Method Details

#linenoObject



52
53
54
55
# File 'lib/ecu/interfaces/lab/lab_lexer.rb', line 52

def lineno
  @doc.byteslice(0, @scan.pos).count("\n") +
    (@scan.beginning_of_line? ? 0 : 1)
end

#next_token(state) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ecu/interfaces/lab/lab_lexer.rb', line 26

def next_token(state)
  @scan.skip(WHITESPACE)

  return if @scan.eos?

  case
  when @scan.skip(SETTINGS_HEADER)                     then :SETTINGS_HEADER
  when @scan.skip(LABELS_HEADER)                       then :LABELS_HEADER
  when @scan.skip("[LABEL]\n")                         then :LABELS_HEADER
  when @scan.skip(SIGNALS_HEADER)                      then :SIGNALS_HEADER
  when @scan.skip(STRING)                              then :STRING
  when @scan.beginning_of_line? && @scan.skip(COMMENT) then :COMMENT
  when @scan.skip(SEPARATOR)                           then :SEPARATOR
  when @scan.skip(NEWLINE)                             then :NEWLINE
  when @scan.skip(ANYTHING)                            then :ANYTHING

  else
    @scan.getch
    :UNKNOWN_CHAR
  end
end

#token_valueObject



48
49
50
# File 'lib/ecu/interfaces/lab/lab_lexer.rb', line 48

def token_value
  @doc.byteslice(@scan.pos - @scan.matched_size, @scan.matched_size)
end