class Parser::Lexer

Public Class Methods

new() click to toggle source
# File lib/amy/parser/lexer.rb, line 6
def initialize
  @filename = ""
  @file     = nil
  @debug    = false
end

Public Instance Methods

eof?() click to toggle source
# File lib/amy/parser/lexer.rb, line 64
def eof?
  @file.eof?
end
lineno() click to toggle source
# File lib/amy/parser/lexer.rb, line 68
def lineno
  @file.lineno
end
next() click to toggle source

Get’s the next token

# 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
rewind() click to toggle source

Set’s the lexer back to the begin

# File lib/amy/parser/lexer.rb, line 60
def rewind
  @file.rewind
end
set_debug(debug) click to toggle source
# File lib/amy/parser/lexer.rb, line 12
def set_debug(debug)
  @debug = debug
end
set_input(file) click to toggle source
# File lib/amy/parser/lexer.rb, line 16
def set_input(file)
  @filename = file
  @file     = File.new(file)
end

Private Instance Methods

build_token(token) click to toggle source
# File lib/amy/parser/lexer.rb, line 106
def build_token(token)
  type = Tokens::STRING
  if (token.start_with?("##"))
    type = Tokens::COMMENT_TAG
  elsif (token.start_with?("#"))
    type = Tokens::COMMENT
  elsif (token.start_with?("@")) 
    type = Tokens::PROPERTY
  end
  Token.new(type, token, @file.lineno)
end
is_end_character?(c) click to toggle source
# File lib/amy/parser/lexer.rb, line 102
def is_end_character?(c)
  eof? or single_char?(c) or !valid_char?(c)
end
is_white_space?(c) click to toggle source
# File lib/amy/parser/lexer.rb, line 98
def is_white_space?(c)
  not (c =~ /\s/).nil?
end
move_till_next_token() click to toggle source
# File lib/amy/parser/lexer.rb, line 74
def move_till_next_token
  there = false
  while not there
    c = @file.getc
    puts "move c:#{c.inspect} v: #{valid_char?(c)} s: #{single_char?(c)}" if @debug
    if eof? then
      there = true
    elsif single_char?(c) or valid_char?(c) then
      @file.seek(-1, IO::SEEK_CUR)
      there = true
    end
  end
  puts "move" if @debug
end
single_char?(c) click to toggle source
# File lib/amy/parser/lexer.rb, line 93
def single_char?(c)
  not (c =~ /['|"|#|\/|\\|\[|\]|\.|\n]/).nil?

end
valid_char?(c) click to toggle source
# File lib/amy/parser/lexer.rb, line 89
def valid_char?(c)
  not (c =~ /[[:alpha:]|[:digit:]|_|@|<|:|-]/).nil?
end