handle_decl_name_and_params.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/parse/context.h"
  5. #include "toolchain/parse/handle.h"
  6. namespace Carbon::Parse {
  7. auto HandleDeclNameAndParams(Context& context) -> void {
  8. auto state = context.PopState();
  9. auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier);
  10. if (!identifier) {
  11. Lex::TokenIndex token = *context.position();
  12. if (context.tokens().GetKind(token) == Lex::TokenKind::FileEnd) {
  13. // The end of file is an unhelpful diagnostic location. Instead, use the
  14. // introducer token.
  15. token = state.token;
  16. }
  17. if (state.token == *context.position()) {
  18. CARBON_DIAGNOSTIC(ExpectedDeclNameAfterPeriod, Error,
  19. "`.` should be followed by a name");
  20. context.emitter().Emit(token, ExpectedDeclNameAfterPeriod);
  21. } else {
  22. CARBON_DIAGNOSTIC(ExpectedDeclName, Error,
  23. "`{0}` introducer should be followed by a name",
  24. Lex::TokenKind);
  25. context.emitter().Emit(token, ExpectedDeclName,
  26. context.tokens().GetKind(state.token));
  27. }
  28. context.ReturnErrorOnState();
  29. context.AddInvalidParse(*context.position());
  30. return;
  31. }
  32. context.AddLeafNode(NodeKind::IdentifierName, *identifier);
  33. switch (context.PositionKind()) {
  34. case Lex::TokenKind::Period:
  35. context.AddNode(NodeKind::NameQualifier,
  36. context.ConsumeChecked(Lex::TokenKind::Period),
  37. state.has_error);
  38. context.PushState(State::DeclNameAndParams);
  39. break;
  40. case Lex::TokenKind::OpenSquareBracket:
  41. state.state = State::DeclNameAndParamsAfterImplicit;
  42. context.PushState(state);
  43. context.PushState(State::PatternListAsImplicit);
  44. break;
  45. case Lex::TokenKind::OpenParen:
  46. state.state = State::DeclNameAndParamsAfterParams;
  47. context.PushState(state);
  48. context.PushState(State::PatternListAsTuple);
  49. break;
  50. default:
  51. break;
  52. }
  53. }
  54. auto HandleDeclNameAndParamsAfterImplicit(Context& context) -> void {
  55. auto state = context.PopState();
  56. if (!context.PositionIs(Lex::TokenKind::OpenParen)) {
  57. CARBON_DIAGNOSTIC(
  58. ParamsRequiredAfterImplicit, Error,
  59. "a `(` for parameters is required after implicit parameters");
  60. context.emitter().Emit(*context.position(), ParamsRequiredAfterImplicit);
  61. context.ReturnErrorOnState();
  62. return;
  63. }
  64. state.state = State::DeclNameAndParamsAfterParams;
  65. context.PushState(state);
  66. context.PushState(State::PatternListAsTuple);
  67. }
  68. auto HandleDeclNameAndParamsAfterParams(Context& context) -> void {
  69. auto state = context.PopState();
  70. if (auto period = context.ConsumeIf(Lex::TokenKind::Period)) {
  71. context.AddNode(NodeKind::NameQualifier, *period, state.has_error);
  72. context.PushState(State::DeclNameAndParams);
  73. }
  74. }
  75. } // namespace Carbon::Parse