Class: Rattler::Parsers::ListParser

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

Overview

ListParser matches terms matched by a term parser in a list with separators matched by a separator parser. ListParser fails unless at least #lower_bound terms are matched and stops matching at #upper_bound.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Combining

#capturing?, #capturing_decidable?, #semantic?, #with_ws

Methods inherited from Parser

#&, #>>, #capturing?, #capturing_decidable?, #labeled?, #list, #one_or_more, #optional, #repeat, #semantic?, #sequence?, #skip, #with_ws, #zero_or_more, #|

Methods included from Runtime::ParserHelper

#select_captures

Methods inherited from Util::Node

#==, #[], #attrs, #can_equal?, #child, #children, #each, #empty?, #eql?, #initialize, #inspect, #method_missing, #name, #pretty_print, #pretty_print_cycle, #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

.[](term_parser, sep_parser, lower_bound, upper_bound) ⇒ ListParser



23
24
25
26
# File 'lib/rattler/parsers/list_parser.rb', line 23

def self.[](term_parser, sep_parser, lower_bound, upper_bound)
  self.new(term_parser, sep_parser.skip,
            :lower_bound => lower_bound, :upper_bound => upper_bound)
end

.parsed(results, *_) ⇒ Object



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

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

Instance Method Details

#lower_bound?Fixnum



68
69
70
# File 'lib/rattler/parsers/list_parser.rb', line 68

def lower_bound?
  lower_bound > 0
end

#parse(scanner, rules, scope = ParserScope.empty) ⇒ Array or Boolean

Parse terms matched by the term parser in a list with separators matched by the separator parser. Return the terms in an array, or true if the term parser is not capturing?. Fails returning false unless at least #lower_bound terms are matched and stops matching at #upper_bound.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rattler/parsers/list_parser.rb', line 49

def parse(scanner, rules, scope = ParserScope.empty)
  a = []
  p = start_pos = scanner.pos
  while result = term_parser.parse(scanner, rules, scope) and
        (!upper_bound or a.size < upper_bound)
    p = scanner.pos
    a << result
    break unless sep_parser.parse(scanner, rules, scope)
  end
  if a.size >= lower_bound
    scanner.pos = p
    capturing? ? a : true
  else
    scanner.pos = start_pos
    false
  end
end

#sep_parserParser



34
35
36
# File 'lib/rattler/parsers/list_parser.rb', line 34

def sep_parser
  children[1]
end

#term_parserParser



29
30
31
# File 'lib/rattler/parsers/list_parser.rb', line 29

def term_parser
  children[0]
end

#upper_bound?Fixnum



73
74
75
# File 'lib/rattler/parsers/list_parser.rb', line 73

def upper_bound?
  not upper_bound.nil?
end

#variable_capture_count?Boolean



78
79
80
# File 'lib/rattler/parsers/list_parser.rb', line 78

def variable_capture_count?
  true
end