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;
|
|
|
|
|
extern crate serde_json;
|
2018-12-05 12:50:12 -08:00
|
|
|
|
2018-12-20 13:36:39 -08:00
|
|
|
use clap::{App, Arg, SubCommand};
|
|
|
|
|
use std::env;
|
|
|
|
|
use std::io::Write;
|
2019-01-02 16:48:44 -08:00
|
|
|
use std::path::PathBuf;
|
2019-01-04 12:42:45 -08:00
|
|
|
use std::process::{exit, Command, Stdio};
|
2019-01-04 15:26:48 -08:00
|
|
|
use std::usize;
|
2018-12-05 12:50:12 -08:00
|
|
|
|
|
|
|
|
mod build_tables;
|
|
|
|
|
mod error;
|
|
|
|
|
mod generate;
|
|
|
|
|
mod grammars;
|
2019-01-02 16:48:44 -08:00
|
|
|
mod logger;
|
2018-12-08 13:44:11 -08:00
|
|
|
mod nfa;
|
2018-12-05 12:50:12 -08:00
|
|
|
mod parse_grammar;
|
|
|
|
|
mod prepare_grammar;
|
|
|
|
|
mod render;
|
|
|
|
|
mod rules;
|
|
|
|
|
mod tables;
|
|
|
|
|
|
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-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")
|
2018-12-20 13:36:39 -08:00
|
|
|
.arg(Arg::with_name("path").index(1)),
|
|
|
|
|
)
|
|
|
|
|
.subcommand(
|
2018-12-05 12:50:12 -08:00
|
|
|
SubCommand::with_name("test")
|
|
|
|
|
.about("Run a parser's tests")
|
|
|
|
|
.arg(Arg::with_name("path").index(1).required(true))
|
|
|
|
|
.arg(Arg::with_name("line").index(2).required(true))
|
2018-12-20 13:36:39 -08:00
|
|
|
.arg(Arg::with_name("column").index(3).required(true)),
|
|
|
|
|
)
|
|
|
|
|
.get_matches();
|
2018-12-08 23:35:48 -08:00
|
|
|
|
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-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()
|
|
|
|
|
});
|
2018-12-20 13:36:39 -08:00
|
|
|
let mut grammar_path = env::current_dir().expect("Failed to read CWD");
|
|
|
|
|
grammar_path.push("grammar.js");
|
|
|
|
|
let grammar_json = load_js_grammar_file(grammar_path);
|
2019-01-04 15:26:48 -08:00
|
|
|
let code =
|
|
|
|
|
generate::generate_parser_for_grammar(&grammar_json, minimize, state_ids_to_log)?;
|
2018-12-08 23:35:48 -08:00
|
|
|
println!("{}", code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2018-12-05 12:50:12 -08:00
|
|
|
}
|
2018-12-20 13:36:39 -08:00
|
|
|
|
|
|
|
|
fn load_js_grammar_file(grammar_path: PathBuf) -> String {
|
|
|
|
|
let mut node_process = Command::new("node")
|
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
|
.spawn()
|
|
|
|
|
.expect("Failed to run `node`");
|
|
|
|
|
|
|
|
|
|
let js_prelude = include_str!("./js/dsl.js");
|
|
|
|
|
let mut node_stdin = node_process
|
|
|
|
|
.stdin
|
|
|
|
|
.take()
|
|
|
|
|
.expect("Failed to open stdin for node");
|
|
|
|
|
write!(
|
|
|
|
|
node_stdin,
|
|
|
|
|
"{}\nconsole.log(JSON.stringify(require(\"{}\"), null, 2));\n",
|
|
|
|
|
js_prelude,
|
|
|
|
|
grammar_path.to_str().unwrap()
|
2019-01-02 16:48:44 -08:00
|
|
|
)
|
|
|
|
|
.expect("Failed to write to node's stdin");
|
2018-12-20 13:36:39 -08:00
|
|
|
drop(node_stdin);
|
|
|
|
|
let output = node_process
|
|
|
|
|
.wait_with_output()
|
|
|
|
|
.expect("Failed to read output from node");
|
|
|
|
|
match output.status.code() {
|
|
|
|
|
None => panic!("Node process was killed"),
|
|
|
|
|
Some(0) => {}
|
|
|
|
|
Some(code) => panic!(format!("Node process exited with status {}", code)),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String::from_utf8(output.stdout).expect("Got invalid UTF8 from node")
|
|
|
|
|
}
|