Class: CodeLexer::Lexer

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

Instance Method Summary collapse

Constructor Details

#initialize(config_path_or_config) ⇒ Lexer

Returns a new instance of Lexer.



7
8
9
10
11
12
13
# File 'lib/code-lexer/lexer.rb', line 7

def initialize(config_path_or_config)
    if config_path_or_config.is_a?(Config)
        @config = config_path_or_config
    else
        @config = Config.new(config_path_or_config)
    end
end

Instance Method Details

#lex(content, abstractor = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/code-lexer/lexer.rb', line 15

def lex(content, abstractor = nil)
    content = content.clone
    tokens = []
    while content.length > 0
        token_name, regex = @config.matching_rule(content)
        content.sub!(regex) do |value|
            tokens << Token.new(token_name, value)
            ""
        end
    end
    
    return LexedContent.new(tokens, abstractor)
end