X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/d9c911529b1480684bb8b6280410f2d09c8525a1..d6848e7305f443c0d55fc83ad93beccdb8a572ea:/Complete.cpp?ds=sidebyside diff --git a/Complete.cpp b/Complete.cpp index b378b60..0cde80c 100644 --- a/Complete.cpp +++ b/Complete.cpp @@ -22,27 +22,24 @@ #include "cycript.hpp" #include "Driver.hpp" -#include "Cycript.tab.hh" #include "Replace.hpp" #include "String.hpp" -static CYExpression *ParseExpression(CYUTF8String code) { +static CYExpression *ParseExpression(CYPool &pool, CYUTF8String code) { std::stringstream stream; stream << '(' << code << ')'; - CYDriver driver(stream); - - cy::parser parser(driver); - if (parser.parse() != 0 || !driver.errors_.empty()) + CYDriver driver(pool, *stream.rdbuf()); + if (driver.Parse() || !driver.errors_.empty()) return NULL; CYOptions options; CYContext context(options); - CYStatement *statement(driver.program_->code_); + CYStatement *statement(driver.script_->code_); _assert(statement != NULL); _assert(statement->next_ == NULL); - CYExpress *express(dynamic_cast(driver.program_->code_)); + CYExpress *express(dynamic_cast(driver.script_->code_)); _assert(express != NULL); CYParenthetical *parenthetical(dynamic_cast(express->expression_)); @@ -54,13 +51,12 @@ static CYExpression *ParseExpression(CYUTF8String code) { _visible char **CYComplete(const char *word, const std::string &line, CYUTF8String (*run)(CYPool &pool, const std::string &)) { CYLocalPool pool; - std::istringstream stream(line); - CYDriver driver(stream); + std::stringbuf stream(line); + CYDriver driver(pool, stream); driver.auto_ = true; - cy::parser parser(driver); - if (parser.parse() != 0 || !driver.errors_.empty()) + if (driver.Parse() || !driver.errors_.empty()) return NULL; if (driver.mode_ == CYDriver::AutoNone) @@ -93,36 +89,55 @@ _visible char **CYComplete(const char *word, const std::string &line, CYUTF8Stri prefix << (*part)->word_ << ':'; } break; + case CYDriver::AutoResolve: + expression = $M(driver.context_, $S("$cyr")); + break; + default: _assert(false); } std::string begin(prefix.str()); - driver.program_ = $ CYProgram($ CYExpress($C3(ParseExpression( + driver.script_ = $ CYScript($ CYExpress($C3(ParseExpression(pool, " function(object, prefix, word) {\n" " var names = [];\n" " var before = prefix.length;\n" " prefix += word;\n" " var entire = prefix.length;\n" - " for (var name in object)\n" - " if (name.substring(0, entire) == prefix)\n" - " names.push(name.substr(before));\n" + " if (false) {\n" + " for (var name in object)\n" + " if (name.substring(0, entire) == prefix)\n" + " names.push(name.substr(before));\n" + " } else do {\n" + " if (object.hasOwnProperty(\"cy$complete\")) {\n" + " names = names.concat(object.cy$complete(prefix));\n" + " continue;\n" + " }\n" + " try {\n" + " var local = Object.getOwnPropertyNames(object);\n" + " } catch (e) {\n" + " continue;\n" + " }\n" + " for (var name of local)\n" + " if (name.substring(0, entire) == prefix)\n" + " names.push(name.substr(before));\n" + " } while (object = typeof object === 'object' ? Object.getPrototypeOf(object) : object.__proto__);\n" " return names;\n" " }\n" ), expression, $S(begin.c_str()), $S(word)))); - driver.program_->Replace(context); + driver.script_->Replace(context); std::stringbuf str; CYOutput out(str, options); - out << *driver.program_; + out << *driver.script_; std::string code(str.str()); CYUTF8String json(run(pool, code)); // XXX: if this fails we should not try to parse it - CYExpression *result(ParseExpression(json)); + CYExpression *result(ParseExpression(pool, json)); if (result == NULL) return NULL; @@ -137,8 +152,12 @@ _visible char **CYComplete(const char *word, const std::string &line, CYUTF8Stri std::string common; bool rest(false); - CYForEach (element, array->elements_) { - CYString *string(dynamic_cast(element->value_)); + for (CYElement *element(array->elements_); element != NULL; ) { + CYElementValue *value(dynamic_cast(element)); + _assert(value != NULL); + element = value->next_; + + CYString *string(dynamic_cast(value->value_)); _assert(string != NULL); std::string completion;