Skip to content

Commit

Permalink
🦄 refactor: Move ByteToIndexMap to position utils
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Mar 25, 2024
1 parent c1fb105 commit 43e987b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 51 deletions.
41 changes: 1 addition & 40 deletions rust/src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize, usize>,
}

impl ByteToIndexMap {
pub fn new() -> Self {
ByteToIndexMap { map: BTreeMap::new() }
}

pub fn get_range_by_span(&self, span: &Span) -> Range<usize> {
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,
Expand Down
3 changes: 2 additions & 1 deletion rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ 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;
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;

Expand Down
17 changes: 9 additions & 8 deletions rust/src/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -264,41 +265,41 @@ 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
.iter()
.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
.iter()
.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
}
Expand Down
59 changes: 59 additions & 0 deletions rust/src/position_utils.rs
Original file line number Diff line number Diff line change
@@ -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<usize, usize>,
}

impl ByteToIndexMap {
pub fn new() -> Self {
ByteToIndexMap { map: BTreeMap::new() }
}

pub fn get_range_by_span(&self, span: &Span) -> Range<usize> {
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);
}
}
4 changes: 2 additions & 2 deletions rust/src/token_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Arc<Vec<TokenAndSpan>>>,
) -> jvalue {
Expand Down

0 comments on commit 43e987b

Please sign in to comment.