10 #include "src/base/logging.h" 11 #include "src/torque/ast.h" 12 #include "src/torque/utils.h" 18 std::string StringLiteralUnquote(
const std::string& s) {
19 DCHECK((
'"' == s.front() &&
'"' == s.back()) ||
20 (
'\'' == s.front() &&
'\'' == s.back()));
21 std::stringstream result;
22 for (
size_t i = 1;
i < s.length() - 1; ++
i) {
49 std::string StringLiteralQuote(
const std::string& s) {
50 std::stringstream result;
52 for (
size_t i = 0;
i < s.length(); ++
i) {
66 result <<
"\\" << s[
i];
76 std::string CurrentPositionAsString() {
77 return PositionAsString(CurrentSourcePosition::Get());
80 DEFINE_CONTEXTUAL_VARIABLE(LintErrorStatus)
82 [[noreturn]]
void ReportErrorString(
const std::string& error) {
83 std::cerr << CurrentPositionAsString() <<
": Torque error: " << error <<
"\n";
84 v8::base::OS::Abort();
87 void LintError(
const std::string& error) {
88 LintErrorStatus::SetLintError();
89 std::cerr << CurrentPositionAsString() <<
": Lint error: " << error <<
"\n";
92 void NamingConventionError(
const std::string& type,
const std::string& name,
93 const std::string& convention) {
94 std::stringstream sstream;
95 sstream << type <<
" \"" << name <<
"\" doesn't follow \"" << convention
96 <<
"\" naming convention.";
97 LintError(sstream.str());
102 bool ContainsUnderscore(
const std::string& s) {
103 if (s.empty())
return false;
104 return s.find(
"_") != std::string::npos;
107 bool ContainsUpperCase(
const std::string& s) {
108 if (s.empty())
return false;
109 return std::any_of(s.begin(), s.end(), [](
char c) {
return isupper(c); });
115 bool IsKeywordLikeName(
const std::string& s) {
116 static const char*
const keyword_like_constants[]{
"True",
"False",
"Hole",
117 "Null",
"Undefined"};
119 return std::find(std::begin(keyword_like_constants),
120 std::end(keyword_like_constants),
121 s) != std::end(keyword_like_constants);
126 bool IsMachineType(
const std::string& s) {
127 static const char*
const machine_types[]{
128 "void",
"never",
"int32",
"uint32",
"int64",
"intptr",
129 "uintptr",
"float32",
"float64",
"bool",
"string",
"int31"};
131 return std::find(std::begin(machine_types), std::end(machine_types), s) !=
132 std::end(machine_types);
137 bool IsLowerCamelCase(
const std::string& s) {
138 if (s.empty())
return false;
139 return islower(s[0]) && !ContainsUnderscore(s);
142 bool IsUpperCamelCase(
const std::string& s) {
143 if (s.empty())
return false;
144 return isupper(s[0]) && !ContainsUnderscore(s);
147 bool IsSnakeCase(
const std::string& s) {
148 if (s.empty())
return false;
149 return !ContainsUpperCase(s);
152 bool IsValidNamespaceConstName(
const std::string& s) {
153 if (s.empty())
return false;
154 if (IsKeywordLikeName(s))
return true;
156 return s[0] ==
'k' && IsUpperCamelCase(s.substr(1));
159 bool IsValidTypeName(
const std::string& s) {
160 if (s.empty())
return false;
161 if (IsMachineType(s))
return true;
163 return IsUpperCamelCase(s);
166 std::string CamelifyString(
const std::string& underscore_string) {
168 bool word_beginning =
true;
169 for (
auto current : underscore_string) {
170 if (current ==
'_' || current ==
'-') {
171 word_beginning =
true;
174 if (word_beginning) {
175 current = toupper(current);
178 word_beginning =
false;
183 std::string DashifyString(
const std::string& underscore_string) {
184 std::string result = underscore_string;
185 std::replace(result.begin(), result.end(),
'_',
'-');
189 void ReplaceFileContentsIfDifferent(
const std::string& file_path,
190 const std::string& contents) {
191 std::ifstream old_contents_stream(file_path.c_str());
192 std::string old_contents;
193 if (old_contents_stream.good()) {
194 std::istreambuf_iterator<char> eos;
196 std::string(std::istreambuf_iterator<char>(old_contents_stream), eos);
197 old_contents_stream.close();
199 if (old_contents.length() == 0 || old_contents != contents) {
200 std::ofstream new_contents_stream;
201 new_contents_stream.open(file_path.c_str());
202 new_contents_stream << contents;
203 new_contents_stream.close();