Class: Parslet::Atoms::Sequence

Inherits:
Base
  • Object
show all
Defined in:
lib/parslet/atoms/sequence.rb,
lib/parslet/atoms/visitor.rb

Overview

A sequence of parslets, matched from left to right. Denoted by ‘>>’

Example:

str('a') >> str('b')  # matches 'a', then 'b'

Constant Summary

Constants included from Precedence

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#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, #maybe, #present?, #repeat, #|

Constructor Details

#initialize(*parslets) ⇒ Sequence

Returns a new instance of Sequence.



9
10
11
12
13
14
15
16
# File 'lib/parslet/atoms/sequence.rb', line 9

def initialize(*parslets)
  super()

  @parslets = parslets
  @error_msgs = {
    :failed  => "Failed to match sequence (#{self.inspect})"
  }
end

Instance Attribute Details

#parsletsObject (readonly)

Returns the value of attribute parslets.



8
9
10
# File 'lib/parslet/atoms/sequence.rb', line 8

def parslets
  @parslets
end

Instance Method Details

#>>(parslet) ⇒ Object



18
19
20
# File 'lib/parslet/atoms/sequence.rb', line 18

def >>(parslet)
  self.class.new(* @parslets+[parslet])
end

#accept(visitor) ⇒ Object

Call back visitors #visit_sequence method. See parslet/export for an example.



42
43
44
# File 'lib/parslet/atoms/visitor.rb', line 42

def accept(visitor)
  visitor.visit_sequence(parslets)
end

#to_s_inner(prec) ⇒ Object



35
36
37
# File 'lib/parslet/atoms/sequence.rb', line 35

def to_s_inner(prec)
  parslets.map { |p| p.to_s(prec) }.join(' ')
end

#try(source, context) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/parslet/atoms/sequence.rb', line 22

def try(source, context)
  succ([:sequence]+parslets.map { |p| 
    success, value = p.apply(source, context) 

    unless success
      return context.err(self, source, @error_msgs[:failed], [value]) 
    end
    
    value
  })
end