From 1c9f5e3001da83a7e48aef21ef72d155c8928035 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Thu, 25 Apr 2024 21:55:23 +0530 Subject: [PATCH] Display the AST even with syntax errors (#11147) ## Summary This PR updates the playground to display the AST even if it contains a syntax error. This could be useful for development and also to give a quick preview of what error recovery looks like. Note that not all recovery is correct but this allows us to iterate quickly on what can be improved. ## Test Plan Build the playground locally and test it. Screenshot 2024-04-25 at 21 02 22 --- crates/ruff_wasm/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/ruff_wasm/src/lib.rs b/crates/ruff_wasm/src/lib.rs index c7a0d269db7e7..e6ca842f5de51 100644 --- a/crates/ruff_wasm/src/lib.rs +++ b/crates/ruff_wasm/src/lib.rs @@ -18,7 +18,7 @@ use ruff_python_codegen::Stylist; use ruff_python_formatter::{format_module_ast, pretty_comments, PyFormatContext, QuoteStyle}; use ruff_python_index::{CommentRangesBuilder, Indexer}; use ruff_python_parser::lexer::LexResult; -use ruff_python_parser::{parse_tokens, tokenize_all, AsMode, Mode}; +use ruff_python_parser::{parse_tokens, tokenize_all, AsMode, Mode, Program}; use ruff_python_trivia::CommentRanges; use ruff_source_file::{Locator, SourceLocation}; use ruff_text_size::Ranged; @@ -250,9 +250,9 @@ impl Workspace { /// Parses the content and returns its AST pub fn parse(&self, contents: &str) -> Result { - let parsed = ruff_python_parser::parse(contents, Mode::Module).map_err(into_error)?; + let program = Program::parse_str(contents, Mode::Module); - Ok(format!("{parsed:#?}")) + Ok(format!("{:#?}", program.into_ast())) } pub fn tokens(&self, contents: &str) -> Result {