Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""Built-in code patterns and indices-helper rewrite table.
 
A :class:`CodePattern` is a single rewrite rule consumed by the PASS2
generator. ``DEFAULT_PATTERNS`` are always active; the named entries
in :func:`lookup_pattern` are opt-in via ``#use NAME`` directives.
"""
 
from __future__ import annotations
 
import re
from dataclasses import dataclass, field
from re import Pattern
 
# ----------------------------------------------------------------------
# Pattern data class
# ----------------------------------------------------------------------
 
 
@dataclass
class CodePattern:
    """A single rewrite rule for the PASS2 generator.
 
    ``type`` selects how the matcher uses this pattern:
      * "control"  — punctuation tokens: ``(``, ``)``, ``,``, ``{``,
                     ``}``, ``$MAIN {``.
      * "param"    — parameter name introduced by ``#param``.
      * "variable" — bare shader variable identifier.
      * "function" — function call: name followed by ``(``.
      * "method"   — method call on a shader variable: ``var.name(...)``.
      * "property" — property access on a shader variable: ``var.prop``.
    """
 
    type: str
    pattern: str | Pattern[str]
 
    # If present, replaces matched group(s). For function/method/property
    # patterns: index 0 replaces the receiver capture (group 1), index 1
    # replaces the name capture (group 2). A ``None`` element means
    # "leave that group as-is".
    replace: str | list[str | None] | None = None
 
    variable_type: str | None = None  # "shader-variable"
    param_type: str | None = None  # "int"
    arg_types: list[str] = field(default_factory=list)  # "expression"|"string"|"auto"
 
 
# ----------------------------------------------------------------------
# Default control-token patterns (always active)
# ----------------------------------------------------------------------
 
 
DEFAULT_PATTERNS: list[CodePattern] = [
    CodePattern(type="control", pattern=re.compile(r"\(")),
    CodePattern(type="control", pattern=re.compile(r"\)")),
    CodePattern(type="control", pattern=re.compile(r",")),
    CodePattern(type="control", pattern=re.compile(r"\{")),
    CodePattern(type="control", pattern=re.compile(r"\}")),
    CodePattern(
        type="control",
        pattern=re.compile(r"(?<![a-zA-Z0-9_])(\$MAIN\s*\{)"),
    ),
]
 
 
# ----------------------------------------------------------------------
# Built-in patterns (opt-in via #use NAME)
# ----------------------------------------------------------------------
 
 
_BUILT_IN_PATTERNS: list[tuple[str, CodePattern]] = [
    (
        "guardAgainstOutOfBoundsWorkgroupSizes",
        CodePattern(
            type="function",
            pattern=re.compile(r"\b(guardAgainstOutOfBoundsWorkgroupSizes)\s*\("),
            replace=["shader_helper.GuardAgainstOutOfBoundsWorkgroupSizes"],
            arg_types=["string"],
        ),
    ),
    (
        "getElementAt",
        CodePattern(
            type="function",
            pattern=re.compile(r"\b(getElementAt)\s*\("),
            replace=["GetElementAt"],
            arg_types=["string", "auto", "expression", "expression"],
        ),
    ),
]
 
 
# ----------------------------------------------------------------------
# Indices-helper patterns
# ----------------------------------------------------------------------
 
 
_INDICES_HELPER_PATTERNS: list[tuple[str, CodePattern]] = [
    (
        ".rank",
        CodePattern(
            type="property",
            pattern=re.compile(r"\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(rank)\b"),
            replace=[None, "Rank()"],
        ),
    ),
    (
        ".numComponents",
        CodePattern(
            type="property",
            pattern=re.compile(r"\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(numComponents)\b"),
            replace=[None, "NumComponents()"],
        ),
    ),
    (
        ".offsetToIndices",
        CodePattern(
            type="method",
            pattern=re.compile(r"\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(offsetToIndices)\s*\("),
            replace=[None, "OffsetToIndices"],
            arg_types=["string"],
        ),
    ),
    (
        ".indicesToOffset",
        CodePattern(
            type="method",
            pattern=re.compile(r"\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(indicesToOffset)\s*\("),
            replace=[None, "IndicesToOffset"],
            arg_types=["string"],
        ),
    ),
    (
        ".indicesSet",
        CodePattern(
            type="method",
            pattern=re.compile(r"\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(indicesSet)\s*\("),
            replace=[None, "IndicesSet"],
            arg_types=["string", "auto", "auto"],
        ),
    ),
    (
        ".indicesGet",
        CodePattern(
            type="method",
            pattern=re.compile(r"\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(indicesGet)\s*\("),
            replace=[None, "IndicesGet"],
            arg_types=["string", "auto"],
        ),
    ),
    (
        ".set",
        CodePattern(
            type="method",
            pattern=re.compile(r"\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(set)\s*\("),
            replace=[None, "Set"],
        ),
    ),
    (
        ".setByOffset",
        CodePattern(
            type="method",
            pattern=re.compile(r"\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(setByOffset)\s*\("),
            replace=[None, "SetByOffset"],
        ),
    ),
    (
        ".setByIndices",
        CodePattern(
            type="method",
            pattern=re.compile(r"\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(setByIndices)\s*\("),
            replace=[None, "SetByIndices"],
            arg_types=["string", "string"],
        ),
    ),
    (
        ".get",
        CodePattern(
            type="method",
            pattern=re.compile(r"\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(get)\s*\("),
            replace=[None, "Get"],
        ),
    ),
    (
        ".getByOffset",
        CodePattern(
            type="method",
            pattern=re.compile(r"\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(getByOffset)\s*\("),
            replace=[None, "GetByOffset"],
        ),
    ),
    (
        ".getByIndices",
        CodePattern(
            type="method",
            pattern=re.compile(r"\b([_a-zA-Z][_a-zA-Z0-9]*)\s*\.\s*(getByIndices)\s*\("),
            replace=[None, "GetByIndices"],
            arg_types=["string"],
        ),
    ),
]
 
 
_PATTERN_MAP: dict[str, CodePattern] = dict(_BUILT_IN_PATTERNS + _INDICES_HELPER_PATTERNS)
 
 
# ----------------------------------------------------------------------
# Public API
# ----------------------------------------------------------------------
 
 
_IDENTIFIER_RE = re.compile(r"^[a-zA-Z_][a-zA-Z0-9_]*$")
 
 
def lookup_pattern(name: str) -> CodePattern | None:
    """Look up a built-in pattern by ``#use`` name. Returns ``None`` if
    the name isn't recognized."""
    return _PATTERN_MAP.get(name)
 
 
def create_param_pattern(name: str) -> CodePattern:
    """Create a runtime ``#param`` pattern that matches the identifier
    as a whole word. Raises ``ValueError`` if the name is not a valid
    identifier."""
    if not _IDENTIFIER_RE.match(name):
        raise ValueError(
            f'Invalid parameter identifier: "{name}". Parameter names '
            f"must start with a letter or underscore and contain only "
            f"letters, digits, and underscores."
        )
    return CodePattern(type="param", pattern=re.compile(rf"\b{re.escape(name)}\b"))