From bf3dcc18d0210d1dffbcb48aed616d5246551cd4 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 12 Aug 2019 09:32:04 -0700 Subject: [PATCH] Fixed trace parsing debug output --- schala-lang/language/src/parsing.rs | 10 ++++++---- schala-lang/language/src/schala.rs | 17 ++++++++++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/schala-lang/language/src/parsing.rs b/schala-lang/language/src/parsing.rs index 60bd508..4cecd6c 100644 --- a/schala-lang/language/src/parsing.rs +++ b/schala-lang/language/src/parsing.rs @@ -245,14 +245,16 @@ impl Parser { } */ - pub fn format_parse_trace(self) -> Vec { - self.parse_record.into_iter().map(|r| { + pub fn format_parse_trace(&self) -> String { + let mut buf = String::new(); + for r in self.parse_record.iter() { let mut indent = String::new(); for _ in 0..r.level { indent.push(' '); } - format!("{}Production `{}`, token: {}", indent, r.production_name, r.next_token) - }).collect() + buf.push_str(&format!("{}Production `{}`, token: {}\n", indent, r.production_name, r.next_token)) + } + buf } } diff --git a/schala-lang/language/src/schala.rs b/schala-lang/language/src/schala.rs index b479ece..167f0cb 100644 --- a/schala-lang/language/src/schala.rs +++ b/schala-lang/language/src/schala.rs @@ -95,16 +95,23 @@ fn tokenizing(input: &str, _handle: &mut Schala, comp: Option<&mut PassDebugArti fn parsing(input: Vec, handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result { use crate::parsing::Parser; + use ParsingDebugType::*; let mut parser = handle.active_parser.take().unwrap_or_else(|| Parser::new(input)); let ast = parser.parse(); - let trace = parser.format_parse_trace(); comp.map(|comp| { - let debug_info = match comp.parsing.as_ref().unwrap_or(&ParsingDebugType::CompactAST) { - ParsingDebugType::CompactAST => format!("{:?}", ast), - ParsingDebugType::ExpandedAST => format!("{:#?}", ast), - ParsingDebugType::Trace => format!("{}", trace[0]) //TODO fix this + let debug_format = comp.parsing.as_ref().unwrap_or(&CompactAST); + let debug_info = match debug_format { + CompactAST => match ast { + Ok(ref ast) => format!("{:?}", ast), + Err(_) => "Error - see output".to_string(), + }, + ExpandedAST => match ast { + Ok(ref ast) => format!("{:#?}", ast), + Err(_) => "Error - see output".to_string(), + }, + Trace => parser.format_parse_trace(), }; comp.add_artifact(debug_info); });