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



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/parser/parser.rb', line 259

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?
  
  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



275
276
277
278
279
280
# File 'lib/parser/parser.rb', line 275

def save_scanned
  pos_start = self.pos
  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`



244
245
246
247
248
# File 'lib/parser/parser.rb', line 244

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`



254
255
256
# File 'lib/parser/parser.rb', line 254

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

#skip_escaping_until(pattern) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
# File 'lib/parser/parser.rb', line 282

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