Lee Campbell | 220ca84 | 2015-07-30 09:27:11 -0700 | [diff] [blame] | 1 | // Copyright (C) 2015 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include <string> |
| 16 | |
| 17 | namespace init { |
| 18 | |
| 19 | // Used to tokenize a std::string. |
| 20 | // Call Next() to advance through each token until it returns false, |
| 21 | // indicating there are no more tokens left in the string. |
| 22 | // The current token can be accessed with current(), which returns |
| 23 | // a Token. |
| 24 | // Supported tokens are: |
| 25 | // TOK_START - Next() has yet to be called |
| 26 | // TOK_END - At the end of string |
| 27 | // TOK_NEWLINE - The end of a line denoted by \n. |
| 28 | // TOK_TEXT - A word. |
| 29 | // Comments are denoted with '#' and the tokenizer will ignore |
| 30 | // the rest of the line. |
| 31 | // Double quotes can be used to insert whitespace into words. |
| 32 | // A backslash at the end of a line denotes continuation and |
| 33 | // a TOK_NEWLINE will not be generated for that line. |
| 34 | class Tokenizer { |
| 35 | public: |
| 36 | Tokenizer(const std::string& data); |
| 37 | ~Tokenizer(); |
| 38 | |
| 39 | enum TokenType { TOK_START, TOK_END, TOK_NEWLINE, TOK_TEXT }; |
| 40 | struct Token { |
| 41 | TokenType type; |
| 42 | std::string text; |
| 43 | }; |
| 44 | |
| 45 | // Returns the curret token. |
| 46 | const Token& current(); |
| 47 | |
| 48 | // Move to the next token, returns false at the end of input. |
| 49 | bool Next(); |
| 50 | |
| 51 | private: |
| 52 | void GetData(); |
| 53 | void AdvChar(); |
| 54 | void AdvText(); |
| 55 | void AdvUntil(char x); |
| 56 | void AdvWhiteSpace(); |
| 57 | void StartText(); |
| 58 | void EndText(); |
| 59 | |
| 60 | const std::string& data_; |
| 61 | Token current_; |
| 62 | |
| 63 | bool eof_; |
| 64 | size_t pos_; |
| 65 | char cur_char_; |
| 66 | size_t tok_start_; |
| 67 | }; |
| 68 | |
| 69 | } // namespace init |