packages/sql-semantics-sqlite/src/Lowering/Leaf/LiteralRule.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\Sqlite\Lowering\Leaf;
6
7use SqlParser\Lexer\Token;
8use SqlParser\Parser\Node;
9use SqlSemantics\Platform\Sqlite\Lowering\Lowering;
10use SqlSemantics\Platform\Sqlite\Model\Diagnostic\Check;
11use SqlSemantics\Platform\Sqlite\Model\Diagnostic\ImplementationGap;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Scalar;
13use SqlSemantics\Platform\Sqlite\Rules\Identifiers;
14use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\BlobLiteral;
15use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\CurrentTime;
16use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\HexLiteral;
17use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
18use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\NullLiteral;
19use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\RealLiteral;
20use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\TextLiteral;
21use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\TimeKeyword;
22
23/**
24 * Lowers literal tokens into exact literal values.
25 *
26 * Rule: SQLITE-LITERAL-001. Scope: term and the numeric token classes. A
27 * numeric token loses its digit separators and becomes a decimal integer, a
28 * hexadecimal integer or a floating point literal by its spelling; a string
29 * is decoded; a BLOB keeps its hexadecimal digits in upper case. Each literal
30 * is recorded as an operand leaf.
31 * Source: https://sqlite.org/lang_expr.html#literal_values_constants_.
32 * Status: Implemented.
33 *
34 * @visibility SqlSemantics\Platform\Sqlite
35 */
36final class LiteralRule
37{
38    /**
39     * @param Lowering $lowering The lowering this rule belongs to
40     */
41    public function __construct(private readonly Lowering $lowering)
42    {
43    }
44
45    /**
46     * Lowers a literal term.
47     *
48     * @throws ImplementationGap When the production has no rule
49     */
50    public function term(Node $term): Scalar
51    {
52        $form = $this->lowering->productions->form($term);
53        $token = $form->token(0);
54
55        return match ($form->signature) {
56            'term: NULL|FLOAT|BLOB' => $this->plain($token),
57            'term: STRING' => $this->lowering->leaves->record(new TextLiteral((new Identifiers())->decode($token->text))),
58            'term: INTEGER', 'term: QNUMBER' => $this->number($token),
59            'term: CTIME_KW' => $this->lowering->leaves->record(new CurrentTime(TimeKeyword::from(strtoupper($token->text)))),
60            default => throw ImplementationGap::production($form),
61        };
62    }
63
64    /**
65     * Lowers the token of the NULL, floating point and BLOB class by its terminal.
66     */
67    public function plain(Token $token): Scalar
68    {
69        if ($token->name === 'NULL') {
70            return $this->lowering->leaves->record(new NullLiteral());
71        }
72        if ($token->name === 'BLOB') {
73            return $this->lowering->leaves->record(new BlobLiteral(strtoupper(substr($token->text, 2, -1))));
74        }
75
76        return $this->number($token);
77    }
78
79    /**
80     * Lowers a numeric token: a decimal integer, a hexadecimal integer or a floating point literal.
81     */
82    public function number(Token $token): IntegerLiteral|HexLiteral|RealLiteral
83    {
84        $text = str_replace('_', '', $token->text);
85        if (preg_match('/\A0[xX]([0-9A-Fa-f]+)\z/', $text, $hex) === 1) {
86            return $this->lowering->leaves->record(new HexLiteral(strtoupper($hex[1])));
87        }
88        if (preg_match('/\A[0-9]+\z/', $text) === 1) {
89            return $this->lowering->leaves->record(new IntegerLiteral($text));
90        }
91        $matched = preg_match('/\A([0-9]*)(?:\.([0-9]*))?(?:[eE]([+-]?[0-9]+))?\z/', $text, $parts, PREG_UNMATCHED_AS_NULL);
92        Check::invariant($matched === 1, 'The numeric token has no numeric spelling: ' . $token->text);
93
94        return $this->lowering->leaves->record(new RealLiteral($parts[1], $parts[2] ?? null, $parts[3] ?? null));
95    }
96}
97