Class: Walrat::StringParslet
- Defined in:
- lib/walrat/string_parslet.rb
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
Instance Method Summary collapse
- #eql?(other) ⇒ Boolean
-
#initialize(string) ⇒ StringParslet
constructor
A new instance of StringParslet.
- #parse(string, options = {}) ⇒ Object
Methods inherited from Parslet
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.
29 30 31 32 |
# File 'lib/walrat/string_parslet.rb', line 29 def initialize string raise ArgumentError if string.nil? self.expected_string = string end |
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
27 28 29 |
# File 'lib/walrat/string_parslet.rb', line 27 def hash @hash end |
Instance Method Details
#eql?(other) ⇒ Boolean
67 68 69 70 |
# File 'lib/walrat/string_parslet.rb', line 67 def eql?(other) other.instance_of? StringParslet and other.expected_string == @expected_string end |
#parse(string, options = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/walrat/string_parslet.rb', line 34 def parse string, = {} raise ArgumentError if string.nil? chars = StringEnumerator.new string parsed = StringResult.new parsed.start = [[:line_start], [: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 |