@@ -26,9 +26,9 @@ def get_parsed_tokens(tokens, parsed=None, token_idx=None, depth=0):
26
26
children = { "type" : "children" , "content" : [] }
27
27
parsed [- 1 ].append (children )
28
28
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 ):
30
30
return
31
- elif state == "newline" :
31
+ elif state == "newline" and ( len ( parsed ) == 0 or token [ "type" ] == "WORD" or token [ "type" ] == "TABS" ) :
32
32
parsed .append ([])
33
33
state = "start"
34
34
@@ -56,22 +56,39 @@ def get_parsed_tokens(tokens, parsed=None, token_idx=None, depth=0):
56
56
return parsed
57
57
58
58
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" ])
61
80
62
81
63
82
def is_deeper (depth , token ):
64
83
new_depth = get_depth (token )
84
+
65
85
if new_depth > depth + 1 :
66
86
line , column = get_token_pos (token )
67
87
raise ValueError (f"Too many tabs found at line { line } , column { column } in { token ['filepath' ]} " )
88
+
68
89
return new_depth > depth
69
90
70
91
71
- def get_depth (token ):
72
- return len (token ["content" ]) if token ["type" ] == "TABS" else 0
73
-
74
-
75
92
def get_token_pos (token ):
76
93
with open (token ["filepath" ], "r" ) as f :
77
94
text = f .read ()
0 commit comments