Class: LogStashCompilerLSCLGrammar::LogStash::Compiler::LSCL::AST::Condition
- Inherits:
-
Node
- Object
- Treetop::Runtime::SyntaxNode
- Node
- LogStashCompilerLSCLGrammar::LogStash::Compiler::LSCL::AST::Condition
show all
- Includes:
- Helpers
- Defined in:
- lib/logstash/compiler/lscl.rb
Constant Summary
Constants included
from Helpers
Helpers::AND_METHOD, Helpers::OR_METHOD
Instance Method Summary
collapse
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
Methods inherited from Node
#section_type
Instance Method Details
#expr ⇒ Object
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
# File 'lib/logstash/compiler/lscl.rb', line 293
def expr
first_element = elements.first
rest_elements = elements.size > 1 ? elements[1].recursive_select(BooleanOperator, Expression, SelectorElement) : []
all_elements = [first_element, *rest_elements]
if all_elements.size == 1
elem = all_elements.first
if elem.is_a?(Selector)
eventValue = elem.recursive_select(SelectorElement).first.expr
jdsl.eTruthy(source_meta, eventValue)
elsif elem.is_a?(RegexpExpression)
elem.expr
else
join_conditions(all_elements)
end
else
join_conditions(all_elements)
end
end
|
#jconvert(sexpr) ⇒ Object
Converts an sexpr of :and or :or to the java imperative IR
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
# File 'lib/logstash/compiler/lscl.rb', line 327
def jconvert(sexpr)
raise "jconvert cannot handle nils!" if sexpr.nil?
if sexpr.java_kind_of?(Java::OrgLogstashConfigIrExpression::Expression)
return sexpr
end
op, left, right = sexpr
left_c = jconvert(left)
right_c = jconvert(right)
case op
when :and
return jdsl.eAnd(left, right);
when :or
return jdsl.eOr(left, right);
else
raise "Unknown op #{jop}"
end
end
|
#join_conditions(all_elements) ⇒ Object
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
# File 'lib/logstash/compiler/lscl.rb', line 349
def join_conditions(all_elements)
out = []
operators = []
all_elements.each do |e|
e_exp = e.expr
if e.is_a?(BooleanOperator)
if operators.last && precedence(operators.last) > precedence(e_exp)
out << operators.pop
end
operators << e_exp
else
out << e_exp
end
end
operators.reverse.each {|o| out << o}
stack = []
expr = []
out.each do |e|
if e.is_a?(Symbol)
rval, lval = stack.pop, stack.pop
stack << jconvert([e, lval, rval])
elsif e.nil?
raise "Nil expr encountered! This should not happen!"
else
stack << e
end
end
stack_to_expr(stack)
end
|
#precedence(op) ⇒ Object
314
315
316
317
318
319
320
321
322
323
324
|
# File 'lib/logstash/compiler/lscl.rb', line 314
def precedence(op)
case op
when AND_METHOD
2
when OR_METHOD
1
else
raise ArgumentError, "Unexpected operator #{op}"
end
end
|
#stack_to_expr(stack) ⇒ Object
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
# File 'lib/logstash/compiler/lscl.rb', line 384
def stack_to_expr(stack)
raise "Got an empty stack! This should not happen!" if stack.empty?
stack = stack.reverse
working_stack = []
while elem = stack.pop
if elem.is_a?(::Method)
right, left = working_stack.pop, working_stack.pop
working_stack << elem.call(left, right)
else
working_stack << elem
end
end
raise "Invariant violated! Stack size > 1" if working_stack.size > 1
working_stack.first
end
|