Skip to content

Commit 7ca5f5d

Browse files
committed
Finish parser! 🎉
1 parent 94b236a commit 7ca5f5d

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

Python/ini_converting/ini_parser.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
def get_parsed_tokens(tokens, parsed, i=0, depth=0):
1+
def get_parsed_tokens(tokens, parsed, i=0, depth=-1):
22
"""
33
start -> tabs -> property -> equals -> value -> newline
44
^ v ^ v
@@ -10,12 +10,14 @@ def get_parsed_tokens(tokens, parsed, i=0, depth=0):
1010
while i < len(tokens):
1111
token = tokens[i]
1212

13-
if state == "start" and token["type"] == "TABS" and is_less_deep(depth, token):
13+
if depth == -1:
14+
parsed.append( { "type": "lines_tokens", "content": [] } )
15+
_, i = get_parsed_tokens(tokens, parsed[-1]["content"], i, depth + 1)
16+
elif state == "start" and token["type"] == "TABS" and is_less_deep(depth, token):
1417
return parsed, i
1518
elif state == "start" and token["type"] == "TABS" and is_deeper(depth, token):
16-
# elif state == "start" and token["type"] == "TABS":
1719
parsed.append( { "type": "lines_tokens", "content": [] } )
18-
_, i = get_parsed_tokens(tokens, parsed[-1]["content"], i, depth + 1)
20+
return get_parsed_tokens(tokens, parsed[-1]["content"], i, depth + 1)
1921
elif state == "start" and token["type"] == "TABS":
2022
parsed.append( { "type": "extra", "content": token["content"] } )
2123
state = "tabs"
@@ -46,16 +48,16 @@ def get_parsed_tokens(tokens, parsed, i=0, depth=0):
4648
parsed.append( { "type": "extra", "content": token["content"] } )
4749
i += 1
4850

49-
return [parsed], i
51+
return parsed, i
5052

5153

5254
def is_less_deep(depth, token):
5355
return get_depth(token["content"]) < depth
5456

5557

56-
# def is_deeper(depth, token):
57-
# # TODO: This should throw an error if it's deeper by more than 1.
58-
# return get_depth(token["content"]) > depth
58+
def is_deeper(depth, token):
59+
# TODO: This should throw an error if it's deeper by more than 1.
60+
return get_depth(token["content"]) > depth
5961

6062

6163
def get_depth(content):

Python/ini_converting/ini_parser_tests.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,33 @@
55

66
def parser_tests():
77
test("simple", [
8-
[
8+
{ "type": "lines_tokens", "content": [
99
{ "type": "property", "content": "AddEffect" }, { "type": "extra", "content": " " }, { "type": "extra", "content": "=" }, { "type": "extra", "content": " " }, { "type": "value", "content": "MOPixel" }
10-
]
10+
]}
11+
])
12+
test("comments", [
13+
{ "type": "lines_tokens", "content": [
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+
]},
1118
])
1219
test("multiple", [
13-
[
20+
{ "type": "lines_tokens", "content": [
1421
{ "type": "property", "content": "Foo" }, { "type": "extra", "content": " " }, { "type": "extra", "content": "=" }, { "type": "extra", "content": " " }, { "type": "value", "content": "Bar" }, { "type": "extra", "content": "\n" },
1522
{ "type": "lines_tokens", "content": [
16-
{ "type": "extra", "content": "\t" }, { "type": "property", "content": "Baz" }, { "type": "extra", "content": " " }, { "type": "extra", "content": "=" }, { "type": "extra", "content": " " }, { "type": "value", "content": "Bee" }
23+
{ "type": "extra", "content": "\t" }, { "type": "property", "content": "Baz" }, { "type": "extra", "content": " " }, { "type": "extra", "content": "=" }, { "type": "extra", "content": " " }, { "type": "value", "content": "Bee" }, { "type": "extra", "content": "\n" }
1724
]}
18-
],
19-
[
25+
]},
26+
{ "type": "lines_tokens", "content": [
2027
{ "type": "property", "content": "A" }, { "type": "extra", "content": " " }, { "type": "extra", "content": "=" }, { "type": "extra", "content": " " }, { "type": "value", "content": "B" }, { "type": "extra", "content": "\n" },
2128
{ "type": "lines_tokens", "content": [
2229
{ "type": "extra", "content": "\t" }, { "type": "property", "content": "C" }, { "type": "extra", "content": " " }, { "type": "extra", "content": "=" }, { "type": "extra", "content": " " }, { "type": "value", "content": "D" }, { "type": "extra", "content": "\n" }
2330
]}
24-
]
31+
]}
2532
])
2633
test("complex", [
27-
[
34+
{ "type": "lines_tokens", "content": [
2835
{ "type": "extra", "content": "\n" },
2936
{ "type": "extra", "content": "// foo"}, { "type": "extra", "content": "\n" },
3037
{ "type": "extra", "content": "/*a\nb\nc*/" }, { "type": "extra", "content": "\n" },
@@ -36,7 +43,7 @@ def parser_tests():
3643
{ "type": "extra", "content": "\t\t" }, { "type": "property", "content": "Xd" }, { "type": "extra", "content": " " }, { "type": "extra", "content": "=" }, { "type": "extra", "content": " " }, { "type": "value", "content": "42" }
3744
]}
3845
]}
39-
],
46+
]},
4047
])
4148

4249

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
// foo
3+
/*a
4+
b
5+
c*/

Python/ini_converting/ini_tokenizer_tests.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ def tokenizer_tests():
66
test("simple", [
77
{ "type": "WORD", "content": "AddEffect" }, { "type": "EXTRA", "content": " " }, { "type": "EQUALS", "content": "=" }, { "type": "EXTRA", "content": " " }, { "type": "WORD", "content": "MOPixel" },
88
])
9+
test("comments", [
10+
{ "type": "NEWLINES", "content": "\n" },
11+
{ "type": "EXTRA", "content": "// foo" }, { "type": "NEWLINES", "content": "\n" },
12+
{ "type": "EXTRA", "content": "/*a\nb\nc*/" }, { "type": "NEWLINES", "content": "\n" },
13+
])
914
test("multiple", [
1015
{ "type": "WORD", "content": "Foo" }, { "type": "EXTRA", "content": " " }, { "type": "EQUALS", "content": "=" }, { "type": "EXTRA", "content": " " }, { "type": "WORD", "content": "Bar" }, { "type": "NEWLINES", "content": "\n" },
1116
{ "type": "TABS", "content": "\t" }, { "type": "WORD", "content": "Baz" }, { "type": "EXTRA", "content": " " }, { "type": "EQUALS", "content": "=" }, { "type": "EXTRA", "content": " " }, { "type": "WORD", "content": "Bee" }, { "type": "NEWLINES", "content": "\n" },

0 commit comments

Comments
 (0)