/src/wabt/include/wabt/wast-lexer.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2016 WebAssembly Community Group participants |
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 WABT_WAST_LEXER_H_ |
18 | | #define WABT_WAST_LEXER_H_ |
19 | | |
20 | | #include <cstddef> |
21 | | #include <cstdio> |
22 | | #include <memory> |
23 | | |
24 | | #include "wabt/common.h" |
25 | | #include "wabt/error.h" |
26 | | #include "wabt/lexer-source-line-finder.h" |
27 | | #include "wabt/literal.h" |
28 | | #include "wabt/opcode.h" |
29 | | #include "wabt/token.h" |
30 | | |
31 | | namespace wabt { |
32 | | |
33 | | class ErrorHandler; |
34 | | class LexerSource; |
35 | | |
36 | | class WastLexer { |
37 | | public: |
38 | | WABT_DISALLOW_COPY_AND_ASSIGN(WastLexer); |
39 | | |
40 | | WastLexer(std::unique_ptr<LexerSource> source, |
41 | | std::string_view filename, |
42 | | Errors*); |
43 | | |
44 | | // Convenience functions. |
45 | | static std::unique_ptr<WastLexer> CreateBufferLexer(std::string_view filename, |
46 | | const void* data, |
47 | | size_t size, |
48 | | Errors*); |
49 | | |
50 | | Token GetToken(); |
51 | | |
52 | | // TODO(binji): Move this out of the lexer. |
53 | 0 | std::unique_ptr<LexerSourceLineFinder> MakeLineFinder() { |
54 | 0 | return std::make_unique<LexerSourceLineFinder>(source_->Clone()); |
55 | 0 | } |
56 | | |
57 | | private: |
58 | | static constexpr int kEof = -1; |
59 | | enum class CharClass { IdChar = 1, Keyword = 2, HexDigit = 4, Digit = 8 }; |
60 | | |
61 | | Location GetLocation(); |
62 | | std::string_view GetText(size_t offset = 0); |
63 | | |
64 | | Token BareToken(TokenType); |
65 | | Token LiteralToken(TokenType, LiteralType); |
66 | | Token TextToken(TokenType, size_t offset = 0); |
67 | | |
68 | | int PeekChar(); |
69 | | int ReadChar(); |
70 | | bool MatchChar(char); |
71 | | bool MatchString(std::string_view); |
72 | | void Newline(); |
73 | | bool ReadBlockComment(); // Returns false if EOF. |
74 | | bool ReadLineComment(); // Returns false if EOF. |
75 | | void ReadWhitespace(); |
76 | | |
77 | | static bool IsCharClass(int c, CharClass); |
78 | 24.8M | static bool IsDigit(int c) { return IsCharClass(c, CharClass::Digit); } |
79 | 15.8M | static bool IsHexDigit(int c) { return IsCharClass(c, CharClass::HexDigit); } |
80 | 25.9M | static bool IsKeyword(int c) { return IsCharClass(c, CharClass::Keyword); } |
81 | 180M | static bool IsIdChar(int c) { return IsCharClass(c, CharClass::IdChar); } |
82 | | |
83 | | bool ReadNum(); |
84 | | bool ReadHexNum(); |
85 | | |
86 | | enum class ReservedChars { None, Some, Id }; |
87 | | ReservedChars ReadReservedChars(); |
88 | 1.51M | bool NoTrailingReservedChars() { |
89 | 1.51M | return ReadReservedChars() == ReservedChars::None; |
90 | 1.51M | } |
91 | | void ReadSign(); |
92 | | Token GetStringToken(); |
93 | | Token GetNumberToken(TokenType); |
94 | | Token GetHexNumberToken(TokenType); |
95 | | Token GetInfToken(); |
96 | | Token GetNanToken(); |
97 | | Token GetNameEqNumToken(std::string_view name, TokenType); |
98 | | Token GetIdChars(); |
99 | | Token GetKeywordToken(); |
100 | | Token GetReservedToken(); |
101 | | |
102 | | std::unique_ptr<LexerSource> source_; |
103 | | std::string filename_; |
104 | | int line_; |
105 | | const char* buffer_; |
106 | | const char* buffer_end_; |
107 | | const char* line_start_; |
108 | | const char* token_start_; |
109 | | const char* cursor_; |
110 | | |
111 | | Errors* errors_; |
112 | | void WABT_PRINTF_FORMAT(3, 4) Error(Location, const char* format, ...); |
113 | | }; |
114 | | |
115 | | } // namespace wabt |
116 | | |
117 | | #endif /* WABT_WAST_LEXER_H_ */ |