*! version 1.0.0  1feb1999  http://www.stata.com/users/wgould/vlists
program define p_vlists, sclass
	version 6
	sret clear
	local n 0
	local nopt 999
	gettoken tok 0: 0, parse(" ([]:")
	while "`tok'" != ":" {
		if "`tok'" == "[" {
			if `nopt' !=999 {
				error 197
			}
			local nopt `n'
			gettoken tok 0 : 0, parse(" ([]:")
		}
		else if "`tok'"=="]" {
			if `nopt'==999 {
				error 197
			}
			gettoken tok 0 : 0, parse(" ([]:")
			if "`tok'" != ":" {
				error 197
			}
		}
		else if "`tok'"== "varname" | "`tok'"=="varlist" {
			local n = `n' + 1
			local v`n' "`tok'"
			gettoken tok : 0, match(p) parse(" ([]:")
			if "`p'" == "(" {
				gettoken tok 0 : 0, match(p) parse(" ([]:")
				local v`n' `"`v`n''(`tok')"'
			}
			gettoken tok 0 : 0, parse(" ([]:")
		}
		else 	error 197
	}


/*
					/* debug code */
	di `"0 |`0'|"'
	di "n = `n' nopt = `nopt'"
	local i 1
	while `i' <= `n' {
		if `i' > `nopt' {
			di "[opt] " _c
		}
		di "`i'. `v`i''"
		local i = `i' + 1
	}
					/* end debug code */
*/

	local i 1
	local user `0'
	while `i' <= `n' {
		gettoken 0 : user, parse(" ,")
		IsEnd `0'
		if `s(end)' {
			if `i'>`nopt' {
				sret local end
				sret local rest `"`user'"'
				sret local n = `i' - 1
				exit
			}
			sret clear
			di in red "too few varlists specified"
			exit 102
		}
		if substr("`0'",1,1)=="(" {
			gettoken 0 user : user,  match(p) parse(" ([,")
			syntax `v`i''
			sret local v`i' "`varlist'"
		}
		else {
			if substr("`v`i''",1,7)=="varname" {
				gettoken 0 user : user, parse(" ,")
			}
			else {
				gettoken 0 user : user, parse(" ,")
				gettoken nxt : user, parse(" ,")
				IsEnd `nxt'
				while !`s(end)' & substr("`nxt'",1,1)!="(" {
					gettoken nxt user : user, parse(" ,")
					local 0 `"`0' `nxt'"'
					gettoken nxt : user, parse(" ,")
					IsEnd `nxt'
				}
			}
			syntax `v`i''
			sret local v`i' "`varlist'"
		}
		local i = `i' + 1
	}
	sret local end
	sret local rest `"`user'"'
	sret local n = `n'
end




program define IsEnd, sclass
	if "`1'"=="if" | "`1'"=="in" | "`1'"=="," | "`1'"=="[" | /*
	*/ "`1'" == "=" | "`1'"=="" {
		sret local end 1
	}
	else	sret local end 0
end
exit