]>
Commit | Line | Data |
---|---|---|
7341eedb JF |
1 | /* Cycript - The Truly Universal Scripting Language |
2 | * Copyright (C) 2009-2016 Jay Freeman (saurik) | |
d9c91152 JF |
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 "cycript.hpp" | |
23 | ||
24 | #include "Driver.hpp" | |
d9c91152 JF |
25 | #include "Replace.hpp" |
26 | #include "String.hpp" | |
27 | ||
2c1d569a | 28 | static CYExpression *ParseExpression(CYPool &pool, CYUTF8String code) { |
d9c91152 JF |
29 | std::stringstream stream; |
30 | stream << '(' << code << ')'; | |
c3d9dbc7 | 31 | CYDriver driver(pool, *stream.rdbuf()); |
8a392978 | 32 | if (driver.Parse() || !driver.errors_.empty()) |
d9c91152 JF |
33 | return NULL; |
34 | ||
35 | CYOptions options; | |
36 | CYContext context(options); | |
37 | ||
a7d8b413 | 38 | CYStatement *statement(driver.script_->code_); |
d9c91152 JF |
39 | _assert(statement != NULL); |
40 | _assert(statement->next_ == NULL); | |
41 | ||
a7d8b413 | 42 | CYExpress *express(dynamic_cast<CYExpress *>(driver.script_->code_)); |
d9c91152 JF |
43 | _assert(express != NULL); |
44 | ||
45 | CYParenthetical *parenthetical(dynamic_cast<CYParenthetical *>(express->expression_)); | |
46 | _assert(parenthetical != NULL); | |
47 | ||
48 | return parenthetical->expression_; | |
49 | } | |
50 | ||
51 | _visible char **CYComplete(const char *word, const std::string &line, CYUTF8String (*run)(CYPool &pool, const std::string &)) { | |
52 | CYLocalPool pool; | |
53 | ||
c3d9dbc7 | 54 | std::stringbuf stream(line); |
2c1d569a | 55 | CYDriver driver(pool, stream); |
d9c91152 JF |
56 | |
57 | driver.auto_ = true; | |
58 | ||
8a392978 | 59 | if (driver.Parse() || !driver.errors_.empty()) |
d9c91152 JF |
60 | return NULL; |
61 | ||
62 | if (driver.mode_ == CYDriver::AutoNone) | |
63 | return NULL; | |
64 | ||
65 | CYExpression *expression; | |
66 | ||
67 | CYOptions options; | |
68 | CYContext context(options); | |
69 | ||
70 | std::ostringstream prefix; | |
71 | ||
72 | switch (driver.mode_) { | |
73 | case CYDriver::AutoPrimary: | |
74 | expression = $ CYThis(); | |
75 | break; | |
76 | ||
77 | case CYDriver::AutoDirect: | |
78 | expression = driver.context_; | |
79 | break; | |
80 | ||
81 | case CYDriver::AutoIndirect: | |
82 | expression = $ CYIndirect(driver.context_); | |
83 | break; | |
84 | ||
85 | case CYDriver::AutoMessage: { | |
86 | CYDriver::Context &thing(driver.contexts_.back()); | |
3d2d95a0 | 87 | expression = $M($C1($V("object_getClass"), thing.context_), $S("prototype")); |
d9c91152 JF |
88 | for (CYDriver::Context::Words::const_iterator part(thing.words_.begin()); part != thing.words_.end(); ++part) |
89 | prefix << (*part)->word_ << ':'; | |
90 | } break; | |
91 | ||
2fad14e5 JF |
92 | case CYDriver::AutoResolve: |
93 | expression = $M(driver.context_, $S("$cyr")); | |
94 | break; | |
95 | ||
84096608 JF |
96 | case CYDriver::AutoStruct: |
97 | expression = $ CYThis(); | |
98 | prefix << "$cys"; | |
99 | break; | |
100 | ||
101 | case CYDriver::AutoEnum: | |
102 | expression = $ CYThis(); | |
103 | prefix << "$cye"; | |
104 | break; | |
105 | ||
d9c91152 JF |
106 | default: |
107 | _assert(false); | |
108 | } | |
109 | ||
110 | std::string begin(prefix.str()); | |
111 | ||
3d2d95a0 JF |
112 | CYBoolean *message; |
113 | if (driver.mode_ == CYDriver::AutoMessage) | |
114 | message = $ CYTrue(); | |
115 | else | |
116 | message = $ CYFalse(); | |
117 | ||
118 | driver.script_ = $ CYScript($ CYExpress($C4(ParseExpression(pool, | |
119 | " function(object, prefix, word, message) {\n" | |
d9c91152 JF |
120 | " var names = [];\n" |
121 | " var before = prefix.length;\n" | |
122 | " prefix += word;\n" | |
123 | " var entire = prefix.length;\n" | |
24d9135d JF |
124 | " if (false) {\n" |
125 | " for (var name in object)\n" | |
126 | " if (name.substring(0, entire) == prefix)\n" | |
84096608 | 127 | " names.push(name);\n" |
24d9135d | 128 | " } else do {\n" |
dd23aba1 | 129 | " if (object.hasOwnProperty(\"cy$complete\")) {\n" |
3d2d95a0 | 130 | " names = names.concat(object.cy$complete(prefix, message));\n" |
dd23aba1 JF |
131 | " continue;\n" |
132 | " }\n" | |
24d9135d JF |
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" | |
84096608 | 140 | " names.push(name);\n" |
24d9135d | 141 | " } while (object = typeof object === 'object' ? Object.getPrototypeOf(object) : object.__proto__);\n" |
d9c91152 JF |
142 | " return names;\n" |
143 | " }\n" | |
3d2d95a0 | 144 | ), expression, $S(begin.c_str()), $S(word), message))); |
d9c91152 | 145 | |
a7d8b413 | 146 | driver.script_->Replace(context); |
d9c91152 JF |
147 | |
148 | std::stringbuf str; | |
149 | CYOutput out(str, options); | |
a7d8b413 | 150 | out << *driver.script_; |
d9c91152 JF |
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 | ||
2c1d569a | 156 | CYExpression *result(ParseExpression(pool, json)); |
d9c91152 JF |
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? | |
5883fc70 | 165 | typedef std::vector<CYUTF8String> Completions; |
d9c91152 JF |
166 | Completions completions; |
167 | ||
168 | std::string common; | |
169 | bool rest(false); | |
170 | ||
fc8fc33d JF |
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 | CYString *string(dynamic_cast<CYString *>(value->value_)); | |
d9c91152 JF |
177 | _assert(string != NULL); |
178 | ||
5883fc70 | 179 | CYUTF8String completion; |
d9c91152 | 180 | if (string->size_ != 0) |
5883fc70 | 181 | completion = {string->value_, string->size_}; |
d9c91152 JF |
182 | else if (driver.mode_ == CYDriver::AutoMessage) |
183 | completion = "]"; | |
184 | else | |
185 | continue; | |
186 | ||
84096608 JF |
187 | completion.data += begin.size(); |
188 | completion.size -= begin.size(); | |
189 | ||
5883fc70 JF |
190 | if (CYStartsWith(completion, "$cy")) |
191 | continue; | |
d9c91152 JF |
192 | completions.push_back(completion); |
193 | ||
194 | if (!rest) { | |
195 | common = completion; | |
196 | rest = true; | |
197 | } else { | |
5883fc70 | 198 | size_t limit(completion.size), size(common.size()); |
d9c91152 JF |
199 | if (size > limit) |
200 | common = common.substr(0, limit); | |
201 | else | |
202 | limit = size; | |
203 | for (limit = 0; limit != size; ++limit) | |
5883fc70 | 204 | if (common[limit] != completion.data[limit]) |
d9c91152 JF |
205 | break; |
206 | if (limit != size) | |
207 | common = common.substr(0, limit); | |
208 | } | |
209 | } | |
210 | ||
211 | size_t count(completions.size()); | |
212 | if (count == 0) | |
213 | return NULL; | |
214 | ||
215 | size_t colon(common.find(':')); | |
216 | if (colon != std::string::npos) | |
217 | common = common.substr(0, colon + 1); | |
218 | if (completions.size() == 1) | |
219 | common += ' '; | |
220 | ||
221 | char **results(reinterpret_cast<char **>(malloc(sizeof(char *) * (count + 2)))); | |
222 | ||
223 | results[0] = strdup(common.c_str()); | |
224 | size_t index(0); | |
225 | for (Completions::const_iterator i(completions.begin()); i != completions.end(); ++i) | |
5883fc70 | 226 | results[++index] = strdup(i->data); |
d9c91152 JF |
227 | results[count + 1] = NULL; |
228 | ||
229 | return results; | |
230 | } |