| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import pathlib |
| 4 |
import typing as ty |
| 5 |
|
| 6 |
import flexcache as fc |
| 7 |
import flexparser as fp |
| 8 |
|
| 9 |
from ..base_defparser import ParserConfig |
| 10 |
from . import block, common, context, defaults, group, plain, system |
| 11 |
|
| 12 |
|
| 13 |
class PintRootBlock( |
| 14 |
fp.RootBlock[ |
| 15 |
ty.Union[ |
| 16 |
plain.CommentDefinition, |
| 17 |
common.ImportDefinition, |
| 18 |
context.ContextDefinition, |
| 19 |
defaults.DefaultsDefinition, |
| 20 |
system.SystemDefinition, |
| 21 |
group.GroupDefinition, |
| 22 |
plain.AliasDefinition, |
| 23 |
plain.DerivedDimensionDefinition, |
| 24 |
plain.DimensionDefinition, |
| 25 |
plain.PrefixDefinition, |
| 26 |
plain.UnitDefinition, |
| 27 |
], |
| 28 |
ParserConfig, |
| 29 |
] |
| 30 |
): |
| 31 |
pass |
| 32 |
|
| 33 |
|
| 34 |
class _PintParser(fp.Parser[PintRootBlock, ParserConfig]): |
| 35 |
"""Parser for the original Pint definition file, with cache.""" |
| 36 |
|
| 37 |
_delimiters = { |
| 38 |
"#": ( |
| 39 |
fp.DelimiterInclude.SPLIT_BEFORE, |
| 40 |
fp.DelimiterAction.CAPTURE_NEXT_TIL_EOL, |
| 41 |
), |
| 42 |
**fp.SPLIT_EOL, |
| 43 |
} |
| 44 |
_root_block_class = PintRootBlock |
| 45 |
_strip_spaces = True |
| 46 |
|
| 47 |
_diskcache: fc.DiskCache | None |
| 48 |
|
| 49 |
def __init__(self, config: ParserConfig, *args: ty.Any, **kwargs: ty.Any): |
| 50 |
self._diskcache = kwargs.pop("diskcache", None) |
| 51 |
super().__init__(config, *args, **kwargs) |
| 52 |
|
| 53 |
def parse_file( |
| 54 |
self, path: pathlib.Path |
| 55 |
) -> fp.ParsedSource[PintRootBlock, ParserConfig]: |
| 56 |
if self._diskcache is None: |
| 57 |
return super().parse_file(path) |
| 58 |
content, _basename = self._diskcache.load(path, super().parse_file) |
| 59 |
return content |
| 60 |
|
| 61 |
|
| 62 |
class DefParser: |
| 63 |
skip_classes: tuple[type, ...] = ( |
| 64 |
fp.BOF, |
| 65 |
fp.BOR, |
| 66 |
fp.BOS, |
| 67 |
fp.EOS, |
| 68 |
plain.CommentDefinition, |
| 69 |
) |
| 70 |
|
| 71 |
def __init__(self, default_config: ParserConfig, diskcache: fc.DiskCache): |
| 72 |
self._default_config = default_config |
| 73 |
self._diskcache = diskcache |
| 74 |
|
| 75 |
def iter_parsed_project( |
| 76 |
self, parsed_project: fp.ParsedProject[PintRootBlock, ParserConfig] |
| 77 |
) -> ty.Generator[fp.ParsedStatement[ParserConfig], None, None]: |
| 78 |
last_location = None |
| 79 |
for stmt in parsed_project.iter_blocks(): |
| 80 |
if isinstance(stmt, fp.BOS): |
| 81 |
if isinstance(stmt, fp.BOF): |
| 82 |
last_location = str(stmt.path) |
| 83 |
continue |
| 84 |
elif isinstance(stmt, fp.BOR): |
| 85 |
last_location = ( |
| 86 |
f"[package: {stmt.package}, resource: {stmt.resource_name}]" |
| 87 |
) |
| 88 |
continue |
| 89 |
else: |
| 90 |
last_location = "orphan string" |
| 91 |
continue |
| 92 |
|
| 93 |
if isinstance(stmt, self.skip_classes): |
| 94 |
continue |
| 95 |
|
| 96 |
assert isinstance(last_location, str) |
| 97 |
if isinstance(stmt, common.DefinitionSyntaxError): |
| 98 |
stmt.set_location(last_location) |
| 99 |
raise stmt |
| 100 |
elif isinstance(stmt, block.DirectiveBlock): |
| 101 |
for exc in stmt.errors: |
| 102 |
exc = common.DefinitionSyntaxError(str(exc)) |
| 103 |
exc.set_position(*stmt.get_position()) |
| 104 |
exc.set_raw( |
| 105 |
(stmt.opening.raw or "") + " [...] " + (stmt.closing.raw or "") |
| 106 |
) |
| 107 |
exc.set_location(last_location) |
| 108 |
raise exc |
| 109 |
|
| 110 |
try: |
| 111 |
yield stmt.derive_definition() |
| 112 |
except Exception as exc: |
| 113 |
exc = common.DefinitionSyntaxError(str(exc)) |
| 114 |
exc.set_position(*stmt.get_position()) |
| 115 |
exc.set_raw(stmt.opening.raw + " [...] " + stmt.closing.raw) |
| 116 |
exc.set_location(last_location) |
| 117 |
raise exc |
| 118 |
else: |
| 119 |
yield stmt |
| 120 |
|
| 121 |
def parse_file( |
| 122 |
self, filename: pathlib.Path | str, cfg: ParserConfig | None = None |
| 123 |
) -> fp.ParsedProject[PintRootBlock, ParserConfig]: |
| 124 |
return fp.parse( |
| 125 |
filename, |
| 126 |
_PintParser, |
| 127 |
cfg or self._default_config, |
| 128 |
diskcache=self._diskcache, |
| 129 |
strip_spaces=True, |
| 130 |
delimiters=_PintParser._delimiters, |
| 131 |
) |
| 132 |
|
| 133 |
def parse_string( |
| 134 |
self, content: str, cfg: ParserConfig | None = None |
| 135 |
) -> fp.ParsedProject[PintRootBlock, ParserConfig]: |
| 136 |
return fp.parse_bytes( |
| 137 |
content.encode("utf-8"), |
| 138 |
_PintParser, |
| 139 |
cfg or self._default_config, |
| 140 |
diskcache=self._diskcache, |
| 141 |
strip_spaces=True, |
| 142 |
delimiters=_PintParser._delimiters, |
| 143 |
) |