dsl: actually test it
Some checks failed
/ build (push) Has been cancelled

and decide how exprs gon work once and for all
This commit is contained in:
🪞👃🪞 2025-07-31 22:33:59 +03:00
parent 9e0b7be9a9
commit 9aa589e39d
3 changed files with 152 additions and 54 deletions

View file

@ -1,32 +1,33 @@
use crate::*;
macro_rules! iter_chars(($source:expr => |$i:ident, $c:ident|$val:expr)=>{
while let Some((($i, $c), next)) = char_indices($source).next() {
$source = next.as_str(); $val } });
macro_rules! def_peek_seek(($peek:ident, $seek:ident, $seek_start:ident, $seek_length:ident)=>{
/// Find a slice corrensponding to a syntax token.
pub const fn $peek (source: &str) -> DslPerhaps<&str> {
match $seek(source) {
Ok(Some((start, length))) => Ok(Some(str_range(source, start, start + length))),
Ok(None) => Ok(None),
Err(e) => Err(e) } }
Err(e) => Err(e)
}
}
/// Find a start and length corresponding to a syntax token.
pub const fn $seek (source: &str) -> DslPerhaps<(usize, usize)> {
match $seek_start(source) {
Ok(Some(start)) => match $seek_length(str_range(source, start, source.len() - start)) {
Ok(Some(start)) => match $seek_length(str_from(source, start)) {
Ok(Some(length)) => Ok(Some((start, length))),
Ok(None) => Ok(None),
Err(e) => Err(e),
},
Ok(None) => Ok(None),
Err(e) => Err(e) } } });
Err(e) => Err(e)
}
}
});
def_peek_seek!(exp_peek, exp_seek, exp_seek_start, exp_seek_length);
pub const fn is_exp_start (c: char) -> bool { matches!(c, '(') }
pub const fn is_exp_end (c: char) -> bool { matches!(c, ')') }
pub const fn is_exp_start (c: char) -> bool { c == '(' }
pub const fn is_exp_end (c: char) -> bool { c == ')' }
pub const fn exp_seek_start (mut source: &str) -> DslPerhaps<usize> {
iter_chars!(source => |i, c| if is_exp_start(c) {
for_each!((i, c) in char_indices(source) => if is_exp_start(c) {
return Ok(Some(i))
} else if !is_whitespace(c) {
return Err(Unexpected(c))
@ -35,18 +36,18 @@ pub const fn exp_seek_start (mut source: &str) -> DslPerhaps<usize> {
}
pub const fn exp_seek_length (mut source: &str) -> DslPerhaps<usize> {
let mut depth = 0;
iter_chars!(source => |i, c| if is_exp_start(c) {
for_each!((i, c) in char_indices(source) => if is_exp_start(c) {
depth += 1;
} else if is_exp_end(c) {
if depth == 0 {
return Err(Unexpected(c))
} else if depth == 1 {
return Ok(Some(i))
return Ok(Some(i + 1))
} else {
depth -= 1;
}
});
Ok(None)
Err(Incomplete)
}
def_peek_seek!(sym_peek, sym_seek, sym_seek_start, sym_seek_length);
@ -54,7 +55,7 @@ pub const fn is_sym_start (c: char) -> bool { matches!(c, ':'|'@') }
pub const fn is_sym_char (c: char) -> bool { matches!(c, 'a'..='z'|'A'..='Z'|'0'..='9'|'-') }
pub const fn is_sym_end (c: char) -> bool { matches!(c, ' '|'\n'|'\r'|'\t'|')') }
pub const fn sym_seek_start (mut source: &str) -> DslPerhaps<usize> {
iter_chars!(source => |i, c| if is_sym_start(c) {
for_each!((i, c) in char_indices(source) => if is_sym_start(c) {
return Ok(Some(i))
} else if !is_whitespace(c) {
return Err(Unexpected(c))
@ -62,20 +63,20 @@ pub const fn sym_seek_start (mut source: &str) -> DslPerhaps<usize> {
Ok(None)
}
pub const fn sym_seek_length (mut source: &str) -> DslPerhaps<usize> {
iter_chars!(source => |i, c| if is_sym_end(c) {
for_each!((i, c) in char_indices(source) => if is_sym_end(c) {
return Ok(Some(i))
} else if !is_sym_char(c) {
return Err(Unexpected(c))
});
Ok(None)
Ok(Some(source.len()))
}
def_peek_seek!(key_peek, key_seek, key_seek_start, key_seek_length);
pub const fn is_key_start (c: char) -> bool { matches!(c, '/'|'a'..='z') }
pub const fn is_key_start (c: char) -> bool { matches!(c, '/'|('a'..='z')) }
pub const fn is_key_char (c: char) -> bool { matches!(c, 'a'..='z'|'0'..='9'|'-'|'/') }
pub const fn is_key_end (c: char) -> bool { matches!(c, ' '|'\n'|'\r'|'\t'|')') }
pub const fn key_seek_start (mut source: &str) -> DslPerhaps<usize> {
iter_chars!(source => |i, c| if is_key_start(c) {
for_each!((i, c) in char_indices(source) => if is_key_start(c) {
return Ok(Some(i))
} else if !is_whitespace(c) {
return Err(Unexpected(c))
@ -83,19 +84,19 @@ pub const fn key_seek_start (mut source: &str) -> DslPerhaps<usize> {
Ok(None)
}
pub const fn key_seek_length (mut source: &str) -> DslPerhaps<usize> {
iter_chars!(source => |i, c| if is_key_end(c) {
for_each!((i, c) in char_indices(source) => if is_key_end(c) {
return Ok(Some(i))
} else if !is_key_char(c) {
return Err(Unexpected(c))
});
Ok(None)
Ok(Some(source.len()))
}
def_peek_seek!(text_peek, text_seek, text_seek_start, text_seek_length);
pub const fn is_text_start (c: char) -> bool { matches!(c, '"') }
pub const fn is_text_end (c: char) -> bool { matches!(c, '"') }
pub const fn text_seek_start (mut source: &str) -> DslPerhaps<usize> {
iter_chars!(source => |i, c| if is_text_start(c) {
for_each!((i, c) in char_indices(source) => if is_text_start(c) {
return Ok(Some(i))
} else if !is_whitespace(c) {
return Err(Unexpected(c))
@ -103,13 +104,13 @@ pub const fn text_seek_start (mut source: &str) -> DslPerhaps<usize> {
Ok(None)
}
pub const fn text_seek_length (mut source: &str) -> DslPerhaps<usize> {
iter_chars!(source => |i, c| if is_text_end(c) { return Ok(Some(i)) });
for_each!((i, c) in char_indices(source) => if is_text_end(c) { return Ok(Some(i)) });
Ok(None)
}
def_peek_seek!(num_peek, num_seek, num_seek_start, num_seek_length);
pub const fn num_seek_start (mut source: &str) -> DslPerhaps<usize> {
iter_chars!(source => |i, c| if is_digit(c) {
for_each!((i, c) in char_indices(source) => if is_digit(c) {
return Ok(Some(i));
} else if !is_whitespace(c) {
return Err(Unexpected(c))
@ -117,7 +118,7 @@ pub const fn num_seek_start (mut source: &str) -> DslPerhaps<usize> {
Ok(None)
}
pub const fn num_seek_length (mut source: &str) -> DslPerhaps<usize> {
iter_chars!(source => |i, c| if is_num_end(c) {
for_each!((i, c) in char_indices(source) => if is_num_end(c) {
return Ok(Some(i))
} else if !is_digit(c) {
return Err(Unexpected(c))
@ -154,7 +155,7 @@ pub const fn peek (mut src: &str) -> DslPerhaps<&str> {
_ if let Ok(Some(num)) = num_peek(src) => num,
_ if let Ok(Some(text)) = text_peek(src) => text,
_ => {
iter_chars!(src => |_i, c| if !is_whitespace(c) {
for_each!((i, c) in char_indices(src) => if !is_whitespace(c) {
return Err(Unexpected(c))
});
return Ok(None)
@ -170,7 +171,7 @@ pub const fn seek (mut src: &str) -> DslPerhaps<(usize, usize)> {
_ if let Ok(Some(num)) = num_seek(src) => num,
_ if let Ok(Some(text)) = text_seek(src) => text,
_ => {
iter_chars!(src => |_i, c| if !is_whitespace(c) {
for_each!((i, c) in char_indices(src) => if !is_whitespace(c) {
return Err(Unexpected(c))
});
return Ok(None)