Просмотр исходного кода

Extend description of parser states to show which tokens they consume. (#3378)

Also some minor improvements and typo fixes to the parser code for
issues found while writing these descriptions.
Richard Smith 2 лет назад
Родитель
Сommit
0fb2924c99

+ 3 - 0
toolchain/parse/context.h

@@ -120,6 +120,9 @@ class Context {
   // Returns the current position and moves past it.
   auto Consume() -> Lex::Token { return *(position_++); }
 
+  // Consumes the current token. Does not return it.
+  auto ConsumeAndDiscard() -> void { ++position_; }
+
   // Parses an open paren token, possibly diagnosing if necessary. Creates a
   // leaf parse node of the specified start kind. The default_token is used when
   // there's no open paren. Returns the open paren token if it was found.

+ 1 - 1
toolchain/parse/handle_expression.cpp

@@ -53,7 +53,7 @@ auto HandleExpr(Context& context) -> void {
                                    *operator_precedence);
     }
 
-    ++context.position();
+    context.ConsumeAndDiscard();
     context.PushStateForExpr(*operator_precedence);
   } else {
     context.PushStateForExprLoop(State::ExprLoop, state.ambient_precedence,

+ 2 - 2
toolchain/parse/handle_function.cpp

@@ -24,10 +24,10 @@ auto HandleFunctionAfterParameters(Context& context) -> void {
   context.PushState(state);
 
   // If there is a return type, parse the expression before adding the return
-  // type nod.e
+  // type node.
   if (context.PositionIs(Lex::TokenKind::MinusGreater)) {
     context.PushState(State::FunctionReturnTypeFinish);
-    ++context.position();
+    context.ConsumeAndDiscard();
     context.PushStateForExpr(PrecedenceGroup::ForType());
   }
 }

+ 4 - 4
toolchain/parse/handle_statement.cpp

@@ -23,7 +23,7 @@ auto HandleStatement(Context& context) -> void {
     case Lex::TokenKind::For: {
       context.PushState(State::StatementForFinish);
       context.PushState(State::StatementForHeader);
-      ++context.position();
+      context.ConsumeAndDiscard();
       break;
     }
     case Lex::TokenKind::If: {
@@ -108,7 +108,7 @@ auto HandleStatementForHeader(Context& context) -> void {
 
     if (auto next_in = context.FindNextOf({Lex::TokenKind::In})) {
       context.SkipTo(*next_in);
-      ++context.position();
+      context.ConsumeAndDiscard();
     }
     state.has_error = true;
     context.PushState(state);
@@ -143,7 +143,7 @@ auto HandleStatementIf(Context& context) -> void {
 
   context.PushState(State::StatementIfConditionFinish);
   context.PushState(State::ParenConditionAsIf);
-  ++context.position();
+  context.ConsumeAndDiscard();
 }
 
 auto HandleStatementIfConditionFinish(Context& context) -> void {
@@ -218,7 +218,7 @@ auto HandleStatementWhile(Context& context) -> void {
 
   context.PushState(State::StatementWhileConditionFinish);
   context.PushState(State::ParenConditionAsWhile);
-  ++context.position();
+  context.ConsumeAndDiscard();
 }
 
 auto HandleStatementWhileConditionFinish(Context& context) -> void {

Разница между файлами не показана из-за своего большого размера
+ 421 - 162
toolchain/parse/state.def


Некоторые файлы не были показаны из-за большого количества измененных файлов