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 #ifndef ANDROID_RS_API_GENERATOR_SCANNER_H
18 #define ANDROID_RS_API_GENERATOR_SCANNER_H
19 
20 #include <fstream>
21 #include <string>
22 
23 struct ParameterEntry;
24 
25 class Scanner {
26 private:
27     std::string mFileName;
28     // The file being parsed
29     FILE* mFile;
30     // The line number of the current entry.
31     int mLineNumber;
32     // The tag of the current entry to be processed.  See FindTag().
33     std::string mTag;
34     // The value of this entry.  See FindTag().
35     std::string mValue;
36     // Was the current tag processed?
37     bool mTagConsumed;
38     // Number of errors encountered.
39     int mErrorCount;
40 
41     /* Returns the next character from the file, incrementing the line count
42      * if \n is found.
43      */
44     int getChar();
45     /* Reads from the file, adding the characters to "segment" until
46      * the delimiter is found, a new line, or the eof.  The delimiter is added.
47      */
48     void readUpTo(char delimiter, std::string* segment);
49     /* Reads from the file, adding the characters to "segment" until
50      * the end of the line.
51      */
52     void readRestOfLine(std::string* segment);
53 
54     /* Finds the next line that's not a comment (a line that starts with #).
55      * This line is parsed into a tag and a value.
56      * A line that starts with a space (or is empty) is considered to have
57      * a null tag and all but the first character are the value.
58      * Lines that start with a non-space charcter should have a ": " to
59      * separate the tag from the value.
60      * Returns false if no more entries.
61      */
62     bool getNextEntry();
63 
64 public:
65     Scanner(const std::string& fileName, FILE* file);
66     bool atEnd();
getValue()67     std::string getValue() { return mValue; }
getNextTag()68     std::string getNextTag() {
69         mTagConsumed = true;
70         return mTag;
71     }
72 
73     // Skips over blank entries, reporting errors that start with a space.
74     void skipBlankEntries();
75     /* Finds the next unprocessed tag.  This entry should start with the specified tag.
76      * Returns false if the tag is not found and prints an error.
77      */
78     bool findTag(const char* tag);
79     // Same as findTag but does not print an error if the tag is not found.
80     bool findOptionalTag(const char* tag);
81     // Keep reading from the stream until the tag is found.
82     void skipUntilTag(const char* tag);
83     // Verifies there's no value.
84     void checkNoValue();
85 
86     std::ostream& error();
87     std::ostream& error(int lineNumber);
88 
89     /* Removes an optional double quoted "documentation" found at the end of a line.
90      * Erases that documention from the input string.
91      */
92     void parseDocumentation(std::string* s, std::string* documentation);
93     /* Parse an arg: definition.  It's of the form:
94      *    type[*] name [, test_option] [, "documentation"]
95      * The type and name are required.  The * indicates it's an output parameter.
96      * The test_option specifiies restrictions on values used when generating the test cases.
97      * It's one of range(), compatible(), conditional(), above().
98      * The documentation is enclosed in double quotes.
99      */
100     ParameterEntry* parseArgString(bool isReturn);
getErrorCount()101     bool getErrorCount() const { return mErrorCount; }
102 };
103 
104 #endif  // ANDROID_RS_API_GENERATOR_SCANNER_H
105