macro wip

This commit is contained in:
Michael Sippel 2024-02-27 03:37:09 +01:00
parent d7502e6af8
commit 5b45f164fb
Signed by: senvas
GPG key ID: F96CF119C34B64A6
4 changed files with 70 additions and 10 deletions

View file

@ -4,3 +4,9 @@ edition = "2018"
name = "laddertypes" name = "laddertypes"
version = "0.1.0" version = "0.1.0"
#[lib]
#proc-macro = true
[dependencies]
laddertype-macro = { path = "./laddertype-macro" }

View file

@ -22,8 +22,12 @@ pub enum ParseError {
impl TypeDict { impl TypeDict {
pub fn parse(&mut self, s: &str) -> Result<TypeTerm, ParseError> { pub fn parse(&mut self, s: &str) -> Result<TypeTerm, ParseError> {
let mut tokens = LadderTypeLexer::from(s.chars()).peekable(); let mut tokens = LadderTypeLexer::from(s.chars());
self.parse_tokens( tokens.peekable() )
}
pub fn parse_tokens<It>(&mut self, mut tokens: Peekable<It>) -> Result<TypeTerm, ParseError>
where It: Iterator<Item = Result<LadderTypeToken, LexError>> {
match self.parse_ladder(&mut tokens) { match self.parse_ladder(&mut tokens) {
Ok(t) => { Ok(t) => {
if let Some(_tok) = tokens.peek() { if let Some(_tok) = tokens.peek() {
@ -36,8 +40,8 @@ impl TypeDict {
} }
} }
fn parse_app<It>(&mut self, tokens: &mut Peekable<LadderTypeLexer<It>>) -> Result<TypeTerm, ParseError> fn parse_app<It>(&mut self, tokens: &mut Peekable<It>) -> Result<TypeTerm, ParseError>
where It: Iterator<Item = char> where It: Iterator<Item = Result<LadderTypeToken, LexError>>
{ {
let mut args = Vec::new(); let mut args = Vec::new();
while let Some(tok) = tokens.peek() { while let Some(tok) = tokens.peek() {
@ -57,8 +61,8 @@ impl TypeDict {
Err(ParseError::UnexpectedEnd) Err(ParseError::UnexpectedEnd)
} }
fn parse_rung<It>(&mut self, tokens: &mut Peekable<LadderTypeLexer<It>>) -> Result<TypeTerm, ParseError> fn parse_rung<It>(&mut self, tokens: &mut Peekable<It>) -> Result<TypeTerm, ParseError>
where It: Iterator<Item = char> where It: Iterator<Item = Result<LadderTypeToken, LexError>>
{ {
match tokens.next() { match tokens.next() {
Some(Ok(LadderTypeToken::Open)) => self.parse_app(tokens), Some(Ok(LadderTypeToken::Open)) => self.parse_app(tokens),
@ -79,8 +83,8 @@ impl TypeDict {
} }
} }
fn parse_ladder<It>(&mut self, tokens: &mut Peekable<LadderTypeLexer<It>>) -> Result<TypeTerm, ParseError> fn parse_ladder<It>(&mut self, tokens: &mut Peekable<It>) -> Result<TypeTerm, ParseError>
where It: Iterator<Item = char> where It: Iterator<Item = Result<LadderTypeToken, LexError>>
{ {
let mut rungs = Vec::new(); let mut rungs = Vec::new();

View file

@ -153,4 +153,3 @@ fn test_lexer_large() {
assert_eq!( lex.next(), None ); assert_eq!( lex.next(), None );
} }

View file

@ -204,3 +204,54 @@ fn test_parser_ladder_large() {
); );
} }
macro_rules! lt_tokenize {
($symbol:ident) => {
crate::lexer::LadderTypeToken::Symbol( "$symbol".into() )
}
(< $rest::tt) => {
crate::lexer::LadderTypeToken::Open,
lt_tokenize!($rest)
}
(> $rest::tt) => {
crate::lexer::LadderTypeToken::Close,
lt_tokenize!($rest)
}
(~ $rest::tt) => {
crate::lexer::LadderTypeToken::Ladder,
lt_tokenize!($rest)
}
}
macro_rules! lt_parse {
($dict:ident, $tokens:tt*) => {
$dict.parse_tokens(
vec![
lt_tokenize!($tokens)
].into_iter().peekable()
)
}
}
#[test]
fn test_proc_macro() {
use laddertype_macro::laddertype;
use crate::lexer::LadderTypeToken;
let mut dict = TypeDict::new();
let t1 = dict.parse_tokens(vec![
Ok(crate::lexer::LadderTypeToken::Open),
Ok(crate::lexer::LadderTypeToken::Symbol("Seq".into())),
Ok(crate::lexer::LadderTypeToken::Symbol("Char".into())),
Ok(crate::lexer::LadderTypeToken::Close)
].into_iter().peekable());
let t2 = dict.parse_tokens(vec![
lt_tokenize!{ <Seq Char> }
].into_iter().peekable());
//lt_parse!( dict, <Seq Char> );
assert_eq!(t1, t2);
}