*! version 1.0.0   06-16-1998   DEM

program define vsplit
   confirm new variable `*'
   local digit = substr("`*'", -1, 1)
   local name = substr("`*'",1,length("`*'")-1)
   if !index("0123456789","`digit'") { error 198 }
   while index("0123456789", "`digit'") {
      local numpart = "`digit'" + "`numpart'"
      local digit = substr("`name'", -1, 1)
      local name = substr("`name'", 1, length("`name'")-1)
   }
   global S_1 = "`name'" + "`digit'"
   global S_2 `numpart'
end

program define aexp
   parse "`*'", parse(" <>/|~!^&*()-=+")
   local leftB = index("`1'", "{")
   local rightB = index("`1'", "}")
   if `leftB' {
      local arref `1'
      while !`rightB' {
         macro shift
         local arref `arref'`1'
         local rightB = index("`1'", "}")
      }
      aelem `arref'
      macro shift
      global S_A_EXP $S_A_ELEM
   }
   else global S_A_EXP
   while "`1'" != "" {
      global S_A_EXP $S_A_EXP `1'
      macro shift
      local leftB = index("`1'", "{")
      local rightB = index("`1'", "}")
      if `leftB' {
         local arref `1'
         while !`rightB' {
            macro shift
            local arref `arref'`1'
            local rightB = index("`1'", "}")
         }
         aelem `arref'
         global S_A_EXP $S_A_EXP $S_A_ELEM
         macro shift
      }
   }
end

program define vlist
   local leftB = index("`*'", "{")
   if `leftB' {
      local aexp $S_A_EXP
      aexp `*'
      local args $S_A_EXP
      global S_A_EXP `aexp'
   }
   else local args `*'
   parse "`args'", parse("*-")
   if "`2'" != "*" & "`2'" != "-" {
      local varlist "required new"
      capture parse "`args'"
      if _rc {
         local varlist "required"
         parse "`args'"
      }
      global S_1 `varlist'
      global S_2 : word count `varlist'
      exit 0
   }

   /* got a variable list */

   else if "`2'" == "*" {
      unabbrev `args'
      exit 0
   }

   local varlist "required new"
   capture parse "`args'"
   if !_rc {
      global S_1 `varlist'
      global S_2 : word count `varlist'
      exit 0
   }
   else if _rc == 110 {
      unabbrev `args'
      exit 0
   }
   capture unabbrev `args'
   if _rc {
      local varlist "required new"
      capture parse "`args'"
      if _rc == 198 {
         parse "`args'", parse(" -")
         while "`1'" != "" {
            if "`2'" == "-" {
               vsplit `1'
               local vname $S_1
               local i $S_2
               vsplit `3'
               if "`vname'" != "$S_1" { error 198 }
               if `i' > $S_2 { error 198 }
               while `i' <= $S_2 {
                  local vars `vars' `vname'`i'
                  local i = `i' + 1
               }
               macro shift
               macro shift
               macro shift
            }
            else local vars `vars' `1'
         }
         local varlist "required new"
         parse "`vars'"
         global S_1 `varlist'
         global S_2 : word count `varlist'
      }
      else if _rc { parse "`args'" }
      else {
         global S_1 `varlist'
         global S_2 : word count `varlist'
      }
   }
end