Skip to content

Commit 0b491a8

Browse files
committed
Fix get_parsed_tokens() by using lookahead to determine whether a next line is shallower or just whitespace
1 parent 595e97b commit 0b491a8

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

Python/ini_converting/ini_parser.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ def get_parsed_tokens(tokens, parsed=None, token_idx=None, depth=0):
2626
children = { "type": "children", "content": [] }
2727
parsed[-1].append(children)
2828
get_parsed_tokens(tokens, children["content"], token_idx, depth + 1)
29-
elif state == "newline" and is_less_deep(depth, token):
29+
elif state == "newline" and depth > 0 and is_shallower(depth, token, tokens, token_idx[0] + 1):
3030
return
31-
elif state == "newline":
31+
elif state == "newline" and (len(parsed) == 0 or token["type"] == "WORD" or token["type"] == "TABS"):
3232
parsed.append([])
3333
state = "start"
3434

@@ -56,22 +56,39 @@ def get_parsed_tokens(tokens, parsed=None, token_idx=None, depth=0):
5656
return parsed
5757

5858

59-
def is_less_deep(depth, token):
60-
return get_depth(token) < depth
59+
def is_shallower(depth, token, tokens, next_token_idx):
60+
if token["type"] == "TABS" and get_depth(token) >= depth:
61+
return False
62+
elif token["type"] == "NEWLINES":
63+
return False
64+
65+
while next_token_idx < len(tokens):
66+
next_token = tokens[next_token_idx]
67+
68+
if next_token["type"] == "WORD":
69+
return True
70+
elif next_token["type"] == "NEWLINES":
71+
return False
72+
73+
next_token_idx += 1
74+
75+
return False # Reached when the while-loop read the last character of the file and didn't return.
76+
77+
78+
def get_depth(token):
79+
return len(token["content"])
6180

6281

6382
def is_deeper(depth, token):
6483
new_depth = get_depth(token)
84+
6585
if new_depth > depth + 1:
6686
line, column = get_token_pos(token)
6787
raise ValueError(f"Too many tabs found at line {line}, column {column} in {token['filepath']}")
88+
6889
return new_depth > depth
6990

7091

71-
def get_depth(token):
72-
return len(token["content"]) if token["type"] == "TABS" else 0
73-
74-
7592
def get_token_pos(token):
7693
with open(token["filepath"], "r") as f:
7794
text = f.read()

0 commit comments

Comments
 (0)