2018-12-20 13:36:39 -08:00
|
|
|
#[macro_use]
|
2019-01-02 16:48:44 -08:00
|
|
|
extern crate lazy_static;
|
2018-12-20 13:36:39 -08:00
|
|
|
#[macro_use]
|
2019-01-02 16:48:44 -08:00
|
|
|
extern crate log;
|
2018-12-20 13:36:39 -08:00
|
|
|
#[macro_use]
|
2019-01-02 16:48:44 -08:00
|
|
|
extern crate serde_derive;
|
|
|
|
|
extern crate hashbrown;
|
2019-01-07 17:57:27 -08:00
|
|
|
extern crate regex;
|
2019-01-09 18:09:55 -08:00
|
|
|
extern crate rsass;
|
2019-01-02 16:48:44 -08:00
|
|
|
extern crate serde_json;
|
2018-12-05 12:50:12 -08:00
|
|
|
|
|
|
|
|
mod error;
|
|
|
|
|
mod generate;
|
2019-01-07 17:57:27 -08:00
|
|
|
mod loader;
|
2019-01-02 16:48:44 -08:00
|
|
|
mod logger;
|
2019-01-07 17:57:27 -08:00
|
|
|
mod parse;
|
|
|
|
|
mod test;
|
2019-01-08 21:03:51 -08:00
|
|
|
mod util;
|
2019-01-07 10:23:01 -08:00
|
|
|
|
2019-01-07 17:57:27 -08:00
|
|
|
use self::loader::Loader;
|
2019-01-07 10:23:01 -08:00
|
|
|
use clap::{App, Arg, SubCommand};
|
|
|
|
|
use std::env;
|
2019-01-07 17:57:27 -08:00
|
|
|
use std::path::Path;
|
2019-01-07 10:23:01 -08:00
|
|
|
use std::process::exit;
|
|
|
|
|
use std::usize;
|
2018-12-05 12:50:12 -08:00
|
|
|
|
2019-01-04 12:42:45 -08:00
|
|
|
fn main() {
|
|
|
|
|
if let Err(e) = run() {
|
|
|
|
|
eprintln!("{}", e.0);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn run() -> error::Result<()> {
|
2018-12-05 12:50:12 -08:00
|
|
|
let matches = App::new("tree-sitter")
|
|
|
|
|
.version("0.1")
|
|
|
|
|
.author("Max Brunsfeld <maxbrunsfeld@gmail.com>")
|
|
|
|
|
.about("Generates and tests parsers")
|
2019-01-02 16:48:44 -08:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("generate")
|
|
|
|
|
.about("Generate a parser")
|
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-08 21:03:51 -08:00
|
|
|
.arg(Arg::with_name("path").index(1).required(true))
|
|
|
|
|
.arg(Arg::with_name("debug").long("debug").short("d"))
|
|
|
|
|
.arg(Arg::with_name("debug-graph").long("debug-graph").short("D")),
|
|
|
|
|
)
|
|
|
|
|
.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
|
|
|
)
|
|
|
|
|
.get_matches();
|
2018-12-08 23:35:48 -08:00
|
|
|
|
2019-01-07 17:57:27 -08:00
|
|
|
let home_dir = dirs::home_dir().unwrap();
|
|
|
|
|
let current_dir = env::current_dir().unwrap();
|
|
|
|
|
let mut loader = Loader::new(home_dir.join(".tree-sitter"));
|
|
|
|
|
|
2019-01-02 16:48:44 -08:00
|
|
|
if let Some(matches) = matches.subcommand_matches("generate") {
|
|
|
|
|
if matches.is_present("log") {
|
|
|
|
|
logger::init();
|
|
|
|
|
}
|
|
|
|
|
|
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-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-01-09 18:09:55 -08:00
|
|
|
generate::generate_parser_for_grammar(¤t_dir, minimize, state_ids_to_log, properties_only)?;
|
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");
|
|
|
|
|
if let Some((language, _)) = loader.language_configuration_at_path(¤t_dir)? {
|
2019-01-08 21:03:51 -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-07 17:57:36 -08:00
|
|
|
loader.find_parsers(&vec![home_dir.join("github")])?;
|
|
|
|
|
let source_path = Path::new(matches.value_of("path").unwrap());
|
|
|
|
|
if let Some((language, _)) = loader.language_for_file_name(source_path)? {
|
2019-01-08 21:03:51 -08:00
|
|
|
parse::parse_file_at_path(language, source_path, debug, debug_graph)?;
|
2019-01-07 17:57:36 -08:00
|
|
|
} else {
|
|
|
|
|
eprintln!("No language found");
|
|
|
|
|
}
|
2018-12-08 23:35:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2018-12-05 12:50:12 -08:00
|
|
|
}
|