1- from typing import Union
2-
31from src .core .query_lang .ast import AST , ANDList , Constraint , Not , ORList , Property
42from src .core .query_lang .tokenizer import ConstraintType , Token , Tokenizer , TokenType
53from 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