diff --git a/src/bin/dmc/cst.rs b/src/bin/dmc/cst.rs new file mode 100644 index 0000000..231fe2a --- /dev/null +++ b/src/bin/dmc/cst.rs @@ -0,0 +1,30 @@ +use deimos::parser::{DeimosParser, Rule}; +use std::path::PathBuf; +use pest::iterators::{Pair, Pairs}; +use pest::Parser; + +fn print_pair(pair: Pair) { + println!("{:?}", pair.as_rule()); + println!("{:?}", pair.as_span()); + println!("----"); + print_pairs(pair.into_inner()); +} + +fn print_pairs(pairs: Pairs) { + for pair in pairs { + print_pair(pair); + } +} + +pub fn show_cst(path: &PathBuf) { + let src = std::fs::read_to_string(path).unwrap(); + let parse_result = DeimosParser::parse(Rule::CompilationUnit, &src); + match parse_result { + Ok(pairs) => { + print_pairs(pairs); + } + Err(error) => { + eprintln!("{:?}", error); + } + } +} \ No newline at end of file diff --git a/src/bin/dmc/main.rs b/src/bin/dmc/main.rs index 535c491..d09feb3 100644 --- a/src/bin/dmc/main.rs +++ b/src/bin/dmc/main.rs @@ -1,3 +1,4 @@ +mod cst; mod ir; mod name_analysis; mod p3; @@ -5,11 +6,12 @@ mod p3; use std::path::PathBuf; +// use crate::unparse::unparse; +use crate::cst::show_cst; +use crate::ir::compile_to_ir; use crate::name_analysis::name_analysis; use crate::p3::pretty_print_parse; -// use crate::unparse::unparse; use clap::{Parser, Subcommand}; -use crate::ir::compile_to_ir; #[derive(Debug, Parser)] #[command(name = "dmc")] @@ -21,46 +23,36 @@ struct Cli { #[derive(Debug, Subcommand)] enum Commands { - #[command(arg_required_else_help = true)] - Unparse { - paths: Vec, - }, - P3 { - paths: Vec, - }, - NameAnalysis { - paths: Vec, - }, - Ir { - paths: Vec, - } + Cst { paths: Vec }, + P3 { paths: Vec }, + NameAnalysis { paths: Vec }, + Ir { paths: Vec }, } fn main() { let args = Cli::parse(); match args.command { - // Commands::Unparse { paths } => { - // for path in paths { - // unparse(&path); - // } - // } + Commands::Cst { paths } => { + for path in &paths { + show_cst(path); + } + } Commands::P3 { paths } => { for path in paths { - pretty_print_parse(&path) + pretty_print_parse(&path); } } Commands::NameAnalysis { paths } => { let result = name_analysis(&paths); if let Err(e) = result { - eprintln!("{}", e) + eprintln!("{}", e); } } Commands::Ir { paths } => { let result = compile_to_ir(&paths); if let Err(e) = result { - eprintln!("{}", e) + eprintln!("{}", e); } } - _ => todo!(), } }