Class: CodeLexer::Token

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

Constant Summary collapse

SPECIAL_TOKEN_OPEN =
"¬"
SPECIAL_TOKEN_CLOSE =
"¬"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, value) ⇒ Token

Returns a new instance of Token.



27
28
29
30
# File 'lib/code-lexer/token.rb', line 27

def initialize(type, value)
    @type = type
    self.value = value
end

Instance Attribute Details

#abstracted_valueObject

Returns the value of attribute abstracted_value.



12
13
14
# File 'lib/code-lexer/token.rb', line 12

def abstracted_value
  @abstracted_value
end

#typeObject

Returns the value of attribute type.



10
11
12
# File 'lib/code-lexer/token.rb', line 10

def type
  @type
end

#valueObject

Returns the value of attribute value.



11
12
13
# File 'lib/code-lexer/token.rb', line 11

def value
  @value
end

Class Method Details

.from_string(string) ⇒ Object



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

def self.from_string(string)
    unless string.start_with?(SPECIAL_TOKEN_OPEN)
        value = string
    else
        value = nil
    end
    
    token = Token.new(:unknown, value)
    token.abstracted_value = string
    
    return token
end

.special(token) ⇒ Object



6
7
8
# File 'lib/code-lexer/token.rb', line 6

def self.special(token)
    "#{SPECIAL_TOKEN_OPEN}#{token}#{SPECIAL_TOKEN_CLOSE}"
end

Instance Method Details

#==(oth) ⇒ Object



45
46
47
# File 'lib/code-lexer/token.rb', line 45

def ==(oth)
    @type == oth.type && @value == oth.value && @abstracted_value == oth.abstracted_value
end

#reset_abstractionObject



49
50
51
52
53
54
55
56
57
# File 'lib/code-lexer/token.rb', line 49

def reset_abstraction
    if @type == :newline
        @abstracted_value = Token.special("NEWLINE")
    elsif @value =~ /\s/
        @abstracted_value = Token.special(@value.gsub(/\s/, "·"))
    else
        @abstracted_value = @value.clone
    end
end

#to_sObject



37
38
39
40
41
42
43
# File 'lib/code-lexer/token.rb', line 37

def to_s
    if @abstracted_value != @value
        return "<#@type:#{@value.inspect}:#{@abstracted_value.inspect}>"
    else
        return "<#@type:#{@value.inspect}>"
    end
end