handle_decl_name_and_params.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. namespace Carbon::Parse {
  6. // Handles DeclNameAndParamsAs(Optional|Required).
  7. static auto HandleDeclNameAndParams(Context& context, State after_name)
  8. -> void {
  9. auto state = context.PopState();
  10. // TODO: Should handle designated names.
  11. if (auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier)) {
  12. context.PushState(state, after_name);
  13. if (context.PositionIs(Lex::TokenKind::Period)) {
  14. context.AddLeafNode(NodeKind::IdentifierName, *identifier);
  15. context.PushState(state, State::PeriodAsDecl);
  16. } else {
  17. context.AddLeafNode(NodeKind::IdentifierName, *identifier);
  18. }
  19. } else {
  20. CARBON_DIAGNOSTIC(ExpectedDeclName, Error,
  21. "`{0}` introducer should be followed by a name.",
  22. Lex::TokenKind);
  23. Lex::TokenIndex location = *context.position();
  24. if (context.tokens().GetKind(location) == Lex::TokenKind::FileEnd) {
  25. // The end of file is often an especially unhelpful location. If that's
  26. // the best we can do here, back up the location to the introducer itself.
  27. location = state.token;
  28. }
  29. context.emitter().Emit(location, ExpectedDeclName,
  30. context.tokens().GetKind(state.token));
  31. context.ReturnErrorOnState();
  32. context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
  33. /*has_error=*/true);
  34. }
  35. }
  36. auto HandleDeclNameAndParamsAsNone(Context& context) -> void {
  37. HandleDeclNameAndParams(context, State::DeclNameAndParamsAfterNameAsNone);
  38. }
  39. auto HandleDeclNameAndParamsAsOptional(Context& context) -> void {
  40. HandleDeclNameAndParams(context, State::DeclNameAndParamsAfterNameAsOptional);
  41. }
  42. auto HandleDeclNameAndParamsAsRequired(Context& context) -> void {
  43. HandleDeclNameAndParams(context, State::DeclNameAndParamsAfterNameAsRequired);
  44. }
  45. enum class Params : int8_t {
  46. None,
  47. Optional,
  48. Required,
  49. };
  50. static auto HandleDeclNameAndParamsAfterName(Context& context, Params params)
  51. -> void {
  52. auto state = context.PopState();
  53. if (context.PositionIs(Lex::TokenKind::Period)) {
  54. // Continue designator processing.
  55. context.PushState(state);
  56. context.PushState(state, State::PeriodAsDecl);
  57. return;
  58. }
  59. // TODO: We can have a parameter list after a name qualifier, regardless of
  60. // whether the entity itself permits or requires parameters:
  61. //
  62. // fn Class(T:! type).AnotherClass(U:! type).Function(v: T) {}
  63. //
  64. // We should retain a `DeclNameAndParams...` state on the stack in all
  65. // cases below to check for a period after a parameter list, which indicates
  66. // that we've not finished parsing the declaration name.
  67. if (params == Params::None) {
  68. return;
  69. }
  70. if (context.PositionIs(Lex::TokenKind::OpenSquareBracket)) {
  71. context.PushState(State::DeclNameAndParamsAfterImplicit);
  72. context.PushState(State::PatternListAsImplicit);
  73. } else if (context.PositionIs(Lex::TokenKind::OpenParen)) {
  74. context.PushState(State::PatternListAsTuple);
  75. } else if (params == Params::Required) {
  76. CARBON_DIAGNOSTIC(ParamsRequiredByIntroducer, Error,
  77. "`{0}` requires a `(` for parameters.", Lex::TokenKind);
  78. context.emitter().Emit(*context.position(), ParamsRequiredByIntroducer,
  79. context.tokens().GetKind(state.token));
  80. context.ReturnErrorOnState();
  81. }
  82. }
  83. auto HandleDeclNameAndParamsAfterNameAsNone(Context& context) -> void {
  84. HandleDeclNameAndParamsAfterName(context, Params::None);
  85. }
  86. auto HandleDeclNameAndParamsAfterNameAsOptional(Context& context) -> void {
  87. HandleDeclNameAndParamsAfterName(context, Params::Optional);
  88. }
  89. auto HandleDeclNameAndParamsAfterNameAsRequired(Context& context) -> void {
  90. HandleDeclNameAndParamsAfterName(context, Params::Required);
  91. }
  92. auto HandleDeclNameAndParamsAfterImplicit(Context& context) -> void {
  93. context.PopAndDiscardState();
  94. if (context.PositionIs(Lex::TokenKind::OpenParen)) {
  95. context.PushState(State::PatternListAsTuple);
  96. } else {
  97. CARBON_DIAGNOSTIC(
  98. ParamsRequiredAfterImplicit, Error,
  99. "A `(` for parameters is required after implicit parameters.");
  100. context.emitter().Emit(*context.position(), ParamsRequiredAfterImplicit);
  101. context.ReturnErrorOnState();
  102. }
  103. }
  104. } // namespace Carbon::Parse