Skip to content

Commit 7b322a0

Browse files
committed
Parser now uses the "tabs" state again and passes three tests
1 parent 83fb7c3 commit 7b322a0

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

Python/ini_converting/ini_parser.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,31 @@ def get_parsed_tokens(tokens, parsed, token_idx, depth=-1):
1010
while token_idx[0] < len(tokens):
1111
token = tokens[token_idx[0]]
1212

13-
if state == "start" and token["type"] == "TABS" and is_less_deep(depth, token):
14-
return
13+
if depth == -1:
14+
parsed.append([])
15+
get_parsed_tokens(tokens, parsed[-1], token_idx, depth + 1)
16+
1517
elif state == "start" and token["type"] == "TABS" and is_deeper(depth, token):
1618
parsed.append(
1719
{ "type": "lines_tokens", "content": [
1820
[
19-
{ "type": "extra", "content": token["content"] }
21+
2022
]
2123
]}
2224
)
23-
token_idx[0] += 1
2425
get_parsed_tokens(tokens, parsed[-1]["content"][0], token_idx, depth + 1)
26+
2527
elif state == "start" and token["type"] == "TABS":
26-
return
27-
elif state == "start" and token["type"] == "WORD" and depth == -1:
28-
parsed.append([])
29-
get_parsed_tokens(tokens, parsed[-1], token_idx, depth + 1)
30-
elif state == "start" and token["type"] == "WORD":
28+
parsed.append( { "type": "extra", "content": token["content"] } )
29+
state = "tabs"
30+
token_idx[0] += 1
31+
elif (state == "start" or state == "tabs") and token["type"] == "WORD":
3132
parsed.append( { "type": "property", "content": token["content"] } )
3233
state = "property"
3334
token_idx[0] += 1
35+
elif state == "start" and is_less_deep(depth, token):
36+
return
37+
3438
elif state == "property" and token["type"] == "EQUALS":
3539
parsed.append( { "type": "extra", "content": token["content"] } )
3640
state = "equals"
@@ -43,6 +47,7 @@ def get_parsed_tokens(tokens, parsed, token_idx, depth=-1):
4347
parsed.append( { "type": "extra", "content": token["content"] } )
4448
state = "start"
4549
token_idx[0] += 1
50+
4651
else:
4752
parsed.append( { "type": "extra", "content": token["content"] } )
4853
token_idx[0] += 1
@@ -51,13 +56,13 @@ def get_parsed_tokens(tokens, parsed, token_idx, depth=-1):
5156

5257

5358
def is_less_deep(depth, token):
54-
return get_depth(token["content"]) < depth
59+
return get_depth(token) < depth
5560

5661

5762
def is_deeper(depth, token):
5863
# TODO: This should throw an error if it's deeper by more than 1.
59-
return get_depth(token["content"]) > depth
64+
return get_depth(token) > depth
6065

6166

62-
def get_depth(content):
63-
return len(content)
67+
def get_depth(token):
68+
return len(token["content"])

Python/ini_converting/ini_parser_tests.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55

66
def parser_tests():
7-
# test("simple", [
8-
# [
9-
# { "type": "property", "content": "AddEffect" }, { "type": "extra", "content": " " }, { "type": "extra", "content": "=" }, { "type": "extra", "content": " " }, { "type": "value", "content": "MOPixel" }
10-
# ]
11-
# ])
12-
# test("comments", [
13-
# [
14-
# { "type": "extra", "content": "\n" },
15-
# { "type": "extra", "content": "// foo"}, { "type": "extra", "content": "\n" },
16-
# { "type": "extra", "content": "/*a\nb\nc*/" }, { "type": "extra", "content": "\n" },
17-
# ],
18-
# ])
7+
test("simple", [
8+
[
9+
{ "type": "property", "content": "AddEffect" }, { "type": "extra", "content": " " }, { "type": "extra", "content": "=" }, { "type": "extra", "content": " " }, { "type": "value", "content": "MOPixel" }
10+
]
11+
])
12+
test("comments", [
13+
[
14+
{ "type": "extra", "content": "\n" },
15+
{ "type": "extra", "content": "// foo"}, { "type": "extra", "content": "\n" },
16+
{ "type": "extra", "content": "/*a\nb\nc*/" }, { "type": "extra", "content": "\n" },
17+
],
18+
])
1919
test("nested", [
2020
[
2121
{ "type": "property", "content": "Foo" }, { "type": "extra", "content": " " }, { "type": "extra", "content": "=" }, { "type": "extra", "content": " " }, { "type": "value", "content": "Bar" }, { "type": "extra", "content": "\n" },

0 commit comments

Comments
 (0)