Sha256: 47c50a0293f4a46acb20d2d9b7363c85dbc970caa049d5754cc60c605829a0bc

Contents?: true

Size: 1.76 KB

Versions: 4

Compression:

Stored size: 1.76 KB

Contents

# File: Lookaround.rb

########################
# TODO: make it a binary expression 
########################


require_relative 'polyadic_expression'	# Access the superclass

module Regex # This module is used as a namespace
  # Lookaround is a zero-width assertion just like the start and end of line anchors.
  # The difference is that lookarounds will actually match characters, but only return the result of the match: match or no match.
  # That is why they are called "assertions". They do not consume characters from the subject,
  # but only assert whether a match is possible or not. 
  class Lookaround < PolyadicExpression
    # The "direction" of the lookaround. Can be ahead or behind. It specifies the relative position of the 
    # expression to match compared to the current 'position' in the subject text.
    attr_reader(:dir)
    
    # The kind indicates whether the assertion is positive (succeeds when there is a match) or negative
    # (assertion succeeds when there is NO match).
    attr_reader(:kind)
    
    # Constructor.
    # [assertedExpression]	A sub-expression to match.
    # [theDir]	One of the following values: [ :ahead, :behind ]
    # [theKind] One of the following values: [ :positive, :negative ]
    def initialize(assertedExpression, theDir, theKind)
      super([assertedExpression])
      @dir, @kind = theDir, theKind
    end
    
  public
    # Conversion method re-definition.
    # Purpose: Return the String representation of the captured expression.
    def to_str()
      result = children[0].to_str
      dir_syntax = (dir == :ahead) ? '' : '<'
      kind_syntax = (kind == :positive)? '=' : '!'
      result << '(?' + dir_syntax + kind_syntax + children[1].to_str + ")"
      return result
    end

  end # class
end # module

# End of file

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rley-0.6.00 examples/general/SRL/lib/regex/lookaround.rb
rley-0.5.14 examples/general/SRL/lib/regex/lookaround.rb
rley-0.5.13 examples/general/SRL/lib/regex/lookaround.rb
rley-0.5.12 examples/general/SRL/lib/regex/lookaround.rb