Class: Dryml::Parser::Source
- Inherits:
-
REXML::Source
- Object
- REXML::Source
- Dryml::Parser::Source
- 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
-
#last_match_offset ⇒ Object
readonly
Returns the value of attribute last_match_offset.
Instance Method Summary collapse
- #advance_buffer(md) ⇒ Object
- #consume(pattern) ⇒ Object
- #current_line ⇒ Object
-
#initialize(src) ⇒ Source
constructor
A new instance of Source.
- #match(pattern, cons = false) ⇒ Object
- #remember_match(m) ⇒ Object
- #scan(pattern, cons = false) ⇒ Object
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_offset ⇒ Object (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_line ⇒ Object
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 |