]>
Commit | Line | Data |
---|---|---|
d9c91152 JF |
1 | /* Cycript - Optimizing JavaScript Compiler/Runtime |
2 | * Copyright (C) 2009-2015 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 "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 << ')'; | |
2c1d569a | 31 | CYDriver driver(pool, stream); |
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 | ||
54 | std::istringstream 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()); | |
87 | expression = $M($C1($V("object_getClass"), thing.context_), $S("messages")); | |
88 | for (CYDriver::Context::Words::const_iterator part(thing.words_.begin()); part != thing.words_.end(); ++part) | |
89 | prefix << (*part)->word_ << ':'; | |
90 | } break; | |
91 | ||
92 | default: | |
93 | _assert(false); | |
94 | } | |
95 | ||
96 | std::string begin(prefix.str()); | |
97 | ||
a7d8b413 | 98 | driver.script_ = $ CYScript($ CYExpress($C3(ParseExpression(pool, |
d9c91152 JF |
99 | " function(object, prefix, word) {\n" |
100 | " var names = [];\n" | |
101 | " var before = prefix.length;\n" | |
102 | " prefix += word;\n" | |
103 | " var entire = prefix.length;\n" | |
104 | " for (var name in object)\n" | |
105 | " if (name.substring(0, entire) == prefix)\n" | |
106 | " names.push(name.substr(before));\n" | |
107 | " return names;\n" | |
108 | " }\n" | |
109 | ), expression, $S(begin.c_str()), $S(word)))); | |
110 | ||
a7d8b413 | 111 | driver.script_->Replace(context); |
d9c91152 JF |
112 | |
113 | std::stringbuf str; | |
114 | CYOutput out(str, options); | |
a7d8b413 | 115 | out << *driver.script_; |
d9c91152 JF |
116 | |
117 | std::string code(str.str()); | |
118 | CYUTF8String json(run(pool, code)); | |
119 | // XXX: if this fails we should not try to parse it | |
120 | ||
2c1d569a | 121 | CYExpression *result(ParseExpression(pool, json)); |
d9c91152 JF |
122 | if (result == NULL) |
123 | return NULL; | |
124 | ||
125 | CYArray *array(dynamic_cast<CYArray *>(result->Primitive(context))); | |
126 | if (array == NULL) | |
127 | return NULL; | |
128 | ||
129 | // XXX: use an std::set? | |
130 | typedef std::vector<std::string> Completions; | |
131 | Completions completions; | |
132 | ||
133 | std::string common; | |
134 | bool rest(false); | |
135 | ||
fc8fc33d JF |
136 | for (CYElement *element(array->elements_); element != NULL; ) { |
137 | CYElementValue *value(dynamic_cast<CYElementValue *>(element)); | |
138 | _assert(value != NULL); | |
139 | element = value->next_; | |
140 | ||
141 | CYString *string(dynamic_cast<CYString *>(value->value_)); | |
d9c91152 JF |
142 | _assert(string != NULL); |
143 | ||
144 | std::string completion; | |
145 | if (string->size_ != 0) | |
146 | completion.assign(string->value_, string->size_); | |
147 | else if (driver.mode_ == CYDriver::AutoMessage) | |
148 | completion = "]"; | |
149 | else | |
150 | continue; | |
151 | ||
152 | completions.push_back(completion); | |
153 | ||
154 | if (!rest) { | |
155 | common = completion; | |
156 | rest = true; | |
157 | } else { | |
158 | size_t limit(completion.size()), size(common.size()); | |
159 | if (size > limit) | |
160 | common = common.substr(0, limit); | |
161 | else | |
162 | limit = size; | |
163 | for (limit = 0; limit != size; ++limit) | |
164 | if (common[limit] != completion[limit]) | |
165 | break; | |
166 | if (limit != size) | |
167 | common = common.substr(0, limit); | |
168 | } | |
169 | } | |
170 | ||
171 | size_t count(completions.size()); | |
172 | if (count == 0) | |
173 | return NULL; | |
174 | ||
175 | size_t colon(common.find(':')); | |
176 | if (colon != std::string::npos) | |
177 | common = common.substr(0, colon + 1); | |
178 | if (completions.size() == 1) | |
179 | common += ' '; | |
180 | ||
181 | char **results(reinterpret_cast<char **>(malloc(sizeof(char *) * (count + 2)))); | |
182 | ||
183 | results[0] = strdup(common.c_str()); | |
184 | size_t index(0); | |
185 | for (Completions::const_iterator i(completions.begin()); i != completions.end(); ++i) | |
186 | results[++index] = strdup(i->c_str()); | |
187 | results[count + 1] = NULL; | |
188 | ||
189 | return results; | |
190 | } |