@@ -213,10 +213,34 @@ public function parse(Parser $parser, TokensList $list): void
213213 // Length of expression tokens is null by default, in order for the $subList to start
214214 // from $list->idx to the end of the $list.
215215 $ lengthOfExpressionTokens = null ;
216+ $ idxOfDelimiter = null ;
217+
218+ // Keep a terminal delimiter in the subparser, but stop before a delimiter
219+ // that separates this CTE expression from another statement.
220+ if ($ list ->getNextOfType (TokenType::Delimiter)) {
221+ $ delimiterIndex = $ list ->idx - 1 ;
222+ $ nextToken = $ list ->getNext ();
223+ if ($ nextToken !== null && $ nextToken ->token !== '' ) {
224+ $ idxOfDelimiter = $ delimiterIndex ;
225+ $ idxOfLastParsedToken = $ idxOfDelimiter + 1 ;
226+ $ lengthOfExpressionTokens = $ idxOfDelimiter - $ idxBeforeSearch ;
227+ }
228+ }
229+
230+ $ list ->idx = $ idxBeforeSearch ;
231+
232+ $ idxOfOn = null ;
233+ $ searchEnd = $ idxOfDelimiter ?? $ list ->count ;
234+ for ($ i = $ idxBeforeSearch ; $ i < $ searchEnd ; ++$ i ) {
235+ $ candidate = $ list ->tokens [$ i ];
236+ if ($ candidate ->type === TokenType::Keyword && $ candidate ->keyword === 'ON ' ) {
237+ $ idxOfOn = $ i ;
238+ $ list ->idx = $ idxOfOn + 1 ;
239+ break ;
240+ }
241+ }
216242
217- if ($ list ->getNextOfTypeAndValue (TokenType::Keyword, 'ON ' )) {
218- // (-1) because getNextOfTypeAndValue returned ON and increased the index.
219- $ idxOfOn = $ list ->idx - 1 ;
243+ if ($ idxOfOn !== null ) {
220244 // We want to make sure that it's `ON DUPLICATE KEY UPDATE`
221245 $ dubplicateToken = $ list ->getNext ();
222246 $ keyToken = $ list ->getNext ();
@@ -228,7 +252,7 @@ public function parse(Parser $parser, TokensList $list): void
228252 ) {
229253 // Index of the last parsed token will be the token before the ON Keyword
230254 $ idxOfLastParsedToken = $ idxOfOn - 1 ;
231- // The length of the expression tokens would be the difference
255+ // The length of expression tokens would be the difference
232256 // between the first unrelated token `ON` and the idx
233257 // before skipping the CTE tokens.
234258 $ lengthOfExpressionTokens = $ idxOfOn - $ idxBeforeSearch ;
0 commit comments