packages/sql-formatter/src/Core/Syntax/Lists.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlFormatter\Core\Syntax;
6
7/**
8 * Marks a clause's list separators while respecting nested expression delimiters.
9 *
10 * @visibility SqlFormatter
11 */
12final class Lists
13{
14    /**
15     * Handles dialect rules that share the generic expression-list production.
16     */
17    public static function mark(Document $document, int $start, int $end): void
18    {
19        $depth = 0;
20        for ($index = $start; $index <= $end; $index++) {
21            $text = $document->tokens[$index]->text;
22            if (in_array($text, ['(', '['], true)) {
23                $depth++;
24            } elseif (in_array($text, [')', ']'], true)) {
25                $depth--;
26            } elseif ($text === ',' && $depth === 0) {
27                $document->commas[$index] = true;
28            }
29        }
30    }
31}
32