Class: StringScanner
- Inherits:
-
Object
- Object
- StringScanner
- Defined in:
- lib/parser/parser.rb
Overview
We have to extend StringScanner a little bit to fit our needs.
Direct Known Subclasses
Instance Method Summary collapse
-
#intelligent_skip_until(pattern) ⇒ Object
skips content within comments, strings and regularexpressions.
- #save_scanned ⇒ Object
-
#scan_until_ahead(pattern) ⇒ String
returns the string until
patternmatches, then consumspattern. -
#scan_until_or_end(pattern) ⇒ String
will stop to scan at the specified pattern or at eos and returns the consumed string.
- #skip_escaping_until(pattern) ⇒ Object
Instance Method Details
#intelligent_skip_until(pattern) ⇒ Object
skips content within comments, strings and regularexpressions
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/parser/parser.rb', line 231 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_scanned ⇒ Object
249 250 251 252 253 254 |
# File 'lib/parser/parser.rb', line 249 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
216 217 218 219 220 |
# File 'lib/parser/parser.rb', line 216 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.
226 227 228 |
# File 'lib/parser/parser.rb', line 226 def scan_until_or_end(pattern) self.scan_until(pattern) or self.scan_until(/$/) end |
#skip_escaping_until(pattern) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/parser/parser.rb', line 256 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 |