Sha256: 01ecb531b3cd3fb7f7aa4ceebc21a9be81d0af9de9263537d22b5af76a24638a

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

module NsMacroProcessor

  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

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ns-macro-processor-0.0.4 lib/ns_macro_processor/tokens.rb
ns-macro-processor-0.0.3 lib/ns_macro_processor/tokens.rb
ns-macro-processor-0.0.2 lib/ns_macro_processor/tokens.rb