Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jsonnetfmt --debug-desugaring #1132

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ struct LiteralNumber : public AST {
/** Represents JSON strings. */
struct LiteralString : public AST {
UString value;
enum TokenKind { SINGLE, DOUBLE, BLOCK, VERBATIM_SINGLE, VERBATIM_DOUBLE };
enum TokenKind { SINGLE, DOUBLE, BLOCK, VERBATIM_SINGLE, VERBATIM_DOUBLE, RAW_DESUGARED };
TokenKind tokenKind;
std::string blockIndent; // Only contains ' ' and '\t'.
std::string blockTermIndent; // Only contains ' ' and '\t'.
Expand Down
9 changes: 5 additions & 4 deletions core/desugarer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ class Desugarer {

LiteralString *str(const UString &s)
{
return make<LiteralString>(E, EF, s, LiteralString::DOUBLE, "", "");
return make<LiteralString>(E, EF, s, LiteralString::RAW_DESUGARED, "", "");
}

LiteralString *str(const LocationRange &loc, const UString &s)
{
return make<LiteralString>(loc, EF, s, LiteralString::DOUBLE, "", "");
return make<LiteralString>(loc, EF, s, LiteralString::RAW_DESUGARED, "", "");
}

LiteralNull *null(void)
Expand Down Expand Up @@ -851,12 +851,13 @@ class Desugarer {
// Nothing to do.

} else if (auto *ast = dynamic_cast<LiteralString *>(ast_)) {
if ((ast->tokenKind != LiteralString::BLOCK) &&
if ((ast->tokenKind != LiteralString::RAW_DESUGARED) &&
(ast->tokenKind != LiteralString::BLOCK) &&
(ast->tokenKind != LiteralString::VERBATIM_DOUBLE) &&
(ast->tokenKind != LiteralString::VERBATIM_SINGLE)) {
ast->value = jsonnet_string_unescape(ast->location, ast->value);
}
ast->tokenKind = LiteralString::DOUBLE;
ast->tokenKind = LiteralString::RAW_DESUGARED;
ast->blockIndent.clear();

} else if (dynamic_cast<const LiteralNull *>(ast_)) {
Expand Down
3 changes: 3 additions & 0 deletions core/formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ class Unparser {
o << ast->originalString;

} else if (auto *ast = dynamic_cast<const LiteralString *>(ast_)) {
assert(ast->tokenKind != LiteralString::RAW_DESUGARED);
if (ast->tokenKind == LiteralString::DOUBLE) {
o << "\"";
o << encode_utf8(ast->value);
Expand Down Expand Up @@ -647,6 +648,7 @@ class EnforceStringStyle : public FmtPass {
EnforceStringStyle(Allocator &alloc, const FmtOpts &opts) : FmtPass(alloc, opts) {}
void visit(LiteralString *lit)
{
assert(lit->tokenKind != LiteralString::RAW_DESUGARED);
if (lit->tokenKind == LiteralString::BLOCK)
return;
if (lit->tokenKind == LiteralString::VERBATIM_DOUBLE)
Expand Down Expand Up @@ -1881,6 +1883,7 @@ class FixIndentation {
column += ast->originalString.length();

} else if (auto *ast = dynamic_cast<LiteralString *>(ast_)) {
assert(ast->tokenKind != LiteralString::RAW_DESUGARED);
if (ast->tokenKind == LiteralString::DOUBLE) {
column += 2 + ast->value.length(); // Include quotes
} else if (ast->tokenKind == LiteralString::SINGLE) {
Expand Down
34 changes: 33 additions & 1 deletion core/libjsonnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,49 @@ extern "C" {
#include "parser.h"
#include "static_analysis.h"
#include "vm.h"
#include "pass.h"
#include "string_utils.h"

namespace {
using ::jsonnet::internal::Allocator;
using ::jsonnet::internal::AST;
using ::jsonnet::internal::FmtOpts;
using ::jsonnet::internal::Fodder;
using ::jsonnet::internal::jsonnet_lex;
using ::jsonnet::internal::jsonnet_string_escape;
using ::jsonnet::internal::RuntimeError;
using ::jsonnet::internal::StaticError;
using ::jsonnet::internal::Tokens;
using ::jsonnet::internal::VmExt;
using ::jsonnet::internal::VmNativeCallback;
using ::jsonnet::internal::VmNativeCallbackMap;
using ::jsonnet::internal::CompilerPass;
using ::jsonnet::internal::LiteralString;

// Used in fmtDebugDesugaring mode to ensure the AST can be pretty-printed.
class ReEscapeStrings : public CompilerPass {
johnbartholomew marked this conversation as resolved.
Show resolved Hide resolved
using CompilerPass::visit;
public:
ReEscapeStrings(Allocator &alloc) : CompilerPass(alloc) {}
void visit(LiteralString *lit)
{
if (lit->tokenKind != LiteralString::RAW_DESUGARED)
return;

// TODO: Share code with formatter.cpp EnforceStringStyle.
unsigned num_single = 0, num_double = 0;
for (char32_t c : lit->value) {
if (c == '\'')
num_single++;
if (c == '"')
num_double++;
}
bool use_single = (num_double > 0) && (num_single == 0);

lit->value = jsonnet_string_escape(lit->value, use_single);
lit->tokenKind = use_single ? LiteralString::SINGLE : LiteralString::DOUBLE;
}
};
} // namespace

static void memory_panic(void)
Expand Down Expand Up @@ -450,8 +480,10 @@ static char *jsonnet_fmt_snippet_aux(JsonnetVm *vm, const char *filename, const
expr = jsonnet_parse(&alloc, tokens);
Fodder final_fodder = tokens.front().fodder;

if (vm->fmtDebugDesugaring)
if (vm->fmtDebugDesugaring) {
jsonnet_desugar(&alloc, expr, &vm->tla);
ReEscapeStrings(alloc).expr(expr);
}

json_str = jsonnet_fmt(expr, final_fodder, vm->fmtOpts);

Expand Down