Class: Parslet::Atoms::Re

Inherits:
Base
  • Object
show all
Defined in:
lib/parslet/atoms/re.rb,
lib/parslet/atoms/visitor.rb

Overview

Matches a special kind of regular expression that only ever matches one character at a time. Useful members of this family are: character ranges, \w, \d, \r, \n, ...

Example:

match('[a-z]')  # matches a-z
match('\s')     # like regexps: matches space characters

Constant Summary

Constants included from Precedence

Precedence::ALTERNATE, Precedence::BASE, Precedence::LOOKAHEAD, Precedence::OUTER, Precedence::REPETITION, Precedence::SEQUENCE

Instance Attribute Summary collapse

Attributes inherited from Base

#label

Instance Method Summary collapse

Methods inherited from Base

#apply, #cached?, #inspect, #parse, #parse_with_debug, precedence, #setup_and_apply, #to_s

Methods included from CanFlatten

#flatten, #flatten_repetition, #flatten_sequence, #foldl, #merge_fold, #warn_about_duplicate_keys

Methods included from DSL

#>>, #absent?, #as, #capture, #ignore, #maybe, #present?, #repeat, #|

Constructor Details

#initialize(match) ⇒ Re

Returns a new instance of Re.



12
13
14
15
16
17
# File 'lib/parslet/atoms/re.rb', line 12

def initialize(match)
  super()

  @match = match.to_s
  @re    = Regexp.new(self.match, Regexp::MULTILINE)
end

Instance Attribute Details

#matchObject (readonly)

Returns the value of attribute match.



11
12
13
# File 'lib/parslet/atoms/re.rb', line 11

def match
  @match
end

#reObject (readonly)

Returns the value of attribute re.



11
12
13
# File 'lib/parslet/atoms/re.rb', line 11

def re
  @re
end

Instance Method Details

#accept(visitor) ⇒ Object

Call back visitors #visit_re method. See parslet/export for an example.



77
78
79
# File 'lib/parslet/atoms/visitor.rb', line 77

def accept(visitor)
  visitor.visit_re(match)
end

#error_msgsObject



19
20
21
22
23
24
# File 'lib/parslet/atoms/re.rb', line 19

def error_msgs
  @error_msgs ||= {
    premature: 'Premature end of input',
    failed: "Failed to match #{match.inspect[1..-2]}"
  }
end

#to_s_inner(prec) ⇒ Object



37
38
39
# File 'lib/parslet/atoms/re.rb', line 37

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

#try(source, context, consume_all) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/parslet/atoms/re.rb', line 26

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