Skip to content

Commit f5284d5

Browse files
committed
Refactor get_parsed_tokens()
1 parent 37d550f commit f5284d5

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

Python/ini_converting/ini_parser.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,15 @@ def get_parsed_tokens(tokens, parsed=None, token_idx=None, depth=0):
1515
while token_idx[0] < len(tokens):
1616
token = tokens[token_idx[0]]
1717

18-
# if state == "newline" and token["type"] == "EXTRA":
19-
# parsed[-1].append( { "type": "extra", "content": token["content"] } )
20-
# token_idx[0] += 1
21-
# elif state == "newline" and token["type"] == "NEWLINES":
22-
# parsed[-1].append( { "type": "extra", "content": token["content"] } )
23-
# token_idx[0] += 1
24-
25-
if state == "newline" and token["type"] == "TABS" and is_deeper(depth, token):
18+
if state == "newline" and is_deeper(depth, token):
2619
children = { "type": "children", "content": [] }
2720
parsed[-1].append(children)
2821
get_parsed_tokens(tokens, children["content"], token_idx, depth + 1)
29-
# state = "start" # TODO: Should state be set to start on this comment's line?
22+
# "state" is deliberately not being changed here.
3023
elif state == "newline" and is_same_depth(token, depth):
3124
parsed.append([])
3225
state = "start"
33-
elif state == "newline" and depth > 0 and is_shallower(depth, token, tokens, token_idx[0] + 1):
26+
elif state == "newline" and is_shallower(depth, token, tokens, token_idx[0] + 1):
3427
return
3528
elif state == "newline" and (len(parsed) == 0 or token["type"] == "WORD"):
3629
parsed.append([])
@@ -69,7 +62,7 @@ def is_same_depth(token, depth):
6962

7063

7164
def is_shallower(depth, token, tokens, next_token_idx):
72-
if token["type"] == "NEWLINES":
65+
if depth == 0 or token["type"] == "NEWLINES":
7366
return False
7467

7568
while next_token_idx < len(tokens):
@@ -86,6 +79,9 @@ def is_shallower(depth, token, tokens, next_token_idx):
8679

8780

8881
def is_deeper(depth, token):
82+
if token["type"] != "TABS":
83+
return False
84+
8985
new_depth = get_depth(token)
9086

9187
if new_depth > depth + 1:

0 commit comments

Comments
 (0)