handle_alias.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/lex/token_kind.h"
  5. #include "toolchain/lex/tokenized_buffer.h"
  6. #include "toolchain/parse/context.h"
  7. #include "toolchain/parse/node_kind.h"
  8. #include "toolchain/parse/state.h"
  9. namespace Carbon::Parse {
  10. auto HandleAlias(Context& context) -> void {
  11. auto state = context.PopState();
  12. context.PushState(state, State::AliasAfterName);
  13. context.PushState(State::DeclNameAndParamsAsNone, state.token);
  14. }
  15. auto HandleAliasAfterName(Context& context) -> void {
  16. auto state = context.PopState();
  17. if (state.has_error) {
  18. context.RecoverFromDeclError(state, NodeKind::Alias,
  19. /*skip_past_likely_end=*/true);
  20. return;
  21. }
  22. if (auto equal = context.ConsumeIf(Lex::TokenKind::Equal)) {
  23. context.AddLeafNode(NodeKind::AliasInitializer, *equal);
  24. context.PushState(state, State::AliasFinish);
  25. context.PushState(State::Expr);
  26. } else {
  27. CARBON_DIAGNOSTIC(ExpectedAliasInitializer, Error,
  28. "`alias` requires a `=` for the source.");
  29. context.emitter().Emit(*context.position(), ExpectedAliasInitializer);
  30. context.RecoverFromDeclError(state, NodeKind::Alias,
  31. /*skip_past_likely_end=*/true);
  32. }
  33. }
  34. auto HandleAliasFinish(Context& context) -> void {
  35. auto state = context.PopState();
  36. if (state.has_error) {
  37. context.RecoverFromDeclError(state, NodeKind::Alias,
  38. /*skip_past_likely_end=*/true);
  39. return;
  40. }
  41. if (auto semi = context.ConsumeIf(Lex::TokenKind::Semi)) {
  42. context.AddNode(NodeKind::Alias, *semi, state.subtree_start,
  43. state.has_error);
  44. } else {
  45. context.EmitExpectedDeclSemi(Lex::TokenKind::Alias);
  46. context.RecoverFromDeclError(state, NodeKind::Alias,
  47. /*skip_past_likely_end=*/true);
  48. }
  49. }
  50. } // namespace Carbon::Parse