handle_alias.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. context.AddNodeExpectingDeclSemi(state, NodeKind::Alias,
  37. Lex::TokenKind::Alias,
  38. /*is_def_allowed=*/false);
  39. }
  40. } // namespace Carbon::Parse