parse.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 "common/check.h"
  5. #include "toolchain/base/pretty_stack_trace_function.h"
  6. #include "toolchain/parse/context.h"
  7. #include "toolchain/parse/node_kind.h"
  8. #include "toolchain/parse/typed_nodes.h"
  9. namespace Carbon::Parse {
  10. // Declare handlers for each parse state.
  11. #define CARBON_PARSE_STATE(Name) auto Handle##Name(Context& context) -> void;
  12. #include "toolchain/parse/state.def"
  13. auto HandleInvalid(Context& context) -> void {
  14. CARBON_FATAL() << "The Invalid state shouldn't be on the stack: "
  15. << context.PopState();
  16. }
  17. auto Parse(Lex::TokenizedBuffer& tokens, DiagnosticConsumer& consumer,
  18. llvm::raw_ostream* vlog_stream) -> Tree {
  19. Lex::TokenLocationTranslator translator(&tokens);
  20. Lex::TokenDiagnosticEmitter emitter(translator, consumer);
  21. // Delegate to the parser.
  22. Tree tree(tokens);
  23. Context context(tree, tokens, emitter, vlog_stream);
  24. PrettyStackTraceFunction context_dumper(
  25. [&](llvm::raw_ostream& output) { context.PrintForStackDump(output); });
  26. context.AddLeafNode(NodeKind::FileStart,
  27. context.ConsumeChecked(Lex::TokenKind::FileStart));
  28. context.PushState(State::DeclScopeLoop);
  29. while (!context.state_stack().empty()) {
  30. switch (context.state_stack().back().state) {
  31. #define CARBON_PARSE_STATE(Name) \
  32. case State::Name: \
  33. Handle##Name(context); \
  34. break;
  35. #include "toolchain/parse/state.def"
  36. }
  37. }
  38. context.AddLeafNode(NodeKind::FileEnd, *context.position());
  39. if (auto verify = tree.Verify(); !verify.ok()) {
  40. // TODO: This is temporarily printing to stderr directly during development.
  41. // If we can, restrict this to a subtree with the error and add it to the
  42. // stack trace (such as with PrettyStackTraceFunction). Otherwise, switch
  43. // back to vlog_stream prior to broader distribution so that end users are
  44. // hopefully comfortable copy-pasting stderr when there are bugs in tree
  45. // construction.
  46. tree.Print(llvm::errs());
  47. CARBON_FATAL() << "Invalid tree returned by Parse(): " << verify.error();
  48. }
  49. return tree;
  50. }
  51. } // namespace Carbon::Parse