mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Make several methods of SharedParserData private (#66670)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/66670 Reviewed By: zhxchen17 Differential Revision: D31674377 Pulled By: gmagogsfm fbshipit-source-id: 5c73b78f842c5c4305047ca98f40bf99bd3d2d60
This commit is contained in:
committed by
Facebook GitHub Bot
parent
e88d1c4f10
commit
e48a4cbf64
@ -182,80 +182,6 @@ struct TORCH_API SharedParserData {
|
||||
#undef ADD_CASE
|
||||
}
|
||||
|
||||
// 1. skip whitespace
|
||||
// 2. handle comment or newline
|
||||
//
|
||||
bool isNumber(const std::string& str, size_t start, size_t* len) {
|
||||
char first = str[start];
|
||||
// strtod allows numbers to start with + or - or nan or inf
|
||||
// http://en.cppreference.com/w/cpp/string/byte/strtof
|
||||
// but we want only the number part, otherwise 1+3 will turn into two
|
||||
// adjacent numbers in the lexer
|
||||
if (first == '-' || first == '+' || isalpha(first))
|
||||
return false;
|
||||
const char* startptr = str.c_str() + start;
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
|
||||
char* endptr;
|
||||
torch::jit::strtod_c(startptr, &endptr);
|
||||
*len = endptr - startptr;
|
||||
// check if the number is complex valued
|
||||
// access is safe because string is assumed to be null terminated
|
||||
if (endptr != nullptr && *endptr == 'j') {
|
||||
*len += 1;
|
||||
}
|
||||
return *len > 0;
|
||||
}
|
||||
|
||||
bool isCharCount(char c, const std::string& str, size_t start, int len) {
|
||||
// count checks from [start, start + len)
|
||||
return start + len <= str.size() &&
|
||||
std::count(str.begin() + start, str.begin() + start + len, c) == len;
|
||||
}
|
||||
|
||||
// python concatenates all adjacent strings "a" "b" == "ab"
|
||||
// strings can be enclosed with 1 or 3 single or double quotes
|
||||
// if enclosed with 3 quotes newlines are valid
|
||||
// as elsewhere, backslash and new line should be ignored
|
||||
bool isString(const std::string& str, size_t start, size_t* len) {
|
||||
char quote = str[start];
|
||||
if (quote != '\"' && quote != '\'')
|
||||
return false;
|
||||
int quote_len = isCharCount(quote, str, start, 3) ? 3 : 1;
|
||||
|
||||
// end is now set past the opening quotation marks
|
||||
size_t end = start + quote_len;
|
||||
while (end < str.size() && !isCharCount(quote, str, end, quote_len)) {
|
||||
if (str[end] == '\n' && quote_len != 3) {
|
||||
return false;
|
||||
}
|
||||
// handle escaped characters. advances past escaped quotation marks,
|
||||
// escaped newlines and escaped backslashes
|
||||
// multi-char escapes like \x1A are handled fine here because the
|
||||
// remainder of the escape are valid string characters anyway
|
||||
if (str[end] == '\\') {
|
||||
end++;
|
||||
}
|
||||
end++;
|
||||
}
|
||||
// set length equal to the complete string including quotations
|
||||
*len = end - start + quote_len;
|
||||
// if end finished without going past the last character of the string than
|
||||
// there is a match
|
||||
return end < str.size();
|
||||
}
|
||||
|
||||
bool isblank(int n) {
|
||||
return isspace(n) && n != '\n';
|
||||
}
|
||||
// Make an exception ignoring comments for type annotation comments
|
||||
bool isTypeComment(const std::string& str, size_t pos) {
|
||||
const std::string type_string = "# type:";
|
||||
if (str.size() < pos + type_string.length()) {
|
||||
return false;
|
||||
}
|
||||
auto match_string = str.substr(pos, type_string.size());
|
||||
return match_string == type_string;
|
||||
}
|
||||
// find the longest match of str.substring(pos) against a token, return true
|
||||
// if successful filling in kind, start,and len
|
||||
bool match(
|
||||
@ -353,6 +279,7 @@ struct TORCH_API SharedParserData {
|
||||
}
|
||||
return matched;
|
||||
}
|
||||
|
||||
bool isUnary(int kind, int* prec);
|
||||
bool isBinary(int kind, int* prec);
|
||||
bool isRightAssociative(int kind) {
|
||||
@ -369,6 +296,83 @@ struct TORCH_API SharedParserData {
|
||||
bool validIdent(size_t i, char n) {
|
||||
return isalpha(n) || n == '_' || (i > 0 && isdigit(n));
|
||||
}
|
||||
|
||||
// 1. skip whitespace
|
||||
// 2. handle comment or newline
|
||||
//
|
||||
bool isNumber(const std::string& str, size_t start, size_t* len) {
|
||||
char first = str[start];
|
||||
// strtod allows numbers to start with + or - or nan or inf
|
||||
// http://en.cppreference.com/w/cpp/string/byte/strtof
|
||||
// but we want only the number part, otherwise 1+3 will turn into two
|
||||
// adjacent numbers in the lexer
|
||||
if (first == '-' || first == '+' || isalpha(first))
|
||||
return false;
|
||||
const char* startptr = str.c_str() + start;
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
|
||||
char* endptr;
|
||||
torch::jit::strtod_c(startptr, &endptr);
|
||||
*len = endptr - startptr;
|
||||
// check if the number is complex valued
|
||||
// access is safe because string is assumed to be null terminated
|
||||
if (endptr != nullptr && *endptr == 'j') {
|
||||
*len += 1;
|
||||
}
|
||||
return *len > 0;
|
||||
}
|
||||
|
||||
bool isCharCount(char c, const std::string& str, size_t start, int len) {
|
||||
// count checks from [start, start + len)
|
||||
return start + len <= str.size() &&
|
||||
std::count(str.begin() + start, str.begin() + start + len, c) == len;
|
||||
}
|
||||
|
||||
// python concatenates all adjacent strings "a" "b" == "ab"
|
||||
// strings can be enclosed with 1 or 3 single or double quotes
|
||||
// if enclosed with 3 quotes newlines are valid
|
||||
// as elsewhere, backslash and new line should be ignored
|
||||
bool isString(const std::string& str, size_t start, size_t* len) {
|
||||
char quote = str[start];
|
||||
if (quote != '\"' && quote != '\'')
|
||||
return false;
|
||||
int quote_len = isCharCount(quote, str, start, 3) ? 3 : 1;
|
||||
|
||||
// end is now set past the opening quotation marks
|
||||
size_t end = start + quote_len;
|
||||
while (end < str.size() && !isCharCount(quote, str, end, quote_len)) {
|
||||
if (str[end] == '\n' && quote_len != 3) {
|
||||
return false;
|
||||
}
|
||||
// handle escaped characters. advances past escaped quotation marks,
|
||||
// escaped newlines and escaped backslashes
|
||||
// multi-char escapes like \x1A are handled fine here because the
|
||||
// remainder of the escape are valid string characters anyway
|
||||
if (str[end] == '\\') {
|
||||
end++;
|
||||
}
|
||||
end++;
|
||||
}
|
||||
// set length equal to the complete string including quotations
|
||||
*len = end - start + quote_len;
|
||||
// if end finished without going past the last character of the string than
|
||||
// there is a match
|
||||
return end < str.size();
|
||||
}
|
||||
|
||||
bool isblank(int n) {
|
||||
return isspace(n) && n != '\n';
|
||||
}
|
||||
|
||||
// Make an exception ignoring comments for type annotation comments
|
||||
bool isTypeComment(const std::string& str, size_t pos) {
|
||||
const std::string type_string = "# type:";
|
||||
if (str.size() < pos + type_string.length()) {
|
||||
return false;
|
||||
}
|
||||
auto match_string = str.substr(pos, type_string.size());
|
||||
return match_string == type_string;
|
||||
}
|
||||
|
||||
TokenTrieRef head;
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user