OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef TOOLS_GN_TOKENIZER_H_ | 5 #ifndef TOOLS_GN_TOKENIZER_H_ |
6 #define TOOLS_GN_TOKENIZER_H_ | 6 #define TOOLS_GN_TOKENIZER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
12 #include "base/strings/string_util.h" | |
13 #include "tools/gn/err.h" | 12 #include "tools/gn/err.h" |
14 #include "tools/gn/token.h" | 13 #include "tools/gn/token.h" |
15 | 14 |
16 class InputFile; | 15 class InputFile; |
17 | 16 |
18 class Tokenizer { | 17 class Tokenizer { |
19 public: | 18 public: |
20 static std::vector<Token> Tokenize(const InputFile* input_file, Err* err); | 19 static std::vector<Token> Tokenize(const InputFile* input_file, Err* err); |
21 | 20 |
22 // Counts lines in the given buffer (the first line is "1") and returns | 21 // Counts lines in the given buffer (the first line is "1") and returns |
23 // the byte offset of the beginning of that line, or (size_t)-1 if there | 22 // the byte offset of the beginning of that line, or (size_t)-1 if there |
24 // aren't that many lines in the file. Note that this will return the byte | 23 // aren't that many lines in the file. Note that this will return the byte |
25 // one past the end of the input if the last character is a newline. | 24 // one past the end of the input if the last character is a newline. |
26 // | 25 // |
27 // This is a helper function for error output so that the tokenizer's | 26 // This is a helper function for error output so that the tokenizer's |
28 // notion of lines can be used elsewhere. | 27 // notion of lines can be used elsewhere. |
29 static size_t ByteOffsetOfNthLine(const base::StringPiece& buf, int n); | 28 static size_t ByteOffsetOfNthLine(const base::StringPiece& buf, int n); |
30 | 29 |
31 // Returns true if the given offset of the string piece counts as a newline. | 30 // Returns true if the given offset of the string piece counts as a newline. |
32 // The offset must be in the buffer. | 31 // The offset must be in the buffer. |
33 static bool IsNewline(const base::StringPiece& buffer, size_t offset); | 32 static bool IsNewline(const base::StringPiece& buffer, size_t offset); |
34 | 33 |
35 static bool IsIdentifierFirstChar(char c) { | 34 static bool IsIdentifierFirstChar(char c); |
36 return base::IsAsciiAlpha(c) || c == '_'; | |
37 } | |
38 | 35 |
39 static bool IsIdentifierContinuingChar(char c) { | 36 static bool IsIdentifierContinuingChar(char c); |
40 // Also allow digits after the first char. | |
41 return IsIdentifierFirstChar(c) || base::IsAsciiDigit(c); | |
42 } | |
43 | 37 |
44 private: | 38 private: |
45 // InputFile must outlive the tokenizer and all generated tokens. | 39 // InputFile must outlive the tokenizer and all generated tokens. |
46 Tokenizer(const InputFile* input_file, Err* err); | 40 Tokenizer(const InputFile* input_file, Err* err); |
47 ~Tokenizer(); | 41 ~Tokenizer(); |
48 | 42 |
49 std::vector<Token> Run(); | 43 std::vector<Token> Run(); |
50 | 44 |
51 void AdvanceToNextToken(); | 45 void AdvanceToNextToken(); |
52 Token::Type ClassifyCurrent() const; | 46 Token::Type ClassifyCurrent() const; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 Err* err_; | 79 Err* err_; |
86 size_t cur_; // Byte offset into input buffer. | 80 size_t cur_; // Byte offset into input buffer. |
87 | 81 |
88 int line_number_; | 82 int line_number_; |
89 int char_in_line_; | 83 int char_in_line_; |
90 | 84 |
91 DISALLOW_COPY_AND_ASSIGN(Tokenizer); | 85 DISALLOW_COPY_AND_ASSIGN(Tokenizer); |
92 }; | 86 }; |
93 | 87 |
94 #endif // TOOLS_GN_TOKENIZER_H_ | 88 #endif // TOOLS_GN_TOKENIZER_H_ |
OLD | NEW |