@verb #4133:"detol*iteral" this none this rxdo #4133 @program #4133:"detol*iteral" this none this "Copied from string utilities (#29):detoliteral by Foenique (#4232) Aug 20 22:41:15 2002" "Copied from Szass' Utilities (#9098):detoliteral by Szass_Sam (#4232)Apr 14 21:49:07 2002" "Programmed by Quadir (#9780)@moo.ca" "Syntax:" " :detoliteral([text]);" "Returns the equivalent of eval(tostr(\"return \",[text], \";\");" "Does not use eval() at any point, a manual parser." "May suspend()." {@text} = args text = tostr(@text) typeof(text) == $STR || raise(E_INVARG, "Cannot detoliteral non-str") {value, ignore, inquotes, inbracket, newitem, quote} = {0, 0, 0, 0, 0, ""} for x in [1..length(text)] "If were told to ignore 'ignore' number of chracters, then lets do it" "We asume they've already been parsed." if (ignore) ignore = ignore - 1 continue endif #4133:tell("got " + text[x..$]) "Alright, simple part, lets find out if we have an OBJ,INT, FLOAT or WAIF. STR and LIST are dealt with later" {match, current} = {{}, ""} if ((match = match(text[x..$], "%(#[0-9]+%):new({")) && match[1] == 1) oclass = toobj(text[x..$][match[1]..match[2]]) lpos = match[2] + 1 lquote = 0 lbrace = 1 lbracepos = -1 "Now we need to parse the list contained within..." for t in [lpos..length(text)] if (text[t] == "\"") lquote = !lquote endif if (text[t] == "}" && !lquote) lbrace = lbrace - 1 endif if (text[t] == "{" && !lquote) lbrace = lbrace + 1 endif if (lbrace == 0) lbracepos = t + 1 break endif endfor lbracepos > 0 || raise(E_INVARG, "unterminated WAIF list") ignore = lbracepos - match[1] + 1 wltext = text[match[2]..lbracepos - 1] waiflist = this:(verb)(wltext) "current = oclass:new(waiflist)" elseif ((match = match(text[x..$], "%(#[0-9]+%)")) && match[1] == 1) ignore = match[2] - match[1] current = toobj(text[x..$][match[1]..match[2]]) elseif ((match = match(text[x..$], "%([0-9]+%.[0-9]+%)")) && match[1] == 1) ignore = match[2] - match[1] current = tofloat(text[x..$][match[1]..match[2]]) elseif ((match = match(text[x..$], "%([0-9]+%)")) && match[1] == 1) ignore = match[2] - match[1] current = toint(text[x..$][match[1]..match[2]]) endif "Lets deal with the actual text now" if (!inquotes && current != "" && newitem) "We found a OBJ|INT|FLOAT, and have a LIST ready to take it" newitem = 0 "Insert in LIST apropriatly" if (inbracket == 1) value = {@value, current} elseif (inbracket == 2) value[$] = {@value[$], current} else value[$][inbracket - 1] = {@value[$][inbracket - 1], current} endif elseif (!inquotes && current != "") "We found a OBJ|INT|FLOAT, and value is ready to take it." "No Return since this doesnt have to be the end of a malformed text value" value = current elseif (!inquotes && inbracket && text[x] == "}") "Ok, so we have a LIST, and were closing it" inbracket = inbracket - 1 elseif (!inquotes && text[x] == "{") "Were opening a LIST, not sure if this is for the value, or if its just nested" "Initialize LIST into value apropriatly" if (inbracket == 0) value = {} elseif (inbracket == 1) value = {@value, {}} elseif (inbracket == 2) value[$] = {@value[$], {}} else value[$][inbracket] = {@value[$][inbracket], {}} endif newitem = 1 inbracket = inbracket + 1 elseif (!inquotes && text[x] == ",") "We have a new list item" newitem = 1 elseif (!inquotes && text[x] == " ") "Spaces really don't do anything" elseif (text[x] == "\"") "Were opening or closing a STR now" if (inquotes) "Close the STR" if (newitem && inbracket == 1) value = {@value, string} elseif (newitem && inbracket == 2) value[$] = {@value[$], string} elseif (newitem) value[$][inbracket - 1] = {@value[$][inbracket - 1], string} else value = string endif inquotes = 0 else "Open the STR" string = "" inquotes = 1 endif elseif (inquotes && text[x] == "\\") "We have a nested STR inside a STR. Special rules apply." z = x + 1 while (`text[z] == "\\" ! ANY') z = z + 1 $cmd_utils:suspend_if_needed(0) endwhile if (text[z] == "\"") temp = (this:slash_level(this:slash_level((z - x) * 2 + 1, 1) - 1, 2) - 1) / 2 string = tostr(string, $su:space(temp, "\\"), "\"") ignore = z - x else string = "" ignore = z - x endif elseif (inquotes) "This is just text inside a STR" string = tostr(string, text[x]) else raise(E_INVARG, "text contained illegal character") endif $cmd_utils:suspend_if_needed(0) endfor "Make sure there were no malformed STR or LIST parameters..." if (inbracket || inquotes) raise(E_INVARG, "text was not a value") endif return value "Last modified by Dax (#789) on Tue May 3 14:03:28 2005 MDT." .