Skip to content

Commit 96ce2da

Browse files
Update to only walk tree once
1 parent 1d8359c commit 96ce2da

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

lib/kql/kql/parser.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,7 @@ def lark_parse(text):
381381

382382
# Check for whitespace around "and" and "or" tokens
383383
lines = text.split('\n')
384-
check_whitespace(collect_token_positions(tree, "and"), 'and', lines)
385-
check_whitespace(collect_token_positions(tree, "or"), 'or', lines)
384+
check_whitespace(collect_token_positions(tree, ["and", "or"]), lines)
386385

387386
return tree
388387
except UnexpectedEOF:

lib/kql/kql/utils.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55

66
import 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
1213
from 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

Comments
 (0)