This commit is contained in:
nora 2022-03-25 20:57:30 +01:00
parent 632f9d3426
commit 8a52378d4d
3 changed files with 42 additions and 20 deletions

View file

@ -3,6 +3,10 @@
use std::path::PathBuf; use std::path::PathBuf;
use logos::Logos;
use crate::lexer::Token;
mod ast; mod ast;
mod lexer; mod lexer;
mod parser; mod parser;
@ -10,3 +14,17 @@ mod parser;
pub fn parse(_str: &str, _file_name: PathBuf) -> Result<ast::File, ()> { pub fn parse(_str: &str, _file_name: PathBuf) -> Result<ast::File, ()> {
todo!() todo!()
} }
pub fn test() {
let lexer = Token::lexer(
"fn foo() {
1 + 5;
struct test {}
}",
);
let len = lexer.source().len();
let r = parser::parse(lexer.spanned(), len, "test_file".into());
println!("{r:#?}");
}

View file

@ -19,7 +19,6 @@ fn ident_parser<'src>() -> impl Parser<Token<'src>, String, Error = Error<'src>>
_ => Err(Simple::expected_input_found(span, Vec::new(), Some(tok))), _ => Err(Simple::expected_input_found(span, Vec::new(), Some(tok))),
}) })
.labelled("identifier") .labelled("identifier")
.boxed()
} }
fn ty_parser<'src>() -> impl Parser<Token<'src>, Ty, Error = Error<'src>> + Clone { fn ty_parser<'src>() -> impl Parser<Token<'src>, Ty, Error = Error<'src>> + Clone {
@ -31,7 +30,7 @@ fn ty_parser<'src>() -> impl Parser<Token<'src>, Ty, Error = Error<'src>> + Clon
Ok(Ty { span, kind }) Ok(Ty { span, kind })
}) })
.boxed() .labelled("type")
} }
fn expr_parser<'src>() -> impl Parser<Token<'src>, Expr, Error = Error<'src>> + Clone { fn expr_parser<'src>() -> impl Parser<Token<'src>, Expr, Error = Error<'src>> + Clone {
@ -128,8 +127,7 @@ fn expr_parser<'src>() -> impl Parser<Token<'src>, Expr, Error = Error<'src>> +
span: 0..0, // lol todo span: 0..0, // lol todo
}) })
}); });
compare.labelled("comparison")
compare.boxed()
}) })
} }
@ -194,7 +192,7 @@ fn function_parser<'src>(
.then(params) .then(params)
.then(ret_ty) .then(ret_ty)
.then( .then(
statement_parser(item.clone()) statement_parser(item)
.repeated() .repeated()
.delimited_by(just(Token::BraceO), just(Token::BraceC)), .delimited_by(just(Token::BraceO), just(Token::BraceC)),
) )
@ -215,11 +213,13 @@ fn struct_parser<'src>() -> impl Parser<Token<'src>, StructDecl, Error = Error<'
.separated_by(just(Token::Comma)) .separated_by(just(Token::Comma))
.delimited_by(just(Token::BraceO), just(Token::BraceC)); .delimited_by(just(Token::BraceO), just(Token::BraceC));
name.then(fields).map(|(name, fields)| StructDecl { name.then(fields)
name, .map(|(name, fields)| StructDecl {
fields, name,
span: Default::default(), fields,
}) span: Default::default(),
})
.labelled("struct")
} }
fn item_parser<'src>() -> impl Parser<Token<'src>, Item, Error = Error<'src>> + Clone { fn item_parser<'src>() -> impl Parser<Token<'src>, Item, Error = Error<'src>> + Clone {
@ -228,15 +228,19 @@ fn item_parser<'src>() -> impl Parser<Token<'src>, Item, Error = Error<'src>> +
.map(Item::FnDecl) .map(Item::FnDecl)
.or(struct_parser().map(Item::StructDecl)) .or(struct_parser().map(Item::StructDecl))
}) })
.labelled("item")
} }
fn file_parser<'src>( fn file_parser<'src>(
file_name: PathBuf, file_name: PathBuf,
) -> impl Parser<Token<'src>, File, Error = Error<'src>> + Clone { ) -> impl Parser<Token<'src>, File, Error = Error<'src>> + Clone {
item_parser().repeated().map(move |items| File { item_parser()
name: file_name.clone(), .repeated()
items, .map(move |items| File {
}) name: file_name.clone(),
items,
})
.labelled("file")
} }
pub fn parse<'src, I>(lexer: I, len: usize, file_name: PathBuf) -> (Option<File>, Vec<Error<'src>>) pub fn parse<'src, I>(lexer: I, len: usize, file_name: PathBuf) -> (Option<File>, Vec<Error<'src>>)
@ -284,11 +288,11 @@ mod tests {
insta::assert_debug_snapshot!(r) insta::assert_debug_snapshot!(r)
} }
#[test] //#[test]
fn nested_function() { //fn nested_function() {
let r = parse("fn foo() { fn foo2() {} fn foo3() {} }"); // let r = parse("fn foo() { fn foo2() {} fn foo3() {} }");
insta::assert_debug_snapshot!(r) // insta::assert_debug_snapshot!(r)
} //}
#[test] #[test]
fn nested_function2() { fn nested_function2() {

View file

@ -1,3 +1,3 @@
fn main() { fn main() {
println!("Hello, world!"); parser::test();
} }