Class: KQL::Tokenizer

Inherits:
Object
  • Object
show all
Defined in:
lib/kql/tokenizer.rb

Defined Under Namespace

Classes: Error, Token

Constant Summary collapse

SYMBOLS =
{
  '(' => :LPAREN,
  ')' => :RPAREN,
  '[' => :LBRACKET,
  ']' => :RBRACKET,
  ',' => :COMMA
}
WHITESPACE =
["\u0009", "\u0020", "\u00A0", "\u1680",
"\u2000", "\u2001", "\u2002", "\u2003",
"\u2004", "\u2005", "\u2006", "\u2007",
"\u2008", "\u2009", "\u200A", "\u202F",
"\u205F", "\u3000", ' ']
NEWLINES =
["\u000A", "\u0085", "\u000C", "\u2028", "\u2029"]
NON_IDENTIFIER_CHARS =
Regexp.escape "#{SYMBOLS.keys.join('')}()/\\<>[]\","
IDENTIFIER_CHARS =
/[^#{NON_IDENTIFIER_CHARS}\x0-\x20]/
INITIAL_IDENTIFIER_CHARS =
/[^#{NON_IDENTIFIER_CHARS}0-9\x0-\x20]/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, start = 0) ⇒ Tokenizer

Returns a new instance of Tokenizer.



61
62
63
64
65
66
67
68
69
# File 'lib/kql/tokenizer.rb', line 61

def initialize(str, start = 0)
  @str = str
  @context = nil
  @index = start
  @buffer = ''
  @previous_context = nil
  @line = 1
  @column = 1
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



39
40
41
# File 'lib/kql/tokenizer.rb', line 39

def index
  @index
end

Instance Method Details

#next_tokenObject



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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/kql/tokenizer.rb', line 71

def next_token
  @context = nil
  @previous_context = nil
  @line_at_start = @line
  @column_at_start = @column
  loop do
    c = @str[@index]
    n = @str[@index + 1]
    case @context
    when nil
      case c
      when '"'
        self.context = :string
        @buffer = ''
        traverse(1)
      when /[0-9\-]/
        self.context = :number
        traverse(1)
        @buffer = c
      when '='
        if n == '>'
          return token(:MAP, '=>').tap { traverse(2) }
        else
          return token(:EQUALS, c).tap { traverse(1) }
        end
      when '>'
        if n == '='
          return token(:GTE, '>=').tap { traverse(2) }
        else
          return token(:GT, c).tap { traverse(1) }
        end
      when '<'
        if n == '='
          return token(:LTE, '<=').tap { traverse(2) }
        else
          return token(:LT, c).tap { traverse(1) }
        end
      when '|'
        if n == '|'
          return token(:OR, '||').tap { traverse(2) }
        else
          self.context = :ident
          @buffer = c
          traverse(1)
        end
      when '^'
        if n == '='
          return token(:STARTS_WITH, '^=').tap { traverse(2) }
        else
          self.context = :ident
          @buffer = c
          traverse(1)
        end
      when '$'
        if n == '='
          return token(:ENDS_WITH, '$=').tap { traverse(2) }
        else
          self.context = :ident
          @buffer = c
          traverse(1)
        end
      when '*'
        if n == '='
          return token(:INCLUDES, '*=').tap { traverse(2) }
        else
          self.context = :ident
          @buffer = c
          traverse(1)
        end
      when '+'
        case n
        when /[0-9]/
          self.context = :number
          traverse(1)
          @buffer = c
        when IDENTIFIER_CHARS
          self.context = :ident
          @buffer = c
          traverse(1)
        else
          return token(:PLUS, '+').tap { traverse(1) }
        end
      when '~'
        case n
        when IDENTIFIER_CHARS
          self.context = :ident
          @buffer = c
          traverse(1)
        else
          return token(:TILDE, '~').tap { traverse(1) }
        end
      when '!'
        case n
        when '='
          return token(:NOT_EQUALS, '!=').tap { traverse(2) }
        else
          self.context = :ident
          @buffer = c
          traverse(1)
        end
      when *SYMBOLS.keys
        return token(SYMBOLS[c], c).tap { traverse(1) }
      when *WHITESPACE
        traverse(1)
      when *NEWLINES
        traverse(1)
        new_line
      when INITIAL_IDENTIFIER_CHARS
        self.context = :ident
        @buffer = c
        traverse(1)
      when nil
        return [false, nil]
      else
        raise_error "Unexpected `#{c}'"
      end
    when :ident
      case c
      when IDENTIFIER_CHARS
        traverse(1)
        @buffer += c
      else
        case @buffer
        when 'true'  then return token(:TRUE, true)
        when 'false' then return token(:FALSE, false)
        when 'null'  then return token(:NULL, nil)
        when 'top', 'name', 'tag', 'props', 'values'
          if c == '(' && n == ')'
            return token(@buffer.upcase.to_sym, "#{@buffer}()").tap { traverse(2) }
          end
        when 'val'
          return token(:VAL, @buffer) if c == '('
        when 'prop'
          return token(:PROP, @buffer) if c == '('
        end
        return token(:IDENT, @buffer)
      end
    when :string
      case c
      when '\\'
        @buffer += c
        @buffer += @str[@index + 1]
        traverse(2)
      when '"'
        return token(:STRING, convert_escapes(@buffer)).tap { traverse(1) }
      when nil
        raise_error 'Unterminated string literal'
      else
        @buffer += c
        traverse(1)
      end
    when :number
      case c
      when /[0-9.\-+_eE]/
        traverse(1)
        @buffer += c
      else
        return parse_number(@buffer)
      end
    end
  end
end