YouCompleteMe/cpp/ycm/IdentifierUtils.cpp

67 lines
2.0 KiB
C++
Raw Normal View History

2012-07-23 23:17:59 -04:00
// Copyright (C) 2011, 2012 Strahinja Val Markovic <val@markovic.io>
//
// This file is part of YouCompleteMe.
//
// YouCompleteMe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// YouCompleteMe is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
#include "IdentifierUtils.h"
#include "standard.h"
#include <boost/regex.hpp>
#include <boost/algorithm/string/regex.hpp>
2013-01-19 23:10:52 -05:00
namespace YouCompleteMe {
2012-07-23 23:17:59 -04:00
2013-01-19 23:10:52 -05:00
const char *COMMENT_AND_STRING_REGEX =
2012-07-23 23:17:59 -04:00
"//.*?$" // Anything following '//'
"|"
"#.*?$" // Anything following '#'
"|"
"/\\*.*?\\*/" // C-style comments, '/* ... */'
"|"
2013-02-23 18:54:44 -05:00
// Anything inside single quotes, '...', but mind the escaped quote
"'(?:\\\\'|.)*?'"
2012-07-23 23:17:59 -04:00
"|"
2013-02-23 18:54:44 -05:00
// Anything inside double quotes, "...", but mind the escaped double quote
"\"(?:\\\\\"|.)*?\"";
2012-07-23 23:17:59 -04:00
2013-01-19 23:10:52 -05:00
const char *IDENTIFIER_REGEX = "[_a-zA-Z]\\w*";
2012-07-23 23:17:59 -04:00
2013-01-19 23:10:52 -05:00
std::string RemoveIdentifierFreeText( std::string text ) {
boost::erase_all_regex( text, boost::regex( COMMENT_AND_STRING_REGEX ) );
return text;
2012-07-23 23:17:59 -04:00
}
std::vector< std::string > ExtractIdentifiersFromText(
2013-01-19 23:10:52 -05:00
const std::string &text ) {
2012-07-23 23:17:59 -04:00
std::string::const_iterator start = text.begin();
std::string::const_iterator end = text.end();
boost::match_results< std::string::const_iterator > matches;
boost::regex expression( IDENTIFIER_REGEX );
std::vector< std::string > identifiers;
2013-01-19 23:10:52 -05:00
while ( boost::regex_search( start, end, matches, expression ) ) {
2012-07-23 23:17:59 -04:00
identifiers.push_back( matches[ 0 ] );
start = matches[ 0 ].second;
}
return identifiers;
}
} // namespace YouCompleteMe