Rename Token.ref to Token.with_reference, add docstring

This commit is contained in:
Darren Burns
2022-02-07 11:47:16 +00:00
parent 719ff543a5
commit 75a2e5f579
2 changed files with 9 additions and 3 deletions

View File

@@ -264,7 +264,7 @@ def substitute_references(tokens: Iterator[Token]) -> Iterable[Token]:
ref_location = token.location
ref_length = cell_len(token.value)
for token in reference_tokens:
yield token.ref(
yield token.with_reference(
ReferencedBy(
name=ref_name,
location=ref_location,
@@ -285,7 +285,7 @@ def substitute_references(tokens: Iterator[Token]) -> Iterable[Token]:
ref_location = token.location
ref_length = cell_len(token.value)
for token in variable_tokens:
yield token.ref(
yield token.with_reference(
ReferencedBy(
name=variable_name,
location=ref_location,

View File

@@ -54,7 +54,13 @@ class Token(NamedTuple):
location: tuple[int, int]
referenced_by: ReferencedBy | None
def ref(self, by: ReferencedBy | None) -> "Token":
def with_reference(self, by: ReferencedBy | None) -> "Token":
"""Return a copy of the Token, with reference information attached.
This is used for variable substitution, where a variable reference
can refer to tokens which were defined elsewhere. With the additional
ReferencedBy data attached, we can track where the token we are referring
to is used.
"""
return Token(
name=self.name,
value=self.value,