Class: Rattler::Parsers::Optional

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

Overview

Optional decorates a parser to match the same but always succeed.

Author:

  • Jason Arhart

Instance Method Summary collapse

Methods included from Combining

#capturing?, #with_ws

Methods inherited from Parser

#&, #capturing?, #labeled?, #one_or_more, #optional, parsed, #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

Instance Method Details

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

Parse using the decorated parser and always succeed. If the decorated parser is capturing? return the result in an array, or an empty array if the wrapped parser fails. If the decorated parser is not capturing? always return true.

Returns:

  • (Array, true)

    an array containing the decorated parser’s parse results if it matches or an empty array if not, or always true if the decorated parser is not capturing?



29
30
31
32
33
34
35
# File 'lib/rattler/parsers/optional.rb', line 29

def parse(scanner, rules, scope = {})
  if result = child.parse(scanner, rules, scope)
    capturing? ? [result] : true
  else
    capturing? ? [] : true
  end
end

#variable_capture_count?Boolean

Returns:

  • (Boolean)


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

def variable_capture_count?
  true
end