Sha256: f4c0e35dfa344f05d9503725b2d4762b20672c68a13ea1bfdad01758e2d142c6

Contents?: true

Size: 1.19 KB

Versions: 23

Compression:

Stored size: 1.19 KB

Contents

module Hobo::Dryml::Parser

  # A REXML source that keeps track of where in the buffer it is
  class Source < REXML::Source

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

    attr_reader :last_match_offset

    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

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

    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

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

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

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

  end

end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
hobo-1.0.3 lib/hobo/dryml/parser/source.rb
hobo-1.0.2 lib/hobo/dryml/parser/source.rb
hobo-1.0.1 lib/hobo/dryml/parser/source.rb
hobo-1.0.0 lib/hobo/dryml/parser/source.rb
hobo-0.9.106 lib/hobo/dryml/parser/source.rb
hobo-0.9.105 lib/hobo/dryml/parser/source.rb
hobo-0.9.104 lib/hobo/dryml/parser/source.rb
hobo-0.9.103 lib/hobo/dryml/parser/source.rb
hobo-0.9.102 lib/hobo/dryml/parser/source.rb
hobo-0.9.101 lib/hobo/dryml/parser/source.rb
hobo-0.9.100 lib/hobo/dryml/parser/source.rb
hobo-0.9.0 lib/hobo/dryml/parser/source.rb
hobo-0.8.10 lib/hobo/dryml/parser/source.rb
hobo-0.8.9 lib/hobo/dryml/parser/source.rb
hobo-0.8.8 lib/hobo/dryml/parser/source.rb
hobo-0.8.2 lib/hobo/dryml/parser/source.rb
hobo-0.8.4 lib/hobo/dryml/parser/source.rb
hobo-0.8.3 lib/hobo/dryml/parser/source.rb
hobo-0.8.1 lib/hobo/dryml/parser/source.rb
hobo-0.8.5 lib/hobo/dryml/parser/source.rb