Class: Autoproj::RosConditionParser

Inherits:
Parslet::Parser
  • Object
show all
Defined in:
lib/autoproj/ros_condition_parser.rb

Overview

Parses a conditional expression Syntax and rules as defined in www.ros.org/reps/rep-0149.html#id20

Defined Under Namespace

Classes: Transform

Constant Summary collapse

RESERVED =
%w[and or].freeze

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ RosConditionParser

Returns a new instance of RosConditionParser.



11
12
13
14
# File 'lib/autoproj/ros_condition_parser.rb', line 11

def initialize(context)
    @context = context
    super()
end

Instance Method Details

#any_of(strings) ⇒ Object



37
38
39
40
# File 'lib/autoproj/ros_condition_parser.rb', line 37

def any_of(strings)
    strings = strings.dup
    strings.reduce(str(strings.shift)) { |acc, s| acc | str(s) }
end

#chain(lhs, operator, operation) ⇒ Object



48
49
50
# File 'lib/autoproj/ros_condition_parser.rb', line 48

def chain(lhs, operator, operation)
    (lhs.as(:lhs) >> operator >> operation.as(:rhs)) | lhs
end

#evaluate(expr) ⇒ Object



31
32
33
34
35
# File 'lib/autoproj/ros_condition_parser.rb', line 31

def evaluate(expr)
    Transform.new.apply(parse(expr.strip), expander: method(:expand))
rescue Parslet::ParseFailed => e
    raise Autoproj::ConfigError, e.message
end

#expand(var) ⇒ Object



16
17
18
19
20
# File 'lib/autoproj/ros_condition_parser.rb', line 16

def expand(var)
    Autoproj.expand(var, @context)
rescue StandardError
    ""
end

#quoted_literal(quote) ⇒ Object



42
43
44
45
46
# File 'lib/autoproj/ros_condition_parser.rb', line 42

def quoted_literal(quote)
    str(quote) >> (
        str(quote).absent? >> any
    ).repeat.maybe.as(:literal) >> str(quote)
end