Class: Mustermann::RegexpBased Abstract

Inherits:
Pattern
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mustermann/regexp_based.rb

Overview

This class is abstract.

Superclass for patterns that internally compile to a regular expression.

See Also:

Direct Known Subclasses

AST::Pattern, Regular

Constant Summary

Constants included from Mustermann

DEFAULT_TYPE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Pattern

#===, #=~, #expand, #match, #named_captures, #names, new, #params, #peek, #peek_params, supported?, supported_options, #to_proc, #to_s, #to_templates, #|

Methods included from Mustermann

[], new

Constructor Details

#initialize(string, **options) ⇒ Pattern

Returns a new instance of Pattern.

Parameters:

  • string (String)

    the string representation of the pattern

  • options (Hash)

    options for fine-tuning the pattern behavior

See Also:



16
17
18
19
20
21
# File 'lib/mustermann/regexp_based.rb', line 16

def initialize(string, **options)
  super
  regexp       = compile(**options)
  @peek_regexp = /\A(#{regexp})/
  @regexp      = /\A#{regexp}\Z/
end

Instance Attribute Details

#regexpRegexp (readonly) Also known as: to_regexp

Returns regular expression equivalent to the pattern.

Returns:

  • (Regexp)

    regular expression equivalent to the pattern.



10
11
12
# File 'lib/mustermann/regexp_based.rb', line 10

def regexp
  @regexp
end

Instance Method Details

#peek_match(string) ⇒ MatchData, ...

Returns MatchData or similar object if the pattern matches.

Parameters:

  • string (String)

    The string to match against

Returns:

See Also:



34
35
36
# File 'lib/mustermann/regexp_based.rb', line 34

def peek_match(string)
  @peek_regexp.match(string)
end

#peek_size(string) ⇒ Integer?

Returns the number of characters that match.

Parameters:

  • string (String)

    The string to match against

Returns:

  • (Integer, nil)

    the number of characters that match



26
27
28
29
# File 'lib/mustermann/regexp_based.rb', line 26

def peek_size(string)
  return unless match = peek_match(string)
  match.to_s.size
end