Class: SrcLexer::Lexer::StringIterator
- Inherits:
-
Object
- Object
- SrcLexer::Lexer::StringIterator
- Defined in:
- lib/src_lexer.rb
Instance Method Summary collapse
- #<(index) ⇒ Object
-
#initialize(str) ⇒ StringIterator
constructor
A new instance of StringIterator.
- #is(target_string) ⇒ Object
- #is_in(target_list) ⇒ Object
- #is_white_space ⇒ Object
- #mark_clear ⇒ Object
- #mark_set ⇒ Object
- #marked? ⇒ Boolean
- #move_next ⇒ Object
- #move_to(target) ⇒ Object
- #move_to_the_end_of_the_line ⇒ Object
- #shift ⇒ Object
Constructor Details
#initialize(str) ⇒ StringIterator
Returns a new instance of StringIterator.
64 65 66 67 68 69 |
# File 'lib/src_lexer.rb', line 64 def initialize(str) @str = str @current_pos = PosInfo.new @marked_pos = PosInfo.new mark_clear() end |
Instance Method Details
#<(index) ⇒ Object
121 122 123 |
# File 'lib/src_lexer.rb', line 121 def <(index) @current_pos.index < index end |
#is(target_string) ⇒ Object
81 82 83 84 85 |
# File 'lib/src_lexer.rb', line 81 def is(target_string) return false if target_string.length.zero? end_pos = (@current_pos.index + target_string.length - 1) @str[@current_pos.index..end_pos] == target_string end |
#is_in(target_list) ⇒ Object
87 88 89 |
# File 'lib/src_lexer.rb', line 87 def is_in(target_list) target_list.find { |target| is(target) } != nil end |
#is_white_space ⇒ Object
125 126 127 |
# File 'lib/src_lexer.rb', line 125 def is_white_space /\s/.match(@str[@current_pos.index]) end |
#mark_clear ⇒ Object
71 72 73 74 75 |
# File 'lib/src_lexer.rb', line 71 def mark_clear @marked_pos.index = -1 @marked_pos.line_no = 0 @marked_pos.char_no = 0 end |
#mark_set ⇒ Object
77 78 79 |
# File 'lib/src_lexer.rb', line 77 def mark_set @marked_pos = @current_pos.clone end |
#marked? ⇒ Boolean
129 130 131 |
# File 'lib/src_lexer.rb', line 129 def marked? @marked_pos.index != -1 end |
#move_next ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/src_lexer.rb', line 91 def move_next if /\n/.match @str[@current_pos.index] @current_pos.line_no += 1 @current_pos.char_no = 1 else @current_pos.char_no += 1 end @current_pos.index += 1 end |
#move_to(target) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/src_lexer.rb', line 107 def move_to(target) char_count_to_target = (@str[@current_pos.index..-1] =~ /#{Regexp.escape(target)}/m) + target.length - 1 chopped_string = @str[@current_pos.index..@current_pos.index + char_count_to_target] @current_pos.index += char_count_to_target match = /.*\n(.*)$/m.match(chopped_string) p match[1].length if match if match @current_pos.char_no = match[1].length else @current_pos.char_no += char_count_to_target end @current_pos.line_no += chopped_string.each_char.select{|char| /\n/.match char}.length end |
#move_to_the_end_of_the_line ⇒ Object
101 102 103 104 105 |
# File 'lib/src_lexer.rb', line 101 def move_to_the_end_of_the_line char_count_to_the_end_of_the_line = (@str[@current_pos.index..-1] =~ /$/) - 1 @current_pos.index += char_count_to_the_end_of_the_line @current_pos.char_no += char_count_to_the_end_of_the_line end |
#shift ⇒ Object
133 134 135 136 137 |
# File 'lib/src_lexer.rb', line 133 def shift result = [@str[@marked_pos.index..(@current_pos.index - 1)], @marked_pos.line_no, @marked_pos.char_no] mark_clear() return result end |