2023-07-30 20:43:52 +03:00
|
|
|
use anyhow::{anyhow, Context, Error, Result};
|
2019-01-17 15:15:40 -08:00
|
|
|
use clap::{App, AppSettings, Arg, SubCommand};
|
2019-12-06 11:41:21 -08:00
|
|
|
use glob::glob;
|
2024-02-03 07:28:53 -06:00
|
|
|
use regex::Regex;
|
2023-07-20 04:57:11 -04:00
|
|
|
use std::collections::HashSet;
|
2023-01-06 06:37:22 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-08-07 17:41:45 -07:00
|
|
|
use std::{env, fs, u64};
|
2023-10-31 16:56:11 -07:00
|
|
|
use tree_sitter::{ffi, Parser, Point};
|
2024-02-03 07:28:53 -06:00
|
|
|
use tree_sitter_cli::test::TestOptions;
|
2019-02-25 12:33:24 -08:00
|
|
|
use tree_sitter_cli::{
|
2023-10-27 11:57:04 +01:00
|
|
|
generate, highlight, logger,
|
|
|
|
|
parse::{self, ParseFileOptions, ParseOutput},
|
|
|
|
|
playground, query, tags, test, test_highlight, test_tags, util, wasm,
|
2019-02-25 12:33:24 -08:00
|
|
|
};
|
2021-06-09 15:03:27 -04:00
|
|
|
use tree_sitter_config::Config;
|
2023-01-23 11:45:10 -08:00
|
|
|
use tree_sitter_highlight::Highlighter;
|
2021-06-09 12:51:28 -04:00
|
|
|
use tree_sitter_loader as loader;
|
2023-01-23 11:45:10 -08:00
|
|
|
use tree_sitter_tags::TagsContext;
|
2018-12-05 12:50:12 -08:00
|
|
|
|
2024-02-04 01:30:33 -05:00
|
|
|
const BUILD_VERSION: &str = env!("CARGO_PKG_VERSION");
|
2019-06-06 13:06:28 -07:00
|
|
|
const BUILD_SHA: Option<&'static str> = option_env!("BUILD_SHA");
|
2022-09-02 12:31:29 -07:00
|
|
|
const DEFAULT_GENERATE_ABI_VERSION: usize = 14;
|
2019-06-06 13:06:28 -07:00
|
|
|
|
2021-06-11 08:22:08 +03:00
|
|
|
fn main() {
|
2021-06-09 15:13:14 -04:00
|
|
|
let result = run();
|
|
|
|
|
if let Err(err) = &result {
|
2021-06-11 08:22:08 +03:00
|
|
|
// Ignore BrokenPipe errors
|
2021-06-09 15:13:14 -04:00
|
|
|
if let Some(error) = err.downcast_ref::<std::io::Error>() {
|
|
|
|
|
if error.kind() == std::io::ErrorKind::BrokenPipe {
|
2021-06-11 08:22:08 +03:00
|
|
|
return;
|
2021-06-09 15:13:14 -04:00
|
|
|
}
|
|
|
|
|
}
|
2021-06-11 08:22:08 +03:00
|
|
|
if !err.to_string().is_empty() {
|
2024-02-04 01:30:33 -05:00
|
|
|
eprintln!("{err:?}");
|
2021-06-11 08:22:08 +03:00
|
|
|
}
|
|
|
|
|
std::process::exit(1);
|
2021-06-09 15:13:14 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn run() -> Result<()> {
|
2024-02-04 01:30:33 -05:00
|
|
|
let version = BUILD_SHA.map_or_else(
|
|
|
|
|
|| BUILD_VERSION.to_string(),
|
|
|
|
|
|build_sha| format!("{BUILD_VERSION} ({build_sha})"),
|
|
|
|
|
);
|
2019-06-06 13:06:28 -07:00
|
|
|
|
2021-07-03 05:26:13 +03:00
|
|
|
let debug_arg = Arg::with_name("debug")
|
|
|
|
|
.help("Show parsing debug log")
|
|
|
|
|
.long("debug")
|
|
|
|
|
.short("d");
|
|
|
|
|
|
|
|
|
|
let debug_graph_arg = Arg::with_name("debug-graph")
|
|
|
|
|
.help("Produce the log.html file with debug graphs")
|
|
|
|
|
.long("debug-graph")
|
|
|
|
|
.short("D");
|
|
|
|
|
|
2021-09-08 00:08:13 +03:00
|
|
|
let debug_build_arg = Arg::with_name("debug-build")
|
|
|
|
|
.help("Compile a parser in debug mode")
|
|
|
|
|
.long("debug-build")
|
|
|
|
|
.short("0");
|
|
|
|
|
|
2021-07-03 05:26:13 +03:00
|
|
|
let paths_file_arg = Arg::with_name("paths-file")
|
|
|
|
|
.help("The path to a file with paths to source file(s)")
|
|
|
|
|
.long("paths")
|
|
|
|
|
.takes_value(true);
|
|
|
|
|
|
|
|
|
|
let paths_arg = Arg::with_name("paths")
|
|
|
|
|
.help("The source file(s) to use")
|
|
|
|
|
.multiple(true);
|
|
|
|
|
|
|
|
|
|
let scope_arg = Arg::with_name("scope")
|
|
|
|
|
.help("Select a language by the scope instead of a file extension")
|
|
|
|
|
.long("scope")
|
|
|
|
|
.takes_value(true);
|
|
|
|
|
|
|
|
|
|
let time_arg = Arg::with_name("time")
|
|
|
|
|
.help("Measure execution time")
|
|
|
|
|
.long("time")
|
|
|
|
|
.short("t");
|
|
|
|
|
|
|
|
|
|
let quiet_arg = Arg::with_name("quiet")
|
|
|
|
|
.help("Suppress main output")
|
|
|
|
|
.long("quiet")
|
|
|
|
|
.short("q");
|
|
|
|
|
|
2022-10-24 16:59:17 -07:00
|
|
|
let wasm_arg = Arg::with_name("wasm")
|
|
|
|
|
.long("wasm")
|
|
|
|
|
.help("compile parsers to wasm instead of native dynamic libraries");
|
2023-07-20 03:42:52 -04:00
|
|
|
let apply_all_captures_arg = Arg::with_name("apply-all-captures")
|
|
|
|
|
.help("Apply all captures to highlights")
|
|
|
|
|
.long("apply-all-captures");
|
2022-10-24 16:59:17 -07:00
|
|
|
|
2018-12-05 12:50:12 -08:00
|
|
|
let matches = App::new("tree-sitter")
|
|
|
|
|
.author("Max Brunsfeld <maxbrunsfeld@gmail.com>")
|
|
|
|
|
.about("Generates and tests parsers")
|
2021-06-27 03:06:15 +03:00
|
|
|
.version(version.as_str())
|
|
|
|
|
.setting(AppSettings::SubcommandRequiredElseHelp)
|
2021-06-23 10:56:23 +03:00
|
|
|
.global_setting(AppSettings::ColoredHelp)
|
2021-06-27 03:06:15 +03:00
|
|
|
.global_setting(AppSettings::DeriveDisplayOrder)
|
2021-06-23 11:16:41 +03:00
|
|
|
.global_setting(AppSettings::DisableHelpSubcommand)
|
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")
|
2021-06-23 10:57:26 +03:00
|
|
|
.alias("gen")
|
|
|
|
|
.alias("g")
|
2019-01-02 16:48:44 -08:00
|
|
|
.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"))
|
2022-01-17 14:45:07 -08:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("abi-version")
|
|
|
|
|
.long("abi")
|
|
|
|
|
.value_name("version")
|
|
|
|
|
.help(&format!(
|
|
|
|
|
concat!(
|
|
|
|
|
"Select the language ABI version to generate (default {}).\n",
|
|
|
|
|
"Use --abi=latest to generate the newest supported version ({}).",
|
|
|
|
|
),
|
|
|
|
|
DEFAULT_GENERATE_ABI_VERSION,
|
|
|
|
|
tree_sitter::LANGUAGE_VERSION,
|
|
|
|
|
)),
|
|
|
|
|
)
|
2021-03-08 12:01:45 -08:00
|
|
|
.arg(Arg::with_name("no-bindings").long("no-bindings"))
|
2023-01-06 06:13:08 +02:00
|
|
|
.arg(
|
2023-01-06 06:37:22 +02:00
|
|
|
Arg::with_name("build")
|
|
|
|
|
.long("build")
|
|
|
|
|
.short("b")
|
|
|
|
|
.help("Compile all defined languages in the current dir"),
|
2023-01-06 06:13:08 +02:00
|
|
|
)
|
|
|
|
|
.arg(&debug_build_arg)
|
2023-01-06 06:37:22 +02:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("libdir")
|
|
|
|
|
.long("libdir")
|
|
|
|
|
.takes_value(true)
|
|
|
|
|
.value_name("path"),
|
|
|
|
|
)
|
2019-05-20 13:25:01 -07:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("report-states-for-rule")
|
|
|
|
|
.long("report-states-for-rule")
|
|
|
|
|
.value_name("rule-name")
|
|
|
|
|
.takes_value(true),
|
|
|
|
|
)
|
2023-07-18 13:24:52 +02:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("js-runtime")
|
|
|
|
|
.long("js-runtime")
|
|
|
|
|
.takes_value(true)
|
|
|
|
|
.value_name("executable")
|
|
|
|
|
.env("TREE_SITTER_JS_RUNTIME")
|
|
|
|
|
.help("Use a JavaScript runtime other than node"),
|
2023-01-06 06:05:07 +02:00
|
|
|
),
|
2019-01-02 16:48:44 -08:00
|
|
|
)
|
2018-12-05 12:50:12 -08:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("parse")
|
2021-06-23 10:57:26 +03:00
|
|
|
.alias("p")
|
2019-09-11 14:44:49 -07:00
|
|
|
.about("Parse files")
|
2021-07-03 05:26:13 +03:00
|
|
|
.arg(&paths_file_arg)
|
|
|
|
|
.arg(&paths_arg)
|
|
|
|
|
.arg(&scope_arg)
|
|
|
|
|
.arg(&debug_arg)
|
2021-09-08 00:08:13 +03:00
|
|
|
.arg(&debug_build_arg)
|
2021-07-03 05:26:13 +03:00
|
|
|
.arg(&debug_graph_arg)
|
2022-10-24 16:59:17 -07:00
|
|
|
.arg(&wasm_arg)
|
2023-02-13 12:33:34 -08:00
|
|
|
.arg(Arg::with_name("output-dot").long("dot"))
|
|
|
|
|
.arg(Arg::with_name("output-xml").long("xml").short("x"))
|
2019-01-17 17:15:10 -08:00
|
|
|
.arg(
|
2021-07-03 05:26:13 +03:00
|
|
|
Arg::with_name("stat")
|
|
|
|
|
.help("Show parsing statistic")
|
|
|
|
|
.long("stat")
|
|
|
|
|
.short("s"),
|
2019-01-17 17:15:10 -08:00
|
|
|
)
|
2021-07-03 05:26:13 +03:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("timeout")
|
|
|
|
|
.help("Interrupt the parsing process by timeout (µs)")
|
|
|
|
|
.long("timeout")
|
|
|
|
|
.takes_value(true),
|
|
|
|
|
)
|
|
|
|
|
.arg(&time_arg)
|
|
|
|
|
.arg(&quiet_arg)
|
2019-04-08 09:21:03 -07:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("edits")
|
2021-07-03 05:26:13 +03:00
|
|
|
.help("Apply edits in the format: \"row,col del_count insert_text\"")
|
2019-04-08 09:21:03 -07:00
|
|
|
.long("edit")
|
|
|
|
|
.short("edit")
|
|
|
|
|
.takes_value(true)
|
|
|
|
|
.multiple(true)
|
|
|
|
|
.number_of_values(1),
|
2023-07-03 20:59:01 -04:00
|
|
|
)
|
|
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("encoding")
|
|
|
|
|
.help("The encoding of the input files")
|
|
|
|
|
.long("encoding")
|
|
|
|
|
.takes_value(true),
|
2019-04-08 09:21:03 -07:00
|
|
|
),
|
2019-01-08 21:03:51 -08:00
|
|
|
)
|
2019-09-11 14:44:49 -07:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("query")
|
2021-06-23 10:57:26 +03:00
|
|
|
.alias("q")
|
2019-09-11 14:44:49 -07:00
|
|
|
.about("Search files using a syntax tree query")
|
|
|
|
|
.arg(
|
2021-07-03 05:26:13 +03:00
|
|
|
Arg::with_name("query-path")
|
|
|
|
|
.help("Path to a file with queries")
|
|
|
|
|
.index(1)
|
|
|
|
|
.required(true),
|
2019-09-11 14:44:49 -07:00
|
|
|
)
|
2023-02-14 14:41:25 -08:00
|
|
|
.arg(&time_arg)
|
|
|
|
|
.arg(&quiet_arg)
|
2021-07-03 05:26:13 +03:00
|
|
|
.arg(&paths_file_arg)
|
|
|
|
|
.arg(&paths_arg.clone().index(2))
|
2020-07-12 20:45:17 +07:00
|
|
|
.arg(
|
2020-07-14 15:04:39 -07:00
|
|
|
Arg::with_name("byte-range")
|
2020-07-12 20:45:17 +07:00
|
|
|
.help("The range of byte offsets in which the query will be executed")
|
|
|
|
|
.long("byte-range")
|
2020-07-14 15:04:39 -07:00
|
|
|
.takes_value(true),
|
2020-07-12 20:45:17 +07:00
|
|
|
)
|
2023-02-14 14:41:25 -08:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("row-range")
|
|
|
|
|
.help("The range of rows in which the query will be executed")
|
|
|
|
|
.long("row-range")
|
|
|
|
|
.takes_value(true),
|
|
|
|
|
)
|
2021-07-03 05:26:13 +03:00
|
|
|
.arg(&scope_arg)
|
2020-10-21 12:37:24 -04:00
|
|
|
.arg(Arg::with_name("captures").long("captures").short("c"))
|
|
|
|
|
.arg(Arg::with_name("test").long("test")),
|
2019-09-11 14:44:49 -07:00
|
|
|
)
|
2020-03-04 14:27:31 -08:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("tags")
|
2021-07-03 05:26:13 +03:00
|
|
|
.about("Generate a list of tags")
|
|
|
|
|
.arg(&scope_arg)
|
|
|
|
|
.arg(&time_arg)
|
|
|
|
|
.arg(&quiet_arg)
|
|
|
|
|
.arg(&paths_file_arg)
|
|
|
|
|
.arg(&paths_arg),
|
2020-03-04 14:27:31 -08:00
|
|
|
)
|
2019-01-08 21:03:51 -08:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("test")
|
2021-06-23 10:57:26 +03:00
|
|
|
.alias("t")
|
2019-01-08 21:03:51 -08:00
|
|
|
.about("Run a parser's tests")
|
|
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("filter")
|
|
|
|
|
.long("filter")
|
|
|
|
|
.short("f")
|
2021-03-13 19:53:10 +01:00
|
|
|
.takes_value(true)
|
2024-02-03 07:28:53 -06:00
|
|
|
.help("[DEPRECATED in favor of include]\nOnly run corpus test cases whose name includes the given string"),
|
|
|
|
|
)
|
|
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("include")
|
|
|
|
|
.long("include")
|
|
|
|
|
.short("i")
|
|
|
|
|
.takes_value(true)
|
|
|
|
|
.help("Only run corpus test cases whose name matches the given regex"),
|
|
|
|
|
)
|
|
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("exclude")
|
|
|
|
|
.long("exclude")
|
|
|
|
|
.short("e")
|
|
|
|
|
.takes_value(true)
|
|
|
|
|
.help(
|
|
|
|
|
"Only run corpus test cases whose name does not match the given regex",
|
|
|
|
|
),
|
2019-01-08 21:03:51 -08:00
|
|
|
)
|
2021-05-16 17:55:58 +03:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("update")
|
|
|
|
|
.long("update")
|
|
|
|
|
.short("u")
|
|
|
|
|
.help("Update all syntax trees in corpus files with current parser output"),
|
|
|
|
|
)
|
2021-07-03 05:26:13 +03:00
|
|
|
.arg(&debug_arg)
|
2021-09-08 00:08:13 +03:00
|
|
|
.arg(&debug_build_arg)
|
2022-10-24 16:59:17 -07:00
|
|
|
.arg(&debug_graph_arg)
|
2023-10-27 11:57:04 +01:00
|
|
|
.arg(&wasm_arg)
|
2023-07-20 03:42:52 -04:00
|
|
|
.arg(&apply_all_captures_arg),
|
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(
|
2021-07-03 05:26:13 +03:00
|
|
|
Arg::with_name("html")
|
|
|
|
|
.help("Generate highlighting as an HTML document")
|
|
|
|
|
.long("html")
|
|
|
|
|
.short("H"),
|
2019-02-19 11:24:50 -08:00
|
|
|
)
|
2023-06-22 09:18:53 -04:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("check")
|
|
|
|
|
.help("Check that highlighting captures conform strictly to standards")
|
|
|
|
|
.long("check"),
|
|
|
|
|
)
|
2023-07-20 04:57:11 -04:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("captures-path")
|
|
|
|
|
.help("Path to a file with captures")
|
|
|
|
|
.long("captures-path")
|
|
|
|
|
.takes_value(true),
|
|
|
|
|
)
|
2023-07-20 08:04:59 -04:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("query-paths")
|
|
|
|
|
.help("Paths to files with queries")
|
|
|
|
|
.long("query-paths")
|
|
|
|
|
.takes_value(true)
|
|
|
|
|
.multiple(true)
|
|
|
|
|
.number_of_values(1),
|
|
|
|
|
)
|
2021-07-03 05:26:13 +03:00
|
|
|
.arg(&scope_arg)
|
|
|
|
|
.arg(&time_arg)
|
|
|
|
|
.arg(&quiet_arg)
|
|
|
|
|
.arg(&paths_file_arg)
|
2023-07-20 03:42:52 -04:00
|
|
|
.arg(&paths_arg)
|
|
|
|
|
.arg(&apply_all_captures_arg),
|
2019-02-19 11:24:50 -08:00
|
|
|
)
|
2019-04-23 14:29:46 -07:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("build-wasm")
|
2021-06-23 10:57:26 +03:00
|
|
|
.alias("bw")
|
2019-04-23 14:29:46 -07:00
|
|
|
.about("Compile a parser to WASM")
|
2019-05-14 11:12:56 -07:00
|
|
|
.arg(
|
2023-12-08 19:42:53 -05:00
|
|
|
Arg::with_name("docker").long("docker").help(
|
|
|
|
|
"Run emscripten via docker or podman even if it is installed locally",
|
|
|
|
|
),
|
2019-05-14 11:12:56 -07:00
|
|
|
)
|
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(
|
2021-06-23 10:58:01 +03:00
|
|
|
SubCommand::with_name("playground")
|
|
|
|
|
.alias("play")
|
|
|
|
|
.alias("pg")
|
|
|
|
|
.alias("web-ui")
|
|
|
|
|
.about("Start local playground for a parser in the browser")
|
2020-07-09 11:28:07 -07:00
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("quiet")
|
|
|
|
|
.long("quiet")
|
|
|
|
|
.short("q")
|
2021-07-29 22:39:37 +02:00
|
|
|
.help("Don't open in default browser"),
|
2020-07-09 11:28:07 -07:00
|
|
|
),
|
2019-05-14 11:12:56 -07:00
|
|
|
)
|
2019-08-07 17:41:45 -07:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("dump-languages")
|
|
|
|
|
.about("Print info about all known language parsers"),
|
|
|
|
|
)
|
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 current_dir = env::current_dir().unwrap();
|
2021-06-09 15:03:27 -04:00
|
|
|
let config = Config::load()?;
|
|
|
|
|
let mut loader = loader::Loader::new()?;
|
2019-01-17 15:15:40 -08:00
|
|
|
|
2021-06-23 10:51:55 +03:00
|
|
|
match matches.subcommand() {
|
|
|
|
|
("init-config", Some(_)) => {
|
2021-06-27 03:03:47 +03:00
|
|
|
if let Ok(Some(config_path)) = Config::find_config_file() {
|
|
|
|
|
return Err(anyhow!(
|
|
|
|
|
"Remove your existing config file first: {}",
|
|
|
|
|
config_path.to_string_lossy()
|
|
|
|
|
));
|
|
|
|
|
}
|
2021-06-23 10:51:55 +03:00
|
|
|
let mut config = Config::initial()?;
|
|
|
|
|
config.add(tree_sitter_loader::Config::initial())?;
|
|
|
|
|
config.add(tree_sitter_cli::highlight::ThemeConfig::default())?;
|
|
|
|
|
config.save()?;
|
|
|
|
|
println!(
|
|
|
|
|
"Saved initial configuration to {}",
|
|
|
|
|
config.location.display()
|
|
|
|
|
);
|
2019-12-05 15:28:16 -08:00
|
|
|
}
|
2021-06-23 10:51:55 +03:00
|
|
|
|
|
|
|
|
("generate", Some(matches)) => {
|
|
|
|
|
let grammar_path = matches.value_of("grammar-path");
|
2023-01-06 06:13:08 +02:00
|
|
|
let debug_build = matches.is_present("debug-build");
|
|
|
|
|
let build = matches.is_present("build");
|
2023-01-06 06:37:22 +02:00
|
|
|
let libdir = matches.value_of("libdir");
|
2023-07-18 13:24:52 +02:00
|
|
|
let js_runtime = matches.value_of("js-runtime");
|
2021-06-23 10:51:55 +03:00
|
|
|
let report_symbol_name = matches.value_of("report-states-for-rule").or_else(|| {
|
|
|
|
|
if matches.is_present("report-states") {
|
|
|
|
|
Some("")
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if matches.is_present("log") {
|
|
|
|
|
logger::init();
|
|
|
|
|
}
|
2023-07-30 20:43:52 +03:00
|
|
|
let abi_version = matches.value_of("abi-version").map_or(
|
|
|
|
|
Ok::<_, Error>(DEFAULT_GENERATE_ABI_VERSION),
|
|
|
|
|
|version| {
|
|
|
|
|
Ok(if version == "latest" {
|
|
|
|
|
tree_sitter::LANGUAGE_VERSION
|
|
|
|
|
} else {
|
|
|
|
|
version
|
|
|
|
|
.parse()
|
|
|
|
|
.with_context(|| "invalid abi version flag")?
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
)?;
|
2021-06-23 10:51:55 +03:00
|
|
|
let generate_bindings = !matches.is_present("no-bindings");
|
|
|
|
|
generate::generate_parser_in_directory(
|
|
|
|
|
¤t_dir,
|
|
|
|
|
grammar_path,
|
2022-01-17 14:45:07 -08:00
|
|
|
abi_version,
|
2021-06-23 10:51:55 +03:00
|
|
|
generate_bindings,
|
|
|
|
|
report_symbol_name,
|
2023-07-18 13:24:52 +02:00
|
|
|
js_runtime,
|
2021-03-08 12:01:45 -08:00
|
|
|
)?;
|
2023-01-06 06:13:08 +02:00
|
|
|
if build {
|
2023-01-06 06:37:22 +02:00
|
|
|
if let Some(path) = libdir {
|
|
|
|
|
loader = loader::Loader::with_parser_lib_path(PathBuf::from(path));
|
|
|
|
|
}
|
2023-01-06 06:13:08 +02:00
|
|
|
loader.use_debug_build(debug_build);
|
|
|
|
|
loader.languages_at_path(¤t_dir)?;
|
|
|
|
|
}
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
2019-10-24 12:01:27 -07:00
|
|
|
|
2021-06-23 10:51:55 +03:00
|
|
|
("test", Some(matches)) => {
|
|
|
|
|
let debug = matches.is_present("debug");
|
|
|
|
|
let debug_graph = matches.is_present("debug-graph");
|
2021-09-08 00:08:13 +03:00
|
|
|
let debug_build = matches.is_present("debug-build");
|
2021-06-23 10:51:55 +03:00
|
|
|
let update = matches.is_present("update");
|
|
|
|
|
let filter = matches.value_of("filter");
|
2024-02-03 07:28:53 -06:00
|
|
|
let include: Option<Regex> =
|
|
|
|
|
matches.value_of("include").and_then(|s| Regex::new(s).ok());
|
|
|
|
|
let exclude: Option<Regex> =
|
|
|
|
|
matches.value_of("exclude").and_then(|s| Regex::new(s).ok());
|
2023-07-20 03:42:52 -04:00
|
|
|
let apply_all_captures = matches.is_present("apply-all-captures");
|
2021-09-08 00:08:13 +03:00
|
|
|
|
2023-01-10 10:44:20 +02:00
|
|
|
if debug {
|
|
|
|
|
// For augmenting debug logging in external scanners
|
|
|
|
|
env::set_var("TREE_SITTER_DEBUG", "1");
|
|
|
|
|
}
|
2021-09-08 00:08:13 +03:00
|
|
|
|
|
|
|
|
loader.use_debug_build(debug_build);
|
|
|
|
|
|
2023-10-31 16:56:11 -07:00
|
|
|
let mut parser = Parser::new();
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "wasm")]
|
|
|
|
|
if matches.is_present("wasm") {
|
2022-10-24 16:59:17 -07:00
|
|
|
let engine = tree_sitter::wasmtime::Engine::default();
|
|
|
|
|
parser
|
2023-11-27 17:46:37 -08:00
|
|
|
.set_wasm_store(tree_sitter::WasmStore::new(engine.clone()).unwrap())
|
2022-10-24 16:59:17 -07:00
|
|
|
.unwrap();
|
|
|
|
|
loader.use_wasm(engine);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 10:51:55 +03:00
|
|
|
let languages = loader.languages_at_path(¤t_dir)?;
|
|
|
|
|
let language = languages
|
|
|
|
|
.first()
|
|
|
|
|
.ok_or_else(|| anyhow!("No language found"))?;
|
2024-02-04 01:30:33 -05:00
|
|
|
parser.set_language(language)?;
|
2022-10-24 16:59:17 -07:00
|
|
|
|
2021-06-23 10:51:55 +03:00
|
|
|
let test_dir = current_dir.join("test");
|
2019-12-05 15:28:16 -08:00
|
|
|
|
2021-06-23 10:51:55 +03:00
|
|
|
// Run the corpus tests. Look for them at two paths: `test/corpus` and `corpus`.
|
|
|
|
|
let mut test_corpus_dir = test_dir.join("corpus");
|
|
|
|
|
if !test_corpus_dir.is_dir() {
|
|
|
|
|
test_corpus_dir = current_dir.join("corpus");
|
|
|
|
|
}
|
|
|
|
|
if test_corpus_dir.is_dir() {
|
2024-02-03 07:28:53 -06:00
|
|
|
let mut opts = TestOptions {
|
|
|
|
|
path: test_corpus_dir,
|
2021-06-23 10:51:55 +03:00
|
|
|
debug,
|
|
|
|
|
debug_graph,
|
|
|
|
|
filter,
|
2024-02-03 07:28:53 -06:00
|
|
|
include,
|
|
|
|
|
exclude,
|
2021-06-23 10:51:55 +03:00
|
|
|
update,
|
2024-02-03 07:28:53 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
test::run_tests_at_path(&mut parser, &mut opts)?;
|
2021-06-23 10:51:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check that all of the queries are valid.
|
2024-02-04 01:30:33 -05:00
|
|
|
test::check_queries_at_path(language, ¤t_dir.join("queries"))?;
|
2021-06-23 10:51:55 +03:00
|
|
|
|
|
|
|
|
// Run the syntax highlighting tests.
|
|
|
|
|
let test_highlight_dir = test_dir.join("highlight");
|
|
|
|
|
if test_highlight_dir.is_dir() {
|
2023-01-23 11:45:10 -08:00
|
|
|
let mut highlighter = Highlighter::new();
|
2023-10-31 16:56:11 -07:00
|
|
|
highlighter.parser = parser;
|
2023-10-27 11:57:04 +01:00
|
|
|
test_highlight::test_highlights(
|
|
|
|
|
&loader,
|
|
|
|
|
&mut highlighter,
|
|
|
|
|
&test_highlight_dir,
|
|
|
|
|
apply_all_captures,
|
|
|
|
|
)?;
|
2023-10-31 16:56:11 -07:00
|
|
|
parser = highlighter.parser;
|
2021-06-23 10:51:55 +03:00
|
|
|
}
|
2021-12-18 09:44:59 -06:00
|
|
|
|
|
|
|
|
let test_tag_dir = test_dir.join("tags");
|
|
|
|
|
if test_tag_dir.is_dir() {
|
2023-01-23 11:45:10 -08:00
|
|
|
let mut tags_context = TagsContext::new();
|
2023-10-31 16:56:11 -07:00
|
|
|
tags_context.parser = parser;
|
2023-01-23 11:45:10 -08:00
|
|
|
test_tags::test_tags(&loader, &mut tags_context, &test_tag_dir)?;
|
2021-12-18 09:44:59 -06:00
|
|
|
}
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
2020-09-29 15:43:30 -04:00
|
|
|
|
2021-06-23 10:51:55 +03:00
|
|
|
("parse", Some(matches)) => {
|
|
|
|
|
let debug = matches.is_present("debug");
|
|
|
|
|
let debug_graph = matches.is_present("debug-graph");
|
2021-09-08 00:08:13 +03:00
|
|
|
let debug_build = matches.is_present("debug-build");
|
2023-02-13 12:33:34 -08:00
|
|
|
|
|
|
|
|
let output = if matches.is_present("output-dot") {
|
|
|
|
|
ParseOutput::Dot
|
|
|
|
|
} else if matches.is_present("output-xml") {
|
|
|
|
|
ParseOutput::Xml
|
|
|
|
|
} else if matches.is_present("quiet") {
|
|
|
|
|
ParseOutput::Quiet
|
|
|
|
|
} else {
|
|
|
|
|
ParseOutput::Normal
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-03 20:59:01 -04:00
|
|
|
let encoding =
|
|
|
|
|
matches
|
|
|
|
|
.values_of("encoding")
|
|
|
|
|
.map_or(Ok(None), |mut e| match e.next() {
|
2023-09-03 06:47:27 +03:00
|
|
|
Some("utf16") => Ok(Some(ffi::TSInputEncodingUTF16)),
|
|
|
|
|
Some("utf8") => Ok(Some(ffi::TSInputEncodingUTF8)),
|
2023-07-03 20:59:01 -04:00
|
|
|
Some(_) => Err(anyhow!("Invalid encoding. Expected one of: utf8, utf16")),
|
|
|
|
|
None => Ok(None),
|
|
|
|
|
})?;
|
|
|
|
|
|
2021-06-23 10:51:55 +03:00
|
|
|
let time = matches.is_present("time");
|
|
|
|
|
let edits = matches
|
|
|
|
|
.values_of("edits")
|
2024-02-04 01:30:33 -05:00
|
|
|
.map_or(Vec::new(), std::iter::Iterator::collect);
|
2023-05-02 10:34:55 +03:00
|
|
|
let cancellation_flag = util::cancel_on_signal();
|
2022-09-06 22:41:52 -07:00
|
|
|
let mut parser = Parser::new();
|
2021-06-23 10:51:55 +03:00
|
|
|
|
2021-06-20 12:14:01 +03:00
|
|
|
if debug {
|
|
|
|
|
// For augmenting debug logging in external scanners
|
|
|
|
|
env::set_var("TREE_SITTER_DEBUG", "1");
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-08 00:08:13 +03:00
|
|
|
loader.use_debug_build(debug_build);
|
|
|
|
|
|
2023-10-31 16:56:11 -07:00
|
|
|
#[cfg(feature = "wasm")]
|
|
|
|
|
if matches.is_present("wasm") {
|
2022-09-06 22:41:52 -07:00
|
|
|
let engine = tree_sitter::wasmtime::Engine::default();
|
|
|
|
|
parser
|
2023-11-27 17:46:37 -08:00
|
|
|
.set_wasm_store(tree_sitter::WasmStore::new(engine.clone()).unwrap())
|
2022-09-06 22:41:52 -07:00
|
|
|
.unwrap();
|
|
|
|
|
loader.use_wasm(engine);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 10:51:55 +03:00
|
|
|
let timeout = matches
|
|
|
|
|
.value_of("timeout")
|
2024-02-04 01:30:33 -05:00
|
|
|
.map_or(0, |t| t.parse::<u64>().unwrap());
|
2021-06-23 10:51:55 +03:00
|
|
|
|
|
|
|
|
let paths = collect_paths(matches.value_of("paths-file"), matches.values_of("paths"))?;
|
|
|
|
|
|
2021-06-26 18:10:09 +00:00
|
|
|
let max_path_length = paths.iter().map(|p| p.chars().count()).max().unwrap_or(0);
|
2021-06-23 10:51:55 +03:00
|
|
|
let mut has_error = false;
|
|
|
|
|
let loader_config = config.get()?;
|
|
|
|
|
loader.find_all_languages(&loader_config)?;
|
|
|
|
|
|
|
|
|
|
let should_track_stats = matches.is_present("stat");
|
|
|
|
|
let mut stats = parse::Stats::default();
|
|
|
|
|
|
|
|
|
|
for path in paths {
|
|
|
|
|
let path = Path::new(&path);
|
2022-01-03 10:57:01 -08:00
|
|
|
|
2021-06-23 10:51:55 +03:00
|
|
|
let language =
|
|
|
|
|
loader.select_language(path, ¤t_dir, matches.value_of("scope"))?;
|
2022-09-06 22:41:52 -07:00
|
|
|
parser
|
2023-11-27 15:50:08 -08:00
|
|
|
.set_language(&language)
|
2022-09-06 22:41:52 -07:00
|
|
|
.context("incompatible language")?;
|
2021-06-23 10:51:55 +03:00
|
|
|
|
2023-07-03 20:59:01 -04:00
|
|
|
let opts = ParseFileOptions {
|
2023-11-27 15:50:08 -08:00
|
|
|
language: language.clone(),
|
2021-06-23 10:51:55 +03:00
|
|
|
path,
|
2023-07-03 20:59:01 -04:00
|
|
|
edits: &edits,
|
2021-06-23 10:51:55 +03:00
|
|
|
max_path_length,
|
2023-02-13 12:33:34 -08:00
|
|
|
output,
|
2023-07-03 20:59:01 -04:00
|
|
|
print_time: time,
|
2021-06-23 10:51:55 +03:00
|
|
|
timeout,
|
|
|
|
|
debug,
|
|
|
|
|
debug_graph,
|
2023-07-03 20:59:01 -04:00
|
|
|
cancellation_flag: Some(&cancellation_flag),
|
|
|
|
|
encoding,
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-04 01:30:33 -05:00
|
|
|
let this_file_errored = parse::parse_file_at_path(&mut parser, &opts)?;
|
2021-06-23 10:51:55 +03:00
|
|
|
|
|
|
|
|
if should_track_stats {
|
|
|
|
|
stats.total_parses += 1;
|
|
|
|
|
if !this_file_errored {
|
|
|
|
|
stats.successful_parses += 1;
|
|
|
|
|
}
|
2020-09-29 15:43:30 -04:00
|
|
|
}
|
2021-06-23 10:51:55 +03:00
|
|
|
|
|
|
|
|
has_error |= this_file_errored;
|
2020-09-29 15:43:30 -04:00
|
|
|
}
|
|
|
|
|
|
2021-06-23 10:51:55 +03:00
|
|
|
if should_track_stats {
|
2024-02-04 01:30:33 -05:00
|
|
|
println!("{stats}");
|
2021-06-23 10:51:55 +03:00
|
|
|
}
|
2020-09-29 12:34:25 -04:00
|
|
|
|
2021-06-23 10:51:55 +03:00
|
|
|
if has_error {
|
|
|
|
|
return Err(anyhow!(""));
|
|
|
|
|
}
|
2019-01-07 17:57:36 -08:00
|
|
|
}
|
2019-02-06 16:17:35 -08:00
|
|
|
|
2021-06-23 10:51:55 +03:00
|
|
|
("query", Some(matches)) => {
|
|
|
|
|
let ordered_captures = matches.values_of("captures").is_some();
|
2023-02-14 14:41:25 -08:00
|
|
|
let quiet = matches.values_of("quiet").is_some();
|
|
|
|
|
let time = matches.values_of("time").is_some();
|
2021-06-23 10:51:55 +03:00
|
|
|
let paths = collect_paths(matches.value_of("paths-file"), matches.values_of("paths"))?;
|
|
|
|
|
let loader_config = config.get()?;
|
|
|
|
|
loader.find_all_languages(&loader_config)?;
|
|
|
|
|
let language = loader.select_language(
|
|
|
|
|
Path::new(&paths[0]),
|
|
|
|
|
¤t_dir,
|
|
|
|
|
matches.value_of("scope"),
|
|
|
|
|
)?;
|
|
|
|
|
let query_path = Path::new(matches.value_of("query-path").unwrap());
|
2023-02-14 14:41:25 -08:00
|
|
|
let byte_range = matches.value_of("byte-range").and_then(|arg| {
|
2024-02-04 01:30:33 -05:00
|
|
|
let mut parts = arg.split(':');
|
2023-02-14 14:41:25 -08:00
|
|
|
let start = parts.next()?.parse().ok()?;
|
|
|
|
|
let end = parts.next().unwrap().parse().ok()?;
|
|
|
|
|
Some(start..end)
|
|
|
|
|
});
|
|
|
|
|
let point_range = matches.value_of("row-range").and_then(|arg| {
|
2024-02-04 01:30:33 -05:00
|
|
|
let mut parts = arg.split(':');
|
2023-02-14 14:41:25 -08:00
|
|
|
let start = parts.next()?.parse().ok()?;
|
|
|
|
|
let end = parts.next().unwrap().parse().ok()?;
|
|
|
|
|
Some(Point::new(start, 0)..Point::new(end, 0))
|
2021-06-23 10:51:55 +03:00
|
|
|
});
|
|
|
|
|
let should_test = matches.is_present("test");
|
|
|
|
|
query::query_files_at_paths(
|
2024-02-04 01:30:33 -05:00
|
|
|
&language,
|
2021-06-23 10:51:55 +03:00
|
|
|
paths,
|
|
|
|
|
query_path,
|
|
|
|
|
ordered_captures,
|
2023-02-14 14:41:25 -08:00
|
|
|
byte_range,
|
|
|
|
|
point_range,
|
2021-06-23 10:51:55 +03:00
|
|
|
should_test,
|
2023-02-14 14:41:25 -08:00
|
|
|
quiet,
|
|
|
|
|
time,
|
2021-06-23 10:51:55 +03:00
|
|
|
)?;
|
2019-02-06 16:17:35 -08:00
|
|
|
}
|
2021-06-23 10:51:55 +03:00
|
|
|
|
|
|
|
|
("tags", Some(matches)) => {
|
|
|
|
|
let loader_config = config.get()?;
|
|
|
|
|
loader.find_all_languages(&loader_config)?;
|
|
|
|
|
let paths = collect_paths(matches.value_of("paths-file"), matches.values_of("paths"))?;
|
|
|
|
|
tags::generate_tags(
|
|
|
|
|
&loader,
|
|
|
|
|
matches.value_of("scope"),
|
|
|
|
|
&paths,
|
|
|
|
|
matches.is_present("quiet"),
|
|
|
|
|
matches.is_present("time"),
|
|
|
|
|
)?;
|
2019-02-19 11:24:50 -08:00
|
|
|
}
|
|
|
|
|
|
2021-06-23 10:51:55 +03:00
|
|
|
("highlight", Some(matches)) => {
|
|
|
|
|
let theme_config: tree_sitter_cli::highlight::ThemeConfig = config.get()?;
|
|
|
|
|
loader.configure_highlights(&theme_config.theme.highlight_names);
|
|
|
|
|
let loader_config = config.get()?;
|
|
|
|
|
loader.find_all_languages(&loader_config)?;
|
|
|
|
|
|
|
|
|
|
let time = matches.is_present("time");
|
|
|
|
|
let quiet = matches.is_present("quiet");
|
|
|
|
|
let html_mode = quiet || matches.is_present("html");
|
2023-06-22 09:18:53 -04:00
|
|
|
let should_check = matches.is_present("check");
|
2021-06-23 10:51:55 +03:00
|
|
|
let paths = collect_paths(matches.value_of("paths-file"), matches.values_of("paths"))?;
|
2023-07-20 03:42:52 -04:00
|
|
|
let apply_all_captures = matches.is_present("apply-all-captures");
|
2020-10-22 15:55:51 -07:00
|
|
|
|
2021-06-23 10:51:55 +03:00
|
|
|
if html_mode && !quiet {
|
|
|
|
|
println!("{}", highlight::HTML_HEADER);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-02 10:34:55 +03:00
|
|
|
let cancellation_flag = util::cancel_on_signal();
|
2021-06-23 10:51:55 +03:00
|
|
|
|
2023-11-27 15:50:08 -08:00
|
|
|
let mut language = None;
|
2021-06-23 10:51:55 +03:00
|
|
|
if let Some(scope) = matches.value_of("scope") {
|
2023-11-27 15:50:08 -08:00
|
|
|
language = loader.language_configuration_for_scope(scope)?;
|
|
|
|
|
if language.is_none() {
|
2024-02-04 01:30:33 -05:00
|
|
|
return Err(anyhow!("Unknown scope '{scope}'"));
|
2021-06-23 10:51:55 +03:00
|
|
|
}
|
2019-02-20 14:38:19 -08:00
|
|
|
}
|
|
|
|
|
|
2024-02-04 01:30:33 -05:00
|
|
|
let query_paths = matches.values_of("query-paths").map(|e| {
|
|
|
|
|
e.collect::<Vec<_>>()
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(std::string::ToString::to_string)
|
|
|
|
|
.collect::<Vec<_>>()
|
2023-07-20 08:04:59 -04:00
|
|
|
});
|
|
|
|
|
|
2021-06-23 10:51:55 +03:00
|
|
|
for path in paths {
|
|
|
|
|
let path = Path::new(&path);
|
2023-11-27 15:50:08 -08:00
|
|
|
let (language, language_config) = match language.clone() {
|
2019-02-20 14:38:19 -08:00
|
|
|
Some(v) => v,
|
2024-02-04 01:30:33 -05:00
|
|
|
None => {
|
|
|
|
|
if let Some(v) = loader.language_configuration_for_file_name(path)? {
|
|
|
|
|
v
|
|
|
|
|
} else {
|
|
|
|
|
eprintln!("No language found for path {path:?}");
|
2021-06-23 10:51:55 +03:00
|
|
|
continue;
|
|
|
|
|
}
|
2024-02-04 01:30:33 -05:00
|
|
|
}
|
2021-06-23 10:51:55 +03:00
|
|
|
};
|
|
|
|
|
|
2023-07-20 08:04:59 -04:00
|
|
|
if let Some(highlight_config) = language_config.highlight_config(
|
|
|
|
|
language,
|
|
|
|
|
apply_all_captures,
|
|
|
|
|
query_paths.as_deref(),
|
|
|
|
|
)? {
|
2023-06-22 09:18:53 -04:00
|
|
|
if should_check {
|
2023-07-20 04:57:11 -04:00
|
|
|
let names = if let Some(path) = matches.value_of("captures-path") {
|
|
|
|
|
let path = Path::new(path);
|
|
|
|
|
let file = fs::read_to_string(path)?;
|
|
|
|
|
let capture_names = file
|
|
|
|
|
.lines()
|
|
|
|
|
.filter_map(|line| {
|
|
|
|
|
if line.trim().is_empty() || line.trim().starts_with(';') {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
line.split(';').next().map(|s| s.trim().trim_matches('"'))
|
|
|
|
|
})
|
|
|
|
|
.collect::<HashSet<_>>();
|
|
|
|
|
highlight_config.nonconformant_capture_names(&capture_names)
|
|
|
|
|
} else {
|
|
|
|
|
highlight_config.nonconformant_capture_names(&HashSet::new())
|
|
|
|
|
};
|
2023-06-22 09:18:53 -04:00
|
|
|
if names.is_empty() {
|
|
|
|
|
eprintln!("All highlight captures conform to standards.");
|
|
|
|
|
} else {
|
|
|
|
|
eprintln!(
|
|
|
|
|
"Non-standard highlight {} detected:",
|
|
|
|
|
if names.len() > 1 {
|
|
|
|
|
"captures"
|
|
|
|
|
} else {
|
|
|
|
|
"capture"
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
for name in names {
|
2024-02-04 01:30:33 -05:00
|
|
|
eprintln!("* {name}");
|
2023-06-22 09:18:53 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 10:51:55 +03:00
|
|
|
let source = fs::read(path)?;
|
|
|
|
|
if html_mode {
|
|
|
|
|
highlight::html(
|
|
|
|
|
&loader,
|
2021-07-03 03:36:01 +03:00
|
|
|
&theme_config.theme,
|
2021-06-23 10:51:55 +03:00
|
|
|
&source,
|
|
|
|
|
highlight_config,
|
|
|
|
|
quiet,
|
|
|
|
|
time,
|
2023-08-02 08:56:26 -05:00
|
|
|
Some(&cancellation_flag),
|
2021-06-23 10:51:55 +03:00
|
|
|
)?;
|
|
|
|
|
} else {
|
|
|
|
|
highlight::ansi(
|
|
|
|
|
&loader,
|
2021-07-03 03:36:01 +03:00
|
|
|
&theme_config.theme,
|
2021-06-23 10:51:55 +03:00
|
|
|
&source,
|
|
|
|
|
highlight_config,
|
|
|
|
|
time,
|
|
|
|
|
Some(&cancellation_flag),
|
|
|
|
|
)?;
|
2019-02-19 11:24:50 -08:00
|
|
|
}
|
2019-02-20 14:38:19 -08:00
|
|
|
} else {
|
2024-02-04 01:30:33 -05:00
|
|
|
eprintln!("No syntax highlighting config found for path {path:?}");
|
2019-02-19 11:24:50 -08:00
|
|
|
}
|
2021-06-23 10:51:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if html_mode && !quiet {
|
|
|
|
|
println!("{}", highlight::HTML_FOOTER);
|
2019-02-19 11:24:50 -08:00
|
|
|
}
|
|
|
|
|
}
|
2020-03-02 23:04:15 +00:00
|
|
|
|
2021-06-23 10:51:55 +03:00
|
|
|
("build-wasm", Some(matches)) => {
|
|
|
|
|
let grammar_path = current_dir.join(matches.value_of("path").unwrap_or(""));
|
2022-09-06 22:41:52 -07:00
|
|
|
wasm::compile_language_to_wasm(
|
2023-10-31 16:56:11 -07:00
|
|
|
&loader,
|
2022-09-06 22:41:52 -07:00
|
|
|
&grammar_path,
|
|
|
|
|
¤t_dir,
|
|
|
|
|
matches.is_present("docker"),
|
|
|
|
|
)?;
|
2020-03-02 23:04:15 +00:00
|
|
|
}
|
2021-06-23 10:51:55 +03:00
|
|
|
|
2021-06-23 10:58:01 +03:00
|
|
|
("playground", Some(matches)) => {
|
2021-06-23 10:51:55 +03:00
|
|
|
let open_in_browser = !matches.is_present("quiet");
|
2023-07-30 20:43:52 +03:00
|
|
|
playground::serve(¤t_dir, open_in_browser)?;
|
2021-06-23 10:51:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
("dump-languages", Some(_)) => {
|
|
|
|
|
let loader_config = config.get()?;
|
|
|
|
|
loader.find_all_languages(&loader_config)?;
|
|
|
|
|
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,
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-08-07 17:41:45 -07:00
|
|
|
}
|
2021-06-23 10:51:55 +03:00
|
|
|
|
|
|
|
|
_ => unreachable!(),
|
2018-12-08 23:35:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2018-12-05 12:50:12 -08:00
|
|
|
}
|
2019-09-11 14:44:49 -07:00
|
|
|
|
2020-07-14 15:04:39 -07:00
|
|
|
fn collect_paths<'a>(
|
|
|
|
|
paths_file: Option<&str>,
|
|
|
|
|
paths: Option<impl Iterator<Item = &'a str>>,
|
2021-06-09 12:32:22 -04:00
|
|
|
) -> Result<Vec<String>> {
|
2020-07-14 15:04:39 -07:00
|
|
|
if let Some(paths_file) = paths_file {
|
|
|
|
|
return Ok(fs::read_to_string(paths_file)
|
2024-02-04 01:30:33 -05:00
|
|
|
.with_context(|| format!("Failed to read paths file {paths_file}"))?
|
2020-07-14 15:04:39 -07:00
|
|
|
.trim()
|
2021-10-25 23:51:01 +02:00
|
|
|
.lines()
|
2020-07-14 15:04:39 -07:00
|
|
|
.map(String::from)
|
|
|
|
|
.collect::<Vec<_>>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(paths) = paths {
|
|
|
|
|
let mut result = Vec::new();
|
2019-12-06 11:41:21 -08:00
|
|
|
|
2020-07-14 15:04:39 -07:00
|
|
|
let mut incorporate_path = |path: &str, positive| {
|
|
|
|
|
if positive {
|
|
|
|
|
result.push(path.to_string());
|
2024-02-04 01:30:33 -05:00
|
|
|
} else if let Some(index) = result.iter().position(|p| p == path) {
|
|
|
|
|
result.remove(index);
|
2019-12-06 11:41:21 -08:00
|
|
|
}
|
2020-07-14 15:04:39 -07:00
|
|
|
};
|
2019-12-06 11:41:21 -08:00
|
|
|
|
2020-07-14 15:04:39 -07:00
|
|
|
for mut path in paths {
|
|
|
|
|
let mut positive = true;
|
2024-02-04 01:30:33 -05:00
|
|
|
if path.starts_with('!') {
|
2020-07-14 15:04:39 -07:00
|
|
|
positive = false;
|
2024-02-04 01:30:33 -05:00
|
|
|
path = path.trim_start_matches('!');
|
2020-07-14 15:04:39 -07:00
|
|
|
}
|
2019-12-06 11:41:21 -08:00
|
|
|
|
2020-07-14 15:04:39 -07:00
|
|
|
if Path::new(path).exists() {
|
|
|
|
|
incorporate_path(path, positive);
|
|
|
|
|
} else {
|
2024-02-04 01:30:33 -05:00
|
|
|
let paths = glob(path).with_context(|| format!("Invalid glob pattern {path:?}"))?;
|
2020-07-14 15:04:39 -07:00
|
|
|
for path in paths {
|
|
|
|
|
if let Some(path) = path?.to_str() {
|
|
|
|
|
incorporate_path(path, positive);
|
|
|
|
|
}
|
2019-12-06 11:41:21 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-14 15:04:39 -07:00
|
|
|
|
2020-09-30 15:49:42 -04:00
|
|
|
if result.is_empty() {
|
2021-06-09 12:32:22 -04:00
|
|
|
return Err(anyhow!(
|
|
|
|
|
"No files were found at or matched by the provided pathname/glob"
|
|
|
|
|
));
|
2020-09-30 15:49:42 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-14 15:04:39 -07:00
|
|
|
return Ok(result);
|
2019-12-06 11:41:21 -08:00
|
|
|
}
|
2020-07-14 15:04:39 -07:00
|
|
|
|
2021-06-09 12:32:22 -04:00
|
|
|
Err(anyhow!("Must provide one or more paths"))
|
2019-12-06 11:41:21 -08:00
|
|
|
}
|