parser_handle_var.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/parser/parser_context.h"
  5. namespace Carbon {
  6. // Handles VarAs(Semicolon|For).
  7. static auto ParserHandleVar(ParserContext& context, ParserState finish_state)
  8. -> void {
  9. context.PopAndDiscardState();
  10. // These will start at the `var`.
  11. context.PushState(finish_state);
  12. context.PushState(ParserState::VarAfterPattern);
  13. context.AddLeafNode(ParseNodeKind::VariableIntroducer, context.Consume());
  14. // This will start at the pattern.
  15. context.PushState(ParserState::PatternAsVariable);
  16. }
  17. auto ParserHandleVarAsSemicolon(ParserContext& context) -> void {
  18. ParserHandleVar(context, ParserState::VarFinishAsSemicolon);
  19. }
  20. auto ParserHandleVarAsFor(ParserContext& context) -> void {
  21. ParserHandleVar(context, ParserState::VarFinishAsFor);
  22. }
  23. auto ParserHandleVarAfterPattern(ParserContext& context) -> void {
  24. auto state = context.PopState();
  25. if (state.has_error) {
  26. if (auto after_pattern =
  27. context.FindNextOf({TokenKind::Equal, TokenKind::Semi})) {
  28. context.SkipTo(*after_pattern);
  29. }
  30. }
  31. if (auto equals = context.ConsumeIf(TokenKind::Equal)) {
  32. context.AddLeafNode(ParseNodeKind::VariableInitializer, *equals);
  33. context.PushState(ParserState::Expression);
  34. }
  35. }
  36. auto ParserHandleVarFinishAsSemicolon(ParserContext& context) -> void {
  37. auto state = context.PopState();
  38. auto end_token = state.token;
  39. if (context.PositionIs(TokenKind::Semi)) {
  40. end_token = context.Consume();
  41. } else {
  42. // TODO: Disambiguate between statement and member declaration.
  43. context.EmitExpectedDeclarationSemi(TokenKind::Var);
  44. state.has_error = true;
  45. if (auto semi_token = context.SkipPastLikelyEnd(state.token)) {
  46. end_token = *semi_token;
  47. }
  48. }
  49. context.AddNode(ParseNodeKind::VariableDeclaration, end_token,
  50. state.subtree_start, state.has_error);
  51. }
  52. auto ParserHandleVarFinishAsFor(ParserContext& context) -> void {
  53. auto state = context.PopState();
  54. auto end_token = state.token;
  55. if (context.PositionIs(TokenKind::In)) {
  56. end_token = context.Consume();
  57. } else if (context.PositionIs(TokenKind::Colon)) {
  58. CARBON_DIAGNOSTIC(ExpectedInNotColon, Error,
  59. "`:` should be replaced by `in`.");
  60. context.emitter().Emit(*context.position(), ExpectedInNotColon);
  61. state.has_error = true;
  62. end_token = context.Consume();
  63. } else {
  64. CARBON_DIAGNOSTIC(ExpectedIn, Error,
  65. "Expected `in` after loop `var` declaration.");
  66. context.emitter().Emit(*context.position(), ExpectedIn);
  67. state.has_error = true;
  68. }
  69. context.AddNode(ParseNodeKind::ForIn, end_token, state.subtree_start,
  70. state.has_error);
  71. }
  72. } // namespace Carbon