parse.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/parse.h"
  5. #include "common/check.h"
  6. #include "common/pretty_stack_trace_function.h"
  7. #include "toolchain/parse/context.h"
  8. #include "toolchain/parse/handle.h"
  9. #include "toolchain/parse/node_kind.h"
  10. #include "toolchain/parse/tree_and_subtrees.h"
  11. namespace Carbon::Parse {
  12. auto HandleInvalid(Context& context) -> void {
  13. CARBON_FATAL("The Invalid state shouldn't be on the stack: {0}",
  14. context.PopState());
  15. }
  16. auto Parse(Lex::TokenizedBuffer& tokens, ParseOptions options) -> Tree {
  17. auto* consumer =
  18. options.consumer ? options.consumer : &Diagnostics::ConsoleConsumer();
  19. // Delegate to the parser.
  20. Tree tree(tokens);
  21. Context context(&tree, &tokens, consumer, options.vlog_stream);
  22. PrettyStackTraceFunction context_dumper(
  23. [&](llvm::raw_ostream& output) { context.PrintForStackDump(output); });
  24. context.AddLeafNode(NodeKind::FileStart,
  25. context.ConsumeChecked(Lex::TokenKind::FileStart));
  26. context.PushState(StateKind::DeclScopeLoopAsRegular);
  27. while (!context.state_stack().empty()) {
  28. switch (context.state_stack().back().kind) {
  29. #define CARBON_PARSE_STATE(Name) \
  30. case StateKind::Name: \
  31. Handle##Name(context); \
  32. break;
  33. #include "toolchain/parse/state.def"
  34. }
  35. }
  36. context.AddLeafNode(NodeKind::FileEnd, *context.position());
  37. // Mark the tree as potentially having errors if there were errors coming in
  38. // from the tokenized buffer or we diagnosed new errors.
  39. tree.set_has_errors(tokens.has_errors() || context.has_errors());
  40. if (options.vlog_stream || options.dump_stream) {
  41. // Flush diagnostics before printing.
  42. consumer->Flush();
  43. }
  44. CARBON_VLOG_TO(options.vlog_stream, "*** Parse::Tree ***\n{0}", tree);
  45. if (options.dump_stream) {
  46. Parse::TreeAndSubtrees tree_and_subtrees(tokens, tree);
  47. if (options.dump_preorder_parse_tree) {
  48. tree_and_subtrees.PrintPreorder(*options.dump_stream);
  49. } else {
  50. tree_and_subtrees.Print(*options.dump_stream);
  51. }
  52. }
  53. if (auto verify = tree.Verify(); !verify.ok()) {
  54. // TODO: Consider printing a subtree as part of the error.
  55. CARBON_FATAL("Invalid tree returned by Parse(): {0}", verify.error());
  56. }
  57. return tree;
  58. }
  59. } // namespace Carbon::Parse