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.

Author:

  • Jason Arhart

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, #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



25
26
27
# File 'lib/rattler/parsers/rule.rb', line 25

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

Instance Method Details

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

Parse using scanner and rules and on success return the result, on failure return a false value.



38
39
40
41
42
43
# File 'lib/rattler/parsers/rule.rb', line 38

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

#with_expr(new_expr) ⇒ Object



45
46
47
# File 'lib/rattler/parsers/rule.rb', line 45

def with_expr(new_expr)
  Rule[name, new_expr, attrs]
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



51
52
53
# File 'lib/rattler/parsers/rule.rb', line 51

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