2019-01-17 15:15:40 -08:00
|
|
|
use clap::{App, AppSettings, Arg, SubCommand};
|
2019-05-30 16:52:30 -07:00
|
|
|
use error::Error;
|
2019-01-07 10:23:01 -08:00
|
|
|
use std::env;
|
2019-01-11 14:48:29 -08:00
|
|
|
use std::fs;
|
2019-01-07 17:57:27 -08:00
|
|
|
use std::path::Path;
|
2019-01-07 10:23:01 -08:00
|
|
|
use std::process::exit;
|
2019-03-14 12:39:04 -07:00
|
|
|
use std::{u64, usize};
|
2019-02-25 12:33:24 -08:00
|
|
|
use tree_sitter_cli::{
|
2019-05-13 21:51:17 -07:00
|
|
|
config, error, generate, highlight, loader, logger, parse, properties, test, wasm, web_ui,
|
2019-02-25 12:33:24 -08:00
|
|
|
};
|
2018-12-05 12:50:12 -08:00
|
|
|
|
2019-01-04 12:42:45 -08:00
|
|
|
fn main() {
|
|
|
|
|
if let Err(e) = run() {
|
2019-05-30 16:52:30 -07:00
|
|
|
eprintln!("{}", e.message());
|
2019-01-04 12:42:45 -08:00
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn run() -> error::Result<()> {
|
2018-12-05 12:50:12 -08:00
|
|
|
let matches = App::new("tree-sitter")
|
2019-01-17 17:15:10 -08:00
|
|
|
.version(concat!(
|
|
|
|
|
env!("CARGO_PKG_VERSION"),
|
|
|
|
|
" (",
|
|
|
|
|
env!("BUILD_SHA"),
|
|
|
|
|
")"
|
|
|
|
|
))
|
2019-01-17 15:15:40 -08:00
|
|
|
.setting(AppSettings::SubcommandRequiredElseHelp)
|
2018-12-05 12:50:12 -08:00
|
|
|
.author("Max Brunsfeld <maxbrunsfeld@gmail.com>")
|
|
|
|
|
.about("Generates and tests parsers")
|
2019-02-25 12:33:24 -08:00
|
|
|
.subcommand(SubCommand::with_name("init-config").about("Generate a default config file"))
|
2019-01-02 16:48:44 -08:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("generate")
|
|
|
|
|
.about("Generate a parser")
|
2019-01-14 14:07:42 -08:00
|
|
|
.arg(Arg::with_name("grammar-path").index(1))
|
2019-01-04 09:11:44 -08:00
|
|
|
.arg(Arg::with_name("log").long("log"))
|
2019-01-09 18:09:55 -08:00
|
|
|
.arg(Arg::with_name("properties-only").long("properties"))
|
2019-01-04 15:26:48 -08:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("state-ids-to-log")
|
|
|
|
|
.long("log-state")
|
|
|
|
|
.takes_value(true),
|
|
|
|
|
)
|
2019-01-04 09:11:44 -08:00
|
|
|
.arg(Arg::with_name("no-minimize").long("no-minimize")),
|
2019-01-02 16:48:44 -08:00
|
|
|
)
|
2018-12-05 12:50:12 -08:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("parse")
|
|
|
|
|
.about("Parse a file")
|
2019-01-17 17:15:10 -08:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("path")
|
|
|
|
|
.index(1)
|
|
|
|
|
.multiple(true)
|
|
|
|
|
.required(true),
|
|
|
|
|
)
|
2019-03-14 12:39:04 -07:00
|
|
|
.arg(Arg::with_name("scope").long("scope").takes_value(true))
|
2019-01-08 21:03:51 -08:00
|
|
|
.arg(Arg::with_name("debug").long("debug").short("d"))
|
2019-01-17 17:15:10 -08:00
|
|
|
.arg(Arg::with_name("debug-graph").long("debug-graph").short("D"))
|
|
|
|
|
.arg(Arg::with_name("quiet").long("quiet").short("q"))
|
2019-03-14 11:52:50 -07:00
|
|
|
.arg(Arg::with_name("time").long("time").short("t"))
|
2019-03-21 10:56:32 -07:00
|
|
|
.arg(Arg::with_name("allow-cancellation").long("cancel"))
|
2019-04-08 09:21:03 -07:00
|
|
|
.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),
|
|
|
|
|
),
|
2019-01-08 21:03:51 -08:00
|
|
|
)
|
|
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("test")
|
|
|
|
|
.about("Run a parser's tests")
|
|
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("filter")
|
|
|
|
|
.long("filter")
|
|
|
|
|
.short("f")
|
|
|
|
|
.takes_value(true),
|
|
|
|
|
)
|
|
|
|
|
.arg(Arg::with_name("debug").long("debug").short("d"))
|
|
|
|
|
.arg(Arg::with_name("debug-graph").long("debug-graph").short("D")),
|
2018-12-20 13:36:39 -08:00
|
|
|
)
|
2019-02-19 11:24:50 -08:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("highlight")
|
|
|
|
|
.about("Highlight a file")
|
|
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("path")
|
|
|
|
|
.index(1)
|
|
|
|
|
.multiple(true)
|
|
|
|
|
.required(true),
|
|
|
|
|
)
|
2019-02-20 14:38:19 -08:00
|
|
|
.arg(Arg::with_name("scope").long("scope").takes_value(true))
|
2019-03-13 15:51:27 -07:00
|
|
|
.arg(Arg::with_name("html").long("html").short("h"))
|
|
|
|
|
.arg(Arg::with_name("time").long("time").short("t")),
|
2019-02-19 11:24:50 -08:00
|
|
|
)
|
2019-04-23 14:29:46 -07:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("build-wasm")
|
|
|
|
|
.about("Compile a parser to WASM")
|
2019-05-14 11:12:56 -07:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("docker")
|
|
|
|
|
.long("docker")
|
|
|
|
|
.help("Run emscripten via docker even if it is installed locally"),
|
|
|
|
|
)
|
2019-04-23 14:29:46 -07:00
|
|
|
.arg(Arg::with_name("path").index(1).multiple(true)),
|
|
|
|
|
)
|
2019-05-14 11:12:56 -07:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("web-ui").about("Test a parser interactively in the browser"),
|
|
|
|
|
)
|
2018-12-20 13:36:39 -08:00
|
|
|
.get_matches();
|
2018-12-08 23:35:48 -08:00
|
|
|
|
2019-02-25 12:33:24 -08:00
|
|
|
let home_dir = dirs::home_dir().expect("Failed to read home directory");
|
2019-01-07 17:57:27 -08:00
|
|
|
let current_dir = env::current_dir().unwrap();
|
2019-02-25 12:33:24 -08:00
|
|
|
let config = config::Config::load(&home_dir);
|
|
|
|
|
let mut loader = loader::Loader::new(config.binary_directory.clone());
|
2019-01-17 15:15:40 -08:00
|
|
|
|
2019-02-25 12:33:24 -08:00
|
|
|
if matches.subcommand_matches("init-config").is_some() {
|
|
|
|
|
let config = config::Config::new(&home_dir);
|
|
|
|
|
config.save(&home_dir)?;
|
|
|
|
|
} else if let Some(matches) = matches.subcommand_matches("generate") {
|
2019-01-02 16:48:44 -08:00
|
|
|
if matches.is_present("log") {
|
|
|
|
|
logger::init();
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 14:07:42 -08:00
|
|
|
let grammar_path = matches.value_of("grammar-path");
|
2019-01-04 09:11:44 -08:00
|
|
|
let minimize = !matches.is_present("no-minimize");
|
2019-01-09 18:09:55 -08:00
|
|
|
let properties_only = matches.is_present("properties-only");
|
2019-03-03 18:53:24 -08:00
|
|
|
let parser_only = grammar_path.is_some();
|
2019-01-04 15:26:48 -08:00
|
|
|
let state_ids_to_log = matches
|
|
|
|
|
.values_of("state-ids-to-log")
|
|
|
|
|
.map_or(Vec::new(), |ids| {
|
|
|
|
|
ids.filter_map(|id| usize::from_str_radix(id, 10).ok())
|
|
|
|
|
.collect()
|
|
|
|
|
});
|
2019-03-03 18:53:24 -08:00
|
|
|
|
2019-02-01 19:57:00 -08:00
|
|
|
if !properties_only {
|
|
|
|
|
generate::generate_parser_in_directory(
|
|
|
|
|
¤t_dir,
|
|
|
|
|
grammar_path,
|
|
|
|
|
minimize,
|
|
|
|
|
state_ids_to_log,
|
|
|
|
|
)?;
|
|
|
|
|
}
|
2019-03-03 18:53:24 -08:00
|
|
|
|
|
|
|
|
if !parser_only {
|
|
|
|
|
properties::generate_property_sheets_in_directory(¤t_dir)?;
|
|
|
|
|
}
|
2019-01-08 21:03:51 -08:00
|
|
|
} else if let Some(matches) = matches.subcommand_matches("test") {
|
|
|
|
|
let debug = matches.is_present("debug");
|
|
|
|
|
let debug_graph = matches.is_present("debug-graph");
|
|
|
|
|
let filter = matches.value_of("filter");
|
2019-01-07 17:57:27 -08:00
|
|
|
let corpus_path = current_dir.join("corpus");
|
2019-01-11 13:30:45 -08:00
|
|
|
if let Some(language) = loader.language_at_path(¤t_dir)? {
|
2019-02-04 09:12:25 -08:00
|
|
|
test::run_tests_at_path(language, &corpus_path, debug, debug_graph, filter)?;
|
2019-01-07 17:57:27 -08:00
|
|
|
} else {
|
|
|
|
|
eprintln!("No language found");
|
|
|
|
|
}
|
2019-01-07 22:01:40 -08:00
|
|
|
} else if let Some(matches) = matches.subcommand_matches("parse") {
|
2019-01-08 21:03:51 -08:00
|
|
|
let debug = matches.is_present("debug");
|
|
|
|
|
let debug_graph = matches.is_present("debug-graph");
|
2019-01-17 17:15:10 -08:00
|
|
|
let quiet = matches.is_present("quiet");
|
|
|
|
|
let time = matches.is_present("time");
|
2019-04-08 09:21:03 -07:00
|
|
|
let edits = matches
|
|
|
|
|
.values_of("edits")
|
|
|
|
|
.map_or(Vec::new(), |e| e.collect());
|
2019-03-21 10:56:32 -07:00
|
|
|
let allow_cancellation = matches.is_present("allow-cancellation");
|
2019-03-14 11:52:50 -07:00
|
|
|
let timeout = matches
|
|
|
|
|
.value_of("timeout")
|
2019-03-14 12:39:04 -07:00
|
|
|
.map_or(0, |t| u64::from_str_radix(t, 10).unwrap());
|
2019-02-25 12:33:24 -08:00
|
|
|
loader.find_all_languages(&config.parser_directories)?;
|
2019-01-17 17:15:10 -08:00
|
|
|
let paths = matches
|
|
|
|
|
.values_of("path")
|
|
|
|
|
.unwrap()
|
|
|
|
|
.into_iter()
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
let max_path_length = paths.iter().map(|p| p.chars().count()).max().unwrap();
|
2019-02-06 16:17:35 -08:00
|
|
|
let mut has_error = false;
|
2019-01-17 17:15:10 -08:00
|
|
|
for path in paths {
|
|
|
|
|
let path = Path::new(path);
|
2019-03-14 12:39:04 -07:00
|
|
|
let language = if let Some(scope) = matches.value_of("scope") {
|
2019-05-30 16:52:30 -07:00
|
|
|
if let Some(config) =
|
|
|
|
|
loader
|
|
|
|
|
.language_configuration_for_scope(scope)
|
|
|
|
|
.map_err(Error::wrap(|| {
|
|
|
|
|
format!("Failed to load language for scope '{}'", scope)
|
|
|
|
|
}))?
|
|
|
|
|
{
|
2019-03-14 12:39:04 -07:00
|
|
|
config.0
|
2019-01-17 17:15:10 -08:00
|
|
|
} else {
|
2019-05-30 16:52:30 -07:00
|
|
|
return Error::err(format!("Unknown scope '{}'", scope));
|
2019-03-14 12:39:04 -07:00
|
|
|
}
|
2019-05-30 16:52:30 -07:00
|
|
|
} 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()
|
|
|
|
|
)
|
|
|
|
|
}))?
|
|
|
|
|
{
|
|
|
|
|
lang
|
|
|
|
|
} else if let Some(lang) =
|
|
|
|
|
loader
|
|
|
|
|
.language_at_path(¤t_dir)
|
|
|
|
|
.map_err(Error::wrap(|| {
|
|
|
|
|
"Failed to load language in current directory"
|
|
|
|
|
}))?
|
|
|
|
|
{
|
|
|
|
|
lang
|
2019-03-14 12:39:04 -07:00
|
|
|
} else {
|
|
|
|
|
eprintln!("No language found");
|
|
|
|
|
return Ok(());
|
|
|
|
|
};
|
2019-02-06 16:17:35 -08:00
|
|
|
has_error |= parse::parse_file_at_path(
|
2019-01-17 17:15:10 -08:00
|
|
|
language,
|
|
|
|
|
path,
|
2019-04-08 09:21:03 -07:00
|
|
|
&edits,
|
2019-01-17 17:15:10 -08:00
|
|
|
max_path_length,
|
|
|
|
|
quiet,
|
|
|
|
|
time,
|
2019-03-14 11:52:50 -07:00
|
|
|
timeout,
|
2019-01-17 17:15:10 -08:00
|
|
|
debug,
|
|
|
|
|
debug_graph,
|
2019-03-21 10:56:32 -07:00
|
|
|
allow_cancellation,
|
2019-01-17 17:15:10 -08:00
|
|
|
)?;
|
2019-01-07 17:57:36 -08:00
|
|
|
}
|
2019-02-06 16:17:35 -08:00
|
|
|
|
|
|
|
|
if has_error {
|
2019-05-30 16:52:30 -07:00
|
|
|
return Error::err(String::new());
|
2019-02-06 16:17:35 -08:00
|
|
|
}
|
2019-02-19 11:24:50 -08:00
|
|
|
} else if let Some(matches) = matches.subcommand_matches("highlight") {
|
|
|
|
|
let paths = matches.values_of("path").unwrap().into_iter();
|
|
|
|
|
let html_mode = matches.is_present("html");
|
2019-03-13 15:51:27 -07:00
|
|
|
let time = matches.is_present("time");
|
2019-02-25 12:33:24 -08:00
|
|
|
loader.find_all_languages(&config.parser_directories)?;
|
2019-02-19 11:24:50 -08:00
|
|
|
|
|
|
|
|
if html_mode {
|
|
|
|
|
println!("{}", highlight::HTML_HEADER);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-20 14:38:19 -08:00
|
|
|
let language_config;
|
|
|
|
|
if let Some(scope) = matches.value_of("scope") {
|
|
|
|
|
language_config = loader.language_configuration_for_scope(scope)?;
|
|
|
|
|
if language_config.is_none() {
|
2019-05-30 16:52:30 -07:00
|
|
|
return Error::err(format!("Unknown scope '{}'", scope));
|
2019-02-20 14:38:19 -08:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
language_config = None;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 11:24:50 -08:00
|
|
|
for path in paths {
|
|
|
|
|
let path = Path::new(path);
|
2019-02-25 12:33:24 -08:00
|
|
|
let (language, language_config) = match language_config {
|
2019-02-20 14:38:19 -08:00
|
|
|
Some(v) => v,
|
|
|
|
|
None => match loader.language_configuration_for_file_name(path)? {
|
|
|
|
|
Some(v) => v,
|
|
|
|
|
None => {
|
|
|
|
|
eprintln!("No language found for path {:?}", path);
|
|
|
|
|
continue;
|
2019-02-19 11:24:50 -08:00
|
|
|
}
|
2019-02-20 14:38:19 -08:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-25 12:33:24 -08:00
|
|
|
if let Some(sheet) = language_config.highlight_property_sheet(language)? {
|
2019-02-20 14:38:19 -08:00
|
|
|
let source = fs::read(path)?;
|
|
|
|
|
if html_mode {
|
2019-02-25 12:33:24 -08:00
|
|
|
highlight::html(&loader, &config.theme, &source, language, sheet)?;
|
2019-02-20 14:38:19 -08:00
|
|
|
} else {
|
2019-03-13 15:51:27 -07:00
|
|
|
highlight::ansi(&loader, &config.theme, &source, language, sheet, time)?;
|
2019-02-19 11:24:50 -08:00
|
|
|
}
|
2019-02-20 14:38:19 -08:00
|
|
|
} else {
|
2019-05-30 16:52:30 -07:00
|
|
|
return Error::err(format!("No syntax highlighting property sheet specified"));
|
2019-02-19 11:24:50 -08:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-23 14:29:46 -07:00
|
|
|
} else if let Some(matches) = matches.subcommand_matches("build-wasm") {
|
|
|
|
|
let grammar_path = current_dir.join(matches.value_of("path").unwrap_or(""));
|
2019-05-14 11:12:56 -07:00
|
|
|
wasm::compile_language_to_wasm(&grammar_path, matches.is_present("docker"))?;
|
|
|
|
|
} else if matches.subcommand_matches("web-ui").is_some() {
|
2019-05-13 21:51:17 -07:00
|
|
|
web_ui::serve(¤t_dir);
|
2018-12-08 23:35:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2018-12-05 12:50:12 -08:00
|
|
|
}
|