Class: XRT::DepthChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/xrt/depth_checker.rb

Instance Method Summary collapse

Instance Method Details

#check(source, enable_color = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/xrt/depth_checker.rb', line 6

def check source, enable_color=nil
  annotated_source = ''
  parser = XRT::Parser.new(source)
  syntax = XRT::Syntax.new

  current_level = 0
  parser.tokens.each{|statement|
    diff = syntax.block_level statement
    current_level += diff
    annotated_source += statement

    if enable_color
      color = 44 + diff
      annotated_source += "\e[#{color}m#{current_level}\e[0m"
    else
      annotated_source += current_level.to_s
    end
  }
  if current_level == 0
    return true, annotated_source
  else
    return false, annotated_source
  end
end

#max_depth(source) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/xrt/depth_checker.rb', line 31

def max_depth source
  parser = XRT::Parser.new(source)
  syntax = XRT::Syntax.new

  max_level = 0
  current_level = 0

  parser.tokens.each{|statement|
    diff = syntax.block_level statement
    current_level += diff
    max_level = [ current_level, max_level].max
  }

  if current_level != 0
    raise 'failed to parse'
  end

  max_level
end