Skip to content

Commit a3ad006

Browse files
committed
add BaseVisitor class
1 parent 33318c5 commit a3ad006

File tree

1 file changed

+31
-1
lines changed
  • tagstudio/src/core/query_lang

1 file changed

+31
-1
lines changed

tagstudio/src/core/query_lang/ast.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from abc import ABC, abstractmethod
12
from enum import Enum
2-
from typing import Union
3+
from typing import Generic, TypeVar, Union
34

45

56
class ConstraintType(Enum): # TODO add remaining ones
@@ -75,3 +76,32 @@ def __init__(self, key: str, value: str) -> None:
7576
super().__init__()
7677
self.key = key
7778
self.value = value
79+
80+
81+
T = TypeVar("T")
82+
83+
84+
class BaseVisitor(ABC, Generic[T]):
85+
def visit(self, node: AST) -> T:
86+
return {
87+
ANDList: self.visit_ANDList,
88+
ORList: self.visit_ORList,
89+
Constraint: self.visit_Constraint,
90+
Property: self.visit_Property,
91+
}[type(node)](node)
92+
93+
@abstractmethod
94+
def visit_ANDList(self, node: ANDList) -> T: # noqa: N802
95+
raise NotImplementedError()
96+
97+
@abstractmethod
98+
def visit_ORList(self, node: ORList) -> T: # noqa: N802
99+
raise NotImplementedError()
100+
101+
@abstractmethod
102+
def visit_Constraint(self, node: Constraint) -> T: # noqa: N802
103+
raise NotImplementedError()
104+
105+
@abstractmethod
106+
def visit_Property(self, node: Property) -> T: # noqa: N802
107+
raise NotImplementedError()

0 commit comments

Comments
 (0)