Class: Walrus::Grammar::StringParslet
- Defined in:
- lib/walrus/grammar/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.
18 19 20 21 |
# File 'lib/walrus/grammar/string_parslet.rb', line 18 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.
16 17 18 |
# File 'lib/walrus/grammar/string_parslet.rb', line 16 def hash @hash end |
Instance Method Details
#eql?(other) ⇒ Boolean
51 52 53 |
# File 'lib/walrus/grammar/string_parslet.rb', line 51 def eql?(other) other.instance_of? StringParslet and other.expected_string == @expected_string end |
#parse(string, options = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/walrus/grammar/string_parslet.rb', line 23 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 |