forked from kschiess/parslet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence.rb
44 lines (36 loc) · 1020 Bytes
/
sequence.rb
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
33
34
35
36
37
38
39
40
41
42
43
44
# A sequence of parslets, matched from left to right. Denoted by '>>'
#
# Example:
#
# str('a') >> str('b') # matches 'a', then 'b'
#
class Parslet::Atoms::Sequence < Parslet::Atoms::Base
attr_reader :parslets
def initialize(*parslets)
super()
@parslets = parslets
@error_msgs = {
:failed => "Failed to match sequence (#{self.inspect})"
}
end
def >>(parslet) # :nodoc:
self.class.new(* @parslets+[parslet])
end
def try(source) # :nodoc:
success([:sequence]+parslets.map { |p|
# Save each parslet as potentially offending (raising an error).
@offending_parslet = p
value = p.apply(source)
return error(source, @error_msgs[:failed]) if value.error?
value.result
})
end
precedence SEQUENCE
def to_s_inner(prec) # :nodoc:
parslets.map { |p| p.to_s(prec) }.join(' ')
end
def error_tree # :nodoc:
Parslet::ErrorTree.new(self).tap { |t|
t.children << @offending_parslet.error_tree if @offending_parslet }
end
end