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



103
104
105
# File 'lib/t_ruby/parser_combinator.rb', line 103

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

#>>(other) ⇒ Object

Sequence: run this parser, then the other



53
54
55
# File 'lib/t_ruby/parser_combinator.rb', line 53

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

#between(left, right) ⇒ Object

Between: parse between left and right delimiters



98
99
100
# File 'lib/t_ruby/parser_combinator.rb', line 98

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

#flat_map(&block) ⇒ Object

FlatMap: transform with another parser



68
69
70
# File 'lib/t_ruby/parser_combinator.rb', line 68

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

#label(name) ⇒ Object

Label: add a descriptive label for error messages



108
109
110
# File 'lib/t_ruby/parser_combinator.rb', line 108

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

#lookaheadObject

Lookahead: check without consuming



113
114
115
# File 'lib/t_ruby/parser_combinator.rb', line 113

def lookahead
  Lookahead.new(self)
end

#manyObject

Many: zero or more repetitions



73
74
75
# File 'lib/t_ruby/parser_combinator.rb', line 73

def many
  Many.new(self)
end

#many1Object

Many1: one or more repetitions



78
79
80
# File 'lib/t_ruby/parser_combinator.rb', line 78

def many1
  Many1.new(self)
end

#map(&block) ⇒ Object

Map: transform the result



63
64
65
# File 'lib/t_ruby/parser_combinator.rb', line 63

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

#not_followed_byObject

Not: succeed only if parser fails



118
119
120
# File 'lib/t_ruby/parser_combinator.rb', line 118

def not_followed_by
  NotFollowedBy.new(self)
end

#optionalObject

Optional: zero or one



83
84
85
# File 'lib/t_ruby/parser_combinator.rb', line 83

def optional
  Optional.new(self)
end

#parse(input, position = 0) ⇒ Object

Raises:



46
47
48
# File 'lib/t_ruby/parser_combinator.rb', line 46

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

#sep_by(delimiter) ⇒ Object

Separated by: parse items separated by delimiter



88
89
90
# File 'lib/t_ruby/parser_combinator.rb', line 88

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

#sep_by1(delimiter) ⇒ Object

Separated by 1: at least one item



93
94
95
# File 'lib/t_ruby/parser_combinator.rb', line 93

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

#|(other) ⇒ Object

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



58
59
60
# File 'lib/t_ruby/parser_combinator.rb', line 58

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