Class: Parslet::Atoms::Dynamic

Inherits:
Base
  • Object
show all
Defined in:
lib/parslet/atoms/dynamic.rb

Overview

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

Constant Summary

Constants included from Precedence

Precedence::ALTERNATE, Precedence::BASE, Precedence::LOOKAHEAD, Precedence::OUTER, Precedence::REPETITION, Precedence::SEQUENCE

Instance Attribute Summary collapse

Attributes inherited from Base

#label

Instance Method Summary collapse

Methods inherited from Base

#accept, #apply, #inspect, #parse, #parse_with_debug, precedence, #setup_and_apply, #to_s

Methods included from CanFlatten

#flatten, #flatten_repetition, #flatten_sequence, #foldl, #merge_fold, #warn_about_duplicate_keys

Methods included from DSL

#>>, #absent?, #as, #capture, #ignore, #maybe, #present?, #repeat, #|

Constructor Details

#initialize(block) ⇒ Dynamic

Returns a new instance of Dynamic.



13
14
15
# File 'lib/parslet/atoms/dynamic.rb', line 13

def initialize(block)
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



11
12
13
# File 'lib/parslet/atoms/dynamic.rb', line 11

def block
  @block
end

Instance Method Details

#cached?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/parslet/atoms/dynamic.rb', line 17

def cached?
  false
end

#to_s_inner(prec) ⇒ Object



28
29
30
# File 'lib/parslet/atoms/dynamic.rb', line 28

def to_s_inner(prec)
  "dynamic { ... }"
end

#try(source, context, consume_all) ⇒ Object



21
22
23
24
25
26
# File 'lib/parslet/atoms/dynamic.rb', line 21

def try(source, context, consume_all)
  result = block.call(source, context)
  
  # Result is a parslet atom.
  return result.apply(source, context, consume_all)
end