Class: PuppetLint::Lexer::Token
- Inherits:
-
Object
- Object
- PuppetLint::Lexer::Token
- Defined in:
- lib/puppet-lint/lexer/token.rb
Overview
Public: Stores a fragment of the manifest and the information about its location in the manifest.
Instance Attribute Summary collapse
-
#column ⇒ Object
readonly
Public: Returns the Integer column number of the line of the manifest text where the Token can be found.
-
#line ⇒ Object
readonly
Public: Returns the Integer line number of the manifest text where the Token can be found.
-
#next_code_token ⇒ Object
Public: Gets/sets the next code token (skips whitespace, comments, etc) in the manifest.
-
#next_token ⇒ Object
Public: Gets/sets the next token in the manifest.
-
#prev_code_token ⇒ Object
Public: Gets/sets the previous code tokne (skips whitespace, comments, etc) in the manifest.
-
#prev_token ⇒ Object
Public: Gets/sets the previous token in the manifest.
-
#raw ⇒ Object
Public: Returns the raw value of the Token.
-
#type ⇒ Object
Public: Returns the Symbol type of the Token.
-
#value ⇒ Object
Public: Returns the String value of the Token.
Instance Method Summary collapse
-
#find_token_of(direction, type, opts = {}) ⇒ Object
Internal: Search from this token to find the next token of a given type in a given direction.
-
#initialize(type, value, line, column) ⇒ Token
constructor
Public: Initialise a new Token object.
-
#inspect ⇒ Object
Public: Produce a human friendly description of the Token when inspected.
- #interpolated_variable? ⇒ Boolean
-
#next_token_of(type, opts = {}) ⇒ Object
Public: Search from this token to find the next token of a given type.
-
#prev_token_of(type, opts = {}) ⇒ Object
Public: Search from this token to find the previous token of a given type.
- #string_prefix ⇒ Object
- #string_suffix ⇒ Object
-
#to_manifest ⇒ Object
Public: Produce a Puppet DSL representation of a Token.
Constructor Details
#initialize(type, value, line, column) ⇒ Token
Public: Initialise a new Token object.
type - An upper case Symbol describing the type of Token. value - The String value of the Token. line - The Integer line number where the Token can be found in the
manifest.
column - The Integer number of characters from the start of the line to
the start of the Token.
Returns the instantiated Token.
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/puppet-lint/lexer/token.rb', line 46 def initialize(type, value, line, column) @value = value @type = type @line = line @column = column @next_token = nil @prev_token = nil @next_code_token = nil @prev_code_token = nil end |
Instance Attribute Details
#column ⇒ Object (readonly)
Public: Returns the Integer column number of the line of the manifest text where the Token can be found.
20 21 22 |
# File 'lib/puppet-lint/lexer/token.rb', line 20 def column @column end |
#line ⇒ Object (readonly)
Public: Returns the Integer line number of the manifest text where the Token can be found.
16 17 18 |
# File 'lib/puppet-lint/lexer/token.rb', line 16 def line @line end |
#next_code_token ⇒ Object
Public: Gets/sets the next code token (skips whitespace, comments, etc) in the manifest.
30 31 32 |
# File 'lib/puppet-lint/lexer/token.rb', line 30 def next_code_token @next_code_token end |
#next_token ⇒ Object
Public: Gets/sets the next token in the manifest.
23 24 25 |
# File 'lib/puppet-lint/lexer/token.rb', line 23 def next_token @next_token end |
#prev_code_token ⇒ Object
Public: Gets/sets the previous code tokne (skips whitespace, comments, etc) in the manifest.
34 35 36 |
# File 'lib/puppet-lint/lexer/token.rb', line 34 def prev_code_token @prev_code_token end |
#prev_token ⇒ Object
Public: Gets/sets the previous token in the manifest.
26 27 28 |
# File 'lib/puppet-lint/lexer/token.rb', line 26 def prev_token @prev_token end |
#raw ⇒ Object
Public: Returns the raw value of the Token.
12 13 14 |
# File 'lib/puppet-lint/lexer/token.rb', line 12 def raw @raw end |
#type ⇒ Object
Public: Returns the Symbol type of the Token.
6 7 8 |
# File 'lib/puppet-lint/lexer/token.rb', line 6 def type @type end |
#value ⇒ Object
Public: Returns the String value of the Token.
9 10 11 |
# File 'lib/puppet-lint/lexer/token.rb', line 9 def value @value end |
Instance Method Details
#find_token_of(direction, type, opts = {}) ⇒ Object
Internal: Search from this token to find the next token of a given type in a given direction.
direction - A Symbol direction to search (:next or :prev). type - A Symbol type of the token to find, or an Array of Symbols. opts - An optional Hash
:value - A token value to search for in addition to type
:skip_blocks - A Boolean to specify whether { } blocks should be
skipped over (defaults to true).
Returns a PuppetLint::Lexer::Token object if a matching token could be found, otherwise nil.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/puppet-lint/lexer/token.rb', line 170 def find_token_of(direction, type, opts = {}) return nil unless [:next, :prev].include?(direction) opts[:skip_blocks] ||= true to_find = Array[*type] token_iter = send("#{direction}_token".to_sym) until token_iter.nil? if to_find.include?(token_iter.type) return token_iter if opts[:value].nil? || token_iter.value == opts[:value] end opening_token = direction == :next ? 'L' : 'R' closing_token = direction == :next ? 'R' : 'L' if opts[:skip_blocks] case token_iter.type when "#{opening_token}BRACE".to_sym token_iter = token_iter.send("#{direction}_token_of".to_sym, ["#{closing_token}BRACE".to_sym, opts]) when "#{opening_token}BRACK".to_sym token_iter = token_iter.send("#{direction}_token_of".to_sym, ["#{closing_token}BRACK".to_sym, opts]) when "#{opening_token}PAREN".to_sym token_iter = token_iter.send("#{direction}_token_of".to_sym, ["#{closing_token}PAREN".to_sym, opts]) end end return nil if token_iter.nil? token_iter = token_iter.send("#{direction}_token".to_sym) end nil end |
#inspect ⇒ Object
Public: Produce a human friendly description of the Token when inspected.
Returns a String describing the Token.
61 62 63 |
# File 'lib/puppet-lint/lexer/token.rb', line 61 def inspect "<Token #{@type.inspect} (#{@value}) @#{@line}:#{@column}>" end |
#interpolated_variable? ⇒ Boolean
202 203 204 205 206 |
# File 'lib/puppet-lint/lexer/token.rb', line 202 def interpolated_variable? return false if type == :TYPE && value != 'type' return true if type == :NAME PuppetLint::Lexer::KEYWORDS.include?(type.to_s.downcase) end |
#next_token_of(type, opts = {}) ⇒ Object
Public: Search from this token to find the next token of a given type.
type - A Symbol type of the token to find, or an Array of Symbols. opts - An optional Hash
:value - A token value to search for in addition to type
:skip_blocks - A Boolean to specify whether { } blocks should be
skipped over (defaults to true).
Returns a PuppetLint::Lexer::Token object if a matching token could be found, otherwise nil.
140 141 142 |
# File 'lib/puppet-lint/lexer/token.rb', line 140 def next_token_of(type, opts = {}) find_token_of(:next, type, opts) end |
#prev_token_of(type, opts = {}) ⇒ Object
Public: Search from this token to find the previous token of a given type.
type - A Symbol type of the token to find, or an Array of Symbols. opts - An optional Hash
:value - A token value to search for in addition to type
:skip_blocks - A Boolean to specify whether { } blocks should be
skipped over (defaults to true).
Returns a PuppetLint::Lexer::Token object if a matching token could be found, otherwise nil.
154 155 156 |
# File 'lib/puppet-lint/lexer/token.rb', line 154 def prev_token_of(type, opts = {}) find_token_of(:prev, type, opts) end |
#string_prefix ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/puppet-lint/lexer/token.rb', line 121 def string_prefix no_enclose_tokens = Set.new([:UNENC_VARIABLE, :DQPRE, :DQMID, :HEREDOC_PRE, :HEREDOC_MID]) if prev_token && no_enclose_tokens.include?(prev_token.type) '' else '}' end end |
#string_suffix ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/puppet-lint/lexer/token.rb', line 112 def string_suffix no_enclose_tokens = Set.new([:UNENC_VARIABLE, :DQMID, :DQPOST, :HEREDOC_MID, :HEREDOC_POST]) if next_token && no_enclose_tokens.include?(next_token.type) '' else '${' end end |
#to_manifest ⇒ Object
Public: Produce a Puppet DSL representation of a Token.
Returns a Puppet DSL String.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/puppet-lint/lexer/token.rb', line 68 def to_manifest case @type when :STRING "\"#{@value}\"" when :SSTRING "'#{@value}'" when :DQPRE "\"#{@value}#{string_suffix}" when :DQPOST "#{string_prefix}#{@value}\"" when :DQMID "#{string_prefix}#{@value}#{string_suffix}" when :VARIABLE enclose_token_types = Set[:DQPRE, :DQMID, :HEREDOC_PRE, :HEREDOC_MID].freeze if !@prev_code_token.nil? && enclose_token_types.include?(@prev_code_token.type) @raw.nil? ? @value : @raw else "$#{@value}" end when :UNENC_VARIABLE "$#{@value}" when :NEWLINE "\n" when :COMMENT "##{@value}" when :REGEX "/#{@value}/" when :MLCOMMENT @raw when :HEREDOC_OPEN "@(#{@value})" when :HEREDOC @raw when :HEREDOC_PRE "#{@value}#{string_suffix}" when :HEREDOC_POST "#{string_prefix}#{@raw}" when :HEREDOC_MID "#{string_prefix}#{@value}#{string_suffix}" else @value end end |