Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(parser): refactor visitor in regexp example #8524

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 39 additions & 43 deletions crates/oxc_parser/examples/regular_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::{env, fs, path::Path, sync::Arc};

use oxc_allocator::Allocator;
use oxc_ast::{ast, AstKind, Visit};
use oxc_ast::{ast::*, Visit};
use oxc_parser::{ParseOptions, Parser};
use oxc_regular_expression::{ConstructorParser as RegExpParser, Options as RegExpParserOptions};
use oxc_span::SourceType;
Expand Down Expand Up @@ -43,55 +43,51 @@ struct RegularExpressionVisitor {
}

impl<'a> Visit<'a> for RegularExpressionVisitor {
fn enter_node(&mut self, kind: AstKind<'a>) {
let allocator = Allocator::default();
fn visit_reg_exp_literal(&mut self, re: &RegExpLiteral<'a>) {
println!("🍀 {}", re.span.source_text(self.source_text.as_ref()));

match kind {
AstKind::RegExpLiteral(re) => {
println!("🍀 {}", re.span.source_text(self.source_text.as_ref()));
println!("{re:#?}");
println!();
}

println!("{re:#?}");
println!();
}
AstKind::NewExpression(new_expr)
if new_expr
.callee
.get_identifier_reference()
.filter(|ident| ident.name == "RegExp")
.is_some() =>
{
println!("🍀 {}", new_expr.span.source_text(&self.source_text));
fn visit_new_expression(&mut self, new_expr: &NewExpression<'a>) {
if new_expr
.callee
.get_identifier_reference()
.filter(|ident| ident.name == "RegExp")
.is_some()
{
println!("🍀 {}", new_expr.span.source_text(&self.source_text));

let pattern_span = match new_expr.arguments.first() {
Some(ast::Argument::StringLiteral(sl)) => sl.span,
_ => return,
};
let pattern_span = match new_expr.arguments.first() {
Some(Argument::StringLiteral(sl)) => sl.span,
_ => return,
};

let flags_span = match new_expr.arguments.get(1) {
Some(ast::Argument::StringLiteral(sl)) => Some(sl.span),
_ => None,
};
let flags_span = match new_expr.arguments.get(1) {
Some(Argument::StringLiteral(sl)) => Some(sl.span),
_ => None,
};

let parsed = RegExpParser::new(
&allocator,
pattern_span.source_text(&self.source_text),
flags_span.map(|span| span.source_text(&self.source_text)),
RegExpParserOptions {
pattern_span_offset: pattern_span.start,
flags_span_offset: flags_span.map_or(0, |span| span.start),
},
)
.parse();
let allocator = Allocator::default();
let parsed = RegExpParser::new(
&allocator,
pattern_span.source_text(&self.source_text),
flags_span.map(|span| span.source_text(&self.source_text)),
RegExpParserOptions {
pattern_span_offset: pattern_span.start,
flags_span_offset: flags_span.map_or(0, |span| span.start),
},
)
.parse();

if let Err(error) = parsed {
let error = error.with_source_code(Arc::clone(&self.source_text));
println!("{error:?}");
return;
}
println!("{parsed:#?}");
println!();
if let Err(error) = parsed {
let error = error.with_source_code(Arc::clone(&self.source_text));
println!("{error:?}");
return;
}
_ => {}
println!("{parsed:#?}");
println!();
}
}
}
Loading