untested cycle detection
This commit is contained in:
parent
caed523ac3
commit
63be17e920
1 changed files with 25 additions and 1 deletions
|
|
@ -1,8 +1,10 @@
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import List
|
from typing import List, Dict, Optional
|
||||||
from typing import Set
|
from typing import Set
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
|
from ytdl_sub.utils.exceptions import StringFormattingException
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class Integer:
|
class Integer:
|
||||||
|
|
@ -74,3 +76,25 @@ class SyntaxTree:
|
||||||
variables.update(token.variables)
|
variables.update(token.variables)
|
||||||
|
|
||||||
return variables
|
return variables
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def detect_cycles(cls, parsed_overrides: Dict[str, "SyntaxTree"]) -> None:
|
||||||
|
variable_dependencies: Dict[Variable, Set[Variable]] = {
|
||||||
|
Variable(name): ast.variables for name, ast in parsed_overrides.items()
|
||||||
|
}
|
||||||
|
|
||||||
|
def _traverse(to_variable: Variable, visited_variables: Optional[List[Variable]] = None) -> None:
|
||||||
|
if visited_variables is None:
|
||||||
|
visited_variables = []
|
||||||
|
|
||||||
|
if to_variable in visited_variables:
|
||||||
|
raise StringFormattingException("Detected cycle in variables")
|
||||||
|
visited_variables.append(to_variable)
|
||||||
|
|
||||||
|
for dep in variable_dependencies[to_variable]:
|
||||||
|
_traverse(to_variable=dep, visited_variables=visited_variables)
|
||||||
|
|
||||||
|
for variable in variable_dependencies.keys():
|
||||||
|
_traverse(variable)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue