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

Returns a parser that matches lists.

Parameters:

  • term_parser (Parser)

    the parser for matching list terms

  • sep_parser (Parser)

    the parser for matching the list separator

  • lower_bound (Integer)

    the minimum number of terms to match

  • upper_bound (Integer)

    the maximum number of terms to match

Returns:



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

Returns lower_bound the minimum number of terms to match.

Returns:

  • (Fixnum)

    lower_bound the minimum number of terms to match



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.

Parameters:

  • scanner (StringScanner)

    the scanner for the current parse

  • rules (RuleSet)

    the grammar rules being used for the current parse

  • scope (ParserScope) (defaults to: ParserScope.empty)

    the scope of captured results

Returns:

  • (Array or 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.



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

Returns the parser for matching the list separator.

Returns:

  • (Parser)

    the parser for matching the list separator



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

def sep_parser
  children[1]
end

#term_parserParser

Returns the parser for matching list terms.

Returns:

  • (Parser)

    the parser for matching list terms



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

def term_parser
  children[0]
end

#upper_bound?Fixnum

Returns upper_bound the maximum number of terms to match.

Returns:

  • (Fixnum)

    upper_bound the maximum number of terms to match



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

def upper_bound?
  not upper_bound.nil?
end

#variable_capture_count?Boolean

Returns true.

Returns:

  • (Boolean)

    true



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

def variable_capture_count?
  true
end