]>
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" | |
25 | #include "Cycript.tab.hh" | |
26 | #include "Replace.hpp" | |
27 | #include "String.hpp" | |
28 | ||
2c1d569a | 29 | static CYExpression *ParseExpression(CYPool &pool, CYUTF8String code) { |
d9c91152 JF |
30 | std::stringstream stream; |
31 | stream << '(' << code << ')'; | |
2c1d569a | 32 | CYDriver driver(pool, stream); |
d9c91152 JF |
33 | |
34 | cy::parser parser(driver); | |
35 | if (parser.parse() != 0 || !driver.errors_.empty()) | |
36 | return NULL; | |
37 | ||
38 | CYOptions options; | |
39 | CYContext context(options); | |
40 | ||
41 | CYStatement *statement(driver.program_->code_); | |
42 | _assert(statement != NULL); | |
43 | _assert(statement->next_ == NULL); | |
44 | ||
45 | CYExpress *express(dynamic_cast<CYExpress *>(driver.program_->code_)); | |
46 | _assert(express != NULL); | |
47 | ||
48 | CYParenthetical *parenthetical(dynamic_cast<CYParenthetical *>(express->expression_)); | |
49 | _assert(parenthetical != NULL); | |
50 | ||
51 | return parenthetical->expression_; | |
52 | } | |
53 | ||
54 | _visible char **CYComplete(const char *word, const std::string &line, CYUTF8String (*run)(CYPool &pool, const std::string &)) { | |
55 | CYLocalPool pool; | |
56 | ||
57 | std::istringstream stream(line); | |
2c1d569a | 58 | CYDriver driver(pool, stream); |
d9c91152 JF |
59 | |
60 | driver.auto_ = true; | |
61 | ||
62 | cy::parser parser(driver); | |
63 | if (parser.parse() != 0 || !driver.errors_.empty()) | |
64 | return NULL; | |
65 | ||
66 | if (driver.mode_ == CYDriver::AutoNone) | |
67 | return NULL; | |
68 | ||
69 | CYExpression *expression; | |
70 | ||
71 | CYOptions options; | |
72 | CYContext context(options); | |
73 | ||
74 | std::ostringstream prefix; | |
75 | ||
76 | switch (driver.mode_) { | |
77 | case CYDriver::AutoPrimary: | |
78 | expression = $ CYThis(); | |
79 | break; | |
80 | ||
81 | case CYDriver::AutoDirect: | |
82 | expression = driver.context_; | |
83 | break; | |
84 | ||
85 | case CYDriver::AutoIndirect: | |
86 | expression = $ CYIndirect(driver.context_); | |
87 | break; | |
88 | ||
89 | case CYDriver::AutoMessage: { | |
90 | CYDriver::Context &thing(driver.contexts_.back()); | |
91 | expression = $M($C1($V("object_getClass"), thing.context_), $S("messages")); | |
92 | for (CYDriver::Context::Words::const_iterator part(thing.words_.begin()); part != thing.words_.end(); ++part) | |
93 | prefix << (*part)->word_ << ':'; | |
94 | } break; | |
95 | ||
96 | default: | |
97 | _assert(false); | |
98 | } | |
99 | ||
100 | std::string begin(prefix.str()); | |
101 | ||
2c1d569a | 102 | driver.program_ = $ CYProgram($ CYExpress($C3(ParseExpression(pool, |
d9c91152 JF |
103 | " function(object, prefix, word) {\n" |
104 | " var names = [];\n" | |
105 | " var before = prefix.length;\n" | |
106 | " prefix += word;\n" | |
107 | " var entire = prefix.length;\n" | |
108 | " for (var name in object)\n" | |
109 | " if (name.substring(0, entire) == prefix)\n" | |
110 | " names.push(name.substr(before));\n" | |
111 | " return names;\n" | |
112 | " }\n" | |
113 | ), expression, $S(begin.c_str()), $S(word)))); | |
114 | ||
115 | driver.program_->Replace(context); | |
116 | ||
117 | std::stringbuf str; | |
118 | CYOutput out(str, options); | |
119 | out << *driver.program_; | |
120 | ||
121 | std::string code(str.str()); | |
122 | CYUTF8String json(run(pool, code)); | |
123 | // XXX: if this fails we should not try to parse it | |
124 | ||
2c1d569a | 125 | CYExpression *result(ParseExpression(pool, json)); |
d9c91152 JF |
126 | if (result == NULL) |
127 | return NULL; | |
128 | ||
129 | CYArray *array(dynamic_cast<CYArray *>(result->Primitive(context))); | |
130 | if (array == NULL) | |
131 | return NULL; | |
132 | ||
133 | // XXX: use an std::set? | |
134 | typedef std::vector<std::string> Completions; | |
135 | Completions completions; | |
136 | ||
137 | std::string common; | |
138 | bool rest(false); | |
139 | ||
140 | CYForEach (element, array->elements_) { | |
141 | CYString *string(dynamic_cast<CYString *>(element->value_)); | |
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 | } |