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.

Author:

  • Jason Arhart

Direct Known Subclasses

List0, List1

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) ⇒ Object

Create a new list parser that matches terms with term_parser and separators with sep_parser



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

def self.[](term_parser, sep_parser)
  self.new(term_parser, sep_parser.skip)
end

.parsed(results, *_) ⇒ Object



21
22
23
# File 'lib/rattler/parsers/list_parser.rb', line 21

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

Instance Method Details

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

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?.

Returns:

  • (Array, true)

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



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rattler/parsers/list_parser.rb', line 47

def parse(scanner, rules, scope = {})
  a = []
  p = scanner.pos
  while result = term_parser.parse(scanner, rules, scope)
    p = scanner.pos
    a << result
    break unless sep_parser.parse(scanner, rules, scope)
  end
  scanner.pos = p
  (capturing? ? a : true) if enough? a
end

#sep_parserObject



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

def sep_parser
  children[1]
end

#term_parserObject



31
32
33
# File 'lib/rattler/parsers/list_parser.rb', line 31

def term_parser
  children[0]
end

#variable_capture_count?Boolean

Returns:

  • (Boolean)


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

def variable_capture_count?
  true
end