swc/ecmascript/parser/examples/lexer.rs
강동윤 0ac55ae68b
Improve ux for rust users (#909)
testing:
 - Remove dependency on relative_path

swc_common:
 - Span's byte positions are now self-contained and `GLOBALS` is not required while parsing.
 - Changed `Comments` into a trait.
 - Provide single-threaded implementation of `Comments`
 - Cargo feature `tty-emiiter` (To remove tty related stuffs )
 - Cargo feature `sourcemap` (To remove sourcemap for web assets)
 - Removed dependency on dashmap

swc_ecma_parser:
 - No duplicated comments.
 - Removed dependency on once_cell and regex
 - Add a test suite to visualize and test span of nodes.

swc_ecma_utils:
 - Removed dependency on parser

swc:
 - Remove dependency on derive_more and path-clean
 - Add multi-threaded implementation of `Comments`

swc_ecmascript:
 - A new crate contains `ast`, `codegen`, `parser`, `utils`, `visit`.
2020-07-31 18:49:07 +09:00

47 lines
1.3 KiB
Rust

use std::sync::Arc;
use swc_common::{
self,
errors::{ColorConfig, Handler},
FileName, SourceMap,
};
use swc_ecma_parser::{lexer::Lexer, Capturing, Parser, StringInput, Syntax};
fn main() {
swc_common::GLOBALS.set(&swc_common::Globals::new(), || {
let cm: Arc<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()),
"function foo() {}".into(),
);
let lexer = Lexer::new(
Syntax::Es(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_module()
.map_err(|e| e.into_diagnostic(&handler).emit())
.expect("Failed to parse module.");
println!("Tokens: {:?}", parser.input().take());
});
}