class Parslet::Atoms::Capture

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]) }

Attributes

name[R]
parslet[R]

Public Class Methods

new(parslet, name) click to toggle source
# File lib/parslet/atoms/capture.rb, line 17
def initialize(parslet, name)
  super()

  @parslet, @name = parslet, name
end

Public Instance Methods

apply(source, context, consume_all) click to toggle source
# File lib/parslet/atoms/capture.rb, line 23
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
to_s_inner(prec) click to toggle source
# File lib/parslet/atoms/capture.rb, line 34
def to_s_inner(prec)
  "(#{name.inspect} = #{parslet.to_s(prec)})"
end