Class: Dryml::Parser::Source

Inherits:
REXML::Source
  • Object
show all
Defined in:
lib/dryml/parser/source.rb

Overview

A REXML source that keeps track of where in the buffer it is

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src) ⇒ Source

Returns a new instance of Source.



6
7
8
9
# File 'lib/dryml/parser/source.rb', line 6

def initialize(src)
  super(src)
  @buffer_offset = 0
end

Instance Attribute Details

#last_match_offsetObject (readonly)

Returns the value of attribute last_match_offset.



11
12
13
# File 'lib/dryml/parser/source.rb', line 11

def last_match_offset
  @last_match_offset
end

Instance Method Details

#advance_buffer(md) ⇒ Object



22
23
24
25
# File 'lib/dryml/parser/source.rb', line 22

def advance_buffer(md)
  @buffer = md.post_match
  @buffer_offset += md.end(0)
end

#consume(pattern) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/dryml/parser/source.rb', line 37

def consume(pattern)
  md = remember_match(pattern.match(@buffer))
  if md
    advance_buffer(md)
    @buffer
  end
end

#current_lineObject



51
52
53
54
# File 'lib/dryml/parser/source.rb', line 51

def current_line
  pos = last_match_offset || 0
  [0, 0, @orig[0..pos].count("\n") + 1]
end

#match(pattern, cons = false) ⇒ Object



45
46
47
48
49
# File 'lib/dryml/parser/source.rb', line 45

def match(pattern, cons=false)
  md = remember_match(pattern.match(@buffer))
  advance_buffer(md) if cons and md
  return md
end

#remember_match(m) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/dryml/parser/source.rb', line 13

def remember_match(m)
  if m
    @last_match = m
    @last_match_offset = @buffer_offset + m.begin(0)
    @orig[@last_match_offset..@last_match_offset+m[0].length] == @buffer[m.begin(0)..m.end(0)]
  end
  m
end

#scan(pattern, cons = false) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/dryml/parser/source.rb', line 27

def scan(pattern, cons=false)
  raise '!'
  return nil if @buffer.nil?
  rv = @buffer.scan(pattern)
  if cons and rv.size > 0
    advance_buffer(Regexp.last_match)
  end
  rv
end