Пример #1
0
    def __init__(self):

        self._parser = Parser(MetaGrammar())
        self._parser.enableLineComments('#')
        self._parser.enableBlockComments('<!--', '-->')
        self._full_backtracking = False
        
        self._ast = None
        self._clear()
Пример #2
0
class MetaParser(object):
    
    def __init__(self):

        self._parser = Parser(MetaGrammar())
        self._parser.enableLineComments('#')
        self._parser.enableBlockComments('<!--', '-->')
        self._full_backtracking = False
        
        self._ast = None
        self._clear()
        
    def compile_string(self, content):
        
        self.parse_string(content)
        
        return self._create_symbols()
        
    def compile_file(self, file_path):
        
        self.parse_file(file_path)
        
        return self._create_symbols()
        
    def _create_symbols(self):
        
        self.analyze()
        
        return Symbols(self._symbols,
                       self._token_types,
                       self._line_comment,
                       self._block_comment,
                       {
                        "full-backtracking" : self._full_backtracking 
                        }
                       )
            
    def parse_string(self, content):
        
        self._ast = self._parser.parseString(content)
        self._clear()
        
    def parse_file(self, file_path):
        
        self._ast = self._parser.parseFile(file_path)
        self._clear()
    
    def analyze(self):
        
        if not self._ast:
            return
        
        self._clear()

        traverser = AstTraverser(self._ast)
        traverser.walk(_AnalyseStep1(self))
        traverser.walk(_AnalyseStep2(self))
        
    def _clear(self):
        
        self._symbols = {}
        self._token_types = []
        self._kw_to_token_id_map = {}
        self._line_comment = None
        self._block_comment = None