Debian Codegraph @

pint/delegates/txt_defparser/block.py

raw
1 """
2 pint.delegates.txt_defparser.block
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5 Classes for Pint Blocks, which are defined by:
6
7 @<block name>
8 <content>
9 @end
10
11 :copyright: 2022 by Pint Authors, see AUTHORS for more details.
12 :license: BSD, see LICENSE for more details.
13 """
14
15 from __future__ import annotations
16
17 from dataclasses import dataclass
18 from typing import Generic, TypeVar
19
20 import flexparser as fp
21
22 from ..base_defparser import ParserConfig, PintParsedStatement
23
24
25 @dataclass(frozen=True)
26 class EndDirectiveBlock(PintParsedStatement):
27 """An EndDirectiveBlock is simply an "@end" statement."""
28
29 @classmethod
30 def from_string(cls, s: str) -> fp.NullableParsedResult[EndDirectiveBlock]:
31 if s == "@end":
32 return cls()
33 return None
34
35
36 OPST = TypeVar("OPST", bound="PintParsedStatement")
37 IPST = TypeVar("IPST", bound="PintParsedStatement")
38
39 DefT = TypeVar("DefT")
40
41
42 @dataclass(frozen=True)
43 class DirectiveBlock(
44 Generic[DefT, OPST, IPST], fp.Block[OPST, IPST, EndDirectiveBlock, ParserConfig]
45 ):
46 """Directive blocks have beginning statement starting with a @ character.
47 and ending with a "@end" (captured using a EndDirectiveBlock).
48
49 Subclass this class for convenience.
50 """
51
52 def derive_definition(self) -> DefT: ...