Sha256: 08820d898325c32f8e3a671c8ca262eb896dd16cf60f4e4e39303630734c6dbd

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

class Tokens

  EOF = -1

  def initialize(input)
    @input = input
  end

  def advance
    if @input.nil? || @input.empty?
      @current = EOF
      return
    end
    ix = 0
    if whitespace?(@input[ix])
      while ix < @input.length && whitespace?(@input[ix])
        ix += 1
      end
      @current = ' '
      @input = @input[ix..-1]
      return
    end
    if ident_start?(@input[ix])
      while ix < @input.length && ident?(@input[ix])
        ix += 1
      end
      @current = @input[0, ix]
      @input = @input[ix..-1]
      return
    end
    @current = @input[0]
    @input = @input[1..-1]
  end

  def peek
    @current
  end

  def push_back(str)
    @input = (@current == EOF) ? str + @input : str + @current + @input
    advance
  end

  private

  def ident_start?(ch)
    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_/".include?(ch)
  end

  def ident?(ch)
    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-/_".include?(ch)
  end

  def whitespace?(ch)
    " \t\n\r".include?(ch)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ns-macro-processor-0.0.1 lib/tokens.rb