Skip to content

Commit 5ac7ae2

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 538788f commit 5ac7ae2

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
@@ -1089,7 +1089,7 @@ std::any ParserVisitor::visitCreateMessage(
10891089
} else {
10901090
name = absl::StrJoin(parts, ".");
10911091
}
1092-
int64_t obj_id = factory_.NextId(SourceRangeFromToken(ctx->op));
1092+
int64_t obj_id = factory_.NextId(SourceRangeFromParserRuleContext(ctx));
10931093
std::vector<StructExprField> fields;
10941094
if (ctx->entries) {
10951095
fields = visitFields(ctx->entries);
@@ -1191,7 +1191,7 @@ std::any ParserVisitor::visitNested(CelParser::NestedContext* ctx) {
11911191
}
11921192

11931193
std::any ParserVisitor::visitCreateList(CelParser::CreateListContext* ctx) {
1194-
int64_t list_id = factory_.NextId(SourceRangeFromToken(ctx->op));
1194+
int64_t list_id = factory_.NextId(SourceRangeFromParserRuleContext(ctx));
11951195
auto elems = visitList(ctx->elems);
11961196
return ExprToAny(factory_.NewList(list_id, std::move(elems)));
11971197
}
@@ -1229,7 +1229,7 @@ std::vector<Expr> ParserVisitor::visitList(CelParser::ExprListContext* ctx) {
12291229
}
12301230

12311231
std::any ParserVisitor::visitCreateMap(CelParser::CreateMapContext* ctx) {
1232-
int64_t struct_id = factory_.NextId(SourceRangeFromToken(ctx->op));
1232+
int64_t struct_id = factory_.NextId(SourceRangeFromParserRuleContext(ctx));
12331233
std::vector<MapExprEntry> entries;
12341234
if (ctx->entries) {
12351235
entries = visitEntries(ctx->entries);

parser/parser_test.cc

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

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

0 commit comments

Comments
 (0)