Class: Rattler::Parsers::Rule

Inherits:
Util::Node show all
Defined in:
lib/rattler/parsers/rule.rb

Overview

Rule is a binding for a parser that can be referenced by name from another rule.

Class Method Summary collapse

Instance Method Summary collapse

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

.[](name, parser, attrs = {}) ⇒ Rule

Create a new Rule.

Parameters:

  • name (Symbol, String)

    the name of the new rule

  • parser (Parser)

    the parser that defines the rule body

Returns:

  • (Rule)

    a new parse rule



15
16
17
# File 'lib/rattler/parsers/rule.rb', line 15

def self.[](name, parser, attrs={})
  self.new(parser, attrs.merge(:name => name.to_sym))
end

Instance Method Details

#parse(scanner, rules, scope = ParserScope.empty) ⇒ Object

Parse using the rule body and on success return the result, on failure return a false value.

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:

  • the matched string, or nil



26
27
28
29
30
31
# File 'lib/rattler/parsers/rule.rb', line 26

def parse(scanner, rules, scope = ParserScope.empty)
  catch(:rule_failed) do
    return expr.parse(scanner, rules, scope)
  end
  false
end

#with_ws(ws) ⇒ Parser

Returns a new parser that uses ws to skip whitespace.

Parameters:

  • ws (Parser)

    the parser used to skip whitespace

Returns:

  • (Parser)

    a new parser that uses ws to skip whitespace



36
37
38
# File 'lib/rattler/parsers/rule.rb', line 36

def with_ws(ws)
  self.with_expr expr.with_ws(ws)
end