Sha256: cc4070858ec56895b2883d9031e9a9b848590bcd74b369683e998102085ec874

Contents?: true

Size: 708 Bytes

Versions: 12

Compression:

Stored size: 708 Bytes

Contents

# Evaluates a block at parse time. The result from the block must be a parser
# (something which implements #apply). In the first case, the parser will then
# be applied to the input, creating the result. 
#
# Dynamic parses are never cached. 
#
# Example: 
#   dynamic { rand < 0.5 ? str('a') : str('b') }
#
class Parslet::Atoms::Dynamic < Parslet::Atoms::Base
  attr_reader :block
  
  def initialize(block)
    @block = block
  end
  
  def cached?
    false
  end
  
  def try(source, context, consume_all)
    result = block.call(source, context)
    
    # Result is a parslet atom.
    return result.apply(source, context, consume_all)
  end
  
  def to_s_inner(prec)
    "dynamic { ... }"
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

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