handle_decl_name_and_params.cpp 4.4 KB

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