Class: Rattler::Parsers::Repeat

Inherits:
Parser show all
Includes:
Combining
Defined in:
lib/rattler/parsers/repeat.rb

Overview

Repeat decorates a parser with repeat counts to match repeatedly (up to the upper bound, if given) and succeed if the decorated parser succeeds at least as many times as specified by the lower bound.

Author:

  • Jason Arhart

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Combining

#capturing?, #with_ws

Methods inherited from Parser

#&, #capturing?, #labeled?, #one_or_more, #optional, #skip, #with_ws, #zero_or_more, #|

Methods inherited from Util::Node

#==, #[], #attrs, #can_equal?, #child, #children, #each, #empty?, #eql?, #initialize, #inspect, #method_missing, #name, #respond_to?, #same_contents?, #to_graphviz, #with_attrs, #with_attrs!, #with_children

Constructor Details

This class inherits a constructor from Rattler::Util::Node

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rattler::Util::Node

Class Method Details

.[](parser, lower_bound, upper_bound) ⇒ Object



14
15
16
# File 'lib/rattler/parsers/repeat.rb', line 14

def self.[](parser, lower_bound, upper_bound)
  self.new(parser, :lower_bound => lower_bound, :upper_bound => upper_bound)
end

.parsed(results, *_) ⇒ Object



19
20
21
22
# File 'lib/rattler/parsers/repeat.rb', line 19

def self.parsed(results, *_) #:nodoc
  parser, bounds = results
  self[parser, *bounds]
end

Instance Method Details

#lower_bound?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/rattler/parsers/repeat.rb', line 63

def lower_bound?
  lower_bound > 0
end

#one_or_more?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/rattler/parsers/repeat.rb', line 55

def one_or_more?
  lower_bound == 1 and not upper_bound?
end

#optional?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/rattler/parsers/repeat.rb', line 59

def optional?
  lower_bound == 0 and upper_bound == 1
end

#parse(scanner, rules, scope = {}) ⇒ Array, Boolean

Parse using the wrapped parser repeatedly until it fails or the upper bound is reached. If the wrapped parser succeeds at least as many times as specified by the lower bound, return the results in an array, or true if the wrapped parser is not capturing?. Return false if the lower bound is not reached.

Returns:

  • (Array, Boolean)

    an array containing the decorated parser’s parse results, or true if the decorated parser is not capturing?, or false if the decorated parser does not succeed up to the lower bound.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rattler/parsers/repeat.rb', line 36

def parse(scanner, rules, scope = {})
  a = []
  start_pos = scanner.pos
  while result = child.parse(scanner, rules, scope)
    a << result
    break if upper_bound? and a.size >= upper_bound
  end
  if a.size >= lower_bound
    capturing? ? a : true
  else
    scanner.pos = start_pos
    false
  end
end

#upper_bound?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/rattler/parsers/repeat.rb', line 67

def upper_bound?
  not upper_bound.nil?
end

#variable_capture_count?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/rattler/parsers/repeat.rb', line 71

def variable_capture_count?
  true
end

#zero_or_more?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/rattler/parsers/repeat.rb', line 51

def zero_or_more?
  lower_bound == 0 and not upper_bound?
end