diff options
author | JJ | 2023-10-27 07:53:32 +0000 |
---|---|---|
committer | JJ | 2023-10-27 07:53:32 +0000 |
commit | f3efbfc03fabae11df0554e67769327d4dc57a83 (patch) | |
tree | 73c372eaa118015b0c0cefe28ecad4b1e1e88c03 /src/parse.rs | |
parent | 6017d62db7600af491592e4f0d78611f33dc6b5e (diff) |
compiler: basic outline of the parser
Diffstat (limited to 'src/parse.rs')
-rw-r--r-- | src/parse.rs | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/src/parse.rs b/src/parse.rs new file mode 100644 index 0000000..9a60863 --- /dev/null +++ b/src/parse.rs @@ -0,0 +1,101 @@ +use multipeek::*; +use crate::lex::*; +use crate::ast::Expr; +use crate::ast::Binding::*; +use crate::ast::Control::*; +use crate::ast::Pattern::*; + +/// Convert a basic TokenStream into an AbstractSyntaxTree +pub fn astify(input: TokenStream, name: &str) -> Result<Expr> { + use Token::*; + use Literal::*; + use Punctuation::*; + + let mut input = multipeek(input); + let mut res = Vec::new(); + while let Some(x) = input.peek() { + res.push(parse(&mut input, 0)?); + } + Ok(Expr::Binding(Module{ id: name.to_string(), body: res })) +} + +fn parse(input: &mut MultiPeek<std::vec::IntoIter<Token>>, depth: usize) -> Result<Expr> { + use Token::*; + use Literal::*; + use Punctuation::*; + let mut input = input; + match input.peek() { + Some(Word(val)) => match val.as_str() { + "pub" => { + input.next(); + if let Some(Word(val)) = input.peek() { + match val.as_str() { + "const" => parse_const(&mut input, true), + "func" => parse_func(&mut input, true), + "type" => parse_type(&mut input, true), + "mod" => parse_mod(&mut input, true), + _ => return Err("unrecognized keyword following pub".into()), + } + } else { + return Err("unrecognized thing following pub".into()); + } + }, + "let" => parse_let(&mut input), + "var" => parse_var(&mut input), + "const" => parse_const(&mut input, false), + "func" => parse_func(&mut input, false), + "type" => parse_type(&mut input, false), + "mod" => parse_mod(&mut input, false), + "from" | "import" => parse_import(&mut input), + "block" => parse_block(&mut input), + "static" => parse_static(&mut input), + "for" => parse_for(&mut input), + "while" => parse_while(&mut input), + "loop" => parse_loop(&mut input), + "if" => parse_if(&mut input), + "when" => parse_when(&mut input), + "try" => parse_try(&mut input), + "match" => parse_match(&mut input), + _ => parse_line(&mut input), + }, + _ => parse_line(&mut input), + } +} + +// Const ::= 'pub'? 'const' Pattern Annotation? '=' Expr +fn parse_const(input: &mut MultiPeek<std::vec::IntoIter<Token>>, public: bool) -> Result<Expr> { todo!() } +// Func ::= 'pub'? ('func' | 'proc') Ident Generics? Parameters? (':' TypeDesc) '=' Body +fn parse_func(input: &mut MultiPeek<std::vec::IntoIter<Token>>, public: bool) -> Result<Expr> { todo!() } +// TypeDecl ::= 'pub'? 'type' Pattern Generics? '=' 'distinct'? 'ref'? TypeDesc +fn parse_type(input: &mut MultiPeek<std::vec::IntoIter<Token>>, public: bool) -> Result<Expr> { todo!() } +// Mod ::= 'pub'? 'mod' Ident ':' Body +fn parse_mod(input: &mut MultiPeek<std::vec::IntoIter<Token>>, public: bool) -> Result<Expr> { todo!() } + +// Let ::= 'let' Pattern Annotation? '=' Expr +fn parse_let(input: &mut MultiPeek<std::vec::IntoIter<Token>>) -> Result<Expr> { todo!() } +// Var ::= 'var' Pattern Annotation? ('=' Expr)? +fn parse_var(input: &mut MultiPeek<std::vec::IntoIter<Token>>) -> Result<Expr> { todo!() } +// Import ::= ('from' Ident)? 'import' Ident (',' Ident)* ('as' Ident)? +fn parse_import(input: &mut MultiPeek<std::vec::IntoIter<Token>>) -> Result<Expr> { todo!() } +// Block ::= 'block' Ident? ':' Body +fn parse_block(input: &mut MultiPeek<std::vec::IntoIter<Token>>) -> Result<Expr> { todo!() } +// Static ::= 'static' ':' Body +fn parse_static(input: &mut MultiPeek<std::vec::IntoIter<Token>>) -> Result<Expr> { todo!() } +// For ::= 'for' Pattern 'in' Expr ':' Body +fn parse_for(input: &mut MultiPeek<std::vec::IntoIter<Token>>) -> Result<Expr> { todo!() } +// While ::= 'while' Expr ':' Body +fn parse_while(input: &mut MultiPeek<std::vec::IntoIter<Token>>) -> Result<Expr> { todo!() } +// Loop ::= 'loop' ':' Body +fn parse_loop(input: &mut MultiPeek<std::vec::IntoIter<Token>>) -> Result<Expr> { todo!() } +// If ::= 'if' Expr ':' Body ('elif' Expr ':' Body)* ('else' ':' Body)? +fn parse_if(input: &mut MultiPeek<std::vec::IntoIter<Token>>) -> Result<Expr> { todo!() } +// When ::= 'when' Expr ':' Body ('elif' Expr ':' Body)* ('else' ':' Body)? +fn parse_when(input: &mut MultiPeek<std::vec::IntoIter<Token>>) -> Result<Expr> { todo!() } +// Try ::= 'try' ':' Body ('except' Ident (',' Ident)* ':' Body) ('finally' ':' Body)? +fn parse_try(input: &mut MultiPeek<std::vec::IntoIter<Token>>) -> Result<Expr> { todo!() } +// Match ::= 'match' Expr ('of' Pattern (',' Pattern)* ('where' Expr)? ':' Body)+ +fn parse_match(input: &mut MultiPeek<std::vec::IntoIter<Token>>) -> Result<Expr> { todo!() } + +fn parse_line(input: &mut MultiPeek<std::vec::IntoIter<Token>>) -> Result<Expr> { todo!() } + +// lex, parse, expand, compile? |