From 43e987bc1b0cd05b37f2791c76f9ffb5316055f2 Mon Sep 17 00:00:00 2001 From: Sam Cao Date: Mon, 25 Mar 2024 08:44:33 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A6=84=20refactor:=20Move=20ByteToIndexMa?= =?UTF-8?q?p=20to=20position=20utils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rust/src/ast_utils.rs | 41 +------------------------- rust/src/lib.rs | 3 +- rust/src/outputs.rs | 17 +++++------ rust/src/position_utils.rs | 59 ++++++++++++++++++++++++++++++++++++++ rust/src/token_utils.rs | 4 +-- 5 files changed, 73 insertions(+), 51 deletions(-) create mode 100644 rust/src/position_utils.rs diff --git a/rust/src/ast_utils.rs b/rust/src/ast_utils.rs index 03ad78be..e16374f6 100644 --- a/rust/src/ast_utils.rs +++ b/rust/src/ast_utils.rs @@ -22,53 +22,14 @@ use jni::JNIEnv; use crate::converter; use crate::jni_utils::JAVA_ARRAY_LIST; +use crate::position_utils::ByteToIndexMap; -use std::collections::BTreeMap; use std::ops::Range; use std::sync::Arc; use deno_ast::swc::ast::*; -use deno_ast::swc::common::source_map::Pos; -use deno_ast::swc::common::Span; use deno_ast::swc::common::Spanned; -pub struct ByteToIndexMap { - map: BTreeMap, -} - -impl ByteToIndexMap { - pub fn new() -> Self { - ByteToIndexMap { map: BTreeMap::new() } - } - - pub fn get_range_by_span(&self, span: &Span) -> Range { - Range { - start: *self - .map - .get(&(span.lo().to_usize() - 1)) - .expect("Couldn't find start index"), - end: *self - .map - .get(&(span.hi().to_usize() - 1)) - .expect("Couldn't find end index"), - } - } - - pub fn register_by_span(&mut self, span: &Span) { - [span.lo().to_usize() - 1, span.hi().to_usize() - 1] - .into_iter() - .for_each(|position| { - if !self.map.contains_key(&position) { - self.map.insert(position, 0); - } - }); - } - - pub fn update(&mut self, key: &usize, value: usize) { - self.map.get_mut(&key).map(|v| *v = value); - } -} - pub struct JavaAstFactory { #[allow(dead_code)] class: GlobalRef, diff --git a/rust/src/lib.rs b/rust/src/lib.rs index dd125b51..77b3ee7d 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -25,7 +25,6 @@ use std::ffi::c_void; use std::ptr::null_mut; pub mod ast_utils; -pub mod token_utils; pub mod converter; pub mod core; pub mod enums; @@ -33,6 +32,8 @@ pub mod error; pub mod jni_utils; pub mod options; pub mod outputs; +pub mod position_utils; +pub mod token_utils; use crate::jni_utils::ToJniType; diff --git a/rust/src/outputs.rs b/rust/src/outputs.rs index 9722d82a..23f1178d 100644 --- a/rust/src/outputs.rs +++ b/rust/src/outputs.rs @@ -32,6 +32,7 @@ use crate::converter; use crate::enums::*; use crate::jni_utils::ToJniType; use crate::options::*; +use crate::position_utils::ByteToIndexMap; struct JavaParseOutput { class: GlobalRef, @@ -230,9 +231,9 @@ impl ParseOutput { } } - pub fn get_byte_to_index_map(&self) -> ast_utils::ByteToIndexMap { + pub fn get_byte_to_index_map(&self) -> ByteToIndexMap { // Register the keys - let mut byte_to_index_map = ast_utils::ByteToIndexMap::new(); + let mut byte_to_index_map = ByteToIndexMap::new(); match &self.program { Some(program) => { if program.is_module() { @@ -264,18 +265,18 @@ impl ParseOutput { byte_to_index_map } - fn register_decl(&self, byte_to_index_map: &mut ast_utils::ByteToIndexMap, decl: &Decl) { + fn register_decl(&self, byte_to_index_map: &mut ByteToIndexMap, decl: &Decl) { match decl { Decl::Var(var_decl) => self.register_var_decl(byte_to_index_map, var_decl.as_ref()), _ => {} }; } - fn register_module(&self, byte_to_index_map: &mut ast_utils::ByteToIndexMap, module: &Module) { + fn register_module(&self, byte_to_index_map: &mut ByteToIndexMap, module: &Module) { byte_to_index_map.register_by_span(&module.span); } - fn register_script(&self, byte_to_index_map: &mut ast_utils::ByteToIndexMap, script: &Script) { + fn register_script(&self, byte_to_index_map: &mut ByteToIndexMap, script: &Script) { byte_to_index_map.register_by_span(&script.span); script .body @@ -283,14 +284,14 @@ impl ParseOutput { .for_each(|stmt| self.register_stmt(byte_to_index_map, stmt)) } - fn register_stmt(&self, byte_to_index_map: &mut ast_utils::ByteToIndexMap, stmt: &Stmt) { + fn register_stmt(&self, byte_to_index_map: &mut ByteToIndexMap, stmt: &Stmt) { match stmt { Stmt::Decl(decl) => self.register_decl(byte_to_index_map, decl), _ => {} }; } - fn register_var_decl(&self, byte_to_index_map: &mut ast_utils::ByteToIndexMap, var_decl: &VarDecl) { + fn register_var_decl(&self, byte_to_index_map: &mut ByteToIndexMap, var_decl: &VarDecl) { byte_to_index_map.register_by_span(&var_decl.span); var_decl .decls @@ -298,7 +299,7 @@ impl ParseOutput { .for_each(|var_declarator| self.register_var_declarator(byte_to_index_map, var_declarator)); } - fn register_var_declarator(&self, byte_to_index_map: &mut ast_utils::ByteToIndexMap, var_declarator: &VarDeclarator) { + fn register_var_declarator(&self, byte_to_index_map: &mut ByteToIndexMap, var_declarator: &VarDeclarator) { byte_to_index_map.register_by_span(&var_declarator.span); // TODO } diff --git a/rust/src/position_utils.rs b/rust/src/position_utils.rs new file mode 100644 index 00000000..1914de58 --- /dev/null +++ b/rust/src/position_utils.rs @@ -0,0 +1,59 @@ +/* +* Copyright (c) 2024. caoccao.com Sam Cao +* All rights reserved. + +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at + +* http://www.apache.org/licenses/LICENSE-2.0 + +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +use std::collections::BTreeMap; +use std::ops::Range; + +use deno_ast::swc::common::source_map::Pos; +use deno_ast::swc::common::Span; + +pub struct ByteToIndexMap { + map: BTreeMap, +} + +impl ByteToIndexMap { + pub fn new() -> Self { + ByteToIndexMap { map: BTreeMap::new() } + } + + pub fn get_range_by_span(&self, span: &Span) -> Range { + Range { + start: *self + .map + .get(&(span.lo().to_usize() - 1)) + .expect("Couldn't find start index"), + end: *self + .map + .get(&(span.hi().to_usize() - 1)) + .expect("Couldn't find end index"), + } + } + + pub fn register_by_span(&mut self, span: &Span) { + [span.lo().to_usize() - 1, span.hi().to_usize() - 1] + .into_iter() + .for_each(|position| { + if !self.map.contains_key(&position) { + self.map.insert(position, 0); + } + }); + } + + pub fn update(&mut self, key: &usize, value: usize) { + self.map.get_mut(&key).map(|v| *v = value); + } +} diff --git a/rust/src/token_utils.rs b/rust/src/token_utils.rs index 475d9587..23938241 100644 --- a/rust/src/token_utils.rs +++ b/rust/src/token_utils.rs @@ -25,10 +25,10 @@ use deno_ast::swc::common::source_map::Pos; use deno_ast::swc::parser::error::Error; use deno_ast::swc::parser::token::{IdentLike, Token, TokenAndSpan, Word}; -use crate::ast_utils; use crate::converter; use crate::enums::*; use crate::jni_utils::JAVA_ARRAY_LIST; +use crate::position_utils::ByteToIndexMap; use std::ops::Range; use std::ptr::null_mut; @@ -952,7 +952,7 @@ pub fn init<'local>(env: &mut JNIEnv<'local>) { pub fn token_and_spans_to_java_list<'local>( env: &mut JNIEnv<'local>, - byte_to_index_map: &ast_utils::ByteToIndexMap, + byte_to_index_map: &ByteToIndexMap, source_text: &str, token_and_spans: Option>>, ) -> jvalue {