Skip to content

Commit a23ecb6

Browse files
Your NameShrey-N
authored andcommitted
Fix anchor recovery killing legal triple quoted strings (#21) [Merging from Test Repo]
1 parent c890ca8 commit a23ecb6

3 files changed

Lines changed: 106 additions & 62 deletions

File tree

‎shell_lite/gbp_core_logic.cpp‎

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ TopographyResult phase1_topography_scan(std::string_view source) {
9797
int bracket_depth = 0;
9898
int brace_depth = 0;
9999
char in_quote = 0;
100+
bool in_triple_quote = false;
100101
bool escaped = false;
101102
bool in_comment = false;
102103
bool in_block_comment = false;
@@ -122,7 +123,12 @@ TopographyResult phase1_topography_scan(std::string_view source) {
122123
} else if (in_quote) {
123124
if (escaped) escaped = false;
124125
else if (c == '\\') escaped = true;
125-
else if (c == in_quote) in_quote = 0;
126+
else if (in_triple_quote && c == in_quote && pos + 2 < source.size() && source[pos + 1] == in_quote && source[pos + 2] == in_quote) {
127+
in_quote = 0;
128+
in_triple_quote = false;
129+
pos += 2;
130+
}
131+
else if (!in_triple_quote && c == in_quote) in_quote = 0;
126132
} else {
127133
if (c == '/' && pos + 1 < source.size() && source[pos + 1] == '*') {
128134
in_block_comment = true;
@@ -138,6 +144,10 @@ TopographyResult phase1_topography_scan(std::string_view source) {
138144
continue;
139145
} else if (c == '"' || c == '\'') {
140146
in_quote = c;
147+
if (pos + 2 < source.size() && source[pos + 1] == c && source[pos + 2] == c) {
148+
in_triple_quote = true;
149+
pos += 2;
150+
}
141151
} else if (c == '(') {
142152
paren_depth++;
143153
} else if (c == ')') {
@@ -164,7 +174,7 @@ TopographyResult phase1_topography_scan(std::string_view source) {
164174
bc_recovery_pos = next_line_start;
165175
bc_recovery_line = line_count + 1;
166176
}
167-
} else if (is_anchor_line(source, next_line_start)) {
177+
} else if (!in_triple_quote && is_anchor_line(source, next_line_start)) {
168178
if (in_quote != 0) {
169179
std::string sl = extract_source_line(source, start_line);
170180
result.diagnostics.push_back(SyntaxError("Unclosed string literal (missing closing " + std::string(1, in_quote) + ")",
@@ -269,23 +279,22 @@ struct ChunkScanResult {
269279
std::vector<GeoNode> nodes;
270280
std::vector<SyntaxError> diagnostics;
271281
char trailing_in_quote = 0;
282+
bool trailing_in_triple_quote = false;
272283
bool trailing_escaped = false;
273284
bool trailing_in_comment = false;
274285
bool trailing_in_block_comment = false;
275286
int trailing_paren_depth = 0;
276287
int trailing_bracket_depth = 0;
277288
int trailing_brace_depth = 0;
278289
int trailing_start_line = 1;
290+
int line_count = 1;
279291
bool has_anchor = false;
280292
int first_anchor_line = 0;
281293
size_t first_anchor_node_index = 0;
282-
int line_count = 1;
283294
};
284295

285296
static ChunkScanResult scan_chunk(std::string_view source, size_t chunk_start, size_t chunk_end, int start_line_num) {
286297
ChunkScanResult result;
287-
result.nodes.reserve((chunk_end - chunk_start) / 40);
288-
289298
size_t pos = chunk_start;
290299
size_t node_start = chunk_start;
291300
int line_count = start_line_num;
@@ -295,6 +304,7 @@ static ChunkScanResult scan_chunk(std::string_view source, size_t chunk_start, s
295304
int bracket_depth = 0;
296305
int brace_depth = 0;
297306
char in_quote = 0;
307+
bool in_triple_quote = false;
298308
bool escaped = false;
299309
bool in_comment = false;
300310
bool in_block_comment = false;
@@ -320,7 +330,12 @@ static ChunkScanResult scan_chunk(std::string_view source, size_t chunk_start, s
320330
} else if (in_quote) {
321331
if (escaped) escaped = false;
322332
else if (c == '\\') escaped = true;
323-
else if (c == in_quote) in_quote = 0;
333+
else if (in_triple_quote && c == in_quote && pos + 2 < source.size() && source[pos + 1] == in_quote && source[pos + 2] == in_quote) {
334+
in_quote = 0;
335+
in_triple_quote = false;
336+
pos += 2;
337+
}
338+
else if (!in_triple_quote && c == in_quote) in_quote = 0;
324339
} else {
325340
if (c == '/' && pos + 1 < source.size() && source[pos + 1] == '*') {
326341
in_block_comment = true;
@@ -336,6 +351,10 @@ static ChunkScanResult scan_chunk(std::string_view source, size_t chunk_start, s
336351
continue;
337352
} else if (c == '"' || c == '\'') {
338353
in_quote = c;
354+
if (pos + 2 < source.size() && source[pos + 1] == c && source[pos + 2] == c) {
355+
in_triple_quote = true;
356+
pos += 2;
357+
}
339358
} else if (c == '(') {
340359
paren_depth++;
341360
} else if (c == ')') {
@@ -355,15 +374,14 @@ static ChunkScanResult scan_chunk(std::string_view source, size_t chunk_start, s
355374
bool in_anomaly = (paren_depth > 0 || bracket_depth > 0 || brace_depth > 0 || in_quote != 0 || in_block_comment);
356375

357376
if (in_anomaly) {
358-
size_t next_line_start = (pos < chunk_end) ? pos + 1 : pos;
359-
if (in_block_comment) {
360-
if (!has_bc_recovery && is_anchor_line(source, next_line_start)) {
361-
has_bc_recovery = true;
362-
bc_recovery_pos = next_line_start;
363-
bc_recovery_line = line_count + 1;
364-
}
365-
} else if (is_anchor_line(source, next_line_start)) {
366-
377+
size_t next_line_start = (pos < chunk_end) ? pos + 1 : pos;
378+
if (in_block_comment) {
379+
if (!has_bc_recovery && is_anchor_line(source, next_line_start)) {
380+
has_bc_recovery = true;
381+
bc_recovery_pos = next_line_start;
382+
bc_recovery_line = line_count + 1;
383+
}
384+
} else if (!in_triple_quote && is_anchor_line(source, next_line_start)) {
367385
if (in_quote != 0) {
368386
std::string sl = extract_source_line(source, current_start_line);
369387
result.diagnostics.push_back(SyntaxError("Unclosed string literal (missing closing " + std::string(1, in_quote) + ")",
@@ -390,12 +408,6 @@ static ChunkScanResult scan_chunk(std::string_view source, size_t chunk_start, s
390408
in_comment = false;
391409
in_block_comment = false;
392410

393-
if (!result.has_anchor) {
394-
result.has_anchor = true;
395-
result.first_anchor_line = line_count + 1;
396-
result.first_anchor_node_index = result.nodes.size();
397-
}
398-
399411
node_start = pos + 1;
400412
current_start_line = line_count + 1;
401413
line_count++;
@@ -420,7 +432,7 @@ static ChunkScanResult scan_chunk(std::string_view source, size_t chunk_start, s
420432
SourceLocation{"", current_start_line, 1, sl, "use either tabs or spaces consistently"}));
421433
}
422434
check_mixed_tabs_continuations(raw_block, current_start_line, source, result.diagnostics);
423-
if (!result.has_anchor && indent == 0 && is_anchor_line(source, node_start)) {
435+
if (is_anchor_line(source, node_start)) {
424436
result.has_anchor = true;
425437
result.first_anchor_line = current_start_line;
426438
result.first_anchor_node_index = result.nodes.size();
@@ -454,6 +466,7 @@ static ChunkScanResult scan_chunk(std::string_view source, size_t chunk_start, s
454466
result.first_anchor_node_index = base + tail.first_anchor_node_index;
455467
}
456468
in_quote = tail.trailing_in_quote;
469+
in_triple_quote = tail.trailing_in_triple_quote;
457470
escaped = tail.trailing_escaped;
458471
in_comment = tail.trailing_in_comment;
459472
in_block_comment = tail.trailing_in_block_comment;
@@ -465,6 +478,7 @@ static ChunkScanResult scan_chunk(std::string_view source, size_t chunk_start, s
465478
}
466479

467480
result.trailing_in_quote = in_quote;
481+
result.trailing_in_triple_quote = in_triple_quote;
468482
result.trailing_escaped = escaped;
469483
result.trailing_in_comment = in_comment;
470484
result.trailing_in_block_comment = in_block_comment;
@@ -672,63 +686,49 @@ TopographyResult phase1_topography_scan_parallel(std::string_view source, size_t
672686
return final_result;
673687
}
674688

675-
676689
GPDAStackManager::GPDAStackManager(std::vector<GeoNode>* nodes) : nodes_ref(nodes) {
677-
if (nodes) {
678-
stack.reserve(nodes->size());
679-
}
690+
stack.reserve(64);
680691
}
681692

682-
GPDAStackManager::~GPDAStackManager() {
683-
clear();
684-
}
693+
GPDAStackManager::~GPDAStackManager() = default;
694+
GPDAStackManager::GPDAStackManager(GPDAStackManager&& other) noexcept = default;
695+
GPDAStackManager& GPDAStackManager::operator=(GPDAStackManager&& other) noexcept = default;
685696

686-
GPDAStackManager::GPDAStackManager(GPDAStackManager&& other) noexcept
687-
: stack(std::move(other.stack)), nodes_ref(other.nodes_ref) {
688-
other.nodes_ref = nullptr;
697+
void GPDAStackManager::clear() {
698+
stack.clear();
689699
}
690700

691-
GPDAStackManager& GPDAStackManager::operator=(GPDAStackManager&& other) noexcept {
692-
if (this != &other) {
693-
stack = std::move(other.stack);
694-
nodes_ref = other.nodes_ref;
695-
other.nodes_ref = nullptr;
701+
void GPDAStackManager::process_node(int node_index) {
702+
if (!nodes_ref || node_index < 0 || static_cast<size_t>(node_index) >= nodes_ref->size()) {
703+
return;
696704
}
697-
return *this;
698-
}
699705

700-
void GPDAStackManager::process_node(int node_index) {
701-
if (!nodes_ref) return;
702-
auto& nodes = *nodes_ref;
703-
704-
int target_indent = nodes[node_index].indent_level;
706+
GeoNode& current_node = (*nodes_ref)[node_index];
705707

706-
while (!stack.empty() && target_indent <= nodes[stack.back()].indent_level) {
707-
stack.pop_back();
708-
}
708+
while (!stack.empty()) {
709+
int top_index = stack.back();
710+
const GeoNode& top_node = (*nodes_ref)[top_index];
709711

710-
if (!stack.empty()) {
711-
int parent_idx = stack.back();
712-
nodes[node_index].parent_index = parent_idx;
713-
nodes[parent_idx].child_indices.push_back(node_index);
714-
} else {
715-
nodes[node_index].parent_index = -1;
712+
if (top_node.indent_level < current_node.indent_level) {
713+
current_node.parent_index = top_index;
714+
(*nodes_ref)[top_index].child_indices.push_back(node_index);
715+
break;
716+
} else {
717+
stack.pop_back();
718+
}
716719
}
717-
stack.push_back(node_index);
718-
}
719720

720-
void GPDAStackManager::clear() {
721-
stack.clear();
722-
nodes_ref = nullptr;
721+
stack.push_back(node_index);
723722
}
724723

725724
void phase2_topology_linking(std::vector<GeoNode>& nodes) {
726725
if (nodes.empty()) return;
727726

728-
GPDAStackManager manager(&nodes);
727+
GPDAStackManager stack_manager(&nodes);
728+
729729
for (size_t i = 0; i < nodes.size(); ++i) {
730-
manager.process_node(static_cast<int>(i));
730+
stack_manager.process_node(static_cast<int>(i));
731731
}
732732
}
733733

734-
}
734+
} // namespace shell_lite

‎shell_lite/lexer.cpp‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,30 @@ Token Lexer::read_string() {
231231
char quote = advance();
232232
size_t start = pos_;
233233
int start_col = col_ - 1;
234+
int start_line = line_;
235+
bool triple = peek() == quote && peek_next() == quote;
236+
if (triple) {
237+
advance();
238+
advance();
239+
start = pos_;
240+
while (!is_at_end()) {
241+
if (peek() == '\\') {
242+
advance();
243+
if (is_at_end()) break;
244+
advance();
245+
continue;
246+
}
247+
if (peek() == quote && pos_ + 2 < source_.size() && source_[pos_ + 1] == quote && source_[pos_ + 2] == quote) break;
248+
advance();
249+
}
250+
std::string_view value = source_.substr(start, pos_ - start);
251+
if (!is_at_end()) {
252+
advance();
253+
advance();
254+
advance();
255+
}
256+
return Token(TokenType::TOK_STRING, value, start_line, start_col);
257+
}
234258
while (!is_at_end() && peek() != quote) {
235259
if (peek() == '\\') {
236260
advance();
@@ -240,7 +264,7 @@ Token Lexer::read_string() {
240264
}
241265
std::string_view value = source_.substr(start, pos_ - start);
242266
if (!is_at_end()) advance();
243-
return Token(TokenType::TOK_STRING, value, line_, start_col);
267+
return Token(TokenType::TOK_STRING, value, start_line, start_col);
244268
}
245269

246270
Token Lexer::read_operator() {

‎tests/test_recovery.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@ def test_unclosed_quote(self):
2424
run_shl_code(code)
2525
self.assertIn("Unclosed string literal", str(ctx.exception))
2626

27+
def test_triple_quoted_anchor_line(self):
28+
code = '''to get_msg()
29+
give """line one
30+
to be continued"""
31+
32+
say get_msg()
33+
'''
34+
output = run_shl_code(code).strip()
35+
self.assertEqual(output, "line one\nto be continued")
36+
37+
def test_triple_single_quoted_anchor_line(self):
38+
code = """to get_msg()
39+
give '''alpha
40+
from the other side'''
41+
42+
say get_msg()
43+
"""
44+
output = run_shl_code(code).strip()
45+
self.assertEqual(output, "alpha\nfrom the other side")
46+
2747
def test_unclosed_paren(self):
2848
code = """val = (100 + 200 + 300
2949
structure Counter

0 commit comments

Comments
 (0)