Class: Rly::FileLex

Inherits:
Lex
  • Object
show all
Defined in:
lib/rly/file_lex.rb

Instance Attribute Summary

Attributes inherited from Lex

#lineno, #pos

Instance Method Summary collapse

Methods inherited from Lex

callables, ignore, ignore_spaces_and_tabs, #ignore_symbol, #input, #inspect, lex_double_quoted_string_tokens, lex_number_tokens, literals, metatokens, metatokens_list, on_error, terminals, token, token_regexps

Constructor Details

#initialize(fn = nil) ⇒ FileLex

Returns a new instance of FileLex.



5
6
7
8
# File 'lib/rly/file_lex.rb', line 5

def initialize(fn=nil)
  @inputstack = []
  push_file(fn) if fn
end

Instance Method Details

#build_token(type, value) ⇒ Object



39
40
41
42
43
# File 'lib/rly/file_lex.rb', line 39

def build_token(type, value)
  tok = LexToken.new(type, value, self, @pos, @lineno)
  tok.location_info[:filename] = @filename
  tok
end

#nextObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rly/file_lex.rb', line 22

def next
  begin
    tok = super

    if tok
      return tok
    else
      if @inputstack.empty?
        return nil
      else
        pop_file
        redo
      end
    end
  end until tok
end

#pop_fileObject



18
19
20
# File 'lib/rly/file_lex.rb', line 18

def pop_file
  (@input, @pos, @filename) = @inputstack.pop
end

#push_file(fn) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/rly/file_lex.rb', line 10

def push_file(fn)
  @inputstack.push([@input, @pos, @filename]) if @filename

  @filename = fn
  @input = open(fn).read
  @pos = 0
end