mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-01-14 16:08:02 -05:00
Run typescript lexer example
This commit is contained in:
43
src/main.rs
43
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<SourceMap> = 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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user