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 ‘pattern` matches, then consums `pattern`.
-
#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
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/parser/parser.rb', line 233 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
251 252 253 254 255 256 |
# File 'lib/parser/parser.rb', line 251 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`
217 218 219 220 221 |
# File 'lib/parser/parser.rb', line 217 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.
228 229 230 |
# File 'lib/parser/parser.rb', line 228 def scan_until_or_end(pattern) self.scan_until(pattern) or self.scan_until(/$/) end |
#skip_escaping_until(pattern) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/parser/parser.rb', line 258 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 |