Class: Glaemscribe::API::Eval::Lexer

Inherits:
Object
  • Object
show all
Defined in:
lib/api/eval.rb

Constant Summary collapse

EXP_TOKENS =
[
  Token.new("bool_or",      "||"),
  Token.new("bool_and",     "&&"),
  Token.new("cond_inf_eq",  "<="),
  Token.new("cond_inf",     "<"),
  Token.new("cond_sup_eq",  ">="),
  Token.new("cond_sup",     ">"),
  Token.new("cond_eq",      "=="),
  Token.new("cond_not_eq",  "!="),
  Token.new("add_plus",     "+"),
  Token.new("add_minus",    "-"),
  Token.new("mult_times",   "*"),
  Token.new("mult_div",     "/"),
  Token.new("mult_modulo",  "%"),
  Token.new("prim_not",     "!"),
  Token.new("prim_lparen",  "("),
  Token.new("prim_rparen",  ")"),
  Token.new("prim_string",  /^'[^']*'/),
  Token.new("prim_string",  /^"[^"]*"/),
  Token.new("prim_const",   /^[a-zA-Z0-9_.]+/)
]
TOKEN_END =
Token.new("prim_end","")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exp) ⇒ Lexer

Returns a new instance of Lexer.



81
82
83
84
85
# File 'lib/api/eval.rb', line 81

def initialize(exp)
  @exp            = exp
  @token_chain    = []
  @retain_last    = false
end

Instance Attribute Details

#expObject (readonly)

Returns the value of attribute exp.



55
56
57
# File 'lib/api/eval.rb', line 55

def exp
  @exp
end

#token_chainObject (readonly)

Returns the value of attribute token_chain.



55
56
57
# File 'lib/api/eval.rb', line 55

def token_chain
  @token_chain
end

Instance Method Details

#advanceObject

Raises:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/api/eval.rb', line 91

def advance
  @exp.strip!
    
  if @retain_last
    @retain_last = false
    return @token_chain.last
  end
    
  if(@exp == TOKEN_END.expression)
    t               = TOKEN_END.clone("")
    @token_chain    << t
    return t
  end
    
  EXP_TOKENS.each{ |token|
    if(token.regexp?) 
      if(token.expression =~ @exp)
        @exp          = $' # Eat the token
        t             = token.clone($~.to_s)
        @token_chain << t 
        return t        
      end
    else
      if(@exp.start_with?(token.expression))
        @exp          = @exp[token.expression.length..-1] 
        t             = token.clone(token.expression)
        @token_chain << t 
        return t
      end
    end
  }      
  raise UnknownToken    
end

#uneatObject



87
88
89
# File 'lib/api/eval.rb', line 87

def uneat
  @retain_last = true
end