From 985ae0bfd7eded06bb8efe7a12196311a5df5b8d Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Tue, 19 Apr 2022 22:11:35 +1000 Subject: [PATCH] Run typescript lexer example --- Cargo.lock | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 43 +++++++++++++++++++++++++++++++++++- 3 files changed, 106 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 89a0341..d93fbb8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -46,6 +46,17 @@ dependencies = [ "syn", ] +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -174,6 +185,15 @@ dependencies = [ "wasi", ] +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "ident_case" version = "1.0.1" @@ -624,6 +644,7 @@ checksum = "f1cf725d3f2283e3bbc8f7803a3f39c0a2fdd81f7cb26dac7afa58b0ffe6299c" dependencies = [ "ahash", "ast_node", + "atty", "better_scoped_tls", "cfg-if", "debug_unreachable", @@ -637,6 +658,7 @@ dependencies = [ "string_cache", "swc_eq_ignore_macros", "swc_visit", + "termcolor", "tracing", "unicode-width", "url", @@ -736,6 +758,15 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + [[package]] name = "tinyvec" version = "1.5.1" @@ -847,6 +878,7 @@ dependencies = [ name = "value_script" version = "0.1.0" dependencies = [ + "swc_common", "swc_ecma_parser", ] @@ -868,6 +900,37 @@ version = "0.10.2+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-sys" version = "0.34.0" diff --git a/Cargo.toml b/Cargo.toml index 65228dd..ff330d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +swc_common = { version = "0.17.22", features=["tty-emitter"] } swc_ecma_parser = "0.102.2" diff --git a/src/main.rs b/src/main.rs index e7a11a9..f98aa77 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,44 @@ +use swc_common::{ + self, + errors::{ColorConfig, Handler}, + sync::Lrc, + FileName, SourceMap, +}; +use swc_ecma_parser::{lexer::Lexer, Capturing, Parser, StringInput, Syntax}; + fn main() { - println!("Hello, world!"); + let cm: Lrc = Default::default(); + let handler = Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(cm.clone())); + + // Real usage + // let fm = cm + // .load_file(Path::new("test.js")) + // .expect("failed to load test.js"); + + let fm = cm.new_source_file( + FileName::Custom("test.js".into()), + "interface Foo {}".into(), + ); + + let lexer = Lexer::new( + Syntax::Typescript(Default::default()), + Default::default(), + StringInput::from(&*fm), + None, + ); + + let capturing = Capturing::new(lexer); + + let mut parser = Parser::new_from(capturing); + + for e in parser.take_errors() { + e.into_diagnostic(&handler).emit(); + } + + let _module = parser + .parse_typescript_module() + .map_err(|e| e.into_diagnostic(&handler).emit()) + .expect("Failed to parse module."); + + println!("Tokens: {:?}", parser.input().take()); }