Class: Gloo::Core::Tokens
- Inherits:
-
Object
- Object
- Gloo::Core::Tokens
- Defined in:
- lib/gloo/core/tokens.rb
Constant Summary collapse
- CALL_OPENERS =
Keywords that open an inline function call, eg. invoke( ... ) or ~>( ... ). Kept here since Tokens is where they get recognized as a single bounded token; Parser#split_params reuses this same list to avoid mistaking a trailing call for its unrelated trailing-optional-param convention.
%w[invoke ~>].freeze
- QUOTE_CHARS =
[ '"', "'" ].freeze
Instance Attribute Summary collapse
-
#cmd ⇒ Object
readonly
Returns the value of attribute cmd.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Instance Method Summary collapse
-
#after_token(token) ⇒ Object
Get the item after a given token.
-
#at(index) ⇒ Object
Get the token at the the requested index.
-
#before_token(token) ⇒ Object
Get the item after a given token.
-
#expr_after(token, break_token = nil) ⇒ Object
Get the expression after the given token.
-
#first ⇒ Object
Get the first token.
-
#index_of(token) ⇒ Object
Get the index of the given token.
-
#initialize(cmd_string) ⇒ Tokens
constructor
Set up the tokens.
-
#last ⇒ Object
Get the last token.
-
#params ⇒ Object
Get all tokens except the first.
-
#second ⇒ Object
Get the second token.
-
#token_count ⇒ Object
Get the number of tokens.
-
#tokens_after(token) ⇒ Object
Get the list of tokens after the given token.
-
#verb ⇒ Object
Get the verb (the first word).
Constructor Details
#initialize(cmd_string) ⇒ Tokens
Set up the tokens. The command string is parsed into tokens during creation.
31 32 33 34 35 |
# File 'lib/gloo/core/tokens.rb', line 31 def initialize( cmd_string ) @cmd = cmd_string @tokens = [] tokenize @cmd end |
Instance Attribute Details
#cmd ⇒ Object (readonly)
Returns the value of attribute cmd.
21 22 23 |
# File 'lib/gloo/core/tokens.rb', line 21 def cmd @cmd end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
21 22 23 |
# File 'lib/gloo/core/tokens.rb', line 21 def tokens @tokens end |
Instance Method Details
#after_token(token) ⇒ Object
Get the item after a given token.
128 129 130 131 132 133 |
# File 'lib/gloo/core/tokens.rb', line 128 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.
86 87 88 |
# File 'lib/gloo/core/tokens.rb', line 86 def at( index ) return @tokens[ index ] if @tokens && @tokens.size >= index end |
#before_token(token) ⇒ Object
Get the item after a given token.
138 139 140 141 142 143 |
# File 'lib/gloo/core/tokens.rb', line 138 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, break_token = nil) ⇒ Object
Get the expression after the given token
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/gloo/core/tokens.rb', line 112 def expr_after( token, break_token = nil ) str = '' token_list = tokens_after( token ) return str if token_list.nil? token_list.each do |t| str << ' ' unless str.empty? break if t == break_token str << t.to_s end return str end |
#first ⇒ Object
Get the first token.
65 66 67 |
# File 'lib/gloo/core/tokens.rb', line 65 def first return @tokens.first if @tokens end |
#index_of(token) ⇒ Object
Get the index of the given token.
93 94 95 96 97 |
# File 'lib/gloo/core/tokens.rb', line 93 def index_of( token ) return nil unless @tokens return @tokens.find_index { |o| o.casecmp( token ).zero? } end |
#last ⇒ Object
Get the last token.
72 73 74 |
# File 'lib/gloo/core/tokens.rb', line 72 def last return @tokens.last if @tokens end |
#params ⇒ Object
Get all tokens except the first.
58 59 60 |
# File 'lib/gloo/core/tokens.rb', line 58 def params return @tokens[ 1..-1 ] end |
#second ⇒ Object
Get the second token.
79 80 81 |
# File 'lib/gloo/core/tokens.rb', line 79 def second return @tokens[ 1 ] if @tokens&.size&.positive? end |
#token_count ⇒ Object
Get the number of tokens
44 45 46 |
# File 'lib/gloo/core/tokens.rb', line 44 def token_count return @tokens.size end |
#tokens_after(token) ⇒ Object
Get the list of tokens after the given token
102 103 104 105 106 107 |
# File 'lib/gloo/core/tokens.rb', line 102 def tokens_after( token ) i = index_of token return @tokens[ i + 1..-1 ] if i && @tokens && @tokens.size > ( i + 1 ) return nil end |
#verb ⇒ Object
Get the verb (the first word)
51 52 53 |
# File 'lib/gloo/core/tokens.rb', line 51 def verb return first end |