Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 1 | /* |
| 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 <iostream> |
| 18 | #include <sstream> |
| 19 | |
| 20 | #include "Scanner.h" |
| 21 | #include "Specification.h" |
| 22 | #include "Utilities.h" |
| 23 | |
| 24 | using namespace std; |
| 25 | |
| 26 | // Maximum of errors we'll report before bailing out. |
| 27 | const int MAX_ERRORS = 10; |
| 28 | |
| 29 | Scanner::Scanner(const string& fileName, FILE* file) |
| 30 | : mFileName(fileName), mFile(file), mLineNumber(0), mTagConsumed(true), mErrorCount(0) { |
| 31 | } |
| 32 | |
| 33 | bool Scanner::atEnd() { |
| 34 | return (mTagConsumed && feof(mFile)) || mErrorCount > MAX_ERRORS; |
| 35 | } |
| 36 | |
| 37 | int Scanner::getChar() { |
| 38 | int c = fgetc(mFile); |
| 39 | if (c == '\n') { |
| 40 | mLineNumber++; |
| 41 | } |
| 42 | return c; |
| 43 | } |
| 44 | |
| 45 | void Scanner::readUpTo(char delimiter, string* segment) { |
| 46 | for (;;) { |
| 47 | int c = getChar(); |
| 48 | if (c == EOF || c == '\n') { |
| 49 | break; |
| 50 | } |
| 51 | segment->push_back((char)c); |
| 52 | if (c == delimiter) { |
| 53 | break; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void Scanner::readRestOfLine(string* segment) { |
| 59 | for (;;) { |
| 60 | int c = getChar(); |
| 61 | if (c == EOF || c == '\n') { |
| 62 | return; |
| 63 | } |
| 64 | segment->push_back((char)c); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | bool Scanner::getNextEntry() { |
| 69 | mTag.clear(); |
| 70 | mValue.clear(); |
| 71 | for (;;) { |
| 72 | int c = getChar(); |
| 73 | if (c == EOF) { |
| 74 | return false; |
| 75 | } |
| 76 | if (c == '#') { |
| 77 | // Skip the comment |
| 78 | string comment; |
| 79 | readRestOfLine(&comment); |
| 80 | continue; |
| 81 | } |
| 82 | if (c == ' ') { |
| 83 | readRestOfLine(&mValue); |
| 84 | break; |
| 85 | } else if (c == '\n') { |
| 86 | break; |
| 87 | } else { |
| 88 | mTag = c; |
| 89 | readUpTo(':', &mTag); |
| 90 | readRestOfLine(&mValue); |
| 91 | trimSpaces(&mValue); |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | ostream& Scanner::error() { |
| 99 | return error(mLineNumber); |
| 100 | } |
| 101 | |
| 102 | ostream& Scanner::error(int lineNumber) { |
| 103 | if (++mErrorCount <= MAX_ERRORS) { |
| 104 | cerr << mFileName << ":" << lineNumber << ": error: "; |
| 105 | } |
| 106 | return cerr; |
| 107 | } |
| 108 | |
| 109 | void Scanner::skipBlankEntries() { |
| 110 | while (findOptionalTag("")) { |
| 111 | if (!mValue.empty()) { |
| 112 | error() << "Unexpected: \" " << mValue << "\".\n"; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | bool Scanner::findTag(const char* tag) { |
| 118 | bool found = findOptionalTag(tag); |
| 119 | if (!found) { |
| 120 | error() << "Found \"" << mTag << "\" while looking for \"" << tag << "\".\n"; |
| 121 | } |
| 122 | mTagConsumed = true; |
| 123 | return found; |
| 124 | } |
| 125 | |
| 126 | bool Scanner::findOptionalTag(const char* tag) { |
| 127 | if (mTagConsumed) { |
| 128 | if (!getNextEntry()) { |
| 129 | return false; |
| 130 | } |
| 131 | } |
| 132 | mTagConsumed = (mTag == tag); |
| 133 | return mTagConsumed; |
| 134 | } |
| 135 | |
| 136 | void Scanner::checkNoValue() { |
| 137 | if (!mValue.empty()) { |
| 138 | error() << "Did not expect \"" << mValue << "\" after \"" << mTag << "\".\n"; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void Scanner::parseDocumentation(string* s, string* documentation) { |
| 143 | size_t docStart = s->find(", \""); |
| 144 | if (docStart == string::npos) { |
| 145 | documentation->erase(); |
| 146 | } else { |
| 147 | size_t first = docStart + 3; |
| 148 | size_t last = s->find('\"', first); |
| 149 | if (last == string::npos) { |
| 150 | error() << "Missing closing double quote\n"; |
| 151 | } |
| 152 | *documentation = s->substr(first, last - first); |
| 153 | s->erase(docStart); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | ParameterEntry* Scanner::parseArgString(bool isReturn) { |
| 158 | string s = mValue; |
| 159 | ParameterEntry* p = new ParameterEntry(); |
| 160 | parseDocumentation(&s, &p->documentation); |
| 161 | |
| 162 | size_t optionStart = s.find(", "); |
| 163 | if (optionStart != string::npos) { |
| 164 | p->testOption = s.substr(optionStart + 2); |
| 165 | s.erase(optionStart); |
| 166 | } |
| 167 | |
| 168 | trimSpaces(&s); |
| 169 | if (!isReturn) { |
| 170 | size_t nameStart = s.rfind(' '); |
| 171 | if (nameStart == string::npos) { |
| 172 | error() << "Missing variable name\n"; |
| 173 | } else { |
| 174 | p->name = s.substr(nameStart + 1); |
| 175 | s.erase(nameStart); |
| 176 | if (p->name.find('*') != string::npos) { |
| 177 | error() << "The '*' should be attached to the type\n"; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (s == "void" && !isReturn) { |
| 183 | error() << "void is only allowed for ret:\n"; |
| 184 | } |
| 185 | p->type = s; |
| 186 | p->lineNumber = mLineNumber; |
| 187 | return p; |
| 188 | } |