Strings With Source Tracking¶
This module provides a string type SourceStr() which remembers the source it originates
from.
The correspondence between the contents of a SourceStr and the source file is stored in a SourceMap which stores a collection of SourceMapSpans, each mapping a contiguous range of characters.
Obtaining and Manipulating Source Strings¶
The easiest way to obtain a SourceStr from a file is to use the read_file function:
- yosys_mau.source_str.read_file(path, *, store_content=None, relative_to=None)¶
Read a file into a
SourceStrthat track’s its source.- Parameters:
store_content (bool | None) – Whether to store the file’s content in memory even when only parts of it are still referenced. By default only files below 1MiB are stored, but this option can force or prevent storing the content.
relative_to (PathLike[Any] | str | None) – The path to resolve relative paths against. If not given, the current working directory is used.
- Returns:
A
SourceStrthat tracks the file’s source.- Return type:
The SourceStr class overrides some of the standard str methods to keep track of their contents.
For some standard python string operations this isn’t possible though, and in that case alternative methods may be present.
There is also a yosys_mau.source_str.re module which provides the same API as the standard library’s re module, but with support for tracking sources when matching within SourceStr objects.
- class yosys_mau.source_str.SourceStr(value, source_map=None)¶
Bases:
strString type which remembers the source it originates from.
A
SourceStrinherits fromstrbut has an additional attributesource_mapto track the string’s origin. When aSourceStroperation returns a new string, e.g. when you slice or concatenate strings, the resulting string will often be aSourceStrwith thesource_mapattribute set accordingly.- Parameters:
- Return type:
- property source_map: SourceMap¶
The source map associated with this string.
Prefer using
source_map()to access this, unless you know for certain that your target string is aSourceStr.
- __getitem__(key)¶
Source tracking slicing.
Not all slicing operations are source tracking, but
[start:stop],[start:],[:stop]and[:]are.
- splitlines(keepends=False)¶
Source tracking implementation of
str.splitlines().
- split(sep=None, maxsplit=-1)¶
Source tracking implementation of
str.split().
- strip(chars=None)¶
Source tracking implementation of
str.strip().
- rstrip(chars=None)¶
Source tracking implementation of
str.rstrip().
- lstrip(chars=None)¶
Source tracking implementation of
str.lstrip().
- replace(old, new, count=-1)¶
Source tracking implementation of
str.replace().
- join(iterable)¶
Source tracking implementation of
str.join().Note
When using join,
selfoften isn’t aSourceStreven when the arguments are. Since we can only override methods according to the receiverself, to ensure source tracking, this has to be invoked asSourceStr.join(self, iterable).
Inspecting String Sources¶
The correspondence between the contents of a SourceStr and the source file is stored in a SourceMap which stores a collection of SourceMapSpans.
The SourceMap class inherits from SourceSpans and SourceMapSpan from SourceSpan, where either super-class only stores the source spans without associating them to any specific string.
They are mostly used when generating diagnostic messages from source strings.
To obtain a SourceMap from either SourceStr or a plain str, use the source_map function, which will return an empty mapping for a plain str:
- yosys_mau.source_str.source_map(string)¶
The source map of a string.
- class yosys_mau.source_str.SourceMap(spans, len)¶
Bases:
SourceSpansMaps string contents to their source files.
It can map different spans of the string to different source files or different parts of the same source file.
The SourceMapSpans are stored in string order, to allow for binary searches.
- Parameters:
spans (tuple[SourceMapSpan, ...])
len (int)
- spans: tuple[SourceMapSpan, ...]¶
Each span maps a contiguous part of the string to a part of a source file.
They are stored in order, to allow for binary searches.
- detached()¶
Returns the source spans of this source map, detached from their position in the string.
- Return type:
- class yosys_mau.source_str.SourceSpans(spans)¶
A collection of source spans.
- Parameters:
spans (tuple[SourceSpan, ...])
- spans: tuple[SourceSpan, ...]¶
Each span covers a contiguous part of a source file.
- close_gaps(max_gap=3, line_mode=False)¶
Sorts contained spans and merges almost adjacent spans.
- Parameters:
- Returns:
A new
SourceSpanswith sorted and merged spans.- Return type:
- class yosys_mau.source_str.SourceMapSpan(len, file_start, file, str_start)¶
Bases:
SourceSpanMaps a span of a string to a corresponding span of a source file.
All offsets use Python’s native string indexing, i.e. they count unicode codepoints.
Generating Diagnostics¶
Diagnostic reporting using source tracking strings.
Warning
The API for error reporting hasn’t been fully designed yet and is likely to change.
- exception yosys_mau.source_str.report.InputError(where, message)¶
An error in user input.
Note
The API for error reporting hasn’t been fully designed yet and is likely to change. Something simple to raise one-off errors like this will certainly stay though.
- class yosys_mau.source_str.report.Report(spans, message)¶
Collects diagnostic information associated to specific source locations.
Warning
The API for error reporting hasn’t been fully designed yet and is likely to change. This class in particular will undergo backwards incompatible API changes and should be considered internal to this library for now.
- Parameters:
spans (SourceSpans)
message (str)
Regular Expression Support¶
The yosys_mau.source_str.re module provides the same API as the standard re module, but with support for tracking sources when matching within SourceStr objects.
Apart from the module contents listed her, this also re-exports the flags
constants defined by the standard re module.
- yosys_mau.source_str.re.compile(pattern, flags=0)¶
Source tracking wrapper for
re.compile().
- yosys_mau.source_str.re.search(pattern, string, flags=0)¶
Source tracking implementation of
re.search().
- yosys_mau.source_str.re.match(pattern, string, flags=0)¶
Source tracking implementation of
re.match().
- yosys_mau.source_str.re.fullmatch(pattern, string, flags=0)¶
Source tracking implementation of
re.fullmatch().
- yosys_mau.source_str.re.split(pattern, string, maxsplit=0, flags=0)¶
Source tracking implementation of
re.split().
- yosys_mau.source_str.re.findall(pattern, string, flags=0)¶
Source tracking implementation of
re.findall().
- yosys_mau.source_str.re.finditer(pattern, string, flags=0)¶
Source tracking implementation of
re.finditer().
- class yosys_mau.source_str.re.Pattern(wrapped)¶
Source tracking wrapper for
rePatternobjects.- search(string, pos=0, endpos=9223372036854775807)¶
Source tracking wrapper for
re.Pattern.search().
- match(string, pos=0, endpos=9223372036854775807)¶
Source tracking wrapper for
re.Pattern.match().
- fullmatch(string, pos=0, endpos=9223372036854775807)¶
Source tracking wrapper for
re.Pattern.fullmatch().
- split(string, maxsplit=0)¶
Source tracking implementation of
re.Pattern.split().
- findall(string, pos=0, endpos=9223372036854775807)¶
Source tracking implementation of
re.Pattern.findall().
- finditer(string, pos=0, endpos=9223372036854775807)¶
Source tracking wrapper for
re.Pattern.finditer().
- sub(repl, string, count=0)¶
Source tracking implementation of
re.Pattern.sub().
- subn(repl, string, count=0)¶
Source tracking implementation of
re.Pattern.subn().
- property flags: int¶
Forwards to
re.Pattern.flags.
- property groups: int¶
Forwards to
re.Pattern.groups.
- property groupindex: Mapping[str, int]¶
Forwards to
re.Pattern.groupindex.
- property pattern: str¶
Forwards to
re.Pattern.pattern.
- class yosys_mau.source_str.re.Match(wrapped)¶
Source tracking wrapper for
reMatchobjects.- property pos: int¶
Forwards to
re.Match.pos.
- property endpos: int¶
Forwards to
re.Match.endpos.
- property string: str¶
Forwards to
re.Match.string.Even though this forwards to the wrapped object, this will return a <project:#SourceStr> when used as match target.
- property lastindex: int | None¶
Forwards to
re.Match.lastindex.
- property lastgroup: str | None¶
Forwards to
re.Match.lastgroup.
- property re: Pattern¶
Source tracking wrapper for
re.Match.re.
- span(group=0)¶
Forwards to
re.Match.span().
- start(group=0)¶
Forwards to
re.Match.start().
- end(group=0)¶
Forwards to
re.Match.end().
- group(group: Literal[0] = 0, /) str¶
- group(group: str | int, /) str | None
- group(group1: str | int, group2: str | int, /, *groups: str | int) tuple[str | None, ...]
Source tracking wrapper for
re.Match.group().
- __getitem__(group: Literal[0]) str¶
- __getitem__(group: int | str) str | None
Source tracking wrapper for
re.Match.__getitem__().
- groups() tuple[str | None, ...]¶
- groups(default: _T) tuple[str | _T, ...]
Source tracking wrapper for
re.Match.groups().
- groupdict() dict[str, str | None]¶
- groupdict(default: _T) dict[str, str | _T]
Source tracking wrapper for
re.Match.groupdict().
- expand(template)¶
Source tracking implementation of
re.Match.expand().