Add look-ahead to custom syntax parser.

This commit is contained in:
Stephen Chung
2020-12-15 19:23:30 +08:00
parent 17310ef576
commit f8c14ba1c4
4 changed files with 16 additions and 12 deletions

View File

@@ -1831,15 +1831,17 @@ fn parse_custom_syntax(
tokens.push(key.into());
loop {
settings.pos = input.peek().unwrap().1;
let (fwd_token, fwd_pos) = input.peek().unwrap();
settings.pos = *fwd_pos;
let settings = settings.level_up();
let required_token =
if let Some(seg) = parse_func(&segments).map_err(|err| err.0.into_err(settings.pos))? {
seg
} else {
break;
};
let required_token = if let Some(seg) = parse_func(&segments, fwd_token.syntax().as_ref())
.map_err(|err| err.0.into_err(settings.pos))?
{
seg
} else {
break;
};
match required_token.as_str() {
MARKER_IDENT => match input.next().unwrap() {

View File

@@ -26,11 +26,11 @@ pub type FnCustomSyntaxEval =
/// A general expression parsing trait object.
#[cfg(not(feature = "sync"))]
pub type FnCustomSyntaxParse =
dyn Fn(&[ImmutableString]) -> Result<Option<ImmutableString>, ParseError>;
dyn Fn(&[ImmutableString], &str) -> Result<Option<ImmutableString>, ParseError>;
/// A general expression parsing trait object.
#[cfg(feature = "sync")]
pub type FnCustomSyntaxParse =
dyn Fn(&[ImmutableString]) -> Result<Option<ImmutableString>, ParseError> + Send + Sync;
dyn Fn(&[ImmutableString], &str) -> Result<Option<ImmutableString>, ParseError> + Send + Sync;
/// An expression sub-tree in an [`AST`][crate::AST].
#[derive(Debug, Clone)]
@@ -185,7 +185,7 @@ impl Engine {
self.register_custom_syntax_raw(
key,
// Construct the parsing function
move |stream| {
move |stream, _| {
if stream.len() >= segments.len() {
Ok(None)
} else {
@@ -213,7 +213,7 @@ impl Engine {
pub fn register_custom_syntax_raw(
&mut self,
key: impl Into<ImmutableString>,
parse: impl Fn(&[ImmutableString]) -> Result<Option<ImmutableString>, ParseError>
parse: impl Fn(&[ImmutableString], &str) -> Result<Option<ImmutableString>, ParseError>
+ SendSync
+ 'static,
new_vars: isize,