def analyze(self, cmake_file, data=None, newline_mode=NewlineMode.Unknown): """Reads CMake file and builds AST from it. :param cmake_file: Container to hold found information. :param data: Optional string buffer to read unit from. If not given, the referenced file at ``filepath`` is read. :param newline_mode: Newline mode (of frontend) to be matched when reading files from disk to get correct character indexes. :return: Reference to filled CMake file container (same as was passed in). """ self.clear() self._cmake_file = cmake_file cmake_file.clear() if not data: # read data buffer first to use newline translation: _logger.debug("Parsing file {}.".format(cmake_file.filepath)) open_newline_mode = NewlineMode.open_newline_mode(newline_mode) try: data = self._readfile(cmake_file, "utf-8", open_newline_mode) except UnicodeDecodeError: _logger.debug("Triggering encoding detection of {}.".format(cmake_file.filepath)) with open(cmake_file.filepath, "rb") as f: data = f.read() result = chardet.detect(data) enc = result["encoding"] conf = result["confidence"] _logger.debug("Detected file encoding {} with confidence {:.2f}.".format(enc, conf)) data = self._readfile(cmake_file, enc, open_newline_mode) else: _logger.debug("Parsing data buffer for {}.".format(cmake_file.filepath)) stream = antlr4.InputStream(data) lexer = cmakeLexer(stream) tstream = antlr4.CommonTokenStream(lexer) parser = cmakeParser(tstream) parser.addErrorListener(self) tree = parser.compilationUnit() _logger.debug("Parse tree complete.") walker = antlr4.ParseTreeWalker() walker.walk(self, tree) _logger.debug("AST complete.") self._cmake_file = None return cmake_file
def update(self, filepath, data=None): """Updates project after changes to file. :param filepath: Path to file that was changed. :param data: Optional content buffer. If given, this buffer is used instead of the actual file content. :return: Future to parsed CMake file (mainly for testing). """ firsttime = len(self.cmake_file_by_path) == 0 # try to guess newline encoding from data provided by frontend: if self._newline_mode == NewlineMode.Unknown and data: self._newline_mode = NewlineMode.detect(data) _logger.debug('Detected newline mode 0x{:02x} from frontend.'.format(self._newline_mode)) cmake_file = self._get_cmake_file(filepath) analyzer = self._analyzer_by_path[filepath] cmake_file_future = analyzer.analyze_async(cmake_file, data, self._newline_mode) cmake_file_future.add_done_callback(self.on_cmap_file_analysis_done) if firsttime: self.load_cmake_srcdir() return cmake_file_future
def test_newline_mode_open_mode(): assert NewlineMode.open_newline_mode(NewlineMode.N) is None assert NewlineMode.open_newline_mode(NewlineMode.R) == "\r" assert NewlineMode.open_newline_mode(NewlineMode.RN) == "\r\n" assert NewlineMode.open_newline_mode(NewlineMode.All) == ""
def test_newline_mode_detect(): assert NewlineMode.detect(None) == NewlineMode.Unknown assert NewlineMode.detect("") == NewlineMode.Unknown assert NewlineMode.detect("Hello") == NewlineMode.Unknown assert NewlineMode.detect("Hello\n") == NewlineMode.N assert NewlineMode.detect("\nHello") == NewlineMode.N assert NewlineMode.detect("He\nllo") == NewlineMode.N assert NewlineMode.detect("He\nllo\n") == NewlineMode.N assert NewlineMode.detect("He\nllo\n ") == NewlineMode.N assert NewlineMode.detect("Hello\r") == NewlineMode.R assert NewlineMode.detect("Hello\r\n") == NewlineMode.RN assert NewlineMode.detect("\rHello\n") == NewlineMode.R | NewlineMode.N assert NewlineMode.detect("\r\nHello\n") == NewlineMode.RN | NewlineMode.N assert NewlineMode.detect("\r\nHel\rlo\n") == NewlineMode.RN | NewlineMode.N | NewlineMode.R == NewlineMode.All
def test_newline_mode_open_mode(): assert NewlineMode.open_newline_mode(NewlineMode.N) is None assert NewlineMode.open_newline_mode(NewlineMode.R) == '\r' assert NewlineMode.open_newline_mode(NewlineMode.RN) == '\r\n' assert NewlineMode.open_newline_mode(NewlineMode.All) == ''
def test_newline_mode_detect(): assert NewlineMode.detect(None) == NewlineMode.Unknown assert NewlineMode.detect('') == NewlineMode.Unknown assert NewlineMode.detect('Hello') == NewlineMode.Unknown assert NewlineMode.detect('Hello\n') == NewlineMode.N assert NewlineMode.detect('\nHello') == NewlineMode.N assert NewlineMode.detect('He\nllo') == NewlineMode.N assert NewlineMode.detect('He\nllo\n') == NewlineMode.N assert NewlineMode.detect('He\nllo\n ') == NewlineMode.N assert NewlineMode.detect('Hello\r') == NewlineMode.R assert NewlineMode.detect('Hello\r\n') == NewlineMode.RN assert NewlineMode.detect('\rHello\n') == NewlineMode.R | NewlineMode.N assert NewlineMode.detect('\r\nHello\n') == NewlineMode.RN | NewlineMode.N assert NewlineMode.detect( '\r\nHel\rlo\n' ) == NewlineMode.RN | NewlineMode.N | NewlineMode.R == NewlineMode.All