feat: refs scie modules

上级 42937fbd
...@@ -6,6 +6,8 @@ Copyright (c) Phodal Huang ...@@ -6,6 +6,8 @@ Copyright (c) Phodal Huang
Copyright (c) Microsoft Corporation. Copyright (c) Microsoft Corporation.
Copyright (c) 2015 Will Speak <will@willspeak.me>, Ivan Ivashchenko <defuz@me.com>, and contributors.
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
......
pub mod scie_scanner; pub mod scie_scanner;
pub mod scie_onig; pub mod scie_onig;
pub mod scie_error;
// > The MIT License (MIT)
// >
// > Copyright (c) 2015 Will Speak <will@willspeak.me>, Ivan Ivashchenko
// > <defuz@me.com>, and contributors.
// >
// > Permission is hereby granted, free of charge, to any person obtaining a copy
// > of this software and associated documentation files (the "Software"), to deal
// > in the Software without restriction, including without limitation the rights
// > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// > copies of the Software, and to permit persons to whom the Software is
// > furnished to do so, subject to the following conditions:
// >
// > The above copyright notice and this permission notice shall be included in all
// > copies or substantial portions of the Software.
// >
// > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// > SOFTWARE.
use std::os::raw::c_int;
use std::ptr::null;
use std::{error, fmt, str};
/// This struture represents an error from the underlying Oniguruma libray.
pub struct ScieOnigError {
code: c_int,
description: String,
}
impl ScieOnigError {
pub fn from_code_and_info(code: c_int, info: &onig_sys::OnigErrorInfo) -> ScieOnigError {
ScieOnigError::new(code, info)
}
fn from_code(code: c_int) -> ScieOnigError {
ScieOnigError::new(code, null())
}
fn new(code: c_int, info: *const onig_sys::OnigErrorInfo) -> ScieOnigError {
let buff = &mut [0; onig_sys::ONIG_MAX_ERROR_MESSAGE_LEN as usize];
let len = unsafe { onig_sys::onig_error_code_to_str(buff.as_mut_ptr(), code, info) };
let description = str::from_utf8(&buff[..len as usize]).unwrap();
ScieOnigError {
code,
description: description.to_owned(),
}
}
/// Return Oniguruma engine error code.
pub fn code(&self) -> i32 {
self.code
}
/// Return error description provided by Oniguruma engine.
pub fn description(&self) -> &str {
&self.description
}
}
impl error::Error for ScieOnigError {
fn description(&self) -> &str {
&self.description
}
}
impl fmt::Display for ScieOnigError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Oniguruma error: {}", self.description())
}
}
impl fmt::Debug for ScieOnigError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Error({}, {})", self.code, self.description())
}
}
//
//
use std::ptr::null_mut; use std::ptr::null_mut;
use onig::{Syntax, EncodedChars}; use onig::{Syntax, EncodedChars};
use std::sync::Mutex; use std::sync::Mutex;
use crate::scanner::scie_error::ScieOnigError;
lazy_static! { lazy_static! {
static ref REGEX_NEW_MUTEX: Mutex<()> = Mutex::new(()); static ref REGEX_NEW_MUTEX: Mutex<()> = Mutex::new(());
...@@ -14,10 +16,12 @@ bitflags! { ...@@ -14,10 +16,12 @@ bitflags! {
} }
} }
pub struct ScieOnig {} pub struct ScieOnig {
raw: onig_sys::OnigRegex,
}
impl ScieOnig { impl ScieOnig {
pub fn hello(pattern: &str) { pub fn hello(pattern: &str) -> Result<Self, ScieOnigError> {
let option = ScieOnigOptions::REGEX_OPTION_NONE; let option = ScieOnigOptions::REGEX_OPTION_NONE;
let syntax = Syntax::default(); let syntax = Syntax::default();
...@@ -33,7 +37,7 @@ impl ScieOnig { ...@@ -33,7 +37,7 @@ impl ScieOnig {
par_end: null_mut(), par_end: null_mut(),
}; };
let _err = unsafe { let err = unsafe {
// Grab a lock to make sure that `onig_new` isn't called by // Grab a lock to make sure that `onig_new` isn't called by
// more than one thread at a time. // more than one thread at a time.
let _guard = REGEX_NEW_MUTEX.lock().unwrap(); let _guard = REGEX_NEW_MUTEX.lock().unwrap();
...@@ -45,8 +49,14 @@ impl ScieOnig { ...@@ -45,8 +49,14 @@ impl ScieOnig {
pattern.encoding(), pattern.encoding(),
syntax as *const Syntax as *mut Syntax as *mut onig_sys::OnigSyntaxType, syntax as *const Syntax as *mut Syntax as *mut onig_sys::OnigSyntaxType,
&mut error, &mut error,
); )
}; };
if err == onig_sys::ONIG_NORMAL as i32 {
Ok(ScieOnig { raw: reg })
} else {
Err(ScieOnigError::from_code_and_info(err, &error))
}
} }
pub fn create_onig_scanner(_sources: Vec<String>) {} pub fn create_onig_scanner(_sources: Vec<String>) {}
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册