handle_alias.cpp 1.7 KB

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