Add parse_sv_str/parse_lib_str
This commit is contained in:
parent
52ca47c4a8
commit
a55fd9e5f2
@ -2,6 +2,8 @@
|
||||
|
||||
## [Unreleased](https://github.com/dalance/sv-parser/compare/v0.4.2...Unreleased) - ReleaseDate
|
||||
|
||||
* [Added] parse_sv_str/parse_lib_str
|
||||
|
||||
## [v0.4.2](https://github.com/dalance/sv-parser/compare/v0.4.1...v0.4.2) - 2019-11-12
|
||||
|
||||
* [Added] re-export DefineText
|
||||
|
@ -127,7 +127,7 @@ pub fn preprocess<T: AsRef<Path>, U: AsRef<Path>>(
|
||||
preprocess_str(&s, path, pre_defines, include_paths)
|
||||
}
|
||||
|
||||
fn preprocess_str<T: AsRef<Path>, U: AsRef<Path>>(
|
||||
pub fn preprocess_str<T: AsRef<Path>, U: AsRef<Path>>(
|
||||
s: &str,
|
||||
path: T,
|
||||
pre_defines: &Defines,
|
||||
|
@ -7,7 +7,9 @@ use std::fmt;
|
||||
use std::path::{Path, PathBuf};
|
||||
pub use sv_parser_error::{Error, ErrorKind};
|
||||
use sv_parser_parser::{lib_parser, sv_parser, Span, SpanInfo};
|
||||
pub use sv_parser_pp::preprocess::{preprocess, Define, DefineText, Defines, PreprocessedText};
|
||||
pub use sv_parser_pp::preprocess::{
|
||||
preprocess, preprocess_str, Define, DefineText, Defines, PreprocessedText,
|
||||
};
|
||||
pub use sv_parser_syntaxtree::*;
|
||||
|
||||
pub struct SyntaxTree {
|
||||
@ -122,6 +124,43 @@ pub fn parse_sv<T: AsRef<Path>, U: AsRef<Path>>(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_sv_str<T: AsRef<Path>, U: AsRef<Path>>(
|
||||
s: &str,
|
||||
path: T,
|
||||
pre_defines: &HashMap<String, Option<Define>>,
|
||||
include_paths: &[U],
|
||||
) -> Result<(SyntaxTree, Defines), Error> {
|
||||
let (text, defines) = preprocess_str(s, path, pre_defines, include_paths)?;
|
||||
let span = Span::new_extra(text.text(), SpanInfo::default());
|
||||
let result = all_consuming(sv_parser)(span);
|
||||
match result {
|
||||
Ok((_, x)) => Ok((
|
||||
SyntaxTree {
|
||||
node: x.into(),
|
||||
text,
|
||||
},
|
||||
defines,
|
||||
)),
|
||||
Err(x) => {
|
||||
let pos = match x {
|
||||
nom::Err::Incomplete(_) => None,
|
||||
nom::Err::Error(e) => error_position(&e),
|
||||
nom::Err::Failure(e) => error_position(&e),
|
||||
};
|
||||
let origin = if let Some(pos) = pos {
|
||||
if let Some(origin) = text.origin(pos) {
|
||||
Some((origin.0.clone(), origin.1))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Err(ErrorKind::Parse(origin).into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_lib<T: AsRef<Path>, U: AsRef<Path>>(
|
||||
path: T,
|
||||
pre_defines: &HashMap<String, Option<Define>>,
|
||||
@ -158,6 +197,43 @@ pub fn parse_lib<T: AsRef<Path>, U: AsRef<Path>>(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_lib_str<T: AsRef<Path>, U: AsRef<Path>>(
|
||||
s: &str,
|
||||
path: T,
|
||||
pre_defines: &HashMap<String, Option<Define>>,
|
||||
include_paths: &[U],
|
||||
) -> Result<(SyntaxTree, Defines), Error> {
|
||||
let (text, defines) = preprocess_str(s, path, pre_defines, include_paths)?;
|
||||
let span = Span::new_extra(text.text(), SpanInfo::default());
|
||||
let result = all_consuming(lib_parser)(span);
|
||||
match result {
|
||||
Ok((_, x)) => Ok((
|
||||
SyntaxTree {
|
||||
node: x.into(),
|
||||
text,
|
||||
},
|
||||
defines,
|
||||
)),
|
||||
Err(x) => {
|
||||
let pos = match x {
|
||||
nom::Err::Incomplete(_) => None,
|
||||
nom::Err::Error(e) => error_position(&e),
|
||||
nom::Err::Failure(e) => error_position(&e),
|
||||
};
|
||||
let origin = if let Some(pos) = pos {
|
||||
if let Some(origin) = text.origin(pos) {
|
||||
Some((origin.0.clone(), origin.1))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Err(ErrorKind::Parse(origin).into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! unwrap_node {
|
||||
($n:expr, $( $ty:tt ),+) => {{
|
||||
|
Loading…
x
Reference in New Issue
Block a user