Class: LogStashCompilerLSCLGrammar::LogStash::Compiler::LSCL::AST::Branch

Inherits:
Node
  • Object
show all
Defined in:
lib/logstash/compiler/lscl.rb

Constant Summary

Constants included from Helpers

Helpers::AND_METHOD, Helpers::BOOLEAN_DSL_METHOD_SIGNATURE, Helpers::NAND_METHOD, Helpers::OR_METHOD, Helpers::XOR_METHOD

Instance Method Summary collapse

Methods inherited from Node

#section_type

Methods included from Helpers

#base_id, #base_protocol, #base_source_with_metadata, #base_source_with_metadata=, #compose, #compose_for, #jdsl, jdsl, #line_and_column, #source_meta

Instance Method Details

#exprObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/logstash/compiler/lscl.rb', line 216

def expr
  # Build this stuff as s-expressions for convenience at first
  # This will turn if/elsif/else blocks into nested if/else trees

  exprs = []
  else_stack = [] # For turning if / elsif / else into nested ifs

  self.recursive_select(Plugin, If, Elsif, Else).each do |node|
    if node.is_a?(If)
      exprs << :if
      exprs << expr_cond(node)
      exprs << expr_body(node)
    elsif node.is_a?(Elsif)
      condition = expr_cond(node)
      body = expr_body(node)
      else_stack << [:if, condition, body]
    elsif node.is_a?(Else)
      body = expr_body(node)
      if else_stack.size >= 1
        else_stack.last << body
      else
        exprs << body
      end
    end
  end

  else_stack.reverse.each_cons(2) do |cons|
    later,earlier = cons
    earlier << later
  end
  exprs << else_stack.first

  # Then convert to the imperative java IR
  javaify_sexpr(exprs)
end

#expr_body(node) ⇒ Object



283
284
285
# File 'lib/logstash/compiler/lscl.rb', line 283

def expr_body(node)
  [:compose, *node.recursive_select(Plugin, Branch).map(&:expr)]
end

#expr_cond(node) ⇒ Object



279
280
281
# File 'lib/logstash/compiler/lscl.rb', line 279

def expr_cond(node)
  node.elements.find {|e| e.is_a?(Condition)}.expr
end

#javaify_sexpr(sexpr) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/logstash/compiler/lscl.rb', line 252

def javaify_sexpr(sexpr)
  return nil if sexpr.nil?

  head, tail = sexpr.first
  tail = sexpr[1..-1]

  if head == :if
    condition, t_branch, f_branch = tail

    java_t_branch = t_branch && javaify_sexpr(t_branch)
    java_f_branch = f_branch && javaify_sexpr(f_branch)

    if java_t_branch || java_f_branch
      # We use the condition as the source with metadata because it hashes correctly
      # It's hard to use the 'real' source due to the re-branching from if / elsif into if/else only
      # branches. We should come back and improve this at some point if that makes a difference
      jdsl.iIf(condition., condition, java_t_branch || jdsl.noop, java_f_branch || jdsl.noop)
    else
      jdsl.noop()
    end
  elsif head == :compose
    tail && tail.size > 0 ? compose(*tail) : jdsl.noop
  else
    raise "Unknown expression #{sexpr}!"
  end
end