@@ -67,7 +67,7 @@ class IndentFormat(NamedTuple):
6767
6868def lang_to_dict (
6969 lang_data : str ,
70- ) -> tuple [dict [str , Any ], CommentData ]:
70+ ) -> tuple [dict [int | str , Any ], CommentData ]:
7171 """Return data and comments from lua table."""
7272 all_tokens : list [lua_parser .Token ] = list (lua_parser .tokenize_cst (lang_data ))
7373 parser = lua_parser .Parser ([t for t in all_tokens if not isinstance (t , lua_parser .CSTToken )])
@@ -160,11 +160,12 @@ def cartrage_return() -> None:
160160
161161 root = cast ("dict[str | int, Any]" , data )
162162 roots : list [dict [str | int , Any ]] = []
163- last_key : str | int | None = None
163+ last_key : str | int | float | None = None
164164
165165 in_blocks = 0
166166 has_keys = False
167167 in_unmarked = False
168+ just_did_list_entry = False
168169 unmarked_index = 1
169170 unmarked_groups : list [tuple [bool , int ]] = []
170171
@@ -189,22 +190,36 @@ def cartrage_return() -> None:
189190
190191 # Handle case where it's just a list and no identifiers
191192 # recorded
192- if all (isinstance (key , int ) and not isinstance (value , dict ) for key , value in root .items ()):
193+ if root and all (isinstance (key , int ) and not isinstance (value , dict ) for key , value in root .items ()):
194+ just_did_list_entry = True
193195 true_start = min (map (int , root ))
194196 start = max (true_start , 1 )
195197 end = max (map (int , root ))
196198 # print(f'{root = }')
197199 # print(f'{start = } {end = }')
200+ unbroken_chain = True
198201 for index in range (start , end + 1 ):
199202 # print(f'{index = }', end=" ")
200- value = root [index ]
201- value = f'"{ value } "' if isinstance (value , str ) else repr (value )
203+ value = root .get (index )
204+ if value is None :
205+ unbroken_chain = False
206+ continue
207+ if isinstance (value , str ):
208+ value = f'"{ value } "'
209+ elif isinstance (value , int | bool ):
210+ value = repr (value ).lower ()
211+ else :
212+ raise ValueError (f"Unhandled key { type (value ) = } " )
213+ if not unbroken_chain :
214+ line += f"[{ index } ] = "
202215 line += value
203216 # No comma if end and does not need other recorded value like 0
204217 if index != end or start != true_start :
205218 line += ","
206- cartrage_return ()
219+ cartrage_return ()
207220 # print()
221+ else :
222+ just_did_list_entry = False
208223 elif token .type_ == TokenType .EndBracket :
209224 in_blocks -= 1
210225 in_unmarked , unmarked_index = unmarked_groups .pop ()
@@ -225,23 +240,26 @@ def cartrage_return() -> None:
225240 if in_blocks > 0 :
226241 line += ","
227242 cartrage_return ()
243+ just_did_list_entry = False
228244 elif token .type_ == TokenType .Newline :
229245 cartrage_return ()
230246 elif token .type_ in {TokenType .Identifier , TokenType .Numeric }:
231247 has_keys = True
232248 last_key = token .text
233249 if token .type_ == TokenType .Numeric :
234250 line += f"[{ token .text } ]"
235- last_key = int (last_key )
251+ last_key = int (last_key ) if "." not in last_key else float ( last_key )
236252 else :
237253 line += token .text
238254 line += " = "
239255 if comments .keys [idx + 1 ].type_ != TokenType .StartBracket :
256+ if last_key not in root :
257+ continue
240258 value = root [last_key ]
241- assert isinstance (value , str | int | float ), "should not unpack collections"
259+ assert isinstance (value , str | int | float | bool ), "should not unpack collections"
242260
243- if isinstance (value , int | float ):
244- line += repr (value )
261+ if isinstance (value , int | float | bool ):
262+ line += repr (value ). lower ()
245263 else :
246264 line += f'"{ value } "'
247265 if comments .keys [idx + 1 ].type_ != TokenType .EndBracket :
@@ -251,6 +269,8 @@ def cartrage_return() -> None:
251269 line += token .text
252270 cartrage_return ()
253271 elif token .type_ == TokenType .Whitespace :
272+ if just_did_list_entry :
273+ continue
254274 line += token .text
255275 ##elif isinstance(token, lua_parser.Separator):
256276 ## line += token.text
@@ -259,7 +279,7 @@ def cartrage_return() -> None:
259279
260280 ## write_dict(comments.keys)
261281
262- return "\n " .join (lines )
282+ return "\n " .join (lines ) or "{}"
263283
264284
265285def run () -> None :
0 commit comments