Class: Ramekin::Scanner
- Inherits:
-
Object
- Object
- Ramekin::Scanner
- Defined in:
- lib/ramekin/scanner.rb
Instance Attribute Summary collapse
-
#match ⇒ Object
readonly
Returns the value of attribute match.
-
#pos ⇒ Object
Returns the value of attribute pos.
Instance Method Summary collapse
- #[](k) ⇒ Object
- #captures ⇒ Object
- #eos? ⇒ Boolean
-
#initialize(str) ⇒ Scanner
constructor
A new instance of Scanner.
- #inspect ⇒ Object
- #peek(n = 1) ⇒ Object
- #scan(re) ⇒ Object
Constructor Details
#initialize(str) ⇒ Scanner
Returns a new instance of Scanner.
5 6 7 8 9 10 |
# File 'lib/ramekin/scanner.rb', line 5 def initialize(str) @str = str @cache = {} @pos = 0 @match = nil end |
Instance Attribute Details
#match ⇒ Object (readonly)
Returns the value of attribute match.
3 4 5 |
# File 'lib/ramekin/scanner.rb', line 3 def match @match end |
#pos ⇒ Object
Returns the value of attribute pos.
4 5 6 |
# File 'lib/ramekin/scanner.rb', line 4 def pos @pos end |
Instance Method Details
#[](k) ⇒ Object
40 41 42 |
# File 'lib/ramekin/scanner.rb', line 40 def [](k) @match && @match[k] end |
#captures ⇒ Object
44 45 46 |
# File 'lib/ramekin/scanner.rb', line 44 def captures @match && @match.captures end |
#eos? ⇒ Boolean
36 37 38 |
# File 'lib/ramekin/scanner.rb', line 36 def eos? @pos >= @str.size end |
#inspect ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/ramekin/scanner.rb', line 12 def inspect prev = @str[@pos-5...@pos] prev = "...#{prev}" if @pos > 5 prev = "#{prev} " unless prev.empty? post = @str[@pos+1...@pos+5] post = "#{post}..." unless @pos+5 >= @str.length post = " #{post}" unless post.empty? "Scanner([#{@pos}] #{prev}>#{@str[@pos]}<#{post})" end |
#peek(n = 1) ⇒ Object
32 33 34 |
# File 'lib/ramekin/scanner.rb', line 32 def peek(n=1) @str[@pos...@pos+n] end |
#scan(re) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/ramekin/scanner.rb', line 24 def scan(re) anchored = anchored_re(re) @match = anchored.match(@str, @pos) @pos = @match.end(0) if @match @match end |