Class: Puppet::Parser::Lexer::TokenList

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/puppet/parser/lexer.rb

Overview

Maintain a list of tokens.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTokenList

Returns a new instance of TokenList.



84
85
86
87
88
89
# File 'lib/vendor/puppet/parser/lexer.rb', line 84

def initialize
  @tokens = {}
  @regex_tokens = []
  @string_tokens = []
  @tokens_by_string = {}
end

Instance Attribute Details

#regex_tokensObject (readonly)

Returns the value of attribute regex_tokens.



57
58
59
# File 'lib/vendor/puppet/parser/lexer.rb', line 57

def regex_tokens
  @regex_tokens
end

#string_tokensObject (readonly)

Returns the value of attribute string_tokens.



57
58
59
# File 'lib/vendor/puppet/parser/lexer.rb', line 57

def string_tokens
  @string_tokens
end

Instance Method Details

#[](name) ⇒ Object



59
60
61
# File 'lib/vendor/puppet/parser/lexer.rb', line 59

def [](name)
  @tokens[name]
end

#add_token(name, regex, options = {}, &block) ⇒ Object

Create a new token.

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/vendor/puppet/parser/lexer.rb', line 64

def add_token(name, regex, options = {}, &block)
  token = Token.new(regex, name)
  raise(ArgumentError, "Token #{name} already exists") if @tokens.include?(name)
  @tokens[token.name] = token
  if token.string
    @string_tokens << token
    @tokens_by_string[token.string] = token
  else
    @regex_tokens << token
  end

  options.each do |name, option|
    token.send(name.to_s + "=", option)
  end

  token.meta_def(:convert, &block) if block_given?

  token
end

#add_tokens(hash) ⇒ Object

Define more tokens.



97
98
99
100
101
# File 'lib/vendor/puppet/parser/lexer.rb', line 97

def add_tokens(hash)
  hash.each do |regex, name|
    add_token(name, regex)
  end
end

#eachObject

Yield each token name and value in turn.



110
111
112
# File 'lib/vendor/puppet/parser/lexer.rb', line 110

def each
  @tokens.each {|name, value| yield name, value }
end

#lookup(string) ⇒ Object

Look up a token by its value, rather than name.



92
93
94
# File 'lib/vendor/puppet/parser/lexer.rb', line 92

def lookup(string)
  @tokens_by_string[string]
end

#sort_tokensObject

Sort our tokens by length, so we know once we match, we’re done. This helps us avoid the O(n^2) nature of token matching.



105
106
107
# File 'lib/vendor/puppet/parser/lexer.rb', line 105

def sort_tokens
  @string_tokens.sort! { |a, b| b.string.length <=> a.string.length }
end