From a4d849b7b60b43b71a5fe406ff0d78ccf197d15d Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sun, 3 Jan 2016 22:11:49 -0800 Subject: [PATCH] Add a ?reparse mode to experiment pretty printing. --- Console.cpp | 31 +++++++++++++++++++++++++++---- Driver.hpp | 1 + Execute.cpp | 8 ++++++++ ObjectiveC/Output.cpp | 43 +++++++++++++++++++++++++++++++++++++++++-- Parser.ypp.in | 5 +++++ String.hpp | 3 +++ 6 files changed, 85 insertions(+), 6 deletions(-) diff --git a/Console.cpp b/Console.cpp index 8f8ff0a..43012ac 100644 --- a/Console.cpp +++ b/Console.cpp @@ -68,6 +68,7 @@ #include #endif +#include "Code.hpp" #include "Driver.hpp" #include "Error.hpp" #include "Highlight.hpp" @@ -291,7 +292,25 @@ static CYUTF8String Run(CYPool &pool, int client, const std::string &code) { static std::ostream *out_; -static void Output(CYUTF8String json, std::ostream *out, bool expand = false) { +static void Output(CYUTF8String json, std::ostream *out, bool expand = false, bool reparse = false) { + CYPool pool; + + if (reparse) do { + CYStream stream(json.data, json.data + json.size); + CYDriver driver(pool, stream); + if (driver.Parse(CYMarkExpression)) + break; + std::stringbuf str; + CYOptions options; + CYOutput out(str, options); + out.pretty_ = true; + out << *driver.context_; + std::string data(str.str()); + json = CYPoolUTF8String(pool, data); + if (json.size == 0) + json.data = NULL; + } while (false); + const char *data(json.data); size_t size(json.size); @@ -607,9 +626,9 @@ static void CYConsolePrepTerm(int meta) { CYConsoleRemapKeys(vi_movement_keymap); } -static void CYOutputRun(const std::string &code, bool expand = false) { +static void CYOutputRun(const std::string &code, bool expand = false, bool reparse = false) { CYPool pool; - Output(Run(pool, client_, code), &std::cout, expand); + Output(Run(pool, client_, code), &std::cout, expand, reparse); } static void Console(CYOptions &options) { @@ -641,6 +660,7 @@ static void Console(CYOptions &options) { bool debug(false); bool expand(false); bool lower(true); + bool reparse(false); out_ = &std::cout; @@ -712,6 +732,9 @@ static void Console(CYOptions &options) { } else if (data == "lower") { lower = !lower; *out_ << "lower == " << (lower ? "true" : "false") << std::endl; + } else if (data == "reparse") { + reparse = !reparse; + *out_ << "reparse == " << (reparse ? "true" : "false") << std::endl; } continue; @@ -775,7 +798,7 @@ static void Console(CYOptions &options) { std::cout << std::endl; } - CYOutputRun(code, expand); + CYOutputRun(code, expand, reparse); } } diff --git a/Driver.hpp b/Driver.hpp index 07188ce..b4f1c1b 100644 --- a/Driver.hpp +++ b/Driver.hpp @@ -41,6 +41,7 @@ struct CYWord; enum CYMark { CYMarkScript, CYMarkModule, + CYMarkExpression, }; class _visible CYDriver { diff --git a/Execute.cpp b/Execute.cpp index 527d2cc..5dbdd58 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -148,6 +148,14 @@ const char *CYPoolCString(CYPool &pool, CYUTF8String utf8) { return pool.strndup(utf8.data, utf8.size); } +CYUTF8String CYPoolUTF8String(CYPool &pool, CYUTF8String utf8) { + return {pool.strndup(utf8.data, utf8.size), utf8.size}; +} + +_visible CYUTF8String CYPoolUTF8String(CYPool &pool, const std::string &value) { + return {pool.strndup(value.data(), value.size()), value.size()}; +} + CYUTF8String CYPoolUTF8String(CYPool &pool, JSContextRef context, JSStringRef value) { return CYPoolUTF8String(pool, CYCastUTF16String(value)); } diff --git a/ObjectiveC/Output.cpp b/ObjectiveC/Output.cpp index 17a4264..e2bb0f8 100644 --- a/ObjectiveC/Output.cpp +++ b/ObjectiveC/Output.cpp @@ -82,7 +82,46 @@ void CYObjCArray::Output(CYOutput &out, CYFlags flags) const { } void CYObjCDictionary::Output(CYOutput &out, CYFlags flags) const { - out << '@' << '{' << '}'; + unsigned count(0); + CYForEach (pair, pairs_) + ++count; + bool large(count > 8); + + out << '@' << '{'; + if (large) { + out << '\n'; + ++out.indent_; + } + + bool comma(false); + CYForEach (pair, pairs_) { + if (!comma) + comma = true; + else { + out << ','; + if (large) + out << '\n'; + else + out << ' '; + } + + if (large) + out << '\t'; + + pair->key_->Output(out, CYAssign::Precedence_, CYNoFlags); + out << ':' << ' '; + pair->value_->Output(out, CYAssign::Precedence_, CYNoFlags); + } + + if (large && out.pretty_) + out << ','; + + if (large) { + out << '\n'; + --out.indent_; + } + + out << '\t' << '}'; } void CYObjCBlock::Output(CYOutput &out, CYFlags flags) const { @@ -101,7 +140,7 @@ void CYObjCBlock::Output(CYOutput &out, CYFlags flags) const { ++out.indent_; out << code_; --out.indent_; - out << '\n' << '}'; + out << '\t' << '}'; } void CYProtocol::Output(CYOutput &out) const { diff --git a/Parser.ypp.in b/Parser.ypp.in index 4b7751d..c3f2379 100644 --- a/Parser.ypp.in +++ b/Parser.ypp.in @@ -228,6 +228,9 @@ type; }) case CYMarkModule: driver.hold_ = yytranslate_(token::MarkModule); break; + case CYMarkExpression: + driver.hold_ = yytranslate_(token::MarkExpression); + break; } }; @@ -768,12 +771,14 @@ type; }) %start Program %token MarkModule %token MarkScript +%token MarkExpression %% Program : MarkScript Script | MarkModule Module + | MarkExpression Expression[expression] { driver.context_ = $expression; } ; /* Lexer State {{{ */ diff --git a/String.hpp b/String.hpp index 75e659c..fe33fcb 100644 --- a/String.hpp +++ b/String.hpp @@ -88,6 +88,9 @@ bool CYGetOffset(const char *value, ssize_t &index); bool CYStartsWith(const CYUTF8String &haystack, const CYUTF8String &needle); const char *CYPoolCString(CYPool &pool, CYUTF8String utf8); +CYUTF8String CYPoolUTF8String(CYPool &pool, CYUTF8String utf8); +CYUTF8String CYPoolUTF8String(CYPool &pool, const std::string &value); + CYUTF8String CYPoolUTF8String(CYPool &pool, CYUTF16String utf16); CYUTF16String CYPoolUTF16String(CYPool &pool, CYUTF8String utf8); -- 2.47.2