Class: Malline::Lexer

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

Constant Summary collapse

TOKEN =
{
  :START_CODE => /<%(if|s)?/,
  :END_CODE   => /%>/,
}

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Lexer

Returns a new instance of Lexer.



11
12
13
14
15
# File 'lib/malline/lexer.rb', line 11

def initialize io
  io  = StringIO.new io if io.class == String
  @ss = StringScanner.new io.read
  @token, @state = [], [:CODE, :DATA]
end

Instance Method Details

#lineObject



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

def line
  @ss.string[0..@ss.pos].lines.count
end

#next_tokenObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/malline/lexer.rb', line 17

def next_token
  return @token.shift unless @token.empty?
  return if @ss.eos?

  start = line
  name = @state.rotate!.first == :DATA ? :START_CODE : :END_CODE
  if data = @ss.scan_until(TOKEN[name])
    @token << [name, @ss.matched, start, line]
    @token << [:CMD, @ss.matched[2..-1], start, line] if @ss.matched.size > 2
    [:DATA, data[0..-(@ss.matched.size+1)], start, line]
  else #gobble gobble gobble
    [:DATA, @ss.scan(/.*/m), start, line]
  end
end