Class: StringScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/parser/parser.rb

Overview

We have to extend StringScanner a little bit to fit our needs.

Direct Known Subclasses

Parser::CommentParser

Instance Method Summary collapse

Instance Method Details

#intelligent_skip_until(pattern) ⇒ Object

skips content within comments, strings and regularexpressions



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/parser/parser.rb', line 220

def intelligent_skip_until(pattern)

  self.skip_escaping_until(/#{pattern}|#{Parser::NON_CODE_PATTERNS.keys.join('|')}/)

  found = self.matched
  
  raise end_of_string_error(pattern) if self.matched.nil?
  
  return if found.match pattern
  
  Parser::NON_CODE_PATTERNS.each do |start_pattern, end_pattern|    
    if found.match start_pattern
      self.skip_escaping_until end_pattern
      return self.intelligent_skip_until pattern
    end    
  end
end

#save_scannedObject



238
239
240
241
242
243
# File 'lib/parser/parser.rb', line 238

def save_scanned
  pos_start = self.pos #- 1 # fixes missing first char
  yield
  pos_end = self.pos
  Range.new(pos_start, pos_end)
end

#scan_until_ahead(pattern) ⇒ String

returns the string until ‘pattern` matches, then consums `pattern`

Examples:

scanner = StringScanner.new("hello     world")
scanner.scan_until_ahead(/\s+/) #=> "hello"
scanner.pos #=> 5

Parameters:

  • pattern (Regexp)

    the pattern to scan until

Returns:

  • (String)

    the String before ‘pattern`



204
205
206
207
208
# File 'lib/parser/parser.rb', line 204

def scan_until_ahead(pattern)
  content = self.scan_until /(?=(#{pattern}))/
  self.skip pattern
  return content
end

#scan_until_or_end(pattern) ⇒ String

will stop to scan at the specified pattern or at eos and returns the consumed string.

Parameters:

  • pattern (Regexp)

    the pattern to scan for

Returns:

  • (String)

    the String before ‘pattern`



215
216
217
# File 'lib/parser/parser.rb', line 215

def scan_until_or_end(pattern)
  self.scan_until(pattern) or self.scan_until(/$/)
end

#skip_escaping_until(pattern) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
# File 'lib/parser/parser.rb', line 245

def skip_escaping_until(pattern) 
     
  self.skip_until(/\\|#{pattern}/)
  
  raise end_of_string_error(pattern) if self.matched.nil?

  if self.matched.match /\\/
    self.getch
    skip_escaping_until(pattern)
  end    
end