|
3 | 3 | import re |
4 | 4 | from contextlib import contextmanager |
5 | 5 | from dataclasses import dataclass, replace |
| 6 | +from typing import Optional, Tuple |
6 | 7 |
|
7 | 8 | HEREDOC_PATTERN = re.compile(r"<<([a-zA-Z][a-zA-Z0-9._-]+)\n([\s\S]*)\1", re.S) |
8 | 9 | HEREDOC_TRIM_PATTERN = re.compile(r"<<-([a-zA-Z][a-zA-Z0-9._-]+)\n([\s\S]*)\1", re.S) |
@@ -39,6 +40,81 @@ class SerializationOptions: |
39 | 40 | strip_string_quotes: bool = False |
40 | 41 |
|
41 | 42 |
|
| 43 | +_SIMPLE_ESCAPES = { |
| 44 | + "n": "\n", |
| 45 | + "r": "\r", |
| 46 | + "t": "\t", |
| 47 | + '"': '"', |
| 48 | + "\\": "\\", |
| 49 | +} |
| 50 | +_UNICODE_ESCAPE_WIDTHS = {"u": 4, "U": 8} |
| 51 | +_HEX_DIGITS = frozenset("0123456789abcdefABCDEF") |
| 52 | +_MAX_CODEPOINT = 0x10FFFF |
| 53 | +_SURROGATES = range(0xD800, 0xE000) |
| 54 | + |
| 55 | + |
| 56 | +def _decode_unicode_escape(text: str, index: int) -> Optional[Tuple[str, int]]: |
| 57 | + """Decode a \\uNNNN or \\UNNNNNNNN escape whose marker sits at `index`. |
| 58 | +
|
| 59 | + Returns None for anything that is not a usable character, leaving the |
| 60 | + caller to preserve the escape verbatim: too few digits, a non-hex digit, a |
| 61 | + codepoint past the Unicode maximum (`chr` raises for those), or a lone |
| 62 | + surrogate, which `chr` accepts but which cannot be encoded to UTF-8. |
| 63 | + """ |
| 64 | + width = _UNICODE_ESCAPE_WIDTHS[text[index]] |
| 65 | + digits = text[index + 1 : index + 1 + width] |
| 66 | + if len(digits) != width or any(char not in _HEX_DIGITS for char in digits): |
| 67 | + return None |
| 68 | + codepoint = int(digits, 16) |
| 69 | + if codepoint > _MAX_CODEPOINT or codepoint in _SURROGATES: |
| 70 | + return None |
| 71 | + return chr(codepoint), index + 1 + width |
| 72 | + |
| 73 | + |
| 74 | +def process_escape_sequences(value: str) -> str: |
| 75 | + """Resolve the escape sequences HCL defines inside a quoted template. |
| 76 | +
|
| 77 | + Used when `strip_string_quotes` is set, which asks for the *value* of a |
| 78 | + string rather than its source form. Escapes are resolved in a single pass, |
| 79 | + so an escaped backslash cannot combine with the character after it: `\\\\n` |
| 80 | + is a backslash followed by "n", not a newline. |
| 81 | +
|
| 82 | + An unrecognized escape is preserved verbatim, backslash included. Terraform |
| 83 | + rejects those outright, but the grammar here accepts them, and a serializer |
| 84 | + is the wrong place to raise an error the parser did not. |
| 85 | + """ |
| 86 | + if "\\" not in value: |
| 87 | + return value |
| 88 | + |
| 89 | + parts = [] |
| 90 | + index = 0 |
| 91 | + length = len(value) |
| 92 | + while index < length: |
| 93 | + char = value[index] |
| 94 | + if char != "\\" or index + 1 >= length: |
| 95 | + parts.append(char) |
| 96 | + index += 1 |
| 97 | + continue |
| 98 | + |
| 99 | + marker = value[index + 1] |
| 100 | + if marker in _SIMPLE_ESCAPES: |
| 101 | + parts.append(_SIMPLE_ESCAPES[marker]) |
| 102 | + index += 2 |
| 103 | + continue |
| 104 | + if marker in _UNICODE_ESCAPE_WIDTHS: |
| 105 | + decoded = _decode_unicode_escape(value, index + 1) |
| 106 | + if decoded is not None: |
| 107 | + parts.append(decoded[0]) |
| 108 | + index = decoded[1] |
| 109 | + continue |
| 110 | + |
| 111 | + parts.append(char) |
| 112 | + parts.append(marker) |
| 113 | + index += 2 |
| 114 | + |
| 115 | + return "".join(parts) |
| 116 | + |
| 117 | + |
42 | 118 | @dataclass |
43 | 119 | class SerializationContext: |
44 | 120 | """Mutable state tracked during serialization traversal.""" |
|
0 commit comments