Class: Eggshell::LineCounter

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

Overview

Tracks line and line count. Correctness depends

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ LineCounter

Returns a new instance of LineCounter.



6
7
8
9
10
11
# File 'lib/eggshell.rb', line 6

def initialize(file = nil)
	@file = file
	@l_stack = []
	@l_count = []
	push
end

Instance Method Details

#fileObject



25
26
27
# File 'lib/eggshell.rb', line 25

def file
	@file
end

#lineObject



13
14
15
# File 'lib/eggshell.rb', line 13

def line
	return @l_stack[-1]
end

#line_numObject



17
18
19
20
21
22
23
# File 'lib/eggshell.rb', line 17

def line_num
	c = 0
	@l_count.each do |lc|
		c += lc
	end
	return c
end

#new_line(line, offset = nil) ⇒ Object

Sets the current line and offset. If offset is not nil, resets the counter to this value. A sample situation where this is needed:

pre. block here \ macro line 1 macro line 2 } \ last block

During execution, ‘@macro` would push a new count state. After execution is over, it’s popped, leaving the line number at the macro start. The main process loop, however, is keeping track of all the lines during its own execution, so it can set the offset to the actual line again.



45
46
47
48
# File 'lib/eggshell.rb', line 45

def new_line(line, offset = nil)
	@l_stack[-1] = line
	@l_count[-1] = offset != nil ? offset : @l_count[-1] + 1
end

#popObject



55
56
57
58
59
# File 'lib/eggshell.rb', line 55

def pop
	@l_stack.pop
	@l_count.pop
	nil
end

#pushObject



50
51
52
53
# File 'lib/eggshell.rb', line 50

def push
	@l_stack << nil
	@l_count << 0
end