Class: GlooLang::Core::Tokens

Inherits:
Object
  • Object
show all
Defined in:
lib/gloo_lang/core/tokens.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd_string) ⇒ Tokens

Set up the tokens. The command string is parsed into tokens during creation.



22
23
24
25
26
# File 'lib/gloo_lang/core/tokens.rb', line 22

def initialize( cmd_string )
  @cmd = cmd_string
  @tokens = []
  tokenize @cmd
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd.



12
13
14
# File 'lib/gloo_lang/core/tokens.rb', line 12

def cmd
  @cmd
end

#tokensObject (readonly)

Returns the value of attribute tokens.



12
13
14
# File 'lib/gloo_lang/core/tokens.rb', line 12

def tokens
  @tokens
end

Instance Method Details

#after_token(token) ⇒ Object

Get the item after a given token.



115
116
117
118
119
120
# File 'lib/gloo_lang/core/tokens.rb', line 115

def after_token( token )
  i = index_of token
  return @tokens[ i + 1 ] if i && @tokens && @tokens.size > ( i + 1 )

  return nil
end

#at(index) ⇒ Object

Get the token at the the requested index.



77
78
79
# File 'lib/gloo_lang/core/tokens.rb', line 77

def at( index )
  return @tokens[ index ] if @tokens && @tokens.size >= index
end

#before_token(token) ⇒ Object

Get the item after a given token.



125
126
127
128
129
130
# File 'lib/gloo_lang/core/tokens.rb', line 125

def before_token( token )
  i = index_of token
  return @tokens[ 0..i - 1 ] if i && @tokens && @tokens.size >= i

  return nil
end

#expr_after(token) ⇒ Object

Get the expression after the given token



103
104
105
106
107
108
109
110
# File 'lib/gloo_lang/core/tokens.rb', line 103

def expr_after( token )
  str = ''
  tokens_after( token ).each do |t|
    str << ' ' unless str.empty?
    str << t.to_s
  end
  return str
end

#firstObject

Get the first token.



56
57
58
# File 'lib/gloo_lang/core/tokens.rb', line 56

def first
  return @tokens.first if @tokens
end

#index_of(token) ⇒ Object

Get the index of the given token.



84
85
86
87
88
# File 'lib/gloo_lang/core/tokens.rb', line 84

def index_of( token )
  return nil unless @tokens

  return @tokens.find_index { |o| o.casecmp( token ).zero? }
end

#lastObject

Get the last token.



63
64
65
# File 'lib/gloo_lang/core/tokens.rb', line 63

def last
  return @tokens.last if @tokens
end

#paramsObject

Get all tokens except the first.



49
50
51
# File 'lib/gloo_lang/core/tokens.rb', line 49

def params
  return @tokens[ 1..-1 ]
end

#secondObject

Get the second token.



70
71
72
# File 'lib/gloo_lang/core/tokens.rb', line 70

def second
  return @tokens[ 1 ] if @tokens&.size&.positive?
end

#token_countObject

Get the number of tokens



35
36
37
# File 'lib/gloo_lang/core/tokens.rb', line 35

def token_count
  return @tokens.size
end

#tokens_after(token) ⇒ Object

Get the list of tokens after the given token



93
94
95
96
97
98
# File 'lib/gloo_lang/core/tokens.rb', line 93

def tokens_after( token )
  i = index_of token
  return @tokens[ i + 1..-1 ] if i && @tokens && @tokens.size > ( i + 1 )

  return nil
end

#verbObject

Get the verb (the first word)



42
43
44
# File 'lib/gloo_lang/core/tokens.rb', line 42

def verb
  return first
end