Class: Token

Inherits:
Object
  • Object
show all
Defined in:
lib/linmeric/Token.rb

Overview

This class generates a token object with three parameters:

  • Attribute

  • Position

  • Value

Author

Massimiliano Dal Mas ([email protected])

License

Distributed under MIT license

Constant Summary collapse

OPERATORS =
Tool.operators
VARIABLE =
/[a-zA-Z_][a-zA-Z0-9_]*/

Instance Method Summary collapse

Constructor Details

#initialize(to_token, pos, sugg = "") ⇒ Token

Creates a new token object

  • argument: token value to name

  • argument: token position

  • argument: ‘suggestion’ (string) to force a specific attribute. “” default



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/linmeric/Token.rb', line 25

def initialize(to_token,pos,sugg = "")
  @My_pos = pos
  if OPERATORS.include? to_token then    
    @My_att = "OPERATOR" unless [">","=","+","-"].include? to_token
    @My_att = "EQUAL_OP" if to_token == "="
    @My_att = "TO_FILE_OP" if to_token == ">"
    @My_att = "SUM_OPERATOR" if '+-'.include? to_token
  elsif sugg != ""
    @My_att = sugg
  elsif to_token == "~"
    @My_att = "EQUALS_TO"
  elsif to_token.include? ":" then
    @My_att = "KEYWORD"
  elsif to_token.match(VARIABLE).to_s.size == to_token.size then
    @My_att = "VARIABLE"
  elsif to_token.number? then
    @My_att = "NUMBER"
  elsif to_token == "(" then
    @My_att = "L_BRACE"
  elsif to_token == ")" then
    @My_att = "R_BRACE"
  elsif to_token == '"' then
    @My_att = "QUOTES"
  else
    @My_att = "GENERAL_STRING"
  end
  @My_self = (to_token.include? ':') ? (to_token.downcase) : (to_token)
end

Instance Method Details

#attributeObject

  • returns: token attribute



55
56
57
# File 'lib/linmeric/Token.rb', line 55

def attribute
  return @My_att
end

#meObject

  • returns: token value



65
66
67
# File 'lib/linmeric/Token.rb', line 65

def me
  return @My_self
end

#positionObject

  • returns: token position



60
61
62
# File 'lib/linmeric/Token.rb', line 60

def position
  return @My_pos
end