oxc/crates/oxc_regular_expression/examples/visitor.rs
camchenry 8d026e1dd9 feat(regular_expression): implement GetSpan for RegExp AST nodes (#6056)
To make it easier to get the `Span` for some node in the Regex AST, I've implemented the `GetSpan` trait for all necessary structs.
2024-09-26 05:51:35 +00:00

29 lines
901 B
Rust

#![allow(clippy::print_stdout)]
use oxc_allocator::Allocator;
use oxc_regular_expression::{
visit::{RegExpAstKind, Visit},
Parser, ParserOptions,
};
use oxc_span::GetSpan;
struct TestVisitor;
impl Visit<'_> for TestVisitor {
fn enter_node(&mut self, kind: RegExpAstKind) {
println!("enter_node: {:?} {kind:?}", kind.span());
}
fn leave_node(&mut self, kind: RegExpAstKind) {
println!("leave_node: {:?} {kind:?}", kind.span());
}
}
fn main() {
let source_text = r"/(https?:\/\/github\.com\/(([^\s]+)\/([^\s]+))\/([^\s]+\/)?(issues|pull)\/([0-9]+))|(([^\s]+)\/([^\s]+))?#([1-9][0-9]*)($|[\s\:\;\-\(\=])/";
let allocator = Allocator::default();
let parser = Parser::new(&allocator, source_text, ParserOptions::default());
let pattern = parser.parse().unwrap().pattern;
let mut visitor = TestVisitor;
visitor.visit_pattern(&pattern);
}