Sha256: cd1096645eac3b660fcf8ad9b2589c6ee0f646d59cb50344903484afca63960a

Contents?: true

Size: 1.23 KB

Versions: 21

Compression:

Stored size: 1.23 KB

Contents

class StringScanner
  attr_reader :pos
  attr_reader :matched

  def initialize(string)
    @string  = string
    @pos     = 0
    @matched = ''
    @working = string
  end

  def scan(regex)
    %x{
      var regex  = new RegExp('^' + regex.toString().substring(1, regex.toString().length - 1)),
          result = regex.exec(#@working);

      if (result == null) {
        #@matched = '';

        return nil;
      }
      else if (typeof(result) === 'object') {
        #@pos      += result[0].length;
        #@working  = #@working.substring(result[0].length);
        #@matched  = result[0];

        return result[0];
      }
      else if (typeof(result) === 'string') {
        #@pos     += result.length;
        #@working  = #@working.substring(result.length);

        return result;
      }
      else {
        return nil;
      }
    }
  end

  def check(regex)
    %x{
      var regexp = new RegExp('^' + regex.toString().substring(1, regex.toString().length - 1)),
          result = regexp.exec(#@working);

      if (result == null) {
        return this.matched = nil;
      }

      return this.matched = result[0];
    }
  end

  def peek(length)
    `#@working.substring(0, length)`
  end

  def eos?
    `#@working.length === 0`
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
opal-0.3.43 opal/strscan.rb
opal-0.3.42 opal/opal/strscan.rb
opal-0.3.41 opal/opal/strscan.rb
opal-0.3.40 lib/assets/javascripts/opal/strscan.rb
opal-0.3.39 lib/assets/javascripts/opal/strscan.rb
opal-0.3.38 lib/assets/javascripts/opal/strscan.rb
opal-0.3.37 lib/assets/javascripts/opal/strscan.rb
opal-0.3.36 lib/assets/javascripts/opal/strscan.rb
opal-0.3.35 lib/assets/javascripts/opal/strscan.rb
opal-0.3.34 lib/assets/javascripts/opal/strscan.rb
opal-0.3.33 core/parser/strscan.rb
opal-0.3.32 core/parser/strscan.rb
opal-0.3.31 core/parser/strscan.rb
opal-0.3.30 core/parser/strscan.rb
opal-0.3.29 core/parser/strscan.rb
opal-0.3.28 core/parser/strscan.rb
opal-0.3.27 core/parser/strscan.rb
opal-0.3.26 core/parser/strscan.rb
opal-0.3.25 core/strscan.rb
opal-0.3.22 core/strscan.rb