Class: TRuby::ParserCombinator::Regex
- Defined in:
- lib/t_ruby/parser_combinator.rb
Overview
Parse using regex
Instance Method Summary collapse
-
#initialize(pattern, description = nil) ⇒ Regex
constructor
A new instance of Regex.
- #parse(input, position = 0) ⇒ Object
Methods inherited from Parser
#<<, #>>, #between, #flat_map, #label, #lookahead, #many, #many1, #map, #not_followed_by, #optional, #sep_by, #sep_by1, #|
Constructor Details
#initialize(pattern, description = nil) ⇒ Regex
Returns a new instance of Regex.
161 162 163 164 |
# File 'lib/t_ruby/parser_combinator.rb', line 161 def initialize(pattern, description = nil) @pattern = pattern.is_a?(Regexp) ? pattern : Regexp.new("^#{pattern}") @description = description || @pattern.inspect end |
Instance Method Details
#parse(input, position = 0) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/t_ruby/parser_combinator.rb', line 166 def parse(input, position = 0) remaining = input[position..] match = @pattern.match(remaining) if match && match.begin(0) == 0 matched = match[0] ParseResult.success(matched, input, position + matched.length) else ParseResult.failure("Expected #{@description}", input, position) end end |