Class: Walrus::Grammar::StringParslet

Inherits:
Parslet
  • Object
show all
Defined in:
lib/walrus/grammar/string_parslet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Parslet

#to_parseable

Methods included from Memoizing

#check_left_recursion, #memoizing_parse

Methods included from ParsletCombining

#&, #>>, #and?, #and_predicate, #choice, #memoizing_parse, #merge, #not!, #not_predicate, #omission, #one_or_more, #optional, #repeat, #repeat_with_default, #repetition, #repetition_with_default, #sequence, #skip, #zero_or_more, #zero_or_one, #|

Constructor Details

#initialize(string) ⇒ StringParslet

Returns a new instance of StringParslet.

Raises:

  • (ArgumentError)


24
25
26
27
# File 'lib/walrus/grammar/string_parslet.rb', line 24

def initialize(string)
  raise ArgumentError if string.nil?
  self.expected_string = string
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



22
23
24
# File 'lib/walrus/grammar/string_parslet.rb', line 22

def hash
  @hash
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/walrus/grammar/string_parslet.rb', line 57

def eql?(other)
  other.instance_of? StringParslet and other.expected_string == @expected_string
end

#parse(string, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/walrus/grammar/string_parslet.rb', line 29

def parse(string, options = {})
  raise ArgumentError if string.nil?
  chars         = StringEnumerator.new(string)
  parsed        = StringResult.new
  parsed.start  = [options[:line_start], options[:column_start]]
  parsed.end    = parsed.start
  expected_string.each_char do |expected_char|
    actual_char = chars.next
    if actual_char.nil?
      raise ParseError.new('unexpected end-of-string (expected "%s") while parsing "%s"' % [ expected_char, expected_string ],
                           :line_end    => parsed.line_end,   :column_end   => parsed.column_end)
    elsif actual_char != expected_char
      raise ParseError.new('unexpected character "%s" (expected "%s") while parsing "%s"' % [ actual_char, expected_char, expected_string],
                           :line_end    => parsed.line_end,   :column_end   => parsed.column_end)
    else
      if actual_char == "\r" or (actual_char == "\n" and chars.last != "\r")  # catches Mac, Windows and UNIX end-of-line markers
        parsed.column_end = 0
        parsed.line_end   = parsed.line_end + 1
      elsif actual_char != "\n"                     # \n is ignored if it is preceded by an \r (already counted above)
        parsed.column_end = parsed.column_end + 1   # everything else gets counted
      end
      parsed << actual_char
    end
  end
  parsed.source_text = parsed.to_s.clone
  parsed
end