Class: Ramekin::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/ramekin/scanner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#matchObject (readonly)

Returns the value of attribute match.



3
4
5
# File 'lib/ramekin/scanner.rb', line 3

def match
  @match
end

#posObject

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

#capturesObject



44
45
46
# File 'lib/ramekin/scanner.rb', line 44

def captures
  @match && @match.captures
end

#eos?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/ramekin/scanner.rb', line 36

def eos?
  @pos >= @str.size
end

#inspectObject



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