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.

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

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



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

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



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

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

Instance Method Details

#lower_bound?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/rattler/parsers/list_parser.rb', line 70

def lower_bound?
  lower_bound > 0
end

#parse(scanner, rules, scope = {}) ⇒ Array, 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.

Returns:

  • (Array, Boolean)

    an array containing the term parser’s parse results, or true if the term parser is not capturing? or false if the parse fails.



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

def parse(scanner, rules, scope = {})
  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_parserObject



37
38
39
# File 'lib/rattler/parsers/list_parser.rb', line 37

def sep_parser
  children[1]
end

#term_parserObject



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

def term_parser
  children[0]
end

#upper_bound?Boolean

Returns:

  • (Boolean)


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

def upper_bound?
  not upper_bound.nil?
end

#variable_capture_count?Boolean

Returns:

  • (Boolean)


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

def variable_capture_count?
  true
end