2 * Copyright 2014 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #define WHITESPACE " \t\n"
22 // Parse the |input| string as a list of type-specific tokens.
23 // This tokenizes the input, using whitespace as separators and '*' as
24 // a single token too. On success, return true and sets |*out| to the
25 // list of tokens. On failure, return false.
27 // Example: 'const char**foo' -> ['const', 'char', '*', '*', 'foo']
29 static bool parseTypeTokens(const std::string& input,
30 std::vector<std::string>* out,
35 // Parse all tokens in the input, treat '*' as a single token.
38 // skip leading whitespace.
39 pos = input.find_first_not_of(WHITESPACE, pos);
40 if (pos == std::string::npos) {
41 break; // end of parse.
44 // If this is a star, ensure it follows a type name.
45 // otherwise treat it as part of the final type.
46 if (input[pos] == '*') {
47 out->push_back(std::string("*"));
52 // find end of type/token.
53 size_t end = input.find_first_of(WHITESPACE "*", pos);
54 if (end == std::string::npos) {
58 std::string str = input.substr(pos, end - pos);
59 if (str.size() == 0) {
60 // Sanity check: should not happen.
62 *error = "Unexpected empty token !?";
72 // Sanity check: require non-empty input
74 *error = "Empty parameter declaration!";
78 // Sanity check: There must be base type name before any '*'
79 for (size_t n = 0; n < out->size(); ++n) {
80 std::string& token = (*out)[n];
82 *error = "Unexpected '*' before type name";
84 } else if (token != "const") {
93 // Given |tokens|, an input vector of strings, join the first |count| items
94 // into a normalized type string, and return it.
95 static std::string buildTypeString(const std::vector<std::string>& tokens,
99 for (size_t n = 0; n < count; ++n) {
100 const std::string& token = tokens[n];
101 if (n > 0 && token != "*") {
104 result.append(token);
110 std::string normalizeTypeDeclaration(const std::string& input) {
111 std::vector<std::string> tokens;
112 if (!parseTypeTokens(input, &tokens, NULL)) {
115 return buildTypeString(tokens, tokens.size());
118 bool parseTypeDeclaration(const std::string& input,
119 std::string* typeName,
120 std::string* error) {
121 // The type name can be made of several tokens, e.g. 'unsigned int'
122 // use an array to store them, and a count variable. Each item can be
123 // one of '*', 'const' or a type name component (e.g. 'struct', 'unsigned')
124 std::vector<std::string> tokens;
126 if (!parseTypeTokens(input, &tokens, error)) {
130 // Sanity check, there must be a least one non-special tokens.
131 size_t nonSpecialCount = 0;
132 for (size_t n = 0; n < tokens.size(); ++n) {
133 if (tokens[n] != "*" && tokens[n] != "const") {
137 if (nonSpecialCount == 0) {
138 *error = "Missing type name";
141 // Build the type name from all tokens before it.
142 *typeName = buildTypeString(tokens, tokens.size());
147 bool parseParameterDeclaration(const std::string& param,
148 std::string* typeName,
149 std::string* variableName,
150 std::string* error) {
151 std::vector<std::string> tokens;
153 if (!parseTypeTokens(param, &tokens, error)) {
157 // Sanity check, there must be a least two non-special tokens.
158 size_t nonSpecialCount = 0;
159 for (size_t n = 0; n < tokens.size(); ++n) {
160 if (tokens[n] != "*" && tokens[n] != "const") {
164 if (nonSpecialCount == 0) {
165 *error = "Missing type name";
168 if (nonSpecialCount == 1) {
169 *error = "Missing variable name";
173 // Sanity check: variable name must not be followed by 'const' or '*'
174 const std::string& lastToken = tokens[tokens.size() - 1U];
175 if (lastToken == "*") {
176 *error = "Extra '*' after variable name";
179 if (lastToken == "const") {
180 *error = "Extra 'const' after variable name";
184 // Extract the variable name as the last token.
186 *variableName = lastToken;
188 // Build the type name from all tokens before it.
189 *typeName = buildTypeString(tokens, tokens.size() - 1U);