From a1ed12f4f4e11420f5403dffee0a97fb09c2a43f Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 13 May 2019 21:51:17 -0700 Subject: [PATCH] Start work on web UI command --- Cargo.lock | 117 +++++++++++++++++++++++++++++++++++ cli/Cargo.toml | 2 + cli/src/lib.rs | 1 + cli/src/main.rs | 5 +- cli/src/wasm.rs | 15 +++-- cli/src/web_ui.html | 111 +++++++++++++++++++++++++++++++++ cli/src/web_ui.rs | 60 ++++++++++++++++++ docs/assets/js/playground.js | 19 +++--- 8 files changed, 316 insertions(+), 14 deletions(-) create mode 100644 cli/src/web_ui.html create mode 100644 cli/src/web_ui.rs diff --git a/Cargo.lock b/Cargo.lock index a28a6889..6248034b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -33,6 +33,11 @@ dependencies = [ "nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ascii" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "atty" version = "0.2.11" @@ -98,6 +103,21 @@ name = "cfg-if" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "chrono" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "chunked_transfer" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "clap" version = "2.32.0" @@ -183,6 +203,16 @@ dependencies = [ "scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "idna" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-normalization 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "indexmap" version = "1.0.2" @@ -228,6 +258,11 @@ dependencies = [ "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "matches" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "memchr" version = "2.1.1" @@ -302,6 +337,11 @@ dependencies = [ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "percent-encoding" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "proc-macro2" version = "0.4.24" @@ -627,6 +667,28 @@ dependencies = [ "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "time" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "tiny_http" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ascii 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)", + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "chunked_transfer 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tree-sitter" version = "0.3.9" @@ -662,8 +724,10 @@ dependencies = [ "smallbitvec 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "spin 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "tiny_http 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "tree-sitter 0.3.9", "tree-sitter-highlight 0.1.5", + "webbrowser 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -682,6 +746,22 @@ name = "ucd-util" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "unicode-bidi" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-normalization" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "smallvec 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "unicode-width" version = "0.1.5" @@ -700,6 +780,16 @@ dependencies = [ "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "url" +version = "1.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "utf8-ranges" version = "1.0.2" @@ -720,6 +810,20 @@ name = "void" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "webbrowser" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "widestring 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "widestring" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "winapi" version = "0.3.6" @@ -744,6 +848,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" "checksum argon2rs 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3f67b0b6a86dae6e67ff4ca2b6201396074996379fba2b92ff649126f37cb392" "checksum arrayvec 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "f405cc4c21cd8b784f6c8fc2adf9bc00f59558f0049b5ec21517f875963040cc" +"checksum ascii 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)" = "97be891acc47ca214468e09425d02cef3af2c94d0d82081cd02061f996802f14" "checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" "checksum autocfg 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4e5f34df7a019573fb8bdc7e24a2bfebe51a2a1d6bfdbaeccedb3c41fc574727" "checksum backtrace 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "89a47830402e9981c5c41223151efcced65a0510c13097c769cede7efb34782a" @@ -753,6 +858,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "94f88df23a25417badc922ab0f5716cc1330e87f71ddd9203b3a3ccd9cedf75d" "checksum cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)" = "f159dfd43363c4d08055a07703eb7a3406b0dac4d0584d96965a3262db3c9d16" "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" +"checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878" +"checksum chunked_transfer 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "498d20a7aaf62625b9bf26e637cf7736417cde1d0c99f1d04d1170229a85cf87" "checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8ff012e225ce166d4422e0e78419d901719760f62ae2b7969ca6b564d1b54a9e" @@ -763,6 +870,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" "checksum hashbrown 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "64b7d419d0622ae02fe5da6b9a5e1964b610a65bb37923b976aeebb6dbb8f86e" +"checksum idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e" "checksum indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7e81a7c05f79578dbc15793d8b619db9ba32b4577003ef3af1a91c416798c58d" "checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" "checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1" @@ -770,6 +878,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum libloading 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9c3ad660d7cb8c5822cd83d10897b0f1f1526792737a179e73896152f85b88c2" "checksum lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "62ebf1391f6acad60e5c8b43706dde4582df75c06698ab44511d15016bc2442c" "checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6" +"checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" "checksum memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0a3eb002f0535929f1199681417029ebea04aadc0c7a4224b46be99c7f5d6a16" "checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945" "checksum nom 4.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9c349f68f25f596b9f44cf0e7c69752a5c633b0550c3ff849518bfba0233774a" @@ -779,6 +888,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum once_cell 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "532c29a261168a45ce28948f9537ddd7a5dd272cc513b3017b1e82a88f962c37" "checksum parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ab41b4aed082705d1056416ae4468b6ea99d52599ecf3169b00088d43113e337" "checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9" +"checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" "checksum proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "77619697826f31a02ae974457af0b29b723e5619e113e9397b8b82c6bd253f09" "checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c" "checksum rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8356f47b32624fef5b3301c1be97e5944ecdd595409cc5da11d05f211db6cfbd" @@ -818,14 +928,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" +"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" +"checksum tiny_http 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1661fa0a44c95d01604bd05c66732a446c657efb62b5164a7a083a3b552b4951" "checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86" +"checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" +"checksum unicode-normalization 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "141339a08b982d942be2ca06ff8b076563cbe223d1befd5450716790d44e2426" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" +"checksum url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a" "checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737" "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" "checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +"checksum webbrowser 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c01efd7cb6939b7f34983f1edff0550e5b21b49e2db4495656295922df8939ac" +"checksum widestring 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "effc0e4ff8085673ea7b9b2e3c73f6bd4d118810c9009ed8f1e16bd96c331db6" "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 6fe23291..a9d5c684 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 5d026cde..1e0d021d 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 9decd720..78f79cc1 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 782b9a43..3fa38c65 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 00000000..58ab0e7f --- /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 00000000..2ea0b4ab --- /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 5f8ff923..6801d699 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); }