from typing import Dict, Optional class Validate: def __init__( self, text: str ) -> None: self.text = text self.idx = 0 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: check = self.text[ self.idx : self.idx + len( x ) ] if check != x: self.error = True self.idx += len( x ) def top( self ) -> None: while not self.eof(): self.stmt() def stmt( self ) -> None: if self.peek() == 'n': self.server() self.spaces() if self.peek() == '\n': self.shift() else: self.comment() def comment( self ) -> None: self.spaces() self.require( '#' ) while not self.eof() and self.peek() != '\n': self.shift() self.require( '\n' ) def spaces( self, req: bool = False ) -> bool: if not self.peek().isspace(): if req: self.error = True return False while self.peek().isspace() and self.peek() != '\n': self.shift() return True def server( self ) -> None: self.require( 'nameserver' ) self.spaces( True ) self.address() def address( self ) -> None: self.num() for i in range( 3 ): self.require( '.' ) self.num() def num( self ) -> None: x = self.shift() if x == '0': return if not x.isdecimal(): self.error = True while self.peek().isdecimal(): self.shift() def ok( self ) -> bool: return not self.error def resolv_valid( text: str ) -> bool: return Validate( text ).ok() import run_tests