Skip to content
Open
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
84 changes: 66 additions & 18 deletions core/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,27 @@ static int whitespace_check(const char *a, const char *b)
return i;
}

static void describe_whitespace(std::stringstream& msg, const std::string& ws) {
int spaces = 0;
int tabs = 0;
for (char c : ws) {
if (c == ' ')
spaces++;
else if (c == '\t')
tabs++;
}
if (spaces > 0 && tabs > 0) {
msg << spaces << (spaces == 1 ? " space" : " spaces") << " and " << tabs
<< (tabs == 1 ? " tab" : " tabs");
} else if (spaces > 0) {
msg << spaces << (spaces == 1 ? " space" : " spaces");
} else if (tabs > 0) {
msg << tabs << (tabs == 1 ? " tab" : " tabs");
} else {
msg << "no indentation";
}
}

/*
static void add_whitespace(Fodder &fodder, const char *s, size_t n)
{
Expand Down Expand Up @@ -755,25 +776,52 @@ Tokens jsonnet_lex(const std::string &filename, const char *input)
// Examine next line
ws_chars = whitespace_check(first_line, c);
if (ws_chars == 0) {
// End of text block
// Skip over any whitespace
while (*c == ' ' || *c == '\t') {
string_block_term_indent += *c;
++c;
}
// Expect |||
if (!(*c == '|' && *(c + 1) == '|' && *(c + 2) == '|')) {
auto msg = "text block not terminated with |||";
throw StaticError(filename, begin, msg);
// End of text block (or indentation error).
// Count actual whitespace on this line.
int actual_ws = 0;
while (c[actual_ws] == ' ' ||
c[actual_ws] == '\t') {
actual_ws++;
}

// Check if this is the terminator |||
bool is_terminator = (c[actual_ws] == '|' &&
c[actual_ws + 1] == '|' &&
c[actual_ws + 2] == '|');

if (!is_terminator) {
// Not a terminator - check if it's an
// indentation issue.
if (actual_ws > 0) {
// Has whitespace but doesn't match expected
// indentation.
std::stringstream msg;
msg << "text block indentation mismatch: "
"expected at least ";
describe_whitespace(msg, string_block_indent);
msg << ", found ";
describe_whitespace(
msg, std::string(c, actual_ws));
throw StaticError(filename, begin, msg.str());
} else {
// No whitespace and no ||| - missing
// terminator.
auto msg =
"text block not terminated with |||";
throw StaticError(filename, begin, msg);
}
c += 3; // Leave after the last |
data = block.str();
kind = Token::STRING_BLOCK;
if (chomp_trailing_nl) {
assert(data.back() == '\n');
data.pop_back();
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the chomp_trailing_nl logic got dropped. I suspect that's causing the test failures (but haven't verified)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry, this is still WIP. Will fix soon.

break; // Out of the while loop.
}

// Valid termination - skip over any whitespace.
while (*c == ' ' || *c == '\t') {
string_block_term_indent += *c;
++c;
}
// Skip the |||
c += 3; // Leave after the last |
data = block.str();
kind = Token::STRING_BLOCK;
break; // Out of the while loop.
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
STATIC ERROR: error.parse.text_block_bad_whitespace.jsonnet:17:1: text block not terminated with |||
STATIC ERROR: error.parse.text_block_bad_whitespace.jsonnet:17:1: text block indentation mismatch: expected at least 4 spaces, found 1 tab
20 changes: 20 additions & 0 deletions test_suite/error.parse.text_block_indent_spaces.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Copyright 2026 Google Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUTHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

|||
foo
bar
|||
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
STATIC ERROR: error.parse.text_block_indent_spaces.jsonnet:17:1: text block indentation mismatch: expected at least 4 spaces, found 2 spaces
Loading