Sha256: acfb8868074cbe7667c02399261abda875b5aea465fd12d6a02bb3525ac2b43f

Contents?: true

Size: 813 Bytes

Versions: 3

Compression:

Stored size: 813 Bytes

Contents

# A sequence of parslets, matched from left to right. Denoted by '>>'
#
# Example: 
#
#   str('a') >> str('b')  # matches 'a', then 'b'
#
class Parslet::Atoms::Sequence < Parslet::Atoms::Base
  attr_reader :parslets
  def initialize(*parslets)
    super()

    @parslets = parslets
    @error_msgs = {
      :failed  => "Failed to match sequence (#{self.inspect})"
    }
  end
  
  def >>(parslet)
    self.class.new(* @parslets+[parslet])
  end
  
  def try(source, context)
    succ([:sequence]+parslets.map { |p| 
      success, value = p.apply(source, context) 

      unless success
        return context.err(self, source, @error_msgs[:failed], [value]) 
      end
      
      value
    })
  end
      
  precedence SEQUENCE
  def to_s_inner(prec)
    parslets.map { |p| p.to_s(prec) }.join(' ')
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
ghazel-parslet-1.4.0.2 lib/parslet/atoms/sequence.rb
ghazel-parslet-1.4.0.1 lib/parslet/atoms/sequence.rb
parslet-1.4.0 lib/parslet/atoms/sequence.rb