File tree Expand file tree Collapse file tree 2 files changed +23
-4
lines changed Expand file tree Collapse file tree 2 files changed +23
-4
lines changed Original file line number Diff line number Diff line change @@ -3904,13 +3904,17 @@ struct AstOptimizer {
3904
3904
std::shared_ptr<T> parent = nullptr ) {
3905
3905
auto found =
3906
3906
std::find (rules_.begin (), rules_.end (), original->name ) != rules_.end ();
3907
- bool opt = mode_ ? !found : found;
3907
+ auto opt = mode_ ? !found : found;
3908
3908
3909
3909
if (opt && original->nodes .size () == 1 ) {
3910
3910
auto child = optimize (original->nodes [0 ], parent);
3911
- return std::make_shared<T>(*child, original->name .data (),
3912
- original->choice_count , original->position ,
3913
- original->length , original->choice );
3911
+ auto ast = std::make_shared<T>(*child, original->name .data (),
3912
+ original->choice_count , original->position ,
3913
+ original->length , original->choice );
3914
+ for (auto node : ast->nodes ) {
3915
+ node->parent = ast;
3916
+ }
3917
+ return ast;
3914
3918
}
3915
3919
3916
3920
auto ast = std::make_shared<T>(*original);
Original file line number Diff line number Diff line change @@ -967,3 +967,18 @@ TEST(GeneralTest, token_to_number_float_test) {
967
967
EXPECT_TRUE (ast->nodes .empty ());
968
968
}
969
969
970
+ TEST (GeneralTest, ParentReferencesShouldNotBeExpired) {
971
+ auto parser = peg::parser (R"(
972
+ ROOT <- OPTIMIZES_AWAY
973
+ OPTIMIZES_AWAY <- ITEM+
974
+ ITEM <- 'a'
975
+ )" );
976
+ parser.enable_ast <peg::Ast>();
977
+
978
+ std::shared_ptr<peg::Ast> ast;
979
+ parser.parse (" aaa" , ast);
980
+ ast = parser.optimize_ast (ast);
981
+
982
+ EXPECT_FALSE (ast->nodes [0 ]->parent .expired ());
983
+ }
984
+
You can’t perform that action at this time.
0 commit comments