Class: Rattler::Parsers::Fail

Inherits:
Parser show all
Defined in:
lib/rattler/parsers/fail.rb

Overview

Fail is a parser that always fails. It can be used to define more useful error messages.

Author:

  • Jason Arhart

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Parser

#&, #labeled?, #one_or_more, #optional, #skip, #variable_capture_count?, #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

.[](type, message) ⇒ Fail

Create a new parser that always fails with message. The type should be one of :expr, :rule or :parse, indicating to simply fail, to cause its parse rule to fail, or to cause the entire parse to fail, respectively.

Parameters:

  • type (Symbol)

    :expr, :rule or :parse

Returns:

  • (Fail)

    a new parser that always fails with message



38
39
40
# File 'lib/rattler/parsers/fail.rb', line 38

def self.[](type, message)
  self.new(:type => type, :message => message)
end

.parsed(results, *_) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/rattler/parsers/fail.rb', line 20

def self.parsed(results, *_) #:nodoc:
  keyword, message_expr = results
  message = eval(message_expr)
  case keyword
  when 'fail'       then self[:expr, message]
  when 'fail_rule'  then self[:rule, message]
  when 'fail_parse' then self[:parse, message]
  end
end

Instance Method Details

#capturing?Boolean

Always false

Returns:

  • (Boolean)

    false



56
57
58
# File 'lib/rattler/parsers/fail.rb', line 56

def capturing?
  false
end

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

Always return false. The parser code generated for this parser should use message as the failure message, and should cause its parse rule to fail if type is :rule or cause the entire parse to fail type is :parse

Returns:

  • false



50
51
52
# File 'lib/rattler/parsers/fail.rb', line 50

def parse(scanner, rules, labeled = {})
  false
end