@@ -168,42 +168,41 @@ struct Lexer {
168168 advance ();
169169 }
170170
171- std::optional<Token> peek () const { return curr; }
172-
173171 bool takeLParen () {
174- auto t = peek ();
175- if (!t || !t->isLParen ()) {
172+ if (!curr || !curr->isLParen ()) {
176173 return false ;
177174 }
178175 advance ();
179176 return true ;
180177 }
181178
179+ bool peekLParen () { return Lexer (*this ).takeLParen (); }
180+
182181 bool takeRParen () {
183- auto t = peek ();
184- if (!t || !t->isRParen ()) {
182+ if (!curr || !curr->isRParen ()) {
185183 return false ;
186184 }
187185 advance ();
188186 return true ;
189187 }
190188
189+ bool peekRParen () { return Lexer (*this ).takeRParen (); }
190+
191191 bool takeUntilParen () {
192192 while (true ) {
193- auto t = peek ();
194- if (!t) {
193+ if (!curr) {
195194 return false ;
196195 }
197- if (t ->isLParen () || t ->isRParen ()) {
196+ if (curr ->isLParen () || curr ->isRParen ()) {
198197 return true ;
199198 }
200199 advance ();
201200 }
202201 }
203202
204203 std::optional<Name> takeID () {
205- if (auto t = peek () ) {
206- if (auto id = t ->getID ()) {
204+ if (curr ) {
205+ if (auto id = curr ->getID ()) {
207206 advance ();
208207 // See comment on takeName.
209208 return Name (std::string (*id));
@@ -213,18 +212,22 @@ struct Lexer {
213212 }
214213
215214 std::optional<std::string_view> takeKeyword () {
216- if (auto t = peek () ) {
217- if (auto keyword = t ->getKeyword ()) {
215+ if (curr ) {
216+ if (auto keyword = curr ->getKeyword ()) {
218217 advance ();
219218 return *keyword;
220219 }
221220 }
222221 return {};
223222 }
224223
224+ std::optional<std::string_view> peekKeyword () {
225+ return Lexer (*this ).takeKeyword ();
226+ }
227+
225228 bool takeKeyword (std::string_view expected) {
226- if (auto t = peek () ) {
227- if (auto keyword = t ->getKeyword ()) {
229+ if (curr ) {
230+ if (auto keyword = curr ->getKeyword ()) {
228231 if (*keyword == expected) {
229232 advance ();
230233 return true ;
@@ -236,16 +239,16 @@ struct Lexer {
236239
237240 std::optional<uint64_t > takeOffset () {
238241 using namespace std ::string_view_literals;
239- if (auto t = peek () ) {
240- if (auto keyword = t ->getKeyword ()) {
242+ if (curr ) {
243+ if (auto keyword = curr ->getKeyword ()) {
241244 if (keyword->substr (0 , 7 ) != " offset=" sv) {
242245 return {};
243246 }
244247 Lexer subLexer (keyword->substr (7 ));
245248 if (subLexer.empty ()) {
246249 return {};
247250 }
248- if (auto o = subLexer.peek () ->getU <uint64_t >()) {
251+ if (auto o = subLexer.curr ->getU <uint64_t >()) {
249252 subLexer.advance ();
250253 if (subLexer.empty ()) {
251254 advance ();
@@ -259,16 +262,16 @@ struct Lexer {
259262
260263 std::optional<uint32_t > takeAlign () {
261264 using namespace std ::string_view_literals;
262- if (auto t = peek () ) {
263- if (auto keyword = t ->getKeyword ()) {
265+ if (curr ) {
266+ if (auto keyword = curr ->getKeyword ()) {
264267 if (keyword->substr (0 , 6 ) != " align=" sv) {
265268 return {};
266269 }
267270 Lexer subLexer (keyword->substr (6 ));
268271 if (subLexer.empty ()) {
269272 return {};
270273 }
271- if (auto a = subLexer.peek () ->getU <uint32_t >()) {
274+ if (auto a = subLexer.curr ->getU <uint32_t >()) {
272275 subLexer.advance ();
273276 if (subLexer.empty ()) {
274277 advance ();
@@ -281,8 +284,8 @@ struct Lexer {
281284 }
282285
283286 template <typename T> std::optional<T> takeU () {
284- if (auto t = peek () ) {
285- if (auto n = t ->getU <T>()) {
287+ if (curr ) {
288+ if (auto n = curr ->getU <T>()) {
286289 advance ();
287290 return n;
288291 }
@@ -291,8 +294,8 @@ struct Lexer {
291294 }
292295
293296 template <typename T> std::optional<T> takeI () {
294- if (auto t = peek () ) {
295- if (auto n = t ->getI <T>()) {
297+ if (curr ) {
298+ if (auto n = curr ->getI <T>()) {
296299 advance ();
297300 return n;
298301 }
@@ -315,8 +318,8 @@ struct Lexer {
315318 std::optional<uint8_t > takeI8 () { return takeI<uint8_t >(); }
316319
317320 std::optional<double > takeF64 () {
318- if (auto t = peek () ) {
319- if (auto d = t ->getF64 ()) {
321+ if (curr ) {
322+ if (auto d = curr ->getF64 ()) {
320323 advance ();
321324 return d;
322325 }
@@ -325,8 +328,8 @@ struct Lexer {
325328 }
326329
327330 std::optional<float > takeF32 () {
328- if (auto t = peek () ) {
329- if (auto f = t ->getF32 ()) {
331+ if (curr ) {
332+ if (auto f = curr ->getF32 ()) {
330333 advance ();
331334 return f;
332335 }
@@ -335,10 +338,11 @@ struct Lexer {
335338 }
336339
337340 std::optional<std::string> takeString () {
338- if (auto t = peek ()) {
339- if (auto s = t->getString ()) {
341+ if (curr) {
342+ if (auto s = curr->getString ()) {
343+ std::string ret (*s);
340344 advance ();
341- return std::string (*s) ;
345+ return ret ;
342346 }
343347 }
344348 return {};
@@ -392,8 +396,8 @@ struct Lexer {
392396 TextPos position () const { return position (getPos ()); }
393397
394398 size_t getPos () const {
395- if (auto t = peek () ) {
396- return getIndex () - t ->span .size ();
399+ if (curr ) {
400+ return getIndex () - curr ->span .size ();
397401 }
398402 return getIndex ();
399403 }
0 commit comments