Module: BuildTool::Cfg::LexerBase

Included in:
Lexer
Defined in:
lib/build-tool/cfg/lexer_base.rb

Overview

Base class for all lexer.

Defined Under Namespace

Modules: ClassMethods Classes: ScanError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/build-tool/cfg/lexer_base.rb', line 12

def self.included( klass )
    klass.extend( ClassMethods )
    klass.module_eval do
        alias_method( :rex_next_token, :next_token )
        remove_method( :next_token )
    end
end

Instance Method Details

#error(str) ⇒ Object

Raises:

  • (ParseError)


68
69
70
71
72
73
# File 'lib/build-tool/cfg/lexer_base.rb', line 68

def error( str )
    if !instance_variable_defined?( "@filename" )
        @filename = "<unknown>"
    end
    raise ParseError, "#{filename}[#{lineno}] #{str}"
end

#next_tokenObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/build-tool/cfg/lexer_base.rb', line 37

def next_token
    begin
        token = rex_next_token
        # puts "#{state.inspect}##{token.inspect} #{lineno}"
        case token[0]
        when :IGNORE;  redo
        when :COMMENT; redo
        when :GARBAGE; raise StandardError.new("can not match: '#{token[1]}' near '#{pre_match()}'")
        else break
        end if !token.nil?
        break
    end while true
    return token
rescue StandardError => e
    error = ScanError.new(e.to_s)
    error.lineno = lineno
    error.filename = filename
    raise error
end

#on_error(error_token_id, error_value, value_stack) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/build-tool/cfg/lexer_base.rb', line 57

def on_error( error_token_id, error_value, value_stack )
    token_name = token_to_str( error_token_id )
    token_name.downcase!
    token = error_value.to_s.inspect

    str = 'parse error on value '
    str << '"' << token_name << '" ' unless token_name == token
    str << token
    error(str)
end

#parse_string(str, filename = "<string>") ⇒ Object



75
76
77
78
# File 'lib/build-tool/cfg/lexer_base.rb', line 75

def parse_string( str, filename = "<string>" )
    @filename = filename
    scan_str( str )
end

#peek(len) ⇒ Object



28
29
30
# File 'lib/build-tool/cfg/lexer_base.rb', line 28

def peek( len )
    @ss.peek( len )
end

#pre_matchObject



32
33
34
35
# File 'lib/build-tool/cfg/lexer_base.rb', line 32

def pre_match
    # *TODO* Improve context
    @ss.pre_match()[-20,20]
end