Exemplo n.º 1
0
    def create_parser ( self, rule : str = None, read_cache : bool = True, write_cache : bool = True ):
        with open( Path( __file__ ).parent / "grammar.lark", "r" ) as f:
            grammar_content = f.read()

            cache_path = self.get_cache_path( rule, grammar_content ) if read_cache or write_cache else None

            parser = None

            if read_cache and cache_path.exists():
                with open( cache_path, "rb" ) as f:
                    parser = Lark.load( f )
            else:
                parser = Lark( grammar_content, start = rule or 'start', 
                    parser='lalr', debug=False, propagate_positions = True, 
                    maybe_placeholders = True )
            
                if write_cache:
                    with open( cache_path, "wb" ) as f:
                        parser.save( f )

            return parser