class Object

Constants

CommandLexer
DEPENDENCIES
ExpressionLexer
Getch
Kbhit
REQUIRED_RUBY_VER
REVISION
SimpleLexer
ZABCON_PATH

setup our search path or libraries

Public Instance Methods

delete_keys(keys) click to toggle source
# File libs/utility_items.rb, line 45
def delete_keys(keys)
  keys.each {|k|
    self.delete(k)
  }
end
each_char() { |x| ... } click to toggle source
# File zabcon.rb, line 93
def each_char
  if block_given?
    scan(/./m) do |x|
      yield x
    end
  else
    scan(/./m)
  end
end
env() click to toggle source
# File libs/utility_items.rb, line 27
def env
  EnvVars.instance
end
getch() click to toggle source
# File libs/printer.rb, line 35
def getch
  Getch.call
end
group(tokens) click to toggle source
# File libs/lexer.rb, line 1093
def group(tokens)
  retval,=group2(tokens,[])
  retval
end
group2(tokens,stack) click to toggle source
# File libs/lexer.rb, line 1098
def group2(tokens,stack)
  raise "Empty tokens" if tokens.empty?

  retval=[]
  need_comma=stack.length>0

  pos=-1
  while ((pos+=1)<tokens.length)
    case tokens[pos].kind
      when stack[-1]
        return retval,pos+1
      when :end
        return retval,pos+1
      when :word, :number, :variable, :escape
        lpos= stack[-1]==:whitespace ? pos+1 : lpos=walk(tokens,pos+1)
        if tokens[lpos].kind==:equals
          new_stack=stack.empty? ? [:whitespace] : stack+[stack[-1]]
          lside=tokens[pos]
          rside, lpos=group2(tokens[lpos+1..-1], new_stack)
          pos+=lpos
          retval<<{lside => rside}
        elsif tokens[lpos].kind==:comma && !need_comma
          new_stack=stack.empty? ? [:whitespace] : stack+[stack[-1]]
          ret, pos=group2(tokens[pos..-1], new_stack)
          retval<<ret
        else
          retval<<tokens[pos]
        end
      when :l_square
        rside, lpos=group2(tokens[pos+1..-1],stack+[:r_square])
        pos+=lpos
        retval<<rside
      when :comma
        raise "Unexpected comma" if !need_comma
      when :whitespace
        #do nothing
      else
        puts "pos: #{pos}"
        puts "tokens[pos]: #{tokens[pos].inspect}"
        puts "stack: #{stack.inspect}"
        exit
    end
  end

  return retval,pos
end
kbhit() click to toggle source
# File libs/printer.rb, line 39
def kbhit
  Kbhit.call
end
select_keys(keys) click to toggle source
# File libs/utility_items.rb, line 37
def select_keys(keys)
  result={}
  keys.each {|k|
    result.merge!({k=>self[k]})
  }
  result
end
split2(*options) click to toggle source

String::split2(*options) Valid options, and ther defaults:

:splitchar = '\s'
  pivot string or regex
:include_split = false
:trim_empty = true
  trim_empty will remove empty and whitespace characters by default
  if include_split is true whitespace characters (but not empty) will
  be included if the splitchar is a whitespace character (default)

This is a special split routine which will split str using splitchar as a split point. If items are within quotes or brackets they will not be split even when splitchar is found within those quotes or brackets.

# File libs/utility_items.rb, line 65
def split2(*options)
  defaults={:split_char=>'\s', :include_split=>false, :trim_empty=>true}

  options=
      if options.empty?
        defaults
      else
        raise "split2 requires parameters to be in hash form" if options[0].class!=Hash
        unknown_keys=options[0].keys-defaults.keys

        raise "Unknown keys: #{unknown_keys.join(", ")}" if !unknown_keys.empty?
        defaults.merge(options[0])
      end

  splitchar=options[:split_char]
  include_split=options[:include_split]
  trim_empty=options[:trim_empty]
  str=self
  quote_chars=["\"", "'"]
  left=["(", "{", "["]
  right=[")", "}", "]"]
  quote_regex=Regexp.escape(quote_chars.to_s)
  left_regex=Regexp.escape(left.to_s)
  right_regex=Regexp.escape(right.to_s)
  splitchar_regex= Regexp.new(/#{splitchar}/)
  stack=[]
  splits=[]
  result=[]
  s=StringScanner.new(str)
  #set up our regex for scanning.  We scan for brackets, quotes and escaped characters
  char_class=Regexp.new("[\\\\#{quote_regex}#{left_regex}#{right_regex}#{splitchar}]")
  while !s.eos?
    s.scan_until(char_class)
    break if !s.matched?  # break out if nothing matched
    ch=str[s.pos-1].chr
    case ch
      when "\\"  #hande an escaped character by moving the pointer up one
        s.getch
      when /[#{quote_regex}]/  #handle a quoted section
        raise "Unbalanced String: #{str}" if (!stack.index(ch).nil? && stack.index(ch)!=(stack.length-1))
        if stack.index(ch)==nil
          stack<<ch
        else
          stack.pop
        end
      when /[#{left_regex}]/  #open bracket found
        stack<<left.index(ch)
      when /[#{right_regex}]/  #close bracket found
        raise "Unbalanced String: #{str}" if ch!=right[stack.last]
        stack.pop
      when /#{splitchar_regex}/  #pivot character found
        splits<<s.pos-1 if stack.empty?
    end
  end

  raise "Unbalanced String: #{str}" if !stack.empty?
  splits<<str.length

  pos=0
  while !splits.empty?
    split_pos=splits.first
    splits.delete(splits.first)
    result<<str[pos..split_pos-1] if split_pos>0
    result<<str[split_pos].chr if !str[split_pos].nil? && include_split
    pos=split_pos+1
  end

    result=result.delete_if {|item|
      if include_split
        #delete the line if nil or empty or the current item is not the splitchar and not full of whitespace
        item.nil? || item.empty? || (item.scan(/^#{splitchar}$/).empty? && !item.scan(/^\s*$/).empty?)
      else
        item.nil? || item.empty? || !item.scan(/^\s*$/).empty?
      end
    } if trim_empty

  result
end
strip_comments() click to toggle source
# File libs/utility_items.rb, line 144
def strip_comments
  splits = self.split2(:split_char=>'#', :include_split=>true)
  if !(index=splits.index('#')).nil?
    if index>0
      splits=splits[0..index-1]
    else
      splits=[]
    end
  end
  splits.join.strip
end
vars() click to toggle source
# File libs/utility_items.rb, line 31
def vars
  GlobalVars.instance
end
walk(tokens,pos,skip=[:whitespace]) click to toggle source
# File libs/lexer.rb, line 1086
def walk(tokens,pos,skip=[:whitespace])
  pos+=1 while (pos<tokens.length &&
      !(skip & [tokens[pos].kind]).empty? &&
      tokens[pos].kind!=:end)
  pos
end