blob: c2f63e4856ba4b21380826905f5c84ed68f233ba [file] [log] [blame]
Christopher Wileyf690be52015-09-14 15:19:10 -07001/*
2 * Copyright (C) 2015, The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include "parse_helpers.h"
18
19#include <algorithm>
20#include <cctype>
21#include <cstring>
22#include <stdlib.h>
23#include <string>
24#include <vector>
25
26#include "aidl_language.h"
27
28namespace android {
29namespace aidl {
30
31char* parse_import_statement(const char* text) {
32 const char* end;
33 int len;
34
35 while (isspace(*text)) {
36 text++;
37 }
38 while (!isspace(*text)) {
39 text++;
40 }
41 while (isspace(*text)) {
42 text++;
43 }
44 end = text;
45 while (!isspace(*end) && *end != ';') {
46 end++;
47 }
48 len = end - text;
49
Casey Dahlin030977a2015-09-29 11:29:35 -070050 char* rv = new char[len + 1];
Christopher Wileyf690be52015-09-14 15:19:10 -070051 memcpy(rv, text, len);
52 rv[len] = '\0';
53
54 return rv;
55}
56
Christopher Wileyf690be52015-09-14 15:19:10 -070057bool is_java_keyword(const char* str) {
58 static const std::vector<std::string> kJavaKeywords{
59 "abstract", "assert", "boolean", "break", "byte",
60 "case", "catch", "char", "class", "const",
61 "continue", "default", "do", "double", "else",
62 "enum", "extends", "final", "finally", "float",
63 "for", "goto", "if", "implements", "import",
64 "instanceof", "int", "interface", "long", "native",
65 "new", "package", "private", "protected", "public",
66 "return", "short", "static", "strictfp", "super",
67 "switch", "synchronized", "this", "throw", "throws",
68 "transient", "try", "void", "volatile", "while",
69 "true", "false", "null",
70 };
71 return std::find(kJavaKeywords.begin(), kJavaKeywords.end(), str) !=
72 kJavaKeywords.end();
73}
74
Casey Dahlin030977a2015-09-29 11:29:35 -070075char* cpp_strdup(const char* in)
76{
77 char *out = new char[std::strlen(in) + 1];
78 strcpy(out, in);
79 return out;
80}
81
Casey Dahlinf2d23f72015-10-02 16:19:19 -070082std::string gather_comments(extra_text_type* extra) {
83 std::string s;
84 for (; extra; extra = extra->next) {
85 if (extra->which == SHORT_COMMENT) {
86 s += extra->data;
87 }
88 else if (extra->which == LONG_COMMENT) {
89 s += "/*";
90 s += extra->data;
91 s += "*/";
92 }
93 }
94 return s;
95}
96
Christopher Wileyf690be52015-09-14 15:19:10 -070097} // namespace android
98} // namespace aidl