use clap::{App, AppSettings, Arg, SubCommand}; use error::Error; use glob::glob; use std::path::Path; use std::process::exit; use std::{env, fs, u64}; use tree_sitter::Language; use tree_sitter_cli::{ config, error, generate, highlight, loader, logger, parse, query, tags, test, test_highlight, wasm, web_ui, }; const BUILD_VERSION: &'static str = env!("CARGO_PKG_VERSION"); const BUILD_SHA: Option<&'static str> = option_env!("BUILD_SHA"); fn main() { if let Err(e) = run() { if !e.message().is_empty() { println!(""); eprintln!("{}", e.message()); } exit(1); } } fn run() -> error::Result<()> { let version = if let Some(build_sha) = BUILD_SHA { format!("{} ({})", BUILD_VERSION, build_sha) } else { BUILD_VERSION.to_string() }; let matches = App::new("tree-sitter") .version(version.as_str()) .setting(AppSettings::SubcommandRequiredElseHelp) .author("Max Brunsfeld ") .about("Generates and tests parsers") .subcommand(SubCommand::with_name("init-config").about("Generate a default config file")) .subcommand( SubCommand::with_name("generate") .about("Generate a parser") .arg(Arg::with_name("grammar-path").index(1)) .arg(Arg::with_name("log").long("log")) .arg(Arg::with_name("prev-abi").long("prev-abi")) .arg( Arg::with_name("report-states-for-rule") .long("report-states-for-rule") .value_name("rule-name") .takes_value(true), ) .arg(Arg::with_name("no-minimize").long("no-minimize")), ) .subcommand( SubCommand::with_name("parse") .about("Parse files") .arg( Arg::with_name("path") .index(1) .multiple(true) .required(true), ) .arg(Arg::with_name("scope").long("scope").takes_value(true)) .arg(Arg::with_name("debug").long("debug").short("d")) .arg(Arg::with_name("debug-graph").long("debug-graph").short("D")) .arg(Arg::with_name("quiet").long("quiet").short("q")) .arg(Arg::with_name("time").long("time").short("t")) .arg(Arg::with_name("allow-cancellation").long("cancel")) .arg(Arg::with_name("timeout").long("timeout").takes_value(true)) .arg( Arg::with_name("edits") .long("edit") .short("edit") .takes_value(true) .multiple(true) .number_of_values(1), ), ) .subcommand( SubCommand::with_name("query") .about("Search files using a syntax tree query") .arg(Arg::with_name("query-path").index(1).required(true)) .arg( Arg::with_name("path") .index(2) .multiple(true) .required(true), ) .arg( Arg::with_name("beg>:>(); loader.find_all_languages(&config.parser_directories)?; let language = select_language( &mut loader, paths[0], ¤t_dir, matches.value_of("scope"), )?; let query_path = Path::new(matches.value_of("query-path").unwrap()); let range = matches.value_of("beg>: = br.split(":").collect(); (r[0].parse().unwrap(), r[1].parse().unwrap()) }); query::query_files_at_paths(language, paths, query_path, ordered_captures, range)?; } else if let Some(matches) = matches.subcommand_matches("tags") { loader.find_all_languages(&config.parser_directories)?; let paths = collect_paths(matches.values_of("inputs").unwrap())?; tags::generate_tags( &loader, matches.value_of("scope"), &paths, matches.is_present("quiet"), matches.is_present("time"), )?; } else if let Some(matches) = matches.subcommand_matches("highlight") { loader.configure_highlights(&config.theme.highlight_names); loader.find_all_languages(&config.parser_directories)?; let time = matches.is_present("time"); let paths = collect_paths(matches.values_of("path").unwrap())?; let html_mode = matches.is_present("html"); if html_mode { println!("{}", highlight::HTML_HEADER); } let mut lang = None; if let Some(scope) = matches.value_of("scope") { lang = loader.language_configuration_for_scope(scope)?; if lang.is_none() { return Error::err(format!("Unknown scope '{}'", scope)); } } for path in paths { let path = Path::new(&path); let (language, language_config) = match lang { Some(v) => v, None => match loader.language_configuration_for_file_name(path)? { Some(v) => v, None => { eprintln!("No language found for path {:?}", path); continue; } }, }; if let Some(highlight_config) = language_config.highlight_config(language)? { let source = fs::read(path)?; if html_mode { highlight::html(&loader, &config.theme, &source, highlight_config, time)?; } else { highlight::ansi(&loader, &config.theme, &source, highlight_config, time)?; } } else { eprintln!("No syntax highlighting config found for path {:?}", path); } } if html_mode { println!("{}", highlight::HTML_FOOTER); } } else if let Some(matches) = matches.subcommand_matches("build-wasm") { let grammar_path = current_dir.join(matches.value_of("path").unwrap_or("")); wasm::compile_language_to_wasm(&grammar_path, matches.is_present("docker"))?; } else if let Some(matches) = matches.subcommand_matches("web-ui") { let open_in_browser = !matches.is_present("quiet"); web_ui::serve(¤t_dir, open_in_browser); } else if matches.subcommand_matches("dump-languages").is_some() { loader.find_all_languages(&config.parser_directories)?; for (configuration, language_path) in loader.get_all_language_configurations() { println!( concat!( "scope: {}\n", "parser: {:?}\n", "highlights: {:?}\n", "file_types: {:?}\n", "content_regex: {:?}\n", "injection_regex: {:?}\n", ), configuration.scope.as_ref().unwrap_or(&String::new()), language_path, configuration.highlights_filenames, configuration.file_types, configuration.content_regex, configuration.injection_regex, ); } } Ok(()) } fn collect_paths<'a>(paths: impl Iterator) -> error::Result> { let mut result = Vec::new(); let mut incorporate_path = |path: &str, positive| { if positive { result.push(path.to_string()); } else { if let Some(index) = result.iter().position(|p| p == path) { result.remove(index); } } }; for mut path in paths { let mut positive = true; if path.starts_with("!") { positive = false; path = path.trim_start_matches("!"); } if Path::new(path).exists() { incorporate_path(path, positive); } else { let paths = glob(path).map_err(Error::wrap(|| format!("Invalid glob pattern {:?}", path)))?; for path in paths { if let Some(path) = path?.to_str() { incorporate_path(path, positive); } } } } Ok(result) } fn select_language( loader: &mut loader::Loader, path: &Path, current_dir: &Path, scope: Option<&str>, ) -> Result { if let Some(scope) = scope { if let Some(config) = loader .language_configuration_for_scope(scope) .map_err(Error::wrap(|| { format!("Failed to load language for scope '{}'", scope) }))? { Ok(config.0) } else { return Error::err(format!("Unknown scope '{}'", scope)); } } else if let Some((lang, _)) = loader .language_configuration_for_file_name(path) .map_err(Error::wrap(|| { format!( "Failed to load language for file name {:?}", path.file_name().unwrap() ) }))? { Ok(lang) } else if let Some(lang) = loader .languages_at_path(¤t_dir) .map_err(Error::wrap(|| { "Failed to load language in current directory" }))? .first() .cloned() { Ok(lang) } else { eprintln!("No language found"); Error::err("No language found".to_string()) } }