Class: Rattler::Parsers::ListParser
- Inherits:
-
Parser
- Object
- Util::Node
- Parser
- Rattler::Parsers::ListParser
- 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
-
.[](term_parser, sep_parser, lower_bound, upper_bound) ⇒ ListParser
A parser that matches lists.
- .parsed(results, *_) ⇒ Object
Instance Method Summary collapse
-
#lower_bound? ⇒ Fixnum
Lower_bound the minimum number of terms to match.
-
#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.
-
#sep_parser ⇒ Parser
The parser for matching the list separator.
-
#term_parser ⇒ Parser
The parser for matching list terms.
-
#upper_bound? ⇒ Fixnum
Upper_bound the maximum number of terms to match.
-
#variable_capture_count? ⇒ Boolean
true
.
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
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.
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.
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_parser ⇒ Parser
Returns 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_parser ⇒ Parser
Returns 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.
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
.
78 79 80 |
# File 'lib/rattler/parsers/list_parser.rb', line 78 def variable_capture_count? true end |