Skip to content

Commit 7efe3c1

Browse files
seirlcopybara-github
authored andcommitted
Fix source range offsets for lists, maps and messages.
Update the parser to derive the source range for list, map, and struct/message creation expressions from the full parser rule context rather than just the opening token. This ensures the recorded offsets in `EnrichedSourceInfo` span the entire composite expression. PiperOrigin-RevId: 814612738
1 parent d301931 commit 7efe3c1

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

parser/parser.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ std::any ParserVisitor::visitCreateMessage(
11041104
} else {
11051105
name = absl::StrJoin(parts, ".");
11061106
}
1107-
int64_t obj_id = factory_.NextId(SourceRangeFromToken(ctx->op));
1107+
int64_t obj_id = factory_.NextId(SourceRangeFromParserRuleContext(ctx));
11081108
std::vector<StructExprField> fields;
11091109
if (ctx->entries) {
11101110
fields = visitFields(ctx->entries);
@@ -1206,7 +1206,7 @@ std::any ParserVisitor::visitNested(CelParser::NestedContext* ctx) {
12061206
}
12071207

12081208
std::any ParserVisitor::visitCreateList(CelParser::CreateListContext* ctx) {
1209-
int64_t list_id = factory_.NextId(SourceRangeFromToken(ctx->op));
1209+
int64_t list_id = factory_.NextId(SourceRangeFromParserRuleContext(ctx));
12101210
auto elems = visitList(ctx->elems);
12111211
return ExprToAny(factory_.NewList(list_id, std::move(elems)));
12121212
}
@@ -1244,7 +1244,7 @@ std::vector<Expr> ParserVisitor::visitList(CelParser::ExprListContext* ctx) {
12441244
}
12451245

12461246
std::any ParserVisitor::visitCreateMap(CelParser::CreateMapContext* ctx) {
1247-
int64_t struct_id = factory_.NextId(SourceRangeFromToken(ctx->op));
1247+
int64_t struct_id = factory_.NextId(SourceRangeFromParserRuleContext(ctx));
12481248
std::vector<MapExprEntry> entries;
12491249
if (ctx->entries) {
12501250
entries = visitEntries(ctx->entries);

parser/parser_test.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,29 @@ TEST_P(ExpressionTest, Parse) {
15201520
}
15211521
}
15221522

1523+
TEST(ExpressionTest, CompositeExpressionOffsets) {
1524+
ParserOptions options;
1525+
std::vector<Macro> macros = Macro::AllMacros();
1526+
1527+
std::string list_expr = "[1, 2]";
1528+
auto list_result = EnrichedParse(list_expr, macros, "<input>", options);
1529+
ASSERT_THAT(list_result, IsOk());
1530+
auto list_offsets = list_result->enriched_source_info().offsets();
1531+
EXPECT_EQ(list_offsets.at(1), std::make_pair(0, 5));
1532+
1533+
std::string map_expr = "{'a': 1}";
1534+
auto map_result = EnrichedParse(map_expr, macros, "<input>", options);
1535+
ASSERT_THAT(map_result, IsOk());
1536+
auto map_offsets = map_result->enriched_source_info().offsets();
1537+
EXPECT_EQ(map_offsets.at(1), std::make_pair(0, 7));
1538+
1539+
std::string msg_expr = "Msg{f: 1}";
1540+
auto msg_result = EnrichedParse(msg_expr, macros, "<input>", options);
1541+
ASSERT_THAT(msg_result, IsOk());
1542+
auto msg_offsets = msg_result->enriched_source_info().offsets();
1543+
EXPECT_EQ(msg_offsets.at(1), std::make_pair(0, 8));
1544+
}
1545+
15231546
TEST(ExpressionTest, TsanOom) {
15241547
Parse(
15251548
"[[a([[???[a[[??[a([[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["

0 commit comments

Comments
 (0)