Class: TRuby::ParserCombinator::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/t_ruby/parser_combinator.rb

Overview

Base parser class

Instance Method Summary collapse

Instance Method Details

#<<(other) ⇒ Object

Skip right: parse both, keep left result



105
106
107
# File 'lib/t_ruby/parser_combinator.rb', line 105

def <<(other)
  SkipRight.new(self, other)
end

#>>(other) ⇒ Object

Sequence: run this parser, then the other



55
56
57
# File 'lib/t_ruby/parser_combinator.rb', line 55

def >>(other)
  Sequence.new(self, other)
end

#between(left, right) ⇒ Object

Between: parse between left and right delimiters



100
101
102
# File 'lib/t_ruby/parser_combinator.rb', line 100

def between(left, right)
  (left >> self << right).map { |(_, val)| val }
end

#flat_map(&block) ⇒ Object

FlatMap: transform with another parser



70
71
72
# File 'lib/t_ruby/parser_combinator.rb', line 70

def flat_map(&block)
  FlatMap.new(self, block)
end

#label(name) ⇒ Object

Label: add a descriptive label for error messages



110
111
112
# File 'lib/t_ruby/parser_combinator.rb', line 110

def label(name)
  Label.new(self, name)
end

#lookaheadObject

Lookahead: check without consuming



115
116
117
# File 'lib/t_ruby/parser_combinator.rb', line 115

def lookahead
  Lookahead.new(self)
end

#manyObject

Many: zero or more repetitions



75
76
77
# File 'lib/t_ruby/parser_combinator.rb', line 75

def many
  Many.new(self)
end

#many1Object

Many1: one or more repetitions



80
81
82
# File 'lib/t_ruby/parser_combinator.rb', line 80

def many1
  Many1.new(self)
end

#map(&block) ⇒ Object

Map: transform the result



65
66
67
# File 'lib/t_ruby/parser_combinator.rb', line 65

def map(&block)
  Map.new(self, block)
end

#not_followed_byObject

Not: succeed only if parser fails



120
121
122
# File 'lib/t_ruby/parser_combinator.rb', line 120

def not_followed_by
  NotFollowedBy.new(self)
end

#optionalObject

Optional: zero or one



85
86
87
# File 'lib/t_ruby/parser_combinator.rb', line 85

def optional
  Optional.new(self)
end

#parse(input, position = 0) ⇒ Object

Raises:



48
49
50
# File 'lib/t_ruby/parser_combinator.rb', line 48

def parse(input, position = 0)
  raise NotImplementedError
end

#sep_by(delimiter) ⇒ Object

Separated by: parse items separated by delimiter



90
91
92
# File 'lib/t_ruby/parser_combinator.rb', line 90

def sep_by(delimiter)
  SepBy.new(self, delimiter)
end

#sep_by1(delimiter) ⇒ Object

Separated by 1: at least one item



95
96
97
# File 'lib/t_ruby/parser_combinator.rb', line 95

def sep_by1(delimiter)
  SepBy1.new(self, delimiter)
end

#|(other) ⇒ Object

Alternative: try this parser, if it fails try the other



60
61
62
# File 'lib/t_ruby/parser_combinator.rb', line 60

def |(other)
  Alternative.new(self, other)
end