Class: Rattler::Parsers::Parser

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

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Runtime::ParserHelper

#select_captures

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

.parsed(*args) ⇒ Object



11
12
13
# File 'lib/rattler/parsers/parser.rb', line 11

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

Instance Method Details

#&(other) ⇒ Sequence

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:

  • (Sequence)

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



62
63
64
# File 'lib/rattler/parsers/parser.rb', line 62

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

#>>(semantic) ⇒ AttributedSequence

A new parser that tries this parser and returns the result of the semantic action if it succeeds

Parameters:

  • semantic (Parser)

    a semantic action.

Returns:

  • (AttributedSequence)

    a new parser that tries this parser and returns the result of the semantic action if it succeeds



69
70
71
# File 'lib/rattler/parsers/parser.rb', line 69

def >>(semantic)
  AttributedSequence[self, semantic]
end

#capturing?Boolean

Returns 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



17
18
19
# File 'lib/rattler/parsers/parser.rb', line 17

def capturing?
  true
end

#capturing_decidable?Boolean

true if it can be determined statically whether the parser returns parse results on success

Returns:

  • (Boolean)

    true if it can be determined statically whether the parser returns parse results on success



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

def capturing_decidable?
  true
end

#labeled?Boolean

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



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

def labeled?
  false
end

#list(sep_parser, lower_bound, upper_bound) ⇒ Repeat

Returns a new parser that matches lists with this parser.

Parameters:

  • sep_parser (Parser)

    the parser for matching the list separator

  • lower_bound (Integer)

    the minimum number of times to match

  • upper_bound (Integer)

    the maximum number of times to match

Returns:

  • (Repeat)

    a new parser that matches lists with this parser



103
104
105
# File 'lib/rattler/parsers/parser.rb', line 103

def list(sep_parser, lower_bound, upper_bound)
  ListParser[self, sep_parser, lower_bound, upper_bound]
end

#one_or_moreRepeat

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:

  • (Repeat)

    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



95
96
97
# File 'lib/rattler/parsers/parser.rb', line 95

def one_or_more
  repeat(1, nil)
end

#optionalRepeat

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

Returns:

  • (Repeat)

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



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

def optional
  repeat(0, 1)
end

#repeat(lower_bound, upper_bound) ⇒ Repeat

Returns a new parser that tries this parser repeatedly.

Parameters:

  • lower_bound (Integer)

    the minimum number of times to match

  • upper_bound (Integer)

    the maximum number of times to match

Returns:

  • (Repeat)

    a new parser that tries this parser repeatedly



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

def repeat(lower_bound, upper_bound)
  Repeat[self, lower_bound, upper_bound]
end

#semantic?Boolean

Returns true if the parser is a semantic action.

Returns:

  • (Boolean)

    true if the parser is a semantic action



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

def semantic?
  false
end

#sequence?Boolean

Returns true if the parser is a sequence.

Returns:

  • (Boolean)

    true if the parser is a sequence



43
44
45
# File 'lib/rattler/parsers/parser.rb', line 43

def sequence?
  false
end

#skipSkip

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

Returns:

  • (Skip)

    a new parser that skips over what this parser matches



108
109
110
# File 'lib/rattler/parsers/parser.rb', line 108

def skip
  Skip[self]
end

#variable_capture_count?Boolean

Returns true if the number of parse results returned by the parser varies based on the input.

Returns:

  • (Boolean)

    true if the number of parse results returned by the parser varies based on the input



23
24
25
# File 'lib/rattler/parsers/parser.rb', line 23

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



114
115
116
# File 'lib/rattler/parsers/parser.rb', line 114

def with_ws(ws)
  self
end

#zero_or_moreRepeat

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

Returns:

  • (Repeat)

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



88
89
90
# File 'lib/rattler/parsers/parser.rb', line 88

def zero_or_more
  repeat(0, nil)
end

#|(other) ⇒ Choice

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:

  • (Choice)

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



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

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