Class: TRuby::ParserCombinator::Parser
- Inherits:
-
Object
- Object
- TRuby::ParserCombinator::Parser
- Defined in:
- lib/t_ruby/parser_combinator/parser.rb
Overview
Base parser class for string-based parsing
Direct Known Subclasses
Alternative, ChainLeft, Choice, EndOfInput, Fail, FlatMap, Label, Lazy, Literal, Lookahead, Many, Many1, Map, NotFollowedBy, Optional, Pure, Regex, Satisfy, SepBy, SepBy1, Sequence, SkipRight
Instance Method Summary collapse
-
#<<(other) ⇒ Object
Skip right: parse both, keep left result.
-
#>>(other) ⇒ Object
Sequence: run this parser, then the other.
-
#between(left, right) ⇒ Object
Between: parse between left and right delimiters.
-
#flat_map(&block) ⇒ Object
FlatMap: transform with another parser.
-
#label(name) ⇒ Object
Label: add a descriptive label for error messages.
-
#lookahead ⇒ Object
Lookahead: check without consuming.
-
#many ⇒ Object
Many: zero or more repetitions.
-
#many1 ⇒ Object
Many1: one or more repetitions.
-
#map(&block) ⇒ Object
Map: transform the result.
-
#not_followed_by ⇒ Object
Not: succeed only if parser fails.
-
#optional ⇒ Object
Optional: zero or one.
- #parse(input, position = 0) ⇒ Object
-
#sep_by(delimiter) ⇒ Object
Separated by: parse items separated by delimiter.
-
#sep_by1(delimiter) ⇒ Object
Separated by 1: at least one item.
-
#|(other) ⇒ Object
Alternative: try this parser, if it fails try the other.
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 |
#lookahead ⇒ Object
Lookahead: check without consuming
74 75 76 |
# File 'lib/t_ruby/parser_combinator/parser.rb', line 74 def lookahead Lookahead.new(self) end |
#many ⇒ Object
Many: zero or more repetitions
34 35 36 |
# File 'lib/t_ruby/parser_combinator/parser.rb', line 34 def many Many.new(self) end |
#many1 ⇒ Object
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_by ⇒ Object
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 |
#optional ⇒ Object
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
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 |