mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
wip: compiles and runs (not enabled yet)
This commit is contained in:
parent
ac3827b8f3
commit
98d2107e4e
15 changed files with 440 additions and 357 deletions
58
edn/src/edn_token.rs
Normal file
58
edn/src/edn_token.rs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
use crate::*;
|
||||
|
||||
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
||||
pub enum Token<'a> {
|
||||
#[default] Nil,
|
||||
Num(&'a str, usize, usize),
|
||||
Sym(&'a str, usize, usize),
|
||||
Key(&'a str, usize, usize),
|
||||
Exp(&'a str, usize, usize, usize),
|
||||
}
|
||||
|
||||
impl<'a> Token<'a> {
|
||||
pub fn chomp (source: &'a str) -> Result<(&'a str, Self), ParseError> {
|
||||
use Token::*;
|
||||
let mut state = Self::default();
|
||||
for (index, c) in source.char_indices() {
|
||||
state = match state {
|
||||
// must begin expression
|
||||
Nil => match c {
|
||||
' '|'\n'|'\r'|'\t' => Nil,
|
||||
'(' => Exp(source, index, 1, 1),
|
||||
':' => Sym(source, index, 1),
|
||||
'0'..='9' => Num(source, index, 1),
|
||||
'a'..='z' => Key(source, index, 1),
|
||||
_ => return Err(ParseError::Unexpected(c))
|
||||
},
|
||||
Num(_, _, 0) => unreachable!(),
|
||||
Sym(_, _, 0) => unreachable!(),
|
||||
Key(_, _, 0) => unreachable!(),
|
||||
Num(source, index, length) => match c {
|
||||
'0'..='9' => Num(source, index, length + 1),
|
||||
' '|'\n'|'\r'|'\t' => return Ok((&source[index+length..], Num(source, index, length))),
|
||||
_ => return Err(ParseError::Unexpected(c))
|
||||
},
|
||||
Sym(source, index, length) => match c {
|
||||
'a'..='z'|'0'..='9'|'-' => Sym(source, index, length + 1),
|
||||
' '|'\n'|'\r'|'\t' => return Ok((&source[index+length..], Sym(source, index, length))),
|
||||
_ => return Err(ParseError::Unexpected(c))
|
||||
},
|
||||
Key(source, index, length) => match c {
|
||||
'a'..='z'|'0'..='9'|'-'|'/' => Key(source, index, length + 1),
|
||||
' '|'\n'|'\r'|'\t' => return Ok((&source[index+length..], Key(source, index, length))),
|
||||
_ => return Err(ParseError::Unexpected(c))
|
||||
},
|
||||
Exp(source, index, length, 0) => match c {
|
||||
' '|'\n'|'\r'|'\t' => return Ok((&source[index+length..], Exp(source, index, length, 0))),
|
||||
_ => return Err(ParseError::Unexpected(c))
|
||||
},
|
||||
Exp(source, index, length, depth) => match c {
|
||||
')' => Exp(source, index, length + 1, depth - 1),
|
||||
'(' => Exp(source, index, length + 1, depth + 1),
|
||||
_ => Exp(source, index, length + 1, depth)
|
||||
},
|
||||
}
|
||||
}
|
||||
Ok(("", state))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue