| 1 |
# Copyright Mozilla Foundation |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
from __future__ import annotations |
| 16 |
|
| 17 |
from dataclasses import dataclass, field |
| 18 |
from typing import Dict, List, Literal, Tuple, Union |
| 19 |
|
| 20 |
__all__ = [ |
| 21 |
"CatchallKey", |
| 22 |
"Declaration", |
| 23 |
"Expression", |
| 24 |
"FunctionAnnotation", |
| 25 |
"Markup", |
| 26 |
"Message", |
| 27 |
"Pattern", |
| 28 |
"PatternMessage", |
| 29 |
"SelectMessage", |
| 30 |
"UnsupportedAnnotation", |
| 31 |
"UnsupportedStatement", |
| 32 |
"VariableRef", |
| 33 |
"Variants", |
| 34 |
] |
| 35 |
|
| 36 |
|
| 37 |
@dataclass |
| 38 |
class VariableRef: |
| 39 |
name: str |
| 40 |
|
| 41 |
|
| 42 |
@dataclass |
| 43 |
class FunctionAnnotation: |
| 44 |
name: str |
| 45 |
options: dict[str, str | VariableRef] = field(default_factory=dict) |
| 46 |
|
| 47 |
|
| 48 |
@dataclass |
| 49 |
class UnsupportedAnnotation: |
| 50 |
source: str |
| 51 |
""" |
| 52 |
The "raw" value (i.e. escape sequences are not processed). |
| 53 |
""" |
| 54 |
|
| 55 |
|
| 56 |
@dataclass |
| 57 |
class Expression: |
| 58 |
""" |
| 59 |
A valid Expression must contain a non-None `arg`, `annotation`, or both. |
| 60 |
""" |
| 61 |
|
| 62 |
arg: str | VariableRef | None |
| 63 |
annotation: FunctionAnnotation | UnsupportedAnnotation | None = None |
| 64 |
attributes: dict[str, str | VariableRef | None] = field(default_factory=dict) |
| 65 |
|
| 66 |
|
| 67 |
@dataclass |
| 68 |
class Markup: |
| 69 |
kind: Literal["open", "standalone", "close"] |
| 70 |
name: str |
| 71 |
options: dict[str, str | VariableRef] = field(default_factory=dict) |
| 72 |
attributes: dict[str, str | VariableRef | None] = field(default_factory=dict) |
| 73 |
|
| 74 |
|
| 75 |
Pattern = List[Union[str, Expression, Markup]] |
| 76 |
""" |
| 77 |
A linear sequence of text and placeholders corresponding to potential output of a message. |
| 78 |
|
| 79 |
String values represent literal text. |
| 80 |
String values include all processing of the underlying text values, including escape sequence processing. |
| 81 |
""" |
| 82 |
|
| 83 |
|
| 84 |
@dataclass |
| 85 |
class CatchallKey: |
| 86 |
value: str | None = field(default=None, compare=False) |
| 87 |
""" |
| 88 |
An optional string identifier for the default/catch-all variant. |
| 89 |
""" |
| 90 |
|
| 91 |
def __hash__(self) -> int: |
| 92 |
""" |
| 93 |
Consider all catchall-keys as equivalent to each other |
| 94 |
""" |
| 95 |
return 1 |
| 96 |
|
| 97 |
|
| 98 |
@dataclass |
| 99 |
class Declaration: |
| 100 |
name: str |
| 101 |
value: Expression |
| 102 |
|
| 103 |
|
| 104 |
@dataclass |
| 105 |
class UnsupportedStatement: |
| 106 |
keyword: str |
| 107 |
""" |
| 108 |
A non-empty string name. |
| 109 |
""" |
| 110 |
|
| 111 |
body: str | None |
| 112 |
""" |
| 113 |
If not empty, the "raw" value (i.e. escape sequences are not processed) |
| 114 |
starting after the keyword and up to the first expression, |
| 115 |
not including leading or trailing whitespace. |
| 116 |
""" |
| 117 |
|
| 118 |
expressions: list[Expression] |
| 119 |
|
| 120 |
|
| 121 |
@dataclass |
| 122 |
class PatternMessage: |
| 123 |
""" |
| 124 |
A message without selectors and with a single pattern. |
| 125 |
""" |
| 126 |
|
| 127 |
pattern: Pattern |
| 128 |
declarations: list[Declaration | UnsupportedStatement] = field(default_factory=list) |
| 129 |
|
| 130 |
|
| 131 |
Variants = Dict[Tuple[Union[str, CatchallKey], ...], Pattern] |
| 132 |
|
| 133 |
|
| 134 |
@dataclass |
| 135 |
class SelectMessage: |
| 136 |
""" |
| 137 |
A message with one or more selectors and a corresponding number of variants. |
| 138 |
""" |
| 139 |
|
| 140 |
selectors: list[Expression] |
| 141 |
variants: Variants |
| 142 |
declarations: list[Declaration | UnsupportedStatement] = field(default_factory=list) |
| 143 |
|
| 144 |
|
| 145 |
Message = Union[PatternMessage, SelectMessage] |