• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <algorithm>
6 #include <fstream>
7 #include <iostream>
8 #include <string>
9 
10 #include "src/torque/ast.h"
11 #include "src/torque/utils.h"
12 
13 namespace v8 {
14 namespace internal {
15 namespace torque {
16 
StringLiteralUnquote(const std::string & s)17 std::string StringLiteralUnquote(const std::string& s) {
18   DCHECK(('"' == s.front() && '"' == s.back()) ||
19          ('\'' == s.front() && '\'' == s.back()));
20   std::stringstream result;
21   for (size_t i = 1; i < s.length() - 1; ++i) {
22     if (s[i] == '\\') {
23       switch (s[++i]) {
24         case 'n':
25           result << '\n';
26           break;
27         case 'r':
28           result << '\r';
29           break;
30         case 't':
31           result << '\t';
32           break;
33         case '\'':
34         case '"':
35         case '\\':
36           result << s[i];
37           break;
38         default:
39           UNREACHABLE();
40       }
41     } else {
42       result << s[i];
43     }
44   }
45   return result.str();
46 }
47 
StringLiteralQuote(const std::string & s)48 std::string StringLiteralQuote(const std::string& s) {
49   std::stringstream result;
50   result << '"';
51   for (size_t i = 0; i < s.length() - 1; ++i) {
52     switch (s[i]) {
53       case '\n':
54         result << "\\n";
55         break;
56       case '\r':
57         result << "\\r";
58         break;
59       case '\t':
60         result << "\\t";
61         break;
62       case '\'':
63       case '"':
64       case '\\':
65         result << "\\" << s[i];
66         break;
67       default:
68         result << s[i];
69     }
70   }
71   result << '"';
72   return result.str();
73 }
74 
CurrentPositionAsString()75 std::string CurrentPositionAsString() {
76   return PositionAsString(CurrentSourcePosition::Get());
77 }
78 
ReportError(const std::string & error)79 [[noreturn]] void ReportError(const std::string& error) {
80   std::cerr << CurrentPositionAsString() << ": Torque error: " << error << "\n";
81   std::abort();
82 }
83 
CamelifyString(const std::string & underscore_string)84 std::string CamelifyString(const std::string& underscore_string) {
85   std::string result;
86   bool word_beginning = true;
87   for (auto current : underscore_string) {
88     if (current == '_' || current == '-') {
89       word_beginning = true;
90       continue;
91     }
92     if (word_beginning) {
93       current = toupper(current);
94     }
95     result += current;
96     word_beginning = false;
97   }
98   return result;
99 }
100 
DashifyString(const std::string & underscore_string)101 std::string DashifyString(const std::string& underscore_string) {
102   std::string result = underscore_string;
103   std::replace(result.begin(), result.end(), '_', '-');
104   return result;
105 }
106 
ReplaceFileContentsIfDifferent(const std::string & file_path,const std::string & contents)107 void ReplaceFileContentsIfDifferent(const std::string& file_path,
108                                     const std::string& contents) {
109   std::ifstream old_contents_stream(file_path.c_str());
110   std::string old_contents;
111   if (old_contents_stream.good()) {
112     std::istreambuf_iterator<char> eos;
113     old_contents =
114         std::string(std::istreambuf_iterator<char>(old_contents_stream), eos);
115     old_contents_stream.close();
116   }
117   if (old_contents.length() == 0 || old_contents != contents) {
118     std::ofstream new_contents_stream;
119     new_contents_stream.open(file_path.c_str());
120     new_contents_stream << contents;
121     new_contents_stream.close();
122   }
123 }
124 
125 }  // namespace torque
126 }  // namespace internal
127 }  // namespace v8
128