Class: RDParser::Scanner
- Inherits:
-
Object
- Object
- RDParser::Scanner
- Defined in:
- lib/rdparser/scanner.rb
Overview
Scanner is basically a proxy class for StringScanner that adds some convenience features. Could probably be a subclass actually, but this was a quick, scrappy job so I could get onto the fun stuff in RDParser!
Instance Method Summary collapse
- #eos? ⇒ Boolean
-
#initialize(string, options = {}) ⇒ Scanner
constructor
A new instance of Scanner.
- #lookahead ⇒ Object
- #position ⇒ Object
- #rollback ⇒ Object
- #rollback_position ⇒ Object
- #rollback_to(posi) ⇒ Object
- #scan(regexp) ⇒ Object
Constructor Details
#initialize(string, options = {}) ⇒ Scanner
Returns a new instance of Scanner.
8 9 10 11 12 |
# File 'lib/rdparser/scanner.rb', line 8 def initialize(string, = {}) @options = @scanner = StringScanner.new(string.dup) @rollback_pointers = [] end |
Instance Method Details
#eos? ⇒ Boolean
18 19 20 |
# File 'lib/rdparser/scanner.rb', line 18 def eos? @scanner.eos? end |
#lookahead ⇒ Object
26 27 28 |
# File 'lib/rdparser/scanner.rb', line 26 def lookahead @scanner.peek(10) end |
#position ⇒ Object
14 15 16 |
# File 'lib/rdparser/scanner.rb', line 14 def position @scanner.pos end |
#rollback ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/rdparser/scanner.rb', line 43 def rollback begin (@scanner.pos = @rollback_pointers.pop) && true rescue nil end end |
#rollback_position ⇒ Object
22 23 24 |
# File 'lib/rdparser/scanner.rb', line 22 def rollback_position @rollback_pointers.last end |
#rollback_to(posi) ⇒ Object
51 52 53 54 |
# File 'lib/rdparser/scanner.rb', line 51 def rollback_to(posi) @scanner.pos = posi @rollback_pointers.delete_if { |p| p >= posi } end |
#scan(regexp) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rdparser/scanner.rb', line 30 def scan(regexp) space = @options[:slurp_whitespace] && @options[:slurp_whitespace] == true ? /\s*/ : // if regexp.class == String regexp = Regexp.new(regexp.gsub(/\(|\)|\+|\|/) { |a| '\\' + a }) end if match = @scanner.scan(/(#{space}(#{regexp}))/) @rollback_pointers << (@scanner.pos - match.length) @options[:slurp_whitespace] && @options[:slurp_whitespace] == true ? match.sub(/^\s*/, '') : match else nil end end |