File: dynamic.rb

package info (click to toggle)
ruby-parslet 1.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 908 kB
  • ctags: 473
  • sloc: ruby: 5,220; makefile: 2
file content (32 lines) | stat: -rw-r--r-- 708 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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