Debian Codegraph @

pint/delegates/txt_defparser/common.py

raw
1 """
2 pint.delegates.txt_defparser.common
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5 Definitions for parsing an Import Statement
6
7 Also DefinitionSyntaxError
8
9 :copyright: 2022 by Pint Authors, see AUTHORS for more details.
10 :license: BSD, see LICENSE for more details.
11 """
12
13 from __future__ import annotations
14
15 from dataclasses import dataclass
16
17 import flexparser as fp
18
19 from ... import errors
20 from ..base_defparser import ParserConfig
21
22
23 class DefinitionSyntaxError(errors.DefinitionSyntaxError, fp.ParsingError):
24 """A syntax error was found in a definition. Combines:
25
26 DefinitionSyntaxError: which provides a message placeholder.
27 fp.ParsingError: which provides raw text, and start and end column and row
28
29 and an extra location attribute in which the filename or reseource is stored.
30 """
31
32 msg: str
33
34 def __init__(self, msg: str, location: str = ""):
35 self.msg = msg
36 self.location = location
37
38 def __str__(self) -> str:
39 msg = (
40 self.msg + "\n " + (self.format_position or "") + " " + (self.raw or "")
41 )
42 if self.location:
43 msg += "\n " + self.location
44 return msg
45
46 def set_location(self, value: str) -> None:
47 super().__setattr__("location", value)
48
49
50 @dataclass(frozen=True)
51 class ImportDefinition(fp.IncludeStatement[ParserConfig]):
52 value: str
53
54 @property
55 def target(self) -> str:
56 return self.value
57
58 @classmethod
59 def from_string(cls, s: str) -> fp.NullableParsedResult[ImportDefinition]:
60 if s.startswith("@import"):
61 return ImportDefinition(s[len("@import") :].strip())
62 return None