# 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"(? 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"))