Class: Rattler::Parsers::Parser

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

Overview

Parser is the base class for all of Rattler’s combinator parser types.

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

.parsed(*args) ⇒ Object



19
20
21
# File 'lib/rattler/parsers/parser.rb', line 19

def self.parsed(*args) #:nodoc:
  self[*args]
end

Instance Method Details

#&(other) ⇒ Object

Returns a new parser that tries both this parser and other and fails unless both parse in sequence.

Parameters:

  • other (Parser)

    the next parser to try if this parser succeeds.

Returns:

  • a new parser that tries both this parser and other and fails unless both parse in sequence



54
55
56
# File 'lib/rattler/parsers/parser.rb', line 54

def &(other)
  Sequence[self, other]
end

#capturing?Boolean

Return true if the parser returns parse results on success, or false if the parser simply returns true on success.

Returns:

  • (Boolean)

    true if the parser returns parse results on success, or false if the parser simply returns true on success



28
29
30
# File 'lib/rattler/parsers/parser.rb', line 28

def capturing?
  true
end

#labeled?Boolean

Return true if the parser associates a label with parse results. Only instances of Label should return true.

Returns:

  • (Boolean)

    true if the parser associates a label with parse results



40
41
42
# File 'lib/rattler/parsers/parser.rb', line 40

def labeled?
  false
end

#one_or_moreObject

Returns a new parser that tries this parser until it fails and returns all of the results if it succeeded at least once and fails otherwise.

Returns:

  • a new parser that tries this parser until it fails and returns all of the results if it succeeded at least once and fails otherwise



72
73
74
# File 'lib/rattler/parsers/parser.rb', line 72

def one_or_more
  OneOrMore[self]
end

#optionalObject

Returns a new parser that tries this parser but returns true if it fails.

Returns:

  • a new parser that tries this parser but returns true if it fails



60
61
62
# File 'lib/rattler/parsers/parser.rb', line 60

def optional
  Optional[self]
end

#skipObject

Returns a new parser that skips over what this parser matches.

Returns:

  • a new parser that skips over what this parser matches



77
78
79
# File 'lib/rattler/parsers/parser.rb', line 77

def skip
  Skip[self]
end

#variable_capture_count?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/rattler/parsers/parser.rb', line 32

def variable_capture_count?
  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



83
84
85
# File 'lib/rattler/parsers/parser.rb', line 83

def with_ws(ws)
  self
end

#zero_or_moreObject

Returns a new parser that tries this parser until it fails and returns all of the results.

Returns:

  • a new parser that tries this parser until it fails and returns all of the results



66
67
68
# File 'lib/rattler/parsers/parser.rb', line 66

def zero_or_more
  ZeroOrMore[self]
end

#|(other) ⇒ Object

Returns a new parser that tries this parser first and if it fails tries other.

Parameters:

  • other (Parser)

    the parser to try if this parser fails.

Returns:

  • a new parser that tries this parser first and if it fails tries other



47
48
49
# File 'lib/rattler/parsers/parser.rb', line 47

def |(other)
  Choice[self, other]
end