Switch to edit mode.
Name(s): detol*iteral1: "Programmed by Quadir (#9780)@moo.ca"
2: "Modified by Whiz (#3135)@moo.ca to work with waifs."
3: "Syntax:"
4: " :detoliteral([text]);"
5: "Returns the equivalent of eval(tostr(\"return \",[text], \";\");"
6: "Does not use eval() at any point, a manual parser."
7: "May suspend()."
8: {@text} = args
9: text = tostr(@text)
10: typeof(text) == $STR || raise(E_INVARG, "Cannot detoliteral non-$STR")
11: {value, ignore, inquotes, inbracket, newitem, quote} = {0, 0, 0, 0, 0, ""}
12: for x in [1..length(text)]
13: "If were told to ignore 'ignore' number of chracters, then lets do it"
14: "We asume they've already been parsed."
15: if (ignore)
16: ignore = ignore - 1
17: continue
18: endif
19: "Alright, simple part, lets find out if we have an OBJ, INT or FLOAT. STR and LIST are dealt with later"
20: {match, current} = {{}, ""}
21: if ((match = match(text[x..$], "%(#[0-9]+%):new({")) && match[1] == 1)
22: oclass = toobj(text[x..$][match[1]..match[2]])
23: lpos = match[2] + 1
24: lquote = 0
25: lbrace = 1
26: lbracepos = -1
27: "Now we need to parse the list contained within..."
28: for t in [lpos..length(text)]
29: if (text[t] == "\"")
30: lquote = !lquote
31: endif
32: if (text[t] == "}" && !lquote)
33: lbrace = lbrace - 1
34: endif
35: if (text[t] == "{" && !lquote)
36: lbrace = lbrace + 1
37: endif
38: if (lbrace == 0)
39: lbracepos = t + 1
40: break
41: endif
42: endfor
43: lbracepos > 0 || raise(E_INVARG, "unterminated WAIF list")
44: ignore = lbracepos - match[1] + 1
45: wltext = text[match[2]..lbracepos - 1]
46: "current = oclass:new(waiflist)"
47: elseif ((match = match(text[x..$], "%(#[0-9]+%)")) && match[1] == 1)
48: ignore = match[2] - match[1]
49: current = toobj(text[x..$][match[1]..match[2]])
50: elseif ((match = match(text[x..$], "%([0-9]+%.[0-9]+%)")) && match[1] == 1)
51: ignore = match[2] - match[1]
52: current = tofloat(text[x..$][match[1]..match[2]])
53: elseif ((match = match(text[x..$], "%([0-9]+%)")) && match[1] == 1)
54: ignore = match[2] - match[1]
55: current = toint(text[x..$][match[1]..match[2]])
56: endif
57: "Lets deal with the actual text now"
58: if (!inquotes && current != "" && newitem)
59: "We found a OBJ|INT|FLOAT, and have a LIST ready to take it"
60: newitem = 0
61: "Insert in LIST apropriatly"
62: if (inbracket == 1)
63: value = {@value, current}
64: elseif (inbracket == 2)
65: value[$] = {@value[$], current}
66: else
67: value[$][inbracket - 1] = {@value[$][inbracket - 1], current}
68: endif
69: elseif (!inquotes && current != "")
70: "We found a OBJ|INT|FLOAT, and value is ready to take it."
71: "No Return since this doesnt have to be the end of a malformed text value"
72: value = current
73: elseif (!inquotes && inbracket && text[x] == "}")
74: "Ok, so we have a LIST, and were closing it"
75: inbracket = inbracket - 1
76: elseif (!inquotes && text[x] == "{")
77: "Were opening a LIST, not sure if this is for the value, or if its just nested"
78: "Initialize LIST into value apropriatly"
79: if (inbracket == 0)
80: value = {}
81: elseif (inbracket == 1)
82: value = {@value, {}}
83: elseif (inbracket == 2)
84: value[$] = {@value[$], {}}
85: else
86: value[$][inbracket] = {@value[$][inbracket], {}}
87: endif
88: newitem = 1
89: inbracket = inbracket + 1
90: elseif (!inquotes && text[x] == ",")
91: "We have a new list item"
92: newitem = 1
93: elseif (!inquotes && text[x] == " ")
94: "Spaces really don't do anything"
95: elseif (text[x] == "\"")
96: "Were opening or closing a STR now"
97: if (inquotes)
98: "Close the STR"
99: if (newitem && inbracket == 1)
100: value = {@value, string}
101: elseif (newitem && inbracket == 2)
102: value[$] = {@value[$], string}
103: elseif (newitem)
104: value[$][inbracket - 1] = {@value[$][inbracket - 1], string}
105: else
106: value = string
107: endif
108: inquotes = 0
109: else
110: "Open the STR"
111: string = ""
112: inquotes = 1
113: endif
114: elseif (inquotes && text[x] == "\\")
115: "We have a nested STR inside a STR. Special rules apply."
116: z = x + 1
117: while (`text[z] == "\\" ! ANY')
118: z = z + 1
119: $cmd_utils:suspend_if_needed(0)
120: endwhile
121: if (text[z] == "\"")
122: temp = (this:slash_level(this:slash_level((z - x) * 2 + 1, 1) - 1, 2) - 1) / 2
123: string = tostr(string, $su:space(temp, "\\"), "\"")
124: ignore = z - x
125: else
126: string = ""
127: ignore = z - x
128: endif
129: elseif (inquotes)
130: "This is just text inside a STR"
131: string = tostr(string, text[x])
132: else
133: raise(E_INVARG, "text contained illegal character")
134: endif
135: $cmd_utils:suspend_if_needed(0)
136: endfor
137: "Make sure there were no malformed STR or LIST parameters..."
138: if (inbracket || inquotes)
139: raise(E_INVARG, "text was not a value")
140: endif
141: return value
142: "Last modified by Dax (#789) on Mon Aug 7 21:45:45 2006 MDT."