Class: ECCSV::Lexer

Inherits:
Object
  • Object
show all
Defined in:
lib/eccsv/lexer.rb

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ Lexer

Returns a new instance of Lexer.



3
4
5
# File 'lib/eccsv/lexer.rb', line 3

def initialize(stream)
  @stream = stream
end

Instance Method Details

#next_tokenObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/eccsv/lexer.rb', line 7

def next_token
  unless @stream.eof?
    token = nil
    match = ""
    line = @stream.line
    col = @stream.col

    until @stream.eof?
      c = @stream.peek
      if token.nil?
        match << c
        @stream.next
        if c == ","
          token = :COMMA
          break
        elsif c == '"'
          token = :QUOTE
          break
        elsif c == "\n"
          token = :NEWLINE
          break
        else
          token = :TEXT
        end
      elsif c != "," && c != '"' && c != "\n"
        match << c
        @stream.next
      else
        break
      end
    end

    if match.length == 0
      raise "Stream error"
    end
    node = Node.new(match, token, line, col)
    [token, node]
  end
end