import run_tests from typing import Dict, Optional class ParseTOML: def __init__( self, toml: str ) -> None: self.text = toml self.idx = 0 self.sec : Section = {} self.key = '' self.parsed : TOML = {} self.error = False self.top() def eof( self ) -> bool: return self.error or self.idx >= len( self.text ) def peek( self ) -> str: if self.eof(): return '' else: return self.text[ self.idx ] def shift( self ) -> str: x = self.peek() self.idx += 1 return x def require( self, x: str ) -> None: if self.shift() != x: self.error = True def top( self ) -> None: while not self.eof(): self.line() def line( self ) -> None: if self.peek() == '[': self.header() else: self.kvpair() self.require( '\n' ) def header( self ) -> None: self.parsed[ self.key ] = self.sec self.sec = {} self.require( '[' ) self.key = self.word() self.require( ']' ) def kvpair( self ) -> None: k = self.word() self.require( '=' ) v = self.word() self.sec[ k ] = v def word( self ) -> str: x = self.shift() if not x.isalpha(): self.error = True while self.peek().isalnum(): x += self.shift() return x def get( self ) -> Optional[ TOML ]: self.parsed[ self.key ] = self.sec return None if self.error else self.parsed def parse_toml( toml: str ) -> Optional[ TOML ]: return ParseTOML( toml ).get()