Sha256: e605790b9b1ae90f195b706878e8db566b04bf660c3ca7c7ed23bc6a27c5b19d

Contents?: true

Size: 960 Bytes

Versions: 12

Compression:

Stored size: 960 Bytes

Contents

# Stores the result of matching an atom against input in the #captures in 
# parse context. Doing so will allow you to pull parts of the ongoing parse
# out later and use them to match other pieces of input. 
#
# Example: 
#   # After this, context.captures[:an_a] returns 'a'
#   str('a').capture(:an_a)
#
#   # Capture and use of the capture: (matches either 'aa' or 'bb')
#   match['ab'].capture(:first) >> 
#     dynamic { |src, ctx| str(ctx.captures[:first]) }
#   
class Parslet::Atoms::Capture < Parslet::Atoms::Base
  attr_reader :parslet, :name

  def initialize(parslet, name)
    super()

    @parslet, @name = parslet, name
  end

  def apply(source, context, consume_all)
    success, value = result = parslet.apply(source, context, consume_all)

    if success
      context.captures[name.to_sym] = 
        flatten(value)
    end
    
    return result
  end
  
  def to_s_inner(prec)
    "(#{name.inspect} = #{parslet.to_s(prec)})"
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
parslet-2.0.0 lib/parslet/atoms/capture.rb
parslet-1.8.2 lib/parslet/atoms/capture.rb
parslet-1.8.1 lib/parslet/atoms/capture.rb
parslet-1.8.0 lib/parslet/atoms/capture.rb
swift-pyrite-0.1.1 vendor/bundle/ruby/2.0.0/gems/parslet-1.7.1/lib/parslet/atoms/capture.rb
swift-pyrite-0.1.0 vendor/bundle/ruby/2.0.0/gems/parslet-1.7.1/lib/parslet/atoms/capture.rb
parslet-1.7.1 lib/parslet/atoms/capture.rb
parslet-1.7.0 lib/parslet/atoms/capture.rb
parslet-1.6.2 lib/parslet/atoms/capture.rb
parslet-1.6.1 lib/parslet/atoms/capture.rb
parslet-1.6.0 lib/parslet/atoms/capture.rb
parslet-1.5.0 lib/parslet/atoms/capture.rb