Alternative during matching. Contains a list of parslets that is tried each one in turn. Only fails if all alternatives fail.
Example:
str('a') | str('b') # matches either 'a' or 'b'
Constructs an Alternative instance using all given parslets in the order given. This is what happens if you call ‘|’ on existing parslets, like this:
str('a') | str('b')
# File lib/parslet/atoms/alternative.rb, line 18 def initialize(*alternatives) super() @alternatives = alternatives @error_msg = "Expected one of #{alternatives.inspect}" end
Call back visitors visit_alternative method. See parslet/export for an example.
# File lib/parslet/atoms/visitor.rb, line 60 def accept(visitor) visitor.visit_alternative(alternatives) end
# File lib/parslet/atoms/alternative.rb, line 47 def to_s_inner(prec) alternatives.map { |a| a.to_s(prec) }.join(' / ') end
# File lib/parslet/atoms/alternative.rb, line 33 def try(source, context, consume_all) errors = alternatives.map { |a| success, value = result = a.apply(source, context, consume_all) return result if success # Aggregate all errors value } # If we reach this point, all alternatives have failed. context.err(self, source, @error_msg, errors) end
Generated with the Darkfish Rdoc Generator 2.