@@ -58,24 +58,28 @@ public override bool TryParse(ref ParseContext context, [NotNullWhen(true)] out
5858 return true ;
5959 }
6060
61+ var position = context . Position ;
62+
6163 if ( ! parser . TryParse ( ref context , out var result ) )
6264 break ;
6365
64- // Prevent infinite loop with zero-width match
65- if ( context . MatchedSegment . Length == 0 )
66+ list . Add ( result ) ;
67+
68+ //
69+ // Prevent infinite loop
70+ //
71+ if ( context . Position == position )
6672 {
6773 //
6874 // Parsing failed in this case because:
69- // 1. The main parser matched zero length, so the position remains unchanged.
75+ // 1. The main parser matched, but the position remained unchanged.
7076 // 2. The terminator didn't match before, and it won't match now.
7177 // 3. Rechecking would yield the same result, making it redundant.
72- // 4. Without a terminator, parsing is considered unsuccessful, and it will never be matched now.
73- // 5. With a zero-width match , it results in an infinite loop.
78+ // 4. Without a terminator, parsing is considered unsuccessful, and it will never match now.
79+ // 5. If a parser matches but the position remains unchanged , it results in an infinite loop.
7480 //
7581 break ;
7682 }
77-
78- list . Add ( result ) ;
7983 }
8084
8185 value = null ;
@@ -115,19 +119,23 @@ public override bool TryParse(ref ParseContext context, out Unit value)
115119 return true ;
116120 }
117121
122+ var position = context . Position ;
123+
118124 if ( ! parser . TryParse ( ref context , out value ) )
119125 break ;
120126
121- // Prevent infinite loop with zero-width match
122- if ( context . MatchedSegment . Length == 0 )
127+ //
128+ // Prevent infinite loop
129+ //
130+ if ( context . Position == position )
123131 {
124132 //
125133 // Parsing failed in this case because:
126- // 1. The main parser matched zero length, so the position remains unchanged.
134+ // 1. The main parser matched, but the position remained unchanged.
127135 // 2. The terminator didn't match before, and it won't match now.
128136 // 3. Rechecking would yield the same result, making it redundant.
129- // 4. Without a terminator, parsing is considered unsuccessful, and it will never be matched now.
130- // 5. With a zero-width match , it results in an infinite loop.
137+ // 4. Without a terminator, parsing is considered unsuccessful, and it will never match now.
138+ // 5. If a parser matches but the position remains unchanged , it results in an infinite loop.
131139 //
132140 break ;
133141 }
0 commit comments