Class: NanDoc::SpecDoc::Playback::SexpScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/nandoc/spec-doc/playback/support/sexp-scanner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sexp) ⇒ SexpScanner

Returns a new instance of SexpScanner.



3
4
5
6
7
# File 'lib/nandoc/spec-doc/playback/support/sexp-scanner.rb', line 3

def initialize sexp
  @sexp = sexp
  @offset = 0
  @last = @sexp.size - 1
end

Instance Attribute Details

#sexpObject (readonly)

Returns the value of attribute sexp.



20
21
22
# File 'lib/nandoc/spec-doc/playback/support/sexp-scanner.rb', line 20

def sexp
  @sexp
end

Instance Method Details

#chimp(arr) ⇒ Object



8
9
10
# File 'lib/nandoc/spec-doc/playback/support/sexp-scanner.rb', line 8

def chimp arr
  arr.size == 1 ? arr.first : arr
end

#currentObject



11
12
13
# File 'lib/nandoc/spec-doc/playback/support/sexp-scanner.rb', line 11

def current
  @sexp[@offset]
end

#eos?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/nandoc/spec-doc/playback/support/sexp-scanner.rb', line 14

def eos?
  @offset > @last
end

#restObject



17
18
19
# File 'lib/nandoc/spec-doc/playback/support/sexp-scanner.rb', line 17

def rest
  @sexp[@offset..-1]
end

#scan(a, *b) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/nandoc/spec-doc/playback/support/sexp-scanner.rb', line 21

def scan a, *b
  current = self.current or return nil
  search = [a,*b]
  ret = nil
  if current[0..search.size-1] == search
    ret = current
    @offset += 1
  end
  ret
end

#scan_assert(a, *b) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/nandoc/spec-doc/playback/support/sexp-scanner.rb', line 31

def scan_assert a, *b
  ret = scan(a, *b)
  if ! ret
    fail("expecting #{a.inspect} had #{current.inspect}")
  end
  ret
end

#skip_to_after(a, *b) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nandoc/spec-doc/playback/support/sexp-scanner.rb', line 38

def skip_to_after a, *b
  search = [a, *b]
  last = search.size - 1
  idx = @sexp.index{ |n| n[0..last] == search }
  ret = false
  if idx
    @offset = idx + 1
    ret = @offset
  end
  ret
end

#skip_to_after_assert(a, *b) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/nandoc/spec-doc/playback/support/sexp-scanner.rb', line 49

def skip_to_after_assert a, *b
  idx = skip_to_after(a, *b)
  if ! idx
    search = [a, *b]
    last = search.size - 1
    nn = @sexp.select{ |n| n[0..last] == search }
    nn = nn.map{ |n| chimp(n[1..-1]).inspect }
    fail("#{chimp(search).inspect} not found. had:(#{nn.join(', ')})")
  end
  idx
end