handle_decl_name_and_params.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. switch (context.PositionKind()) {
  33. case Lex::TokenKind::Period:
  34. context.AddLeafNode(NodeKind::IdentifierNameNotBeforeParams, *identifier);
  35. context.AddNode(NodeKind::NameQualifierWithoutParams,
  36. context.ConsumeChecked(Lex::TokenKind::Period),
  37. state.has_error);
  38. context.PushState(State::DeclNameAndParams);
  39. break;
  40. case Lex::TokenKind::OpenSquareBracket:
  41. context.AddLeafNode(NodeKind::IdentifierNameBeforeParams, *identifier);
  42. state.state = State::DeclNameAndParamsAfterImplicit;
  43. context.PushState(state);
  44. context.PushState(State::PatternListAsImplicit);
  45. break;
  46. case Lex::TokenKind::OpenParen:
  47. context.AddLeafNode(NodeKind::IdentifierNameBeforeParams, *identifier);
  48. state.state = State::DeclNameAndParamsAfterParams;
  49. context.PushState(state);
  50. context.PushState(State::PatternListAsTuple);
  51. break;
  52. default:
  53. context.AddLeafNode(NodeKind::IdentifierNameNotBeforeParams, *identifier);
  54. break;
  55. }
  56. }
  57. auto HandleDeclNameAndParamsAfterImplicit(Context& context) -> void {
  58. auto state = context.PopState();
  59. if (!context.PositionIs(Lex::TokenKind::OpenParen)) {
  60. CARBON_DIAGNOSTIC(
  61. ParamsRequiredAfterImplicit, Error,
  62. "a `(` for parameters is required after implicit parameters");
  63. context.emitter().Emit(*context.position(), ParamsRequiredAfterImplicit);
  64. context.ReturnErrorOnState();
  65. return;
  66. }
  67. state.state = State::DeclNameAndParamsAfterParams;
  68. context.PushState(state);
  69. context.PushState(State::PatternListAsTuple);
  70. }
  71. auto HandleDeclNameAndParamsAfterParams(Context& context) -> void {
  72. auto state = context.PopState();
  73. if (auto period = context.ConsumeIf(Lex::TokenKind::Period)) {
  74. context.AddNode(NodeKind::NameQualifierWithParams, *period,
  75. state.has_error);
  76. context.PushState(State::DeclNameAndParams);
  77. }
  78. }
  79. } // namespace Carbon::Parse