mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 19:56:44 +01:00
parent
08a8dff93d
commit
cb47c4d0ff
11 changed files with 513 additions and 492 deletions
|
|
@ -1,12 +1,183 @@
|
|||
use crate::*;
|
||||
|
||||
/// CST stores strings as source references and expressions as new [SourceIter] instances.
|
||||
pub type CstValue<'source> = Value<&'source str, SourceIter<'source>>;
|
||||
/// Token sharing memory with source reference.
|
||||
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
||||
pub struct CstToken<'source>(pub CstValue<'source>, pub CstMeta<'source>);
|
||||
|
||||
/// Reference to the source slice.
|
||||
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct CstMeta<'source> {
|
||||
pub source: &'source str,
|
||||
pub start: usize,
|
||||
pub length: usize,
|
||||
}
|
||||
|
||||
/// Token sharing memory with source reference.
|
||||
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
||||
pub struct CstToken<'source>(pub CstValue<'source>, pub CstMeta<'source>);
|
||||
|
||||
impl<'source> CstToken<'source> {
|
||||
pub const fn new (
|
||||
source: &'source str, start: usize, length: usize, value: CstValue<'source>
|
||||
) -> Self {
|
||||
Self(value, CstMeta { source, start, length })
|
||||
}
|
||||
pub const fn end (&self) -> usize {
|
||||
self.1.start.saturating_add(self.1.length)
|
||||
}
|
||||
pub const fn slice (&'source self) -> &'source str {
|
||||
self.slice_source(self.1.source)
|
||||
}
|
||||
pub const fn slice_source <'range> (&'source self, source: &'range str) -> &'range str {
|
||||
str_range(source, self.1.start, self.end())
|
||||
}
|
||||
pub const fn slice_source_exp <'range> (&'source self, source: &'range str) -> &'range str {
|
||||
str_range(source, self.1.start.saturating_add(1), self.end())
|
||||
}
|
||||
pub const fn with_value (self, value: CstValue<'source>) -> Self {
|
||||
Self(value, self.1)
|
||||
}
|
||||
pub const fn value (&self) -> CstValue<'source> {
|
||||
self.0
|
||||
}
|
||||
pub const fn error (self, error: DslError) -> Self {
|
||||
Self(Value::Err(error), self.1)
|
||||
}
|
||||
pub const fn grow (self) -> Self {
|
||||
Self(self.0, CstMeta { length: self.1.length.saturating_add(1), ..self.1 })
|
||||
}
|
||||
pub const fn grow_num (self, m: usize, c: char) -> Self {
|
||||
use Value::*;
|
||||
match to_digit(c) {
|
||||
Result::Ok(n) => Self(Num(10*m+n), self.grow().1),
|
||||
Result::Err(e) => Self(Err(e), self.grow().1),
|
||||
}
|
||||
}
|
||||
pub const fn grow_key (self) -> Self {
|
||||
use Value::*;
|
||||
let token = self.grow();
|
||||
token.with_value(Key(token.slice_source(self.1.source)))
|
||||
}
|
||||
pub const fn grow_sym (self) -> Self {
|
||||
use Value::*;
|
||||
let token = self.grow();
|
||||
token.with_value(Sym(token.slice_source(self.1.source)))
|
||||
}
|
||||
pub const fn grow_str (self) -> Self {
|
||||
use Value::*;
|
||||
let token = self.grow();
|
||||
token.with_value(Str(token.slice_source(self.1.source)))
|
||||
}
|
||||
pub const fn grow_exp (self) -> Self {
|
||||
use Value::*;
|
||||
let token = self.grow();
|
||||
if let Exp(depth, _) = token.value() {
|
||||
token.with_value(Exp(depth, SourceIter::new(token.slice_source_exp(self.1.source))))
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
pub const fn grow_in (self) -> Self {
|
||||
let token = self.grow_exp();
|
||||
if let Value::Exp(depth, source) = token.value() {
|
||||
token.with_value(Value::Exp(depth.saturating_add(1), source))
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
pub const fn grow_out (self) -> Self {
|
||||
let token = self.grow_exp();
|
||||
if let Value::Exp(depth, source) = token.value() {
|
||||
if depth > 0 {
|
||||
token.with_value(Value::Exp(depth - 1, source))
|
||||
} else {
|
||||
return self.error(Unexpected(')'))
|
||||
}
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn to_number (digits: &str) -> DslResult<usize> {
|
||||
let mut value = 0;
|
||||
iterate!(char_indices(digits) => (_, c) => match to_digit(c) {
|
||||
Ok(digit) => value = 10 * value + digit,
|
||||
Result::Err(e) => return Result::Err(e)
|
||||
});
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
pub const fn to_digit (c: char) -> DslResult<usize> {
|
||||
Ok(match c {
|
||||
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
|
||||
'5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
|
||||
_ => return Result::Err(Unexpected(c))
|
||||
})
|
||||
}
|
||||
|
||||
/// Static iteration helper.
|
||||
#[macro_export] macro_rules! iterate {
|
||||
($expr:expr => $arg: pat => $body:expr) => {
|
||||
let mut iter = $expr;
|
||||
while let Some(($arg, next)) = iter.next() {
|
||||
$body;
|
||||
iter = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn peek_src <'a> (source: &'a str) -> Option<CstToken<'a>> {
|
||||
use Value::*;
|
||||
let mut token: CstToken<'a> = CstToken::new(source, 0, 0, Nil);
|
||||
iterate!(char_indices(source) => (start, c) => token = match token.value() {
|
||||
Err(_) => return Some(token),
|
||||
Nil => match c {
|
||||
' '|'\n'|'\r'|'\t' =>
|
||||
token.grow(),
|
||||
'(' =>
|
||||
CstToken::new(source, start, 1, Exp(1, SourceIter::new(str_range(source, start, start + 1)))),
|
||||
'"' =>
|
||||
CstToken::new(source, start, 1, Str(str_range(source, start, start + 1))),
|
||||
':'|'@' =>
|
||||
CstToken::new(source, start, 1, Sym(str_range(source, start, start + 1))),
|
||||
'/'|'a'..='z' =>
|
||||
CstToken::new(source, start, 1, Key(str_range(source, start, start + 1))),
|
||||
'0'..='9' =>
|
||||
CstToken::new(source, start, 1, match to_digit(c) {
|
||||
Ok(c) => Value::Num(c),
|
||||
Result::Err(e) => Value::Err(e)
|
||||
}),
|
||||
_ => token.error(Unexpected(c))
|
||||
},
|
||||
Str(_) => match c {
|
||||
'"' => return Some(token),
|
||||
_ => token.grow_str(),
|
||||
},
|
||||
Num(n) => match c {
|
||||
'0'..='9' => token.grow_num(n, c),
|
||||
' '|'\n'|'\r'|'\t'|')' => return Some(token),
|
||||
_ => token.error(Unexpected(c))
|
||||
},
|
||||
Sym(_) => match c {
|
||||
'a'..='z'|'A'..='Z'|'0'..='9'|'-' => token.grow_sym(),
|
||||
' '|'\n'|'\r'|'\t'|')' => return Some(token),
|
||||
_ => token.error(Unexpected(c))
|
||||
},
|
||||
Key(_) => match c {
|
||||
'a'..='z'|'0'..='9'|'-'|'/' => token.grow_key(),
|
||||
' '|'\n'|'\r'|'\t'|')' => return Some(token),
|
||||
_ => token.error(Unexpected(c))
|
||||
},
|
||||
Exp(depth, _) => match depth {
|
||||
0 => return Some(token.grow_exp()),
|
||||
_ => match c {
|
||||
')' => token.grow_out(),
|
||||
'(' => token.grow_in(),
|
||||
_ => token.grow_exp(),
|
||||
}
|
||||
},
|
||||
});
|
||||
match token.value() {
|
||||
Nil => None,
|
||||
_ => Some(token),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue