Class: Parser::Lexer

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

Instance Method Summary collapse

Constructor Details

#initializeLexer

Returns a new instance of Lexer.



6
7
8
9
10
# File 'lib/amy/parser/lexer.rb', line 6

def initialize
  @filename = ""
  @file     = nil
  @debug    = false
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/amy/parser/lexer.rb', line 64

def eof?
  @file.eof?
end

#linenoObject



68
69
70
# File 'lib/amy/parser/lexer.rb', line 68

def lineno
  @file.lineno
end

#nextObject

Get’s the next token

Raises:

  • (IOError)


24
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
# File 'lib/amy/parser/lexer.rb', line 24

def next
  raise IOError.new("Stream is at the end of file.") if eof?
  end_of_token = false
  token = ""
  while not end_of_token
    c = @file.getc
    puts "next c: #{c.inspect} v: #{valid_char?(c)} s: #{single_char?(c)} e: #{is_end_character?(c)}" if @debug
    if eof? then
      end_of_token = true
    elsif (single_char?(c)) then
      if (token.empty?) then
          token = c
          next_token = @file.getc
          if ('#' == token and '#' == next_token) then
            token << next_token
          else
            @file.seek(-1, IO::SEEK_CUR)
          end
      else
        @file.seek(-1, IO::SEEK_CUR)
      end
      end_of_token = true
    elsif (valid_char?(c)) then
      token << c
    elsif is_end_character?(c) then
      move_till_next_token
      end_of_token = (not token.empty?)
    end
  end
  puts "next" if @debug
  build_token(token)
end

#rewindObject

Set’s the lexer back to the begin



60
61
62
# File 'lib/amy/parser/lexer.rb', line 60

def rewind
  @file.rewind
end

#set_debug(debug) ⇒ Object



12
13
14
# File 'lib/amy/parser/lexer.rb', line 12

def set_debug(debug)
  @debug = debug
end

#set_input(file) ⇒ Object



16
17
18
19
# File 'lib/amy/parser/lexer.rb', line 16

def set_input(file)
  @filename = file
  @file     = File.new(file)
end