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

Attributes inherited from Base

#label

Instance Method Summary collapse

Methods inherited from Base

#apply, #cached?, #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(*parslets) ⇒ Sequence

Returns a new instance of Sequence.



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

def initialize(*parslets)
  super()

  @parslets = parslets
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



21
22
23
# File 'lib/parslet/atoms/sequence.rb', line 21

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

#error_msgsObject



15
16
17
18
19
# File 'lib/parslet/atoms/sequence.rb', line 15

def error_msgs
  @error_msgs ||= {
    failed: "Failed to match sequence (#{inspect})"
  }
end

#to_s_inner(prec) ⇒ Object



45
46
47
# File 'lib/parslet/atoms/sequence.rb', line 45

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

#try(source, context, consume_all) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/parslet/atoms/sequence.rb', line 25

def try(source, context, consume_all)
  # Presize an array
  result = Array.new(parslets.size + 1)
  result[0] = :sequence
  
  parslets.each_with_index do |p, idx|
    child_consume_all = consume_all && (idx == parslets.size-1)
    success, value = p.apply(source, context, child_consume_all) 

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

    result[idx+1] = value
  end
  
  return succ(result)
end