Class: PuppetDBQuery::Tokenizer

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Logging
Defined in:
lib/puppetdb_query/tokenizer.rb

Overview

tokenize puppetdb queries rubocop:disable Metrics/ClassLength

Constant Summary collapse

SINGLE_CHAR_TO_TOKEN =
{
  "!" => :not,
  "=" => :equal,
  "(" => :begin,
  ")" => :end,
  "[" => :list_begin,
  "]" => :list_end,
  "<" => :less,
  ">" => :greater,
  "~" => :match,
  "," => :comma,
}.freeze
DOUBLE_CHAR_TO_TOKEN =
{
  "!=" => :not_equal,
  "!~" => :not_match,
  "~>" => :match_array,
  "<=" => :less_or_equal,
  ">=" => :greater_or_equal,
}.freeze
STRING_TO_TOKEN =
{
  "not"   => :not,
  "or"    => :or,
  "and"   => :and,
  "in"    => :in,
  "is"    => :is,
  "null"  => :null,
  "true"  => :true,
  "false" => :false,
}.freeze
LANGUAGE_TOKENS =
SINGLE_CHAR_TO_TOKEN.merge(DOUBLE_CHAR_TO_TOKEN).merge(STRING_TO_TOKEN).freeze
LANGUAGE_STRINGS =
LANGUAGE_TOKENS.invert.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, #logger, #logger=

Constructor Details

#initialize(text) ⇒ Tokenizer

Returns a new instance of Tokenizer.



67
68
69
70
# File 'lib/puppetdb_query/tokenizer.rb', line 67

def initialize(text)
  @text = text
  @position = 0
end

Instance Attribute Details

#positionObject (readonly)

Returns the value of attribute position.



64
65
66
# File 'lib/puppetdb_query/tokenizer.rb', line 64

def position
  @position
end

#textObject (readonly)

Returns the value of attribute text.



65
66
67
# File 'lib/puppetdb_query/tokenizer.rb', line 65

def text
  @text
end

Class Method Details

.idem(query) ⇒ Object



60
61
62
# File 'lib/puppetdb_query/tokenizer.rb', line 60

def self.idem(query)
  query(symbols(query))
end

.query(symbols) ⇒ Object



52
53
54
# File 'lib/puppetdb_query/tokenizer.rb', line 52

def self.query(symbols)
  symbols.map { |v| symbol_to_string(v) }.join(" ")
end

.symbol_to_string(s) ⇒ Object



56
57
58
# File 'lib/puppetdb_query/tokenizer.rb', line 56

def self.symbol_to_string(s)
  (LANGUAGE_STRINGS[s] || (s.is_a?(Symbol) ? s.to_s : nil) || s.inspect).to_s
end

.symbols(query) ⇒ Object



45
46
47
48
49
50
# File 'lib/puppetdb_query/tokenizer.rb', line 45

def self.symbols(query)
  r = []
  tokenizer = Tokenizer.new(query)
  r << tokenizer.next_token until tokenizer.empty?
  r
end

Instance Method Details

#eachObject



82
83
84
# File 'lib/puppetdb_query/tokenizer.rb', line 82

def each
  yield next_token until empty?
end

#empty?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/puppetdb_query/tokenizer.rb', line 78

def empty?
  position >= text.size
end

#next_tokenObject



72
73
74
75
76
# File 'lib/puppetdb_query/tokenizer.rb', line 72

def next_token
  skip_whitespace
  return nil if empty?
  read_token
end