parser_handle_var.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. context.emitter().Emit(*context.position(), ExpectedSemiAfterExpression);
  43. state.has_error = true;
  44. if (auto semi_token = context.SkipPastLikelyEnd(state.token)) {
  45. end_token = *semi_token;
  46. }
  47. }
  48. context.AddNode(ParseNodeKind::VariableDeclaration, end_token,
  49. state.subtree_start, state.has_error);
  50. }
  51. auto ParserHandleVarFinishAsFor(ParserContext& context) -> void {
  52. auto state = context.PopState();
  53. auto end_token = state.token;
  54. if (context.PositionIs(TokenKind::In)) {
  55. end_token = context.Consume();
  56. } else if (context.PositionIs(TokenKind::Colon)) {
  57. CARBON_DIAGNOSTIC(ExpectedInNotColon, Error,
  58. "`:` should be replaced by `in`.");
  59. context.emitter().Emit(*context.position(), ExpectedInNotColon);
  60. state.has_error = true;
  61. end_token = context.Consume();
  62. } else {
  63. CARBON_DIAGNOSTIC(ExpectedIn, Error,
  64. "Expected `in` after loop `var` declaration.");
  65. context.emitter().Emit(*context.position(), ExpectedIn);
  66. state.has_error = true;
  67. }
  68. context.AddNode(ParseNodeKind::ForIn, end_token, state.subtree_start,
  69. state.has_error);
  70. }
  71. } // namespace Carbon