Sha256: d477e27255d46020ab9e7b0b47a093f461533163a861d49a391ebb398bbbf8e2

Contents?: true

Size: 1014 Bytes

Versions: 9

Compression:

Stored size: 1014 Bytes

Contents

# Matches a special kind of regular expression that only ever matches one
# character at a time. Useful members of this family are: <code>character
# ranges, \\w, \\d, \\r, \\n, ...</code>
#
# Example: 
#
#   match('[a-z]')  # matches a-z
#   match('\s')     # like regexps: matches space characters
#
class Parslet::Atoms::Re < Parslet::Atoms::Base
  attr_reader :match, :re
  def initialize(match)
    super()

    @match = match.to_s
    @re    = Regexp.new(self.match, Regexp::MULTILINE)
    @error_msgs = {
      :premature  => "Premature end of input", 
      :failed     => "Failed to match #{match.inspect[1..-2]}"
    }
  end

  def try(source, context, consume_all)
    return succ(source.consume(1)) if source.matches?(@re)
    
    # No string could be read
    return context.err(self, source, @error_msgs[:premature]) \
      if source.chars_left < 1
        
    # No match
    return context.err(self, source, @error_msgs[:failed])
  end

  def to_s_inner(prec)
    match.inspect[1..-2]
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
parslet-1.8.1 lib/parslet/atoms/re.rb
parslet-1.8.0 lib/parslet/atoms/re.rb
swift-pyrite-0.1.1 vendor/bundle/ruby/2.0.0/gems/parslet-1.7.1/lib/parslet/atoms/re.rb
swift-pyrite-0.1.0 vendor/bundle/ruby/2.0.0/gems/parslet-1.7.1/lib/parslet/atoms/re.rb
parslet-1.7.1 lib/parslet/atoms/re.rb
parslet-1.7.0 lib/parslet/atoms/re.rb
parslet-1.6.2 lib/parslet/atoms/re.rb
parslet-1.6.1 lib/parslet/atoms/re.rb
parslet-1.6.0 lib/parslet/atoms/re.rb