Class: Parslet::Atoms::Re
- 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
-
#match ⇒ Object
readonly
Returns the value of attribute match.
-
#re ⇒ Object
readonly
Returns the value of attribute re.
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
Call back visitors #visit_re method.
-
#initialize(match) ⇒ Re
constructor
A new instance of Re.
- #to_s_inner(prec) ⇒ Object
- #try(source, context) ⇒ Object
Methods inherited from Base
#apply, #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, #maybe, #present?, #repeat, #|
Constructor Details
#initialize(match) ⇒ Re
Returns a new instance of Re.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/parslet/atoms/re.rb', line 12 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 |
Instance Attribute Details
#match ⇒ Object (readonly)
Returns the value of attribute match.
11 12 13 |
# File 'lib/parslet/atoms/re.rb', line 11 def match @match end |
#re ⇒ Object (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 |
#to_s_inner(prec) ⇒ Object
34 35 36 |
# File 'lib/parslet/atoms/re.rb', line 34 def to_s_inner(prec) match.inspect[1..-2] end |
#try(source, context) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/parslet/atoms/re.rb', line 23 def try(source, context) 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 |