|
2 | 2 | from dataclasses import dataclass
|
3 | 3 | from enum import StrEnum, auto
|
4 | 4 | from typing import TypeAlias, NamedTuple, Union, Optional
|
5 |
| - |
| 5 | +import re |
6 | 6 | import cedarscript_grammar
|
7 | 7 | from tree_sitter import Parser
|
8 | 8 |
|
@@ -243,20 +243,20 @@ class LoopControl(StrEnum):
|
243 | 243 | class CaseWhen:
|
244 | 244 | """Represents a WHEN condition in a CASE statement"""
|
245 | 245 | empty: bool = False
|
246 |
| - regex: Optional[str] = None |
247 |
| - prefix: Optional[str] = None |
248 |
| - suffix: Optional[str] = None |
249 |
| - indent_level: Optional[int] = None |
250 |
| - line_number: Optional[int] = None |
| 246 | + regex: str | None = None |
| 247 | + prefix: str | None = None |
| 248 | + suffix: str | None = None |
| 249 | + indent_level: int | None = None |
| 250 | + line_number: int | None = None |
251 | 251 |
|
252 | 252 |
|
253 | 253 | @dataclass
|
254 | 254 | class CaseAction:
|
255 | 255 | """Represents a THEN action in a CASE statement"""
|
256 |
| - loop_control: Optional[LoopControl] = None |
| 256 | + loop_control: LoopControl | None = None |
257 | 257 | remove: bool = False
|
258 |
| - replace: Optional[str] = None |
259 |
| - indent: Optional[int] = None |
| 258 | + replace: str | None = None |
| 259 | + indent: int | None = None |
260 | 260 | content: Optional[str | tuple[Region, int | None]] = None
|
261 | 261 |
|
262 | 262 |
|
@@ -689,7 +689,7 @@ def parse_case_when(self, node) -> CaseWhen:
|
689 | 689 | if self.find_first_by_field_name(node, 'empty'):
|
690 | 690 | when.empty = True
|
691 | 691 | elif regex := self.find_first_by_field_name(node, 'regex'):
|
692 |
| - when.regex = self.parse_string(regex) |
| 692 | + when.regex = re.compile(self.parse_string(regex)) |
693 | 693 | elif prefix := self.find_first_by_field_name(node, 'prefix'):
|
694 | 694 | when.prefix = self.parse_string(prefix)
|
695 | 695 | elif suffix := self.find_first_by_field_name(node, 'suffix'):
|
@@ -784,12 +784,12 @@ def parse_string(node):
|
784 | 784 | case x if x.startswith("r'''") or x.startswith('r"""'):
|
785 | 785 | text = text[4:-3]
|
786 | 786 | case x if x.startswith("r'") or x.startswith('r"'):
|
787 |
| - text = text[3:-1] |
| 787 | + text = text[2:-1] |
788 | 788 | case _:
|
789 | 789 | raise ValueError(f"Invalid raw string: `{text}`")
|
790 | 790 | case 'single_quoted_string':
|
791 | 791 | text = text[1:-1] # Remove surrounding quotes
|
792 |
| - text = text.replace("\\'", "'").replace('\\"', '"').replace("\\t", '\t') |
| 792 | + text = text.replace(r"\'", "'").replace(r'\"', '"').replace(r"\t", '\t').replace("\\\\", "\\") |
793 | 793 | case 'multi_line_string':
|
794 | 794 | text = text[3:-3]
|
795 | 795 |
|
|
0 commit comments