mirror of
https://github.com/Noratrieb/ub.git
synced 2026-01-14 16:45:05 +01:00
things
This commit is contained in:
parent
632f9d3426
commit
8a52378d4d
3 changed files with 42 additions and 20 deletions
|
|
@ -3,6 +3,10 @@
|
|||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use logos::Logos;
|
||||
|
||||
use crate::lexer::Token;
|
||||
|
||||
mod ast;
|
||||
mod lexer;
|
||||
mod parser;
|
||||
|
|
@ -10,3 +14,17 @@ mod parser;
|
|||
pub fn parse(_str: &str, _file_name: PathBuf) -> Result<ast::File, ()> {
|
||||
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:#?}");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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))),
|
||||
})
|
||||
.labelled("identifier")
|
||||
.boxed()
|
||||
}
|
||||
|
||||
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 })
|
||||
})
|
||||
.boxed()
|
||||
.labelled("type")
|
||||
}
|
||||
|
||||
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
|
||||
})
|
||||
});
|
||||
|
||||
compare.boxed()
|
||||
compare.labelled("comparison")
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -194,7 +192,7 @@ fn function_parser<'src>(
|
|||
.then(params)
|
||||
.then(ret_ty)
|
||||
.then(
|
||||
statement_parser(item.clone())
|
||||
statement_parser(item)
|
||||
.repeated()
|
||||
.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))
|
||||
.delimited_by(just(Token::BraceO), just(Token::BraceC));
|
||||
|
||||
name.then(fields).map(|(name, fields)| StructDecl {
|
||||
name,
|
||||
fields,
|
||||
span: Default::default(),
|
||||
})
|
||||
name.then(fields)
|
||||
.map(|(name, fields)| StructDecl {
|
||||
name,
|
||||
fields,
|
||||
span: Default::default(),
|
||||
})
|
||||
.labelled("struct")
|
||||
}
|
||||
|
||||
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)
|
||||
.or(struct_parser().map(Item::StructDecl))
|
||||
})
|
||||
.labelled("item")
|
||||
}
|
||||
|
||||
fn file_parser<'src>(
|
||||
file_name: PathBuf,
|
||||
) -> impl Parser<Token<'src>, File, Error = Error<'src>> + Clone {
|
||||
item_parser().repeated().map(move |items| File {
|
||||
name: file_name.clone(),
|
||||
items,
|
||||
})
|
||||
item_parser()
|
||||
.repeated()
|
||||
.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>>)
|
||||
|
|
@ -284,11 +288,11 @@ mod tests {
|
|||
insta::assert_debug_snapshot!(r)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_function() {
|
||||
let r = parse("fn foo() { fn foo2() {} fn foo3() {} }");
|
||||
insta::assert_debug_snapshot!(r)
|
||||
}
|
||||
//#[test]
|
||||
//fn nested_function() {
|
||||
// let r = parse("fn foo() { fn foo2() {} fn foo3() {} }");
|
||||
// insta::assert_debug_snapshot!(r)
|
||||
//}
|
||||
|
||||
#[test]
|
||||
fn nested_function2() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue