1 // Copyright 2020 The Tint Authors. 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 #ifndef SRC_READER_WGSL_LEXER_H_ 16 #define SRC_READER_WGSL_LEXER_H_ 17 18 #include <string> 19 20 #include "src/reader/wgsl/token.h" 21 22 namespace tint { 23 namespace reader { 24 namespace wgsl { 25 26 /// Converts the input stream into a series of Tokens 27 class Lexer { 28 public: 29 /// Creates a new Lexer 30 /// @param file_path the path to the file containing the source 31 /// @param content the source content 32 Lexer(const std::string& file_path, const Source::FileContent* content); 33 ~Lexer(); 34 35 /// Returns the next token in the input stream. 36 /// @return Token 37 Token next(); 38 39 private: 40 /// Advances past whitespace and comments, if present 41 /// at the current position. 42 /// @returns error token, EOF, or uninitialized 43 Token skip_whitespace_and_comments(); 44 /// Advances past a comment at the current position, 45 /// if one exists. 46 /// @returns uninitialized token on success, or error 47 Token skip_comment(); 48 49 Token build_token_from_int_if_possible(Source source, 50 size_t start, 51 size_t end, 52 int32_t base); 53 Token check_keyword(const Source&, const std::string&); 54 55 /// The try_* methods have the following in common: 56 /// - They assume there is at least one character to be consumed, 57 /// i.e. the input has not yet reached end of file. 58 /// - They return an initialized token when they match and consume 59 /// a token of the specified kind. 60 /// - Some can return an error token. 61 /// - Otherwise they return an uninitialized token when they did not 62 /// match a token of the specfied kind. 63 Token try_float(); 64 Token try_hex_float(); 65 Token try_hex_integer(); 66 Token try_ident(); 67 Token try_integer(); 68 Token try_punctuation(); 69 70 Source begin_source() const; 71 void end_source(Source&) const; 72 73 /// @returns true if the end of the input has been reached. 74 bool is_eof() const; 75 /// @param ch a character 76 /// @returns true if 'ch' is an alphabetic character 77 bool is_alpha(char ch) const; 78 /// @param ch a character 79 /// @returns true if 'ch' is a decimal digit 80 bool is_digit(char ch) const; 81 /// @param ch a character 82 /// @returns true if 'ch' is a hexadecimal digit 83 bool is_hex(char ch) const; 84 /// @param ch a character 85 /// @returns true if 'ch' is a digit, an alphabetic character, 86 /// or an underscore. 87 bool is_alphanum_underscore(char ch) const; 88 bool matches(size_t pos, const std::string& substr); 89 90 /// The source file path 91 std::string const file_path_; 92 /// The source file content 93 Source::FileContent const* const content_; 94 /// The length of the input 95 uint32_t len_ = 0; 96 /// The current position within the input 97 uint32_t pos_ = 0; 98 /// The current location within the input 99 Source::Location location_; 100 }; 101 102 } // namespace wgsl 103 } // namespace reader 104 } // namespace tint 105 106 #endif // SRC_READER_WGSL_LEXER_H_ 107