diff --git a/internal/kclvm_py/scripts/requirements.txt b/internal/kclvm_py/scripts/requirements.txt index 13531f315..9e397ab00 100644 --- a/internal/kclvm_py/scripts/requirements.txt +++ b/internal/kclvm_py/scripts/requirements.txt @@ -1,5 +1,4 @@ wheel==0.34.2 -twine==3.2.0 pyyaml==5.4 pytest-xdist==2.2.1 lark-parser==0.11.3 @@ -22,4 +21,3 @@ gunicorn==20.1.0 parsy==1.3.0 wasmer==1.0.0 wasmer_compiler_cranelift==1.0.0 -pyopenssl diff --git a/kclvm/compiler_base/span/src/lib.rs b/kclvm/compiler_base/span/src/lib.rs index 50c8ae215..dd750aad9 100644 --- a/kclvm/compiler_base/span/src/lib.rs +++ b/kclvm/compiler_base/span/src/lib.rs @@ -9,9 +9,39 @@ pub mod span; pub use rustc_span::fatal_error; -pub use span::{BytePos, Span, DUMMY_SP}; +pub use span::{BytePos, Span, SpanData, DUMMY_SP}; pub type SourceMap = rustc_span::SourceMap; pub type SourceFile = rustc_span::SourceFile; pub type FilePathMapping = rustc_span::source_map::FilePathMapping; pub type Loc = rustc_span::Loc; + +/// Get the filename from `SourceMap` by `Span`. +/// +/// # Examples +/// +/// ```rust +/// # use compiler_base_span::{span_to_filename_string, span::new_byte_pos, FilePathMapping, SourceMap}; +/// # use rustc_span::SpanData; +/// # use std::path::PathBuf; +/// # use std::fs; +/// +/// // 1. You need to hold a `SourceMap` at first. +/// let filename = fs::canonicalize(&PathBuf::from("./src/test_datas/code_snippet")).unwrap().display().to_string(); +/// let src = std::fs::read_to_string(filename.clone()).unwrap(); +/// let sm = SourceMap::new(FilePathMapping::empty()); +/// sm.new_source_file(PathBuf::from(filename.clone()).into(), src.to_string()); +/// +/// // 2. You got the span in `SourceMap`. +/// let code_span = SpanData { +/// lo: new_byte_pos(21), +/// hi: new_byte_pos(22), +/// }.span(); +/// +/// // 3. You can got the filename by `span_to_filename_string()`. +/// assert_eq!(filename, span_to_filename_string(&code_span, &sm)); +/// ``` +#[inline] +pub fn span_to_filename_string(span: &Span, sm: &SourceMap) -> String { + format!("{}", sm.span_to_filename(*span).prefer_remapped()) +} diff --git a/kclvm/compiler_base/span/src/span.rs b/kclvm/compiler_base/span/src/span.rs index 7a9a160c1..886d10237 100644 --- a/kclvm/compiler_base/span/src/span.rs +++ b/kclvm/compiler_base/span/src/span.rs @@ -2,4 +2,18 @@ use rustc_span; pub type BytePos = rustc_span::BytePos; pub type Span = rustc_span::Span; +pub type SpanData = rustc_span::SpanData; pub const DUMMY_SP: Span = rustc_span::DUMMY_SP; + +/// New a `BytePos` +/// +/// # Examples +/// +/// ```rust +/// # use compiler_base_span::span::new_byte_pos; +/// let byte_pos = new_byte_pos(10); +/// ``` +#[inline] +pub fn new_byte_pos(arg: u32) -> rustc_span::BytePos { + rustc_span::BytePos(arg) +} diff --git a/kclvm/compiler_base/span/src/test_datas/code_snippet b/kclvm/compiler_base/span/src/test_datas/code_snippet new file mode 100644 index 000000000..47d492e14 --- /dev/null +++ b/kclvm/compiler_base/span/src/test_datas/code_snippet @@ -0,0 +1,2 @@ +Line 1 Code Snippet. +Line 2 Code Snippet. diff --git a/scripts/build-windows/requirements.release.txt b/scripts/build-windows/requirements.release.txt index 4a2af87d0..6d75d6625 100644 --- a/scripts/build-windows/requirements.release.txt +++ b/scripts/build-windows/requirements.release.txt @@ -1,5 +1,4 @@ wheel==0.34.2 -twine==3.2.0 pyyaml==5.4 pytest-xdist==2.2.1 lark-parser==0.11.3 @@ -21,4 +20,3 @@ gunicorn==20.1.0 parsy==1.3.0 wasmer==1.0.0 wasmer_compiler_cranelift==1.0.0 -pyopenssl