def parse(cls, line: str, file_handle: TextIO) -> "LevelTemplate": directive, comment = split_comment(line) name = directive[2:] if not name: raise ValueError("Directive missing name.") chunks = [] level_template = cls(name, comment, chunks) next_line = file_handle.peek() while next_line: next_line, comment = split_comment(next_line) # We've reached the next Template, return if next_line.startswith(DirectivePrefixes.TEMPLATE.value): return level_template if next_line or comment: chunk = Chunk.parse(file_handle) chunks.append(chunk) else: # Advance the cursor and peek the next line. file_handle.advance() next_line = file_handle.peek() return level_template