55
66import re
77
8- from lark import Token # noqa: F401
9- from lark import Tree
8+ from lark import (
9+ Token ,
10+ Tree ,
11+ )
1012
11- from typing import List
1213from kql .errors import KqlParseError
1314
1415
15- def check_whitespace (token_positions : List , token : str , lines : List [str ]) -> None :
16+ def check_whitespace (token_positions : list [ tuple [ int , int , str ]] , lines : list [str ]) -> None :
1617 """Check for whitespace around a token."""
17- for line_num , column in token_positions :
18+ for line_num , column , token in token_positions :
1819 # Check the substring at the given position
1920 line = lines [line_num - 1 ]
2021 start = column - 1
@@ -27,27 +28,26 @@ def check_whitespace(token_positions: List, token: str, lines: List[str]) -> Non
2728 # Check for whitespace around the token
2829 if (
2930 start > 0
30- and (end < len (line ) and re .match (r"\s" , line [end ]) or end == len (line ))
31+ and (( end < len (line ) and re .match (r"\s" , line [end ]) ) or end == len (line ))
3132 and re .match (r"\s" , line [start - 1 ])
3233 ):
3334 continue
34- else :
35- raise KqlParseError (
36- error_msg = f"Missing whitespace around '{ token } ' token" ,
37- line = line_num ,
38- column = column ,
39- source = line ,
40- width = len (token ),
41- trailer = None
42- )
43-
44-
45- def collect_token_positions (tree : Tree , token : str ) -> List :
46- """Collect token positions from a tree."""
35+ raise KqlParseError (
36+ error_msg = f"Missing whitespace around '{ token } ' token" ,
37+ line = line_num ,
38+ column = column ,
39+ source = line ,
40+ width = len (token ),
41+ trailer = None
42+ )
43+
44+
45+ def collect_token_positions (tree : Tree , token_list : list [str ]) -> list [tuple [int , int , str ]]:
46+ """Collect token positions from a tree for a list of tokens."""
4747 token_positions = []
4848 for child in tree .children :
49- if isinstance (child , Token ) and child .value .lower () in [token ]:
50- token_positions .append ((child .line , child .column ))
49+ if isinstance (child , Token ) and child .value .lower () in [token . lower () for token in token_list ]:
50+ token_positions .append ((child .line , child .column , child . value ))
5151 elif isinstance (child , Tree ):
52- token_positions .extend (collect_token_positions (child , token ))
52+ token_positions .extend (collect_token_positions (child , token_list ))
5353 return token_positions
0 commit comments