diff --git a/Cargo.lock b/Cargo.lock index a28a6889d..6248034be 100644 Binary files a/Cargo.lock and b/Cargo.lock differ diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 6fe232910..a9d5c6842 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -33,6 +33,8 @@ serde_derive = "1.0" regex-syntax = "0.6.4" regex = "1" rsass = "^0.9.8" +tiny_http = "0.6" +webbrowser = "0.5.1" [dependencies.tree-sitter] version = ">= 0.3.7" diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 5d026cdef..1e0d021d9 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -9,6 +9,7 @@ pub mod properties; pub mod test; pub mod util; pub mod wasm; +pub mod web_ui; #[cfg(test)] mod tests; diff --git a/cli/src/main.rs b/cli/src/main.rs index 9decd7204..78f79cc1d 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -5,7 +5,7 @@ use std::path::Path; use std::process::exit; use std::{u64, usize}; use tree_sitter_cli::{ - config, error, generate, highlight, loader, logger, parse, properties, test, wasm, + config, error, generate, highlight, loader, logger, parse, properties, test, wasm, web_ui, }; fn main() { @@ -95,6 +95,7 @@ fn run() -> error::Result<()> { .about("Compile a parser to WASM") .arg(Arg::with_name("path").index(1).multiple(true)), ) + .subcommand(SubCommand::with_name("ui").about("Test a parser interactively in the browser")) .get_matches(); let home_dir = dirs::home_dir().expect("Failed to read home directory"); @@ -245,6 +246,8 @@ fn run() -> error::Result<()> { } 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)?; + } else if matches.subcommand_matches("ui").is_some() { + web_ui::serve(¤t_dir); } Ok(()) diff --git a/cli/src/wasm.rs b/cli/src/wasm.rs index 782b9a439..3fa38c65f 100644 --- a/cli/src/wasm.rs +++ b/cli/src/wasm.rs @@ -5,10 +5,7 @@ use std::fs; use std::path::Path; use std::process::Command; -pub fn compile_language_to_wasm(language_dir: &Path) -> Result<()> { - let src_dir = language_dir.join("src"); - - // Parse the grammar.json to find out the language name. +pub fn get_grammar_name(src_dir: &Path) -> Result { let grammar_json_path = src_dir.join("grammar.json"); let grammar_json = fs::read_to_string(&grammar_json_path).map_err(|e| { format!( @@ -22,7 +19,13 @@ pub fn compile_language_to_wasm(language_dir: &Path) -> Result<()> { grammar_json_path, e ) })?; - let output_filename = format!("tree-sitter-{}.wasm", grammar.name); + Ok(grammar.name) +} + +pub fn compile_language_to_wasm(language_dir: &Path) -> Result<()> { + let src_dir = language_dir.join("src"); + let grammar_name = get_grammar_name(&src_dir)?; + let output_filename = format!("tree-sitter-{}.wasm", grammar_name); // Get the current user id so that files created in the docker container will have // the same owner. @@ -54,7 +57,7 @@ pub fn compile_language_to_wasm(language_dir: &Path) -> Result<()> { "-s", "TOTAL_MEMORY=33554432", "-s", - &format!("EXPORTED_FUNCTIONS=[\"_tree_sitter_{}\"]", grammar.name), + &format!("EXPORTED_FUNCTIONS=[\"_tree_sitter_{}\"]", grammar_name), "-fno-exceptions", "-I", "src", diff --git a/cli/src/web_ui.html b/cli/src/web_ui.html new file mode 100644 index 000000000..58ab0e7f9 --- /dev/null +++ b/cli/src/web_ui.html @@ -0,0 +1,111 @@ + + Tree-sitter + + + + + +
+
+
+ + +
+ +
+ + +
+
+ +
+ + + + +
+

+      
+
+
+ + + + + + + + + + + + diff --git a/cli/src/web_ui.rs b/cli/src/web_ui.rs new file mode 100644 index 000000000..2ea0b4ab0 --- /dev/null +++ b/cli/src/web_ui.rs @@ -0,0 +1,60 @@ +use super::wasm; +use std::fs; +use std::net::TcpListener; +use std::path::Path; +use std::str::FromStr; +use tiny_http::{Header, Response, Server}; +use webbrowser; + +const PLAYGROUND_JS: &'static [u8] = include_bytes!("../../docs/assets/js/playground.js"); +const LIB_JS: &'static [u8] = include_bytes!("../../lib/binding_web/tree-sitter.js"); +const LIB_WASM: &'static [u8] = include_bytes!("../../lib/binding_web/tree-sitter.wasm"); +const HTML: &'static [u8] = include_bytes!("./web_ui.html"); + +pub fn serve(grammar_path: &Path) { + let port = get_available_port().expect("Couldn't find an available port"); + let url = format!("127.0.0.1:{}", port); + let server = Server::http(&url).expect("Failed to start web server"); + let grammar_name = wasm::get_grammar_name(&grammar_path.join("src")) + .map_err(|e| format!("Failed to get wasm filename: {:?}", e)) + .unwrap(); + let language_wasm = fs::read(format!("./tree-sitter-{}.wasm", grammar_name)).unwrap(); + + webbrowser::open(&format!("http://127.0.0.1:{}", port)) + .map_err(|e| format!("Failed to open '{}' in a web browser. Error: {}", url, e)) + .unwrap(); + + let html_header = Header::from_str("Content-type: text/html").unwrap(); + let js_header = Header::from_str("Content-type: application/javascript").unwrap(); + let wasm_header = Header::from_str("Content-type: application/wasm").unwrap(); + + for request in server.incoming_requests() { + let (body, header) = match request.url() { + "/" => (HTML, &html_header), + "/playground.js" => (PLAYGROUND_JS, &js_header), + "/tree-sitter.js" => (LIB_JS, &js_header), + "/tree-sitter.wasm" => (LIB_WASM, &wasm_header), + "/tree-sitter-parser.wasm" => (language_wasm.as_slice(), &wasm_header), + _ => { + request + .respond(Response::from_string("Not found").with_status_code(404)) + .expect("Failed to write HTTP response"); + continue; + } + }; + let response = Response::from_string("") + .with_data(body, Some(body.len())) + .with_header(header.clone()); + request + .respond(response) + .expect("Failed to write HTTP response"); + } +} + +fn get_available_port() -> Option { + (8000..12000).find(port_is_available) +} + +fn port_is_available(port: &u16) -> bool { + TcpListener::bind(("127.0.0.1", *port)).is_ok() +} diff --git a/docs/assets/js/playground.js b/docs/assets/js/playground.js index 5f8ff923d..6801d6995 100644 --- a/docs/assets/js/playground.js +++ b/docs/assets/js/playground.js @@ -11,10 +11,7 @@ let tree; const demoContainer = document.getElementById('playground-container'); const languagesByName = {}; - await Promise.all([ - codeInput.value = await fetch(scriptURL).then(r => r.text()), - TreeSitter.init() - ]); + await TreeSitter.init(); const parser = new TreeSitter(); const codeEditor = CodeMirror.fromTextArea(codeInput, { @@ -182,11 +179,13 @@ let tree; const node = tree.rootNode.namedDescendantForPosition(start, end); if (treeRows) { if (treeRowHighlightedIndex !== -1) { - treeRows[treeRowHighlightedIndex] = treeRows[treeRowHighlightedIndex].replace('highlighted', 'plain'); + const row = treeRows[treeRowHighlightedIndex]; + if (row) treeRows[treeRowHighlightedIndex] = row.replace('highlighted', 'plain'); } treeRowHighlightedIndex = treeRows.findIndex(row => row.includes(`data-id=${node.id}`)); if (treeRowHighlightedIndex !== -1) { - treeRows[treeRowHighlightedIndex] = treeRows[treeRowHighlightedIndex].replace('plain', 'highlighted'); + const row = treeRows[treeRowHighlightedIndex]; + if (row) treeRows[treeRowHighlightedIndex] = row.replace('plain', 'highlighted'); } cluster.update(treeRows); const lineHeight = cluster.options.item_height; @@ -220,7 +219,13 @@ let tree; function handleLoggingChange() { if (loggingCheckbox.checked) { - parser.setLogger(console.log); + parser.setLogger((message, lexing) => { + if (lexing) { + console.log(" ", message) + } else { + console.log(message) + } + }); } else { parser.setLogger(null); }