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