Class: Klam::Lexer

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

Defined Under Namespace

Classes: CloseParen, OpenParen

Constant Summary collapse

SYMBOL_CHARS =
/[-=*\/+_?$!\@~><&%'#`;:{}a-zA-Z0-9.,]/

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ Lexer

Returns a new instance of Lexer.



15
16
17
18
# File 'lib/klam/lexer.rb', line 15

def initialize(stream)
  @stream = stream
  @buffer = []
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/klam/lexer.rb', line 20

def eof?
  @buffer.empty? && @stream.eof?
end

#getcObject



24
25
26
27
28
29
30
# File 'lib/klam/lexer.rb', line 24

def getc
  if @buffer.empty?
    @stream.getc
  else
    @buffer.pop
  end
end

#nextObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/klam/lexer.rb', line 36

def next
  drain_whitespace
  unless eof?
    c = getc
    case c
    when '('
      OpenParen.instance
    when ')'
      CloseParen.instance
    when '"'
      consume_string
    when SYMBOL_CHARS
      ungetc(c)
      consume_number_or_symbol
    else
      raise Klam::SyntaxError, "illegal character: #{c}"
    end
  end
end

#ungetc(c) ⇒ Object



32
33
34
# File 'lib/klam/lexer.rb', line 32

def ungetc(c)
  @buffer.push(c)
end