Class: PuppetDBQuery::Tokenizer

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

Overview

tokenize puppetdb queries FIXME: distinguish between language tokens and other tokens 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



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

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

Instance Attribute Details

#positionObject (readonly)

Returns the value of attribute position.



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

def position
  @position
end

#textObject (readonly)

Returns the value of attribute text.



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

def text
  @text
end

Class Method Details

.idem(query) ⇒ Object



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

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

.query(symbols) ⇒ Object



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

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

.symbol_to_string(s) ⇒ Object



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

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



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

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

Instance Method Details

#eachObject



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

def each
  yield next_token until empty?
end

#empty?Boolean



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

def empty?
  position >= text.size
end

#next_tokenObject



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

def next_token
  skip_whitespace
  return nil if empty?
  read_token
end