Class: Norikra::Query::ASTLibFunctionNode

Inherits:
ASTNode
  • Object
show all
Defined in:
lib/norikra/query/ast.rb

Overview

LIB_FUNCTION

Instance Attribute Summary

Attributes inherited from ASTNode

#children, #name

Instance Method Summary collapse

Methods inherited from ASTNode

#child, #find, #initialize, #listup, #to_a

Constructor Details

This class inherits a constructor from Norikra::Query::ASTNode

Instance Method Details

#fields(default_target = nil, known_targets_aliases = []) ⇒ Object



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
233
234
235
236
# File 'lib/norikra/query/ast.rb', line 201

def fields(default_target=nil, known_targets_aliases=[])
  if @children.size <= 2
    # single function like 'now()', function-name and "("
    []

  elsif @children[1].nodetype?(:prop, :lib, :subquery)
    # first element should be func name if second element is property, library call or subqueries
    self.listup('EVENT_PROP_EXPR').map{|c| c.fields(default_target, known_targets_aliases)}.reduce(&:+) || []

  elsif @children[1].name =~ /^(-)?\d+(\.\d+)$/ || @children[1].name =~ /^'[^']*'$/ || @children[1].name =~ /^"[^"]*"$/
    # first element should be func name if secod element is number/string literal
    self.listup('EVENT_PROP_EXPR').map{|c| c.fields(default_target, known_targets_aliases)}.reduce(&:+) || []

  elsif Norikra::Query.imported_java_class?(@children[0].name)
    # Java imported class name (ex: 'Math.abs(-1)')
    self.listup('EVENT_PROP_EXPR').map{|c| c.fields(default_target, known_targets_aliases)}.reduce(&:+) || []

  else
    # first element may be property
    #  * simple 'fieldname.funcname()'
    #  * fully qualified 'target.fieldname.funcname()'
    #  * simple/fully-qualified container field access 'fieldname.key.$0.funcname()' or 'target.fieldname.$1.funcname()'
    target,fieldname = if @children[0].name.include?('.')
                         parts = @children[0].name.split('.')
                         if known_targets_aliases.include?(parts[0])
                           [ parts[0], parts[1..-1].join(".") ]
                         else
                           [ default_target, @children[0].name ]
                         end
                       else
                         [default_target,@children[0].name]
                       end
    children_list = self.listup('EVENT_PROP_EXPR').map{|c| c.fields(default_target, known_targets_aliases)}.reduce(&:+) || []
    [{:f => fieldname, :t => target}] + children_list
  end
end

#nodetype?(*sym) ⇒ Boolean

“max(size)” => [“LIB_FUNCTION”, “max”, [“EVENT_PROP_EXPR”, [“EVENT_PROP_SIMPLE”, “size”]], “(”]

Returns:

  • (Boolean)


197
198
199
# File 'lib/norikra/query/ast.rb', line 197

def nodetype?(*sym)
  sym.include?(:lib) || sym.include?(:libfunc)
end