Skip to content

Commit bb7196e

Browse files
committed
simplify structure of parsed AST
1 parent 1e8c72c commit bb7196e

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

tagstudio/src/core/query_lang/ast.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from abc import ABC, abstractmethod
22
from enum import Enum
3-
from typing import Generic, TypeVar, Union
3+
from typing import Generic, TypeVar
44

55

66
class ConstraintType(Enum): # TODO add remaining ones
@@ -37,19 +37,19 @@ def __repr__(self) -> str:
3737

3838

3939
class ANDList(AST):
40-
terms: list[Union["ORList", "Constraint", "Not"]]
40+
terms: list[AST]
4141

42-
def __init__(self, terms: list[Union["ORList", "Constraint", "Not"]]) -> None:
42+
def __init__(self, terms: list[AST]) -> None:
4343
super().__init__()
4444
for term in terms:
4545
term.parent = self
4646
self.terms = terms
4747

4848

4949
class ORList(AST):
50-
elements: list[ANDList]
50+
elements: list[AST]
5151

52-
def __init__(self, elements: list[ANDList]) -> None:
52+
def __init__(self, elements: list[AST]) -> None:
5353
super().__init__()
5454
for element in elements:
5555
element.parent = self

tagstudio/src/core/query_lang/parser.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Union
2-
31
from src.core.query_lang.ast import AST, ANDList, Constraint, Not, ORList, Property
42
from src.core.query_lang.tokenizer import ConstraintType, Token, Tokenizer, TokenType
53
from src.core.query_lang.util import ParsingError
@@ -25,24 +23,24 @@ def parse(self) -> AST:
2523
raise ParsingError(self.next_token.start, self.next_token.end, "Syntax Error")
2624
return out
2725

28-
def __or_list(self) -> ORList:
26+
def __or_list(self) -> AST:
2927
terms = [self.__and_list()]
3028

3129
while self.__is_next_or():
3230
self.__eat(TokenType.ULITERAL)
3331
terms.append(self.__and_list())
3432

35-
return ORList(terms)
33+
return ORList(terms) if len(terms) > 1 else terms[0]
3634

3735
def __is_next_or(self) -> bool:
3836
return self.next_token.type == TokenType.ULITERAL and self.next_token.value.upper() == "OR"
3937

40-
def __and_list(self) -> ANDList:
38+
def __and_list(self) -> AST:
4139
elements = [self.__term()]
4240
while self.next_token.type != TokenType.EOF and not self.__is_next_or():
4341
self.__skip_and()
4442
elements.append(self.__term())
45-
return ANDList(elements)
43+
return ANDList(elements) if len(elements) > 1 else elements[0]
4644

4745
def __skip_and(self) -> None:
4846
if self.__is_next_and():
@@ -54,7 +52,7 @@ def __skip_and(self) -> None:
5452
def __is_next_and(self) -> bool:
5553
return self.next_token.type == TokenType.ULITERAL and self.next_token.value.upper() == "AND"
5654

57-
def __term(self) -> Union[ORList, Constraint, Not]:
55+
def __term(self) -> AST:
5856
if self.__is_next_not():
5957
self.__eat(TokenType.ULITERAL)
6058
return Not(self.__term())
@@ -108,9 +106,3 @@ def __eat(self, type: TokenType) -> Token:
108106

109107
def __syntax_error(self, msg: str = "Syntax Error") -> ParsingError:
110108
return ParsingError(self.next_token.start, self.next_token.end, msg)
111-
112-
113-
if __name__ == "__main__": # TODO remove
114-
print("") # noqa: T201
115-
p = Parser("Mario AND Luigi tag:test[parent=Color,color=red] OR mediatype:test")
116-
print(p.parse()) # noqa: T201

0 commit comments

Comments
 (0)