Class: TRuby::ParserCombinator::Satisfy

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

Overview

Parse a single character matching predicate

Instance Method Summary collapse

Methods inherited from Parser

#<<, #>>, #between, #flat_map, #label, #lookahead, #many, #many1, #map, #not_followed_by, #optional, #sep_by, #sep_by1, #|

Constructor Details

#initialize(predicate, description = "character") ⇒ Satisfy



145
146
147
148
# File 'lib/t_ruby/parser_combinator.rb', line 145

def initialize(predicate, description = "character")
  @predicate = predicate
  @description = description
end

Instance Method Details

#parse(input, position = 0) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/t_ruby/parser_combinator.rb', line 150

def parse(input, position = 0)
  if position < input.length && @predicate.call(input[position])
    ParseResult.success(input[position], input, position + 1)
  else
    ParseResult.failure("Expected #{@description}", input, position)
  end
end