Class: TRuby::ParserCombinator::Parser

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

Overview

Base parser class for string-based parsing

Instance Method Summary collapse

Instance Method Details

#<<(other) ⇒ Object

Skip right: parse both, keep left result



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

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

#>>(other) ⇒ Object

Sequence: run this parser, then the other



14
15
16
# File 'lib/t_ruby/parser_combinator/parser.rb', line 14

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

#between(left, right) ⇒ Object

Between: parse between left and right delimiters



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

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

#flat_map(&block) ⇒ Object

FlatMap: transform with another parser



29
30
31
# File 'lib/t_ruby/parser_combinator/parser.rb', line 29

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

#label(name) ⇒ Object

Label: add a descriptive label for error messages



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

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

#lookaheadObject

Lookahead: check without consuming



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

def lookahead
  Lookahead.new(self)
end

#manyObject

Many: zero or more repetitions



34
35
36
# File 'lib/t_ruby/parser_combinator/parser.rb', line 34

def many
  Many.new(self)
end

#many1Object

Many1: one or more repetitions



39
40
41
# File 'lib/t_ruby/parser_combinator/parser.rb', line 39

def many1
  Many1.new(self)
end

#map(&block) ⇒ Object

Map: transform the result



24
25
26
# File 'lib/t_ruby/parser_combinator/parser.rb', line 24

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

#not_followed_byObject

Not: succeed only if parser fails



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

def not_followed_by
  NotFollowedBy.new(self)
end

#optionalObject

Optional: zero or one



44
45
46
# File 'lib/t_ruby/parser_combinator/parser.rb', line 44

def optional
  Optional.new(self)
end

#parse(input, position = 0) ⇒ Object

Raises:

  • (NotImplementedError)


7
8
9
# File 'lib/t_ruby/parser_combinator/parser.rb', line 7

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

#sep_by(delimiter) ⇒ Object

Separated by: parse items separated by delimiter



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

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

#sep_by1(delimiter) ⇒ Object

Separated by 1: at least one item



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

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

#|(other) ⇒ Object

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



19
20
21
# File 'lib/t_ruby/parser_combinator/parser.rb', line 19

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