Debian Codegraph @

blueprintcompiler/language/translated.py

raw
1 # translated.py
2 #
3 # Copyright 2022 James Westman <[email protected]>
4 #
5 # This file is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation; either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # This file is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #
18 # SPDX-License-Identifier: LGPL-3.0-or-later
19
20 from .common import *
21 from .contexts import ValueTypeCtx
22
23
24 class Translated(AstNode):
25 grammar = AnyOf(
26 ["_", "(", UseQuoted("string"), ")"],
27 [
28 "C_",
29 "(",
30 UseQuoted("context"),
31 ",",
32 UseQuoted("string"),
33 ")",
34 ],
35 )
36
37 @property
38 def string(self) -> str:
39 return self.tokens["string"]
40
41 @property
42 def translate_context(self) -> T.Optional[str]:
43 return self.tokens["context"]
44
45 @property
46 def string_token(self) -> Token:
47 return self.group.tokens["string"]
48
49 @validate()
50 def validate_for_type(self) -> None:
51 expected_type = self.context[ValueTypeCtx].value_type
52 if expected_type is not None and not expected_type.assignable_to(StringType()):
53 raise CompileError(
54 f"Cannot convert translated string to {expected_type.full_name}"
55 )
56
57 @validate("context")
58 def context_double_quoted(self):
59 if self.translate_context is None:
60 return
61
62 if not str(self.group.tokens["context"]).startswith('"'):
63 raise CompileWarning("gettext may not recognize single-quoted strings")
64
65 @validate("string")
66 def string_double_quoted(self):
67 if not str(self.group.tokens["string"]).startswith('"'):
68 raise CompileWarning("gettext may not recognize single-quoted strings")
69
70 @docs()
71 def ref_docs(self):
72 return get_docs_section("Syntax Translated")