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

Syntax error from long string inside table constructor brackets #291

Open
hugomg opened this issue Oct 28, 2023 · 1 comment
Open

Syntax error from long string inside table constructor brackets #291

hugomg opened this issue Oct 28, 2023 · 1 comment

Comments

@hugomg
Copy link

hugomg commented Oct 28, 2023

LuaFormatter version 1.3.6 produces invalid output if the input program contains a [[ string inside a [ ] = table constructor.

t = { [ [[abc]] ] = 1}

When we format this, the spaces are removed and the result is a syntax error

t = {[[[abc]]] = 1}

The good news is that LuaFormatter already does the right thing for array brackets such as xs[ [[abc]] ]. Perhaps whatever logic is being used there should also be used for table constructors.

@hugomg
Copy link
Author

hugomg commented Oct 28, 2023

For reference, I think the parts responsible for writing arrays and fields are these ones:

// if table key is a nested string, keep the whitespace
// example:
// x = {}
// x[ [[key]] ] = "value"
if (!expString.empty() && expString[0] == '[') {
cur_writer() << ctx->LSB()->getText();
cur_writer() << commentAfter(ctx->LSB(), " ");
visitExp(ctx->exp());
cur_writer() << commentAfter(ctx->exp(), " ");
cur_writer() << ctx->RSB()->getText();
} else {
cur_writer() << ctx->LSB()->getText();
cur_writer() << commentAfter(ctx->LSB(), "");
visitExp(ctx->exp());
cur_writer() << commentAfter(ctx->exp(), "");
cur_writer() << ctx->RSB()->getText();
}

antlrcpp::Any FormatVisitor::visitField(LuaParser::FieldContext* ctx) {
LOG_FUNCTION_BEGIN();
std::string eq_space = config_.get<bool>("spaces_around_equals_in_field") ? " " : "";
if (ctx->LSB() != nullptr) {
cur_writer() << ctx->LSB()->getText();
cur_writer() << commentAfter(ctx->LSB(), "");
visitExp(ctx->exp()[0]);
cur_writer() << commentAfter(ctx->exp()[0], "");
cur_writer() << ctx->RSB()->getText();
cur_writer() << commentAfter(ctx->RSB(), eq_space);
cur_writer() << ctx->EQL()->getText();
cur_writer() << commentAfter(ctx->EQL(), eq_space);
visitExp(ctx->exp()[1]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant