Skip to content

Commit 208062a

Browse files
committed
Add append() and token_error() for fixing wrong tabbing
1 parent 6352de0 commit 208062a

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

Python/ini_converting/ini_parser.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def get_parsed_tokens(tokens, parsed=None, token_idx=None, depth=0):
1616
token = tokens[token_idx[0]]
1717

1818
if state == "newline" and is_deeper(depth, token, tokens, token_idx[0] + 1):
19-
children = { "type": "children", "content": [] }
20-
parsed[-1].append(children)
19+
children = { "type": "children", "content": [], "index": token["index"], "filepath": token["filepath"] }
20+
append(children, parsed)
2121
get_parsed_tokens(tokens, children["content"], token_idx, depth + 1)
2222
# "state" is deliberately not being changed here.
2323
elif state == "newline" and is_same_depth(depth, token, tokens, token_idx[0] + 1):
@@ -30,39 +30,50 @@ def get_parsed_tokens(tokens, parsed=None, token_idx=None, depth=0):
3030
state = "start"
3131

3232
elif state == "start" and token["type"] == "WORD":
33-
parsed[-1].append( { "type": "property", "content": token["content"] } )
33+
append( { "type": "property", "content": token["content"] }, parsed )
3434
state = "property"
3535
token_idx[0] += 1
3636
elif state == "property" and token["type"] == "EQUALS":
37-
parsed[-1].append( { "type": "extra", "content": token["content"] } )
37+
append( { "type": "extra", "content": token["content"] }, parsed )
3838
state = "equals"
3939
token_idx[0] += 1
4040
elif state == "property" and token["type"] == "NEWLINES":
41-
parsed[-1].append( { "type": "extra", "content": token["content"] } )
41+
append( { "type": "extra", "content": token["content"] }, parsed )
4242
state = "newline"
4343
token_idx[0] += 1
4444
elif state == "equals" and token["type"] == "WORD":
45-
parsed[-1].append( { "type": "value", "content": token["content"] } )
45+
append( { "type": "value", "content": token["content"] }, parsed )
4646
state = "value"
4747
token_idx[0] += 1
4848
elif state == "value" and token["type"] == "NEWLINES":
49-
parsed[-1].append( { "type": "extra", "content": token["content"] } )
49+
append( { "type": "extra", "content": token["content"] }, parsed )
5050
state = "newline"
5151
token_idx[0] += 1
5252

5353
else:
54-
parsed[-1].append( { "type": "extra", "content": token["content"] } )
54+
append( { "type": "extra", "content": token["content"] }, parsed )
5555
token_idx[0] += 1
5656

5757
return parsed
5858

5959

60+
def append(token, parsed):
61+
if len(parsed) == 0:
62+
token_error(token, "Incorrect tabbing at {line}, column {column} in {filepath}")
63+
64+
parsed[-1].append(token)
65+
66+
67+
def token_error(token, message):
68+
line, column = get_token_position(token)
69+
raise ValueError(message.format(line=line, column=column, filepath=token["filepath"]))
70+
71+
6072
def is_deeper(depth, token, tokens, next_token_idx):
6173
new_depth = get_depth(token, tokens, next_token_idx)
6274

6375
if new_depth > depth + 1:
64-
line, column = get_token_position(token)
65-
raise ValueError(f"Too many tabs found at line {line}, column {column} in {token['filepath']}")
76+
token_error(token, "Too many tabs found at line {line}, column {column} in {filepath}")
6677

6778
return new_depth > depth
6879

0 commit comments

Comments
 (0)