]> git.saurik.com Git - cycript.git/blob - Complete.cpp
With -p on all platforms, we can't use asprintf().
[cycript.git] / Complete.cpp
1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
3 */
4
5 /* GNU Affero General Public License, Version 3 {{{ */
6 /*
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include <typeinfo>
23
24 #include "cycript.hpp"
25
26 #include "Driver.hpp"
27 #include "Replace.hpp"
28 #include "String.hpp"
29
30 static CYExpression *ParseExpression(CYPool &pool, CYUTF8String code) {
31 std::stringstream stream;
32 stream << '(' << code << ')';
33 CYDriver driver(pool, *stream.rdbuf());
34 if (driver.Parse() || !driver.errors_.empty())
35 return NULL;
36
37 CYOptions options;
38 CYContext context(options);
39
40 CYStatement *statement(driver.script_->code_);
41 _assert(statement != NULL);
42 _assert(statement->next_ == NULL);
43
44 CYExpress *express(dynamic_cast<CYExpress *>(driver.script_->code_));
45 _assert(express != NULL);
46
47 CYParenthetical *parenthetical(dynamic_cast<CYParenthetical *>(express->expression_));
48 _assert(parenthetical != NULL);
49
50 return parenthetical->expression_;
51 }
52
53 _visible char **CYComplete(const char *word, const std::string &line, CYUTF8String (*run)(CYPool &pool, const std::string &)) {
54 CYLocalPool pool;
55
56 std::stringbuf stream(line);
57 CYDriver driver(pool, stream);
58
59 driver.auto_ = true;
60
61 if (driver.Parse() || !driver.errors_.empty())
62 return NULL;
63
64 if (driver.mode_ == CYDriver::AutoNone)
65 return NULL;
66
67 CYExpression *expression;
68
69 CYOptions options;
70 CYContext context(options);
71
72 std::ostringstream prefix;
73
74 switch (driver.mode_) {
75 case CYDriver::AutoPrimary:
76 expression = $ CYThis();
77 break;
78
79 case CYDriver::AutoDirect:
80 expression = driver.context_;
81 break;
82
83 case CYDriver::AutoIndirect:
84 expression = $ CYIndirect(driver.context_);
85 break;
86
87 case CYDriver::AutoMessage: {
88 CYDriver::Context &thing(driver.contexts_.back());
89 expression = $M($C1($V("object_getClass"), thing.context_), $S("prototype"));
90 for (CYDriver::Context::Words::const_iterator part(thing.words_.begin()); part != thing.words_.end(); ++part)
91 prefix << (*part)->word_ << ':';
92 } break;
93
94 case CYDriver::AutoResolve:
95 expression = $M(driver.context_, $S("$cyr"));
96 break;
97
98 case CYDriver::AutoStruct:
99 expression = $ CYThis();
100 prefix << "$cys";
101 break;
102
103 case CYDriver::AutoEnum:
104 expression = $ CYThis();
105 prefix << "$cye";
106 break;
107
108 default:
109 _assert(false);
110 }
111
112 std::string begin(prefix.str());
113
114 CYBoolean *message;
115 if (driver.mode_ == CYDriver::AutoMessage)
116 message = $ CYTrue();
117 else
118 message = $ CYFalse();
119
120 driver.script_ = $ CYScript($ CYExpress($C4(ParseExpression(pool,
121 " function(object, prefix, word, message) {\n"
122 " var names = [];\n"
123 " var before = prefix.length;\n"
124 " prefix += word;\n"
125 " var entire = prefix.length;\n"
126 " if (false) {\n"
127 " for (var name in object)\n"
128 " if (name.substring(0, entire) == prefix)\n"
129 " names.push(name);\n"
130 " } else do {\n"
131 " if (object.hasOwnProperty(\"cy$complete\"))\n"
132 " names = names.concat(object.cy$complete(prefix, message));\n"
133 " try {\n"
134 " var local = Object.getOwnPropertyNames(object);\n"
135 " } catch (e) {\n"
136 " continue;\n"
137 " }\n"
138 " for (var name of local)\n"
139 " if (name.substring(0, entire) == prefix)\n"
140 " names.push(name);\n"
141 " } while (object = typeof object === 'object' ? Object.getPrototypeOf(object) : object.__proto__);\n"
142 " return names;\n"
143 " }\n"
144 ), expression, $S(begin.c_str()), $S(word), message)));
145
146 driver.script_->Replace(context);
147
148 std::stringbuf str;
149 CYOutput out(str, options);
150 out << *driver.script_;
151
152 std::string code(str.str());
153 CYUTF8String json(run(pool, code));
154 // XXX: if this fails we should not try to parse it
155
156 CYExpression *result(ParseExpression(pool, json));
157 if (result == NULL)
158 return NULL;
159
160 CYArray *array(dynamic_cast<CYArray *>(result->Primitive(context)));
161 if (array == NULL)
162 return NULL;
163
164 // XXX: use an std::set?
165 typedef std::vector<CYUTF8String> Completions;
166 Completions completions;
167
168 std::string common;
169 bool rest(false);
170
171 for (CYElement *element(array->elements_); element != NULL; ) {
172 CYElementValue *value(dynamic_cast<CYElementValue *>(element));
173 _assert(value != NULL);
174 element = value->next_;
175
176 _assert(value->value_ != NULL);
177 CYString *string(value->value_->String(context));
178 if (string == NULL)
179 CYThrow("string was actually %s", typeid(*value->value_).name());
180
181 CYUTF8String completion(string->value_, string->size_);
182 _assert(completion.size >= begin.size());
183 completion.data += begin.size();
184 completion.size -= begin.size();
185
186 if (completion.size == 0 && driver.mode_ == CYDriver::AutoMessage)
187 completion = "]";
188
189 if (CYStartsWith(completion, "$cy"))
190 continue;
191 completions.push_back(completion);
192
193 if (!rest) {
194 common = completion;
195 rest = true;
196 } else {
197 size_t limit(completion.size), size(common.size());
198 if (size > limit)
199 common = common.substr(0, limit);
200 else
201 limit = size;
202 for (limit = 0; limit != size; ++limit)
203 if (common[limit] != completion.data[limit])
204 break;
205 if (limit != size)
206 common = common.substr(0, limit);
207 }
208 }
209
210 size_t count(completions.size());
211 if (count == 0)
212 return NULL;
213
214 size_t colon(common.find(':'));
215 if (colon != std::string::npos)
216 common = common.substr(0, colon + 1);
217 if (completions.size() == 1)
218 common += ' ';
219
220 char **results(reinterpret_cast<char **>(malloc(sizeof(char *) * (count + 2))));
221
222 results[0] = strdup(common.c_str());
223 size_t index(0);
224 for (Completions::const_iterator i(completions.begin()); i != completions.end(); ++i)
225 results[++index] = strdup(i->data);
226 results[count + 1] = NULL;
227
228 return results;
229 }