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

Feat(compiler-base): add util functions to span. #186

Merged
merged 7 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions internal/kclvm_py/scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,4 +21,3 @@ gunicorn==20.1.0
parsy==1.3.0
wasmer==1.0.0
wasmer_compiler_cranelift==1.0.0
pyopenssl
32 changes: 31 additions & 1 deletion kclvm/compiler_base/span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
14 changes: 14 additions & 0 deletions kclvm/compiler_base/span/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions kclvm/compiler_base/span/src/test_datas/code_snippet
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Line 1 Code Snippet.
Line 2 Code Snippet.
2 changes: 0 additions & 2 deletions scripts/build-windows/requirements.release.txt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,4 +20,3 @@ gunicorn==20.1.0
parsy==1.3.0
wasmer==1.0.0
wasmer_compiler_cranelift==1.0.0
pyopenssl