Class: Fold::Precompiler

Inherits:
Object
  • Object
show all
Includes:
FoldFactory, SliceFactory
Defined in:
lib/fold/precompiler.rb

Constant Summary

Constants included from FoldFactory

FoldFactory::Indent

Instance Method Summary collapse

Methods included from SliceFactory

#detect_slice_class, included, #process

Methods included from FoldFactory

#count_soft_tabs, #detect_class, included, #produce

Instance Method Details

#fold(lines) ⇒ 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fold/precompiler.rb', line 6

def fold lines
  last_line= produce

  return last_line if lines.empty?

  parent_line= nil
  
  parent_stack= []

  lines.each do |text|
    line = produce process(text)

    indent = line.tabs - last_line.tabs

    raise IndentationError if indent > 1 
    
    if step_in? indent
      parent_line = last_line
      parent_line.children << line
      parent_stack << parent_line
    end
  
    if step_aside? indent
      parent_line.children << line 
    end
    
    if step_out? indent
      parent_stack= parent_stack.slice 0, parent_stack.length + indent
      parent_line = parent_stack.last
      parent_line.children << line
    end

    line.parent = parent_line unless parent_line.nil?

    last_line= line
  end
  
  parent_stack.first
end

#step_aside?(indent) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/fold/precompiler.rb', line 54

def step_aside? indent
  indent == 0
end

#step_in?(indent) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/fold/precompiler.rb', line 46

def step_in? indent
  indent == 1
end

#step_out?(indent) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/fold/precompiler.rb', line 50

def step_out? indent
  indent < 0
end