Rotth is a stack based concatenative language highly inspired by Porth
0

Configure Feed

Select the types of activity you want to include in your feed.

Parser work

Ivan Chinenov (Apr 1, 2024, 10:41 PM +0300) 574b43b0 cba8e176

+670 -432
+1 -1
itempath/src/lib.rs
··· 88 88 path 89 89 }}; 90 90 () => {{ 91 - ItemPathBuf::new() 91 + $crate::ItemPathBuf::new() 92 92 }}; 93 93 } 94 94
+12 -12
rotth-parser/src/ast.rs
··· 53 53 inner: Word(name), 54 54 }, 55 55 .. 56 - }) => name.clone(), 56 + }) => *name, 57 57 TopLevel::Const(Const { 58 58 name: 59 59 Spanned { ··· 61 61 inner: Word(name), 62 62 }, 63 63 .. 64 - }) => name.clone(), 64 + }) => *name, 65 65 TopLevel::Var(Var { 66 66 name: 67 67 Spanned { ··· 69 69 inner: Word(name), 70 70 }, 71 71 .. 72 - }) => name.clone(), 72 + }) => *name, 73 73 TopLevel::Struct(Struct { 74 74 name: 75 75 Spanned { ··· 77 77 inner: Word(name), 78 78 }, 79 79 .. 80 - }) => name.clone(), 80 + }) => *name, 81 81 TopLevel::Use(Use { 82 82 use_: _, 83 83 name: ··· 87 87 }, 88 88 from: _, 89 89 path: _, 90 - }) => name.clone(), 90 + }) => *name, 91 91 TopLevel::Module(ModuleDef { 92 92 module: _, 93 93 name: ··· 95 95 span: _, 96 96 inner: Word(name), 97 97 }, 98 - }) => name.clone(), 98 + }) => *name, 99 99 } 100 100 } 101 101 } ··· 512 512 }, 513 513 } in root.uses 514 514 { 515 - let name = name.inner.0.clone(); 515 + let name = name.inner.0; 516 516 let mut path = path.inner; 517 517 path.push(name); 518 518 if let Some(Spanned { span: _, inner: _ }) = ast.find(&path) { ··· 534 534 } 535 535 536 536 for Spanned { span, inner: proc } in root.procs { 537 - let name = proc.name.inner.0.clone(); 537 + let name = proc.name.inner.0; 538 538 let resolved = ResolvedItem::Proc(Rc::new(proc)); 539 539 540 540 if let Some(redefined) = ast.ast.get(&name) { ··· 558 558 inner: const_, 559 559 } in root.consts 560 560 { 561 - let name = const_.name.inner.0.clone(); 561 + let name = const_.name.inner.0; 562 562 let resolved = ResolvedItem::Const(Rc::new(const_)); 563 563 564 564 if let Some(redefined) = ast.ast.get(&name) { ··· 578 578 } 579 579 580 580 for Spanned { span, inner: var } in root.vars { 581 - let name = var.name.inner.0.clone(); 581 + let name = var.name.inner.0; 582 582 let resolved = ResolvedItem::Var(Rc::new(var)); 583 583 584 584 if let Some(redefined) = ast.ast.get(&name) { ··· 602 602 inner: struct_, 603 603 } in root.structs 604 604 { 605 - let name = struct_.name.inner.0.clone(); 605 + let name = struct_.name.inner.0; 606 606 let resolved = match make_struct(struct_) { 607 607 Ok(s) => ResolvedItem::Struct(Rc::new(s)), 608 608 Err(mut es) => { ··· 646 646 redefined_item: redefined.span, 647 647 })) 648 648 } else { 649 - fields.insert(name.clone(), field); 649 + fields.insert(*name, field); 650 650 } 651 651 } 652 652 let mut generics = Vec::new();
slate-frontend/src/hir.rs

This is a binary file and will not be displayed.

+21 -13
slate-frontend/src/lexer.rs
··· 26 26 } 27 27 28 28 fn to_interned_string(l: &'_ mut Lexer<'_, Token>) -> Intern<String> { 29 - Intern::new(l.slice().to_string()) 29 + Intern::from_ref(l.slice()) 30 30 } 31 31 32 32 fn to_smol_str(l: &'_ mut Lexer<'_, Token>) -> SmolStr { ··· 96 96 KwIf, 97 97 #[token("else")] 98 98 KwElse, 99 - #[token("proc")] 100 - KwProc, 99 + #[token("func")] 100 + KwFunc, 101 101 #[token("while")] 102 102 KwWhile, 103 - #[token("do")] 104 - KwDo, 103 + #[token("lambda")] 104 + KwLambda, 105 105 #[token("const")] 106 106 KwConst, 107 107 #[token("static")] ··· 110 110 KwLet, 111 111 #[token("struct")] 112 112 KwStruct, 113 - #[token("cast")] 114 - KwCast, 113 + #[token("impl")] 114 + KwImpl, 115 + #[token("for")] 116 + KwFor, 115 117 116 118 #[regex(r";.*\n", logos::skip)] 117 119 Comment, ··· 158 160 Token::KwMatch => write!(f, "match"), 159 161 Token::KwIf => write!(f, "if"), 160 162 Token::KwElse => write!(f, "else"), 161 - Token::KwProc => write!(f, "proc"), 163 + Token::KwFunc => write!(f, "func"), 162 164 Token::KwWhile => write!(f, "while"), 163 - Token::KwDo => write!(f, "do"), 165 + Token::KwFor => write!(f, "for"), 166 + Token::KwLambda => write!(f, "lambda"), 164 167 Token::KwConst => write!(f, "const"), 165 168 Token::KwStatic => write!(f, "static"), 166 169 Token::KwLet => write!(f, "let"), 167 170 Token::KwStruct => write!(f, "struct"), 168 - Token::KwCast => write!(f, "cast"), 171 + Token::KwImpl => write!(f, "impl"), 172 + 169 173 Token::Comment => write!(f, "<comment>"), 170 174 Token::Indent(level) => write!(f, "<indent-{}>", level), 171 175 Token::Whitespace => write!(f, "<whitespace>"), ··· 180 184 } 181 185 } 182 186 183 - pub fn lex(src: &str, path: Intern<PathBuf>) -> Vec<(Token, Span)> { 184 - Token::lexer(src) 187 + pub fn lex(src: &str, path: Intern<PathBuf>) -> (Vec<(Token, Span)>, Span) { 188 + let tt: Vec<_> = Token::lexer(src) 185 189 .spanned() 186 190 .map(|(t, s)| match t { 187 191 Ok(t) => (t, Span::new(path, s.start, s.end)), 188 192 Err(()) => (Token::Error, Span::new(path, s.start, s.end)), 189 193 }) 190 - .collect() 194 + .collect(); 195 + let eoi = tt 196 + .last() 197 + .map_or(Span::point(path, 0), |(_, s)| Span::point(path, s.end)); 198 + (tt, eoi) 191 199 }
+1
slate-frontend/src/lib.rs
··· 1 1 #![feature(box_patterns)] 2 2 3 + pub mod hir; 3 4 pub mod lexer; 4 5 pub mod parser; 5 6
+2 -2
slate-frontend/src/main.rs
··· 11 11 fn main() -> std::io::Result<()> { 12 12 let args = Args::parse(); 13 13 let src = std::fs::read_to_string(&args.source)?; 14 - let tokens = slate_frontend::lexer::lex(&src, Intern::new(args.source)); 14 + let (tokens, eoi) = slate_frontend::lexer::lex(&src, Intern::new(args.source)); 15 15 println!("{tokens:?}"); 16 - let ast = slate_frontend::parser::parse(tokens).unwrap(); 16 + let ast = slate_frontend::parser::parse(tokens, eoi).unwrap(); 17 17 dbg!(ast); 18 18 19 19 Ok(())
+45 -6
slate-frontend/src/parser.rs
··· 2 2 input::{Input, Stream}, 3 3 Parser, 4 4 }; 5 - use spanner::Span; 5 + use internment::Intern; 6 + use itempath::ItemPathBuf; 7 + use spanner::{Span, Spanned}; 6 8 7 - use crate::{lexer::Token, ParserError}; 9 + use crate::{ 10 + lexer::{self, Token}, 11 + ParserError, 12 + }; 8 13 9 14 pub use self::ast::Module; 15 + use self::ast::{ModuleDef, Word}; 10 16 11 17 mod ast; 12 18 mod parsers; 13 19 mod types; 14 20 15 - pub fn parse(tokens: Vec<(Token, Span)>) -> Result<Module, ParserError<'static>> { 16 - let len = tokens.len(); 17 - let path = tokens.first().unwrap().1.file; 21 + pub fn parse(tokens: Vec<(Token, Span)>, eoi: Span) -> Result<Module, ParserError<'static>> { 18 22 parsers::file() 19 - .parse(Stream::from_iter(tokens).spanned(Span::point(path, len))) 23 + .parse(Stream::from_iter(tokens).spanned(eoi)) 20 24 .into_result() 21 25 .map_err(|e| e.into()) 22 26 } 27 + 28 + pub fn resolve_modules(root: Module, path: ItemPathBuf) -> Result<(), ParserError<'static>> { 29 + let mut errors = ParserError(Vec::new()); 30 + for Spanned { 31 + span, 32 + inner: 33 + ModuleDef { 34 + module: _, 35 + name: 36 + Spanned { 37 + span: _, 38 + inner: Word(name), 39 + }, 40 + }, 41 + } in root.modules 42 + { 43 + let mut file = (*span.file).clone(); 44 + file.push(&**name); 45 + file.set_extension("sl"); 46 + 47 + let Ok(src) = std::fs::read_to_string(&file) else { 48 + errors.0.push(crate::Error::UnresolvedInclude(span)); 49 + continue; 50 + }; 51 + let (tokens, eoi) = lexer::lex(&src, Intern::from_ref(&file)); 52 + let mut path = path.clone(); 53 + path.push(name); 54 + let module = parse(tokens, eoi)?; 55 + let module = match resolve_modules(module, path) { 56 + Ok(module) => module, 57 + Err(es) => errors.0.extend(es.0), 58 + }; 59 + } 60 + Ok(()) 61 + }
+148 -125
slate-frontend/src/parser/ast.rs
··· 1 - use std::fmt::Debug; 1 + use std::{collections::HashMap, fmt::Debug}; 2 2 3 + use super::types::Type; 3 4 use internment::Intern; 4 5 use itempath::ItemPathBuf; 5 6 use spanner::Spanned; 6 7 7 - use crate::lexer::Token; 8 + #[derive(Debug, Clone)] 9 + pub struct Ast { 10 + pub modules: HashMap<ItemPathBuf, ResolvedModule>, 11 + } 8 12 9 - use super::types::Type; 13 + #[derive(Debug, Clone)] 14 + pub struct ResolvedModule { 15 + pub funcs: Vec<Spanned<Func>>, 16 + pub consts: Vec<Spanned<Const>>, 17 + pub statics: Vec<Spanned<Static>>, 18 + pub structs: Vec<Spanned<Struct>>, 19 + pub inherent_impls: Vec<Spanned<InherentImpl>>, 20 + pub trait_impls: Vec<Spanned<TraitImpl>>, 21 + pub uses: Vec<Spanned<Use>>, 22 + } 10 23 11 24 #[derive(Debug, Clone)] 12 25 pub struct Module { 13 - pub procs: Vec<Spanned<Proc>>, 26 + pub funcs: Vec<Spanned<Func>>, 14 27 pub consts: Vec<Spanned<Const>>, 15 - pub vars: Vec<Spanned<Static>>, 28 + pub statics: Vec<Spanned<Static>>, 16 29 pub structs: Vec<Spanned<Struct>>, 30 + pub inherent_impls: Vec<Spanned<InherentImpl>>, 31 + pub trait_impls: Vec<Spanned<TraitImpl>>, 17 32 pub modules: Vec<Spanned<ModuleDef>>, 18 33 pub uses: Vec<Spanned<Use>>, 19 34 } ··· 49 64 50 65 #[derive(Clone)] 51 66 pub enum TopLevel { 52 - Proc(Proc), 67 + Func(Func), 53 68 Const(Const), 54 - Var(Static), 69 + Static(Static), 55 70 Struct(Struct), 71 + InherentImpl(InherentImpl), 72 + TraitImpl(TraitImpl), 56 73 Use(Use), 57 74 Module(ModuleDef), 58 75 } 59 76 60 - impl TopLevel { 61 - pub fn name(&self) -> Intern<String> { 77 + impl Debug for TopLevel { 78 + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 62 79 match self { 63 - TopLevel::Proc(Proc { 64 - name: 65 - Spanned { 66 - span: _, 67 - inner: Word(name), 68 - }, 69 - .. 70 - }) => *name, 71 - TopLevel::Const(Const { 72 - name: 73 - Spanned { 74 - span: _, 75 - inner: Word(name), 76 - }, 77 - .. 78 - }) => *name, 79 - TopLevel::Var(Static { 80 - name: 81 - Spanned { 82 - span: _, 83 - inner: Word(name), 84 - }, 85 - .. 86 - }) => *name, 87 - TopLevel::Struct(Struct { 88 - name: 89 - Spanned { 90 - span: _, 91 - inner: Word(name), 92 - }, 93 - .. 94 - }) => *name, 95 - TopLevel::Use(Use { 96 - use_: _, 97 - name: 98 - Spanned { 99 - span: _, 100 - inner: Word(name), 101 - }, 102 - from: _, 103 - path: _, 104 - }) => *name, 105 - TopLevel::Module(ModuleDef { 106 - module: _, 107 - name: 108 - Spanned { 109 - span: _, 110 - inner: Word(name), 111 - }, 112 - }) => *name, 80 + TopLevel::Func(arg0) => arg0.fmt(f), 81 + TopLevel::Const(arg0) => arg0.fmt(f), 82 + TopLevel::Static(arg0) => arg0.fmt(f), 83 + TopLevel::Struct(arg0) => arg0.fmt(f), 84 + TopLevel::InherentImpl(arg0) => arg0.fmt(f), 85 + TopLevel::TraitImpl(arg0) => arg0.fmt(f), 86 + TopLevel::Use(arg0) => arg0.fmt(f), 87 + TopLevel::Module(arg0) => arg0.fmt(f), 113 88 } 114 89 } 115 90 } 116 91 117 - impl Debug for TopLevel { 92 + #[derive(Clone)] 93 + pub enum ImplLevel { 94 + Func(Func), 95 + Const(Const), 96 + Static(Static), 97 + } 98 + 99 + impl Debug for ImplLevel { 118 100 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 119 101 match self { 120 - TopLevel::Proc(arg0) => arg0.fmt(f), 121 - TopLevel::Const(arg0) => arg0.fmt(f), 122 - TopLevel::Var(arg0) => arg0.fmt(f), 123 - TopLevel::Struct(arg0) => arg0.fmt(f), 124 - TopLevel::Use(arg0) => arg0.fmt(f), 125 - TopLevel::Module(arg0) => arg0.fmt(f), 102 + ImplLevel::Func(arg0) => arg0.fmt(f), 103 + ImplLevel::Const(arg0) => arg0.fmt(f), 104 + ImplLevel::Static(arg0) => arg0.fmt(f), 126 105 } 127 106 } 128 107 } 129 108 130 109 #[derive(Debug, Clone)] 131 - pub struct Proc { 132 - pub proc: Spanned<Keyword>, 133 - pub generics: Option<Spanned<Generics>>, 110 + pub struct Func { 111 + pub func: Spanned<Keyword>, 112 + pub generics: Option<Generics>, 134 113 pub name: Spanned<Word>, 135 - pub signature: Spanned<ProcSignature>, 136 - pub do_: Spanned<Keyword>, 114 + pub signature: FuncSignature, 137 115 pub body: Vec<Spanned<Expr>>, 138 116 } 139 117 ··· 146 124 Match, 147 125 If, 148 126 Else, 149 - Proc, 127 + Func, 150 128 While, 151 - Do, 129 + For, 152 130 Let, 153 131 Const, 154 132 Static, 155 133 Struct, 134 + Impl, 156 135 Cast, 136 + Lambda, 157 137 } 158 138 159 139 impl std::fmt::Display for Keyword { ··· 165 145 #[derive(Debug, Clone)] 166 146 pub struct Struct { 167 147 pub struct_: Spanned<Keyword>, 168 - pub generics: Option<Spanned<Generics>>, 148 + pub generics: Option<Generics>, 169 149 pub name: Spanned<Word>, 170 - pub do_: Spanned<Keyword>, 171 - pub body: Vec<Spanned<NameTypePair>>, 150 + pub body: Vec<NameTypePair>, 151 + } 152 + 153 + #[derive(Debug, Clone)] 154 + pub struct StructLiteral { 155 + pub ty: Spanned<ItemPathBuf>, 156 + pub fields: Vec<FieldValuePair>, 157 + } 158 + 159 + #[derive(Debug, Clone)] 160 + pub struct InherentImpl { 161 + pub impl_: Spanned<Keyword>, 162 + pub generics: Option<Generics>, 163 + pub ty: Spanned<Type>, 164 + pub body: Vec<Spanned<ImplLevel>>, 165 + } 166 + 167 + #[derive(Debug, Clone)] 168 + pub struct TraitImpl { 169 + pub impl_: Spanned<Keyword>, 170 + pub generics: Option<Generics>, 171 + pub trait_: Spanned<ItemPathBuf>, 172 + pub for_: Spanned<Keyword>, 173 + pub ty: Spanned<Type>, 174 + pub body: Vec<Spanned<ImplLevel>>, 172 175 } 173 176 174 177 #[derive(Debug, Clone)] 175 178 pub struct Const { 176 179 pub const_: Spanned<Keyword>, 177 180 pub name: Spanned<Word>, 178 - pub signature: Spanned<ConstSignature>, 179 - pub do_: Spanned<Keyword>, 180 - pub body: Vec<Spanned<Expr>>, 181 + pub signature: TypeAscription, 182 + pub assign: Spanned<Punctuation>, 183 + pub expr: Spanned<Expr>, 181 184 } 182 185 183 186 #[derive(Debug, Clone)] ··· 194 197 pub path: Spanned<ItemPathBuf>, 195 198 } 196 199 197 - #[derive(Debug, Clone)] 198 - pub struct Qualifiers { 199 - pub items: Vec<Spanned<ItemPathBuf>>, 200 - pub from: Spanned<Keyword>, 200 + #[derive(Clone, Debug)] 201 + pub struct Return { 202 + pub return_: Spanned<Keyword>, 203 + pub expr: Option<Spanned<Expr>>, 201 204 } 202 205 203 - #[derive(Debug, Clone)] 204 - pub struct Read { 205 - pub read: Spanned<Token>, 206 - pub ty: Spanned<Type>, 206 + #[derive(Clone, Debug)] 207 + pub struct Lambda { 208 + pub lambda: Spanned<Keyword>, 209 + pub arglist: Vec<LambdaArg>, 210 + pub body: Vec<Spanned<Expr>>, 207 211 } 208 212 209 - #[derive(Debug, Clone)] 210 - pub struct Write { 211 - pub write: Spanned<Token>, 212 - pub ty: Spanned<Type>, 213 + #[derive(Clone, Debug)] 214 + pub struct LambdaArg { 215 + pub name: Spanned<Word>, 216 + pub ascription: Option<TypeAscription>, 213 217 } 214 218 215 219 #[derive(Clone)] 216 220 pub enum Expr { 217 - Keyword(Keyword), 218 - Type(Type), 221 + Return(Box<Return>), 219 222 220 223 Let(Box<Let>), 224 + Lambda(Lambda), 221 225 222 226 While(Box<While>), 223 227 224 - If(If), 228 + If(Box<If>), 225 229 Cond(Box<Match>), 226 230 227 - Cast(Cast), 228 - Read(Read), 229 - Write(Write), 230 231 Ref(Box<Unary>), 231 232 Deref(Box<Unary>), 232 233 ··· 243 244 Assign(Box<Binary>), 244 245 Call(Box<Call>), 245 246 246 - Static(Static), 247 + Static(Box<Static>), 248 + Const(Box<Const>), 247 249 } 248 250 249 251 impl Debug for Expr { 250 252 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 251 253 match self { 252 - Expr::Read(r) => write!(f, "@{:?}", r.ty), 253 - Expr::Write(w) => write!(f, "@{:?}", w.ty), 254 - Expr::Keyword(arg0) => arg0.fmt(f), 255 - Expr::Type(arg0) => arg0.fmt(f), 254 + Expr::Return(arg0) => arg0.fmt(f), 256 255 Expr::Let(arg0) => arg0.fmt(f), 256 + Expr::Lambda(arg0) => arg0.fmt(f), 257 257 Expr::While(arg0) => arg0.fmt(f), 258 258 Expr::If(arg0) => arg0.fmt(f), 259 259 Expr::Cond(arg0) => arg0.fmt(f), 260 - Expr::Cast(arg0) => arg0.fmt(f), 261 260 Expr::Word(arg0) => arg0.fmt(f), 262 261 Expr::Path(arg0) => arg0.fmt(f), 263 262 Expr::Literal(arg0) => arg0.fmt(f), ··· 268 267 Expr::Div(arg0) => arg0.fmt(f), 269 268 Expr::Eq(arg0) => arg0.fmt(f), 270 269 Expr::Assign(arg0) => arg0.fmt(f), 271 - Expr::Static(arg0) => arg0.fmt(f), 272 270 Expr::Deref(arg0) => arg0.fmt(f), 273 271 Expr::Ref(arg0) => arg0.fmt(f), 274 272 Expr::Call(arg0) => arg0.fmt(f), 273 + Expr::Static(arg0) => arg0.fmt(f), 274 + Expr::Const(arg0) => arg0.fmt(f), 275 275 } 276 276 } 277 277 } ··· 300 300 #[derive(Debug, Clone)] 301 301 pub struct Generics { 302 302 pub left_bracket: Spanned<Punctuation>, 303 - pub tys: Vec<Spanned<Word>>, 303 + pub tys: Vec<Generic>, 304 304 pub right_bracket: Spanned<Punctuation>, 305 + } 306 + 307 + #[derive(Debug, Clone)] 308 + pub struct Generic { 309 + pub ty: Spanned<Word>, 310 + pub constraint: Option<Constraint>, 311 + } 312 + 313 + #[derive(Debug, Clone)] 314 + pub struct Constraint { 315 + pub sep: Spanned<Punctuation>, 316 + pub constraint: Vec<Spanned<ItemPathBuf>>, 305 317 } 306 318 307 319 #[derive(Debug, Clone, Eq, PartialEq)] ··· 322 334 pub struct Static { 323 335 pub static_: Spanned<Keyword>, 324 336 pub name: Spanned<Word>, 337 + pub signature: TypeAscription, 338 + pub assign: Spanned<Punctuation>, 339 + pub expr: Spanned<Expr>, 340 + } 341 + 342 + #[derive(Debug, Clone)] 343 + pub struct TypeAscription { 325 344 pub sep: Spanned<Punctuation>, 326 345 pub ty: Spanned<Type>, 327 346 } 328 347 329 348 #[derive(Debug, Clone)] 330 - pub struct ConstSignature { 331 - pub sep: Spanned<Punctuation>, 332 - pub tys: Vec<Spanned<Type>>, 349 + pub struct FuncSignature { 350 + pub ins: Vec<NameTypePair>, 351 + pub return_: Option<SignatureReturn>, 333 352 } 334 353 335 354 #[derive(Debug, Clone)] 336 - pub struct ProcSignature { 337 - pub ins: Vec<Spanned<Type>>, 338 - pub sep: Option<Spanned<Punctuation>>, 339 - pub outs: Option<Vec<Spanned<Type>>>, 355 + pub struct SignatureReturn { 356 + pub arrow: Spanned<Punctuation>, 357 + pub ty: Spanned<Type>, 340 358 } 341 359 342 360 #[derive(Debug, Clone)] ··· 347 365 } 348 366 349 367 #[derive(Debug, Clone)] 368 + pub struct FieldValuePair { 369 + pub name: Spanned<Word>, 370 + pub sep: Spanned<Punctuation>, 371 + pub expr: Spanned<Expr>, 372 + } 373 + 374 + #[derive(Debug, Clone)] 350 375 pub struct While { 351 376 pub while_: Spanned<Keyword>, 352 377 pub cond: Spanned<Expr>, 353 - pub do_: Spanned<Keyword>, 354 378 pub body: Vec<Spanned<Expr>>, 355 - } 356 - 357 - #[derive(Debug, Clone)] 358 - pub struct Cast { 359 - pub cast: Spanned<Keyword>, 360 - pub ty: Spanned<Type>, 361 379 } 362 380 363 381 #[derive(Debug, Clone)] 364 382 pub struct If { 365 383 pub if_: Spanned<Keyword>, 384 + pub cond: Spanned<Expr>, 366 385 pub truth: Vec<Spanned<Expr>>, 367 386 pub lie: Option<Else>, 368 387 } ··· 377 396 pub struct Match { 378 397 pub match_: Spanned<Keyword>, 379 398 pub expr: Spanned<Expr>, 380 - pub do_: Spanned<Keyword>, 381 399 pub branches: Vec<Spanned<MatchBranch>>, 382 400 } 383 401 ··· 392 410 pub struct Let { 393 411 pub let_: Spanned<Keyword>, 394 412 pub pat: Spanned<Word>, 413 + pub ascription: Option<TypeAscription>, 395 414 pub assign: Spanned<Punctuation>, 396 415 pub body: Spanned<Expr>, 397 416 } ··· 402 421 UInt(u64), 403 422 String(Intern<String>), 404 423 Char(char), 424 + Unit, 425 + Struct(StructLiteral), 405 426 } 406 427 407 428 impl Debug for Literal { 408 429 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 409 430 match self { 410 - Self::Bool(arg0) => arg0.fmt(f), 411 - Self::Int(arg0) => arg0.fmt(f), 412 - Self::UInt(arg0) => arg0.fmt(f), 413 - Self::String(arg0) => arg0.fmt(f), 414 - Self::Char(arg0) => arg0.fmt(f), 431 + Literal::Bool(arg0) => arg0.fmt(f), 432 + Literal::Int(arg0) => arg0.fmt(f), 433 + Literal::UInt(arg0) => arg0.fmt(f), 434 + Literal::String(arg0) => arg0.fmt(f), 435 + Literal::Char(arg0) => arg0.fmt(f), 436 + Literal::Unit => write!(f, "Unit"), 437 + Literal::Struct(arg0) => arg0.fmt(f), 415 438 } 416 439 } 417 440 }
+394 -269
slate-frontend/src/parser/parsers.rs
··· 10 10 11 11 use super::{ 12 12 ast::{ 13 - Binary, Call, Cast, Const, ConstSignature, Else, Expr, FieldAccess, GenericParams, 14 - Generics, If, Keyword, Let, Literal, Match, MatchBranch, Module, ModuleDef, NameTypePair, 15 - Proc, ProcSignature, Punctuation, Read, Static, Struct, TopLevel, Unary, Use, While, Word, 13 + Binary, Call, Const, Constraint, Else, Expr, FieldAccess, FieldValuePair, Func, 14 + FuncSignature, Generic, GenericParams, Generics, If, ImplLevel, InherentImpl, Keyword, 15 + Lambda, LambdaArg, Let, Literal, Match, MatchBranch, Module, ModuleDef, NameTypePair, 16 + Punctuation, Return, SignatureReturn, Static, Struct, StructLiteral, TopLevel, TraitImpl, 17 + TypeAscription, Unary, Use, While, Word, 16 18 }, 17 19 types::{Custom, Primitive, Type}, 18 20 }; 19 21 20 - trait RParser<'i, I, O> 22 + trait NParser<'i, I, O> 21 23 where 22 - Self: Parser<'i, I, Spanned<O>, extra::Full<Rich<'i, Token, Span>, Vec<usize>, usize>> + Clone, 24 + Self: Parser<'i, I, O, extra::Full<Rich<'i, Token, Span>, (), usize>> + Clone, 25 + I: ValueInput<'i, Token = Token, Span = Span>, 26 + { 27 + } 28 + impl<'i, I, O, T> NParser<'i, I, O> for T 29 + where 30 + T: Parser<'i, I, O, extra::Full<Rich<'i, Token, Span>, (), usize>> + Clone, 23 31 I: ValueInput<'i, Token = Token, Span = Span>, 24 32 { 25 33 } 26 34 27 - impl<'i, I, O, T> RParser<'i, I, O> for T 35 + trait SParser<'i, I, O> 36 + where 37 + Self: Parser<'i, I, Spanned<O>, extra::Full<Rich<'i, Token, Span>, (), usize>> + Clone, 38 + I: ValueInput<'i, Token = Token, Span = Span>, 39 + { 40 + } 41 + impl<'i, I, O, T> SParser<'i, I, O> for T 28 42 where 29 - T: Parser<'i, I, Spanned<O>, extra::Full<Rich<'i, Token, Span>, Vec<usize>, usize>> + Clone, 43 + T: Parser<'i, I, Spanned<O>, extra::Full<Rich<'i, Token, Span>, (), usize>> + Clone, 30 44 I: ValueInput<'i, Token = Token, Span = Span>, 31 45 { 32 46 } 33 47 34 - fn ty<'i, I>() -> impl RParser<'i, I, Type> 48 + fn ty<'i, I>() -> impl SParser<'i, I, Type> 35 49 where 36 50 I: ValueInput<'i, Token = Token, Span = Span>, 37 51 { 38 52 recursive(|ty| { 39 53 let generic_params = lbracket() 40 - .then(ty.repeated().at_least(1).collect()) 54 + .then(ty.repeated().collect()) 41 55 .then(rbracket()) 42 56 .map_with(|((left_bracket, tys), right_bracket), span| Spanned { 43 57 span: span.span(), ··· 48 62 }, 49 63 }); 50 64 just(Token::OpAnd) 65 + .to(()) 51 66 .repeated() 52 - .collect::<Vec<_>>() 67 + .collect::<Vec<()>>() 53 68 .then(path().then(generic_params.or_not())) 54 69 .map_with(|(ptr, (ty, params)), span| { 55 70 let mut ty = if let Some(type_name) = ty.inner.first() { ··· 120 135 .map(Into::into) 121 136 } 122 137 123 - fn literal<'i, I>() -> impl RParser<'i, I, Literal> 138 + fn literal<'i, I>(expr: impl SParser<'i, I, Expr>) -> impl SParser<'i, I, Literal> 124 139 where 125 140 I: ValueInput<'i, Token = Token, Span = Span>, 126 141 { 127 - select! { 128 - Token::Bool(b) = span => Spanned { span: span.span(), inner: Literal::Bool(b)}, 129 - Token::Num(n) = span => { 130 - if let Ok(i) = n.parse::<i64>(){ 131 - Spanned{ span: span.span(),inner: Literal::Int(i) } 132 - } 133 - else { 134 - Spanned{ span: span.span(), inner: Literal::UInt(n.parse().unwrap()) } 135 - } 142 + choice(( 143 + lparen().then(rparen()).map_with(|_, extra| Spanned { 144 + span: extra.span(), 145 + inner: Literal::Unit, 146 + }), 147 + path() 148 + .then(body_unspanned(field_value_pair(expr))) 149 + .map_with(|(ty, fields), extra| Spanned { 150 + span: extra.span(), 151 + inner: Literal::Struct(StructLiteral { ty, fields }), 152 + }), 153 + select! { 154 + Token::Bool(b) = span => Spanned { span: span.span(), inner: Literal::Bool(b)}, 155 + Token::Num(n) = span => { 156 + if let Ok(i) = n.parse::<i64>(){ 157 + Spanned{ span: span.span(),inner: Literal::Int(i) } 158 + } 159 + else { 160 + Spanned{ span: span.span(), inner: Literal::UInt(n.parse().unwrap()) } 161 + } 162 + }, 163 + Token::String(s) = span => { 164 + let span: spanner::Span = span.span(); 165 + let s = parse_string(&s, span.context()).ok()?; 166 + Spanned{ span, inner: Literal::String(s) }}, 167 + Token::Char(c) = span => Spanned { span: span.span(), inner: Literal::Char(c) }, 136 168 }, 137 - Token::String(s) = span => { 138 - let span: spanner::Span = span.span(); 139 - let s = parse_string(&s, span.context()).ok()?; 140 - Spanned{ span, inner: Literal::String(s) }}, 141 - Token::Char(c) = span => Spanned { span: span.span(), inner: Literal::Char(c) }, 142 - } 169 + )) 143 170 .labelled("Literal") 144 171 } 145 172 146 - fn literal_expr<'i, I>() -> impl RParser<'i, I, Expr> 173 + fn literal_expr<'i, I>(expr: impl SParser<'i, I, Expr>) -> impl SParser<'i, I, Expr> 147 174 where 148 175 I: ValueInput<'i, Token = Token, Span = Span>, 149 176 { 150 - literal().map(|Spanned { span, inner }| Spanned { 177 + literal(expr).map(|Spanned { span, inner }| Spanned { 151 178 span, 152 179 inner: Expr::Literal(inner), 153 180 }) 154 181 } 155 182 156 - fn kw_module<'i, I>() -> impl RParser<'i, I, Keyword> 183 + fn kw_module<'i, I>() -> impl SParser<'i, I, Keyword> 157 184 where 158 185 I: ValueInput<'i, Token = Token, Span = Span>, 159 186 { ··· 163 190 }) 164 191 } 165 192 166 - fn kw_use<'i, I>() -> impl RParser<'i, I, Keyword> 193 + fn kw_use<'i, I>() -> impl SParser<'i, I, Keyword> 167 194 where 168 195 I: ValueInput<'i, Token = Token, Span = Span>, 169 196 { ··· 173 200 }) 174 201 } 175 202 176 - fn kw_from<'i, I>() -> impl RParser<'i, I, Keyword> 203 + fn kw_from<'i, I>() -> impl SParser<'i, I, Keyword> 177 204 where 178 205 I: ValueInput<'i, Token = Token, Span = Span>, 179 206 { ··· 183 210 }) 184 211 } 185 212 186 - fn kw_let<'i, I>() -> impl RParser<'i, I, Keyword> 213 + fn kw_let<'i, I>() -> impl SParser<'i, I, Keyword> 187 214 where 188 215 I: ValueInput<'i, Token = Token, Span = Span>, 189 216 { ··· 193 220 }) 194 221 } 195 222 196 - fn kw_while<'i, I>() -> impl RParser<'i, I, Keyword> 223 + fn kw_lambda<'i, I>() -> impl SParser<'i, I, Keyword> 197 224 where 198 225 I: ValueInput<'i, Token = Token, Span = Span>, 199 226 { 200 - just(Token::KwWhile).map_with(|_, extra| Spanned { 227 + just(Token::KwLambda).map_with(|_, extra| Spanned { 201 228 span: extra.span(), 202 - inner: Keyword::While, 229 + inner: Keyword::Lambda, 203 230 }) 204 231 } 205 - fn kw_match<'i, I>() -> impl RParser<'i, I, Keyword> 232 + 233 + fn kw_while<'i, I>() -> impl SParser<'i, I, Keyword> 206 234 where 207 235 I: ValueInput<'i, Token = Token, Span = Span>, 208 236 { 209 - just(Token::KwMatch).map_with(|_, extra| Spanned { 237 + just(Token::KwWhile).map_with(|_, extra| Spanned { 210 238 span: extra.span(), 211 - inner: Keyword::Match, 239 + inner: Keyword::While, 212 240 }) 213 241 } 214 - fn kw_if<'i, I>() -> impl RParser<'i, I, Keyword> 242 + 243 + fn kw_for<'i, I>() -> impl SParser<'i, I, Keyword> 215 244 where 216 245 I: ValueInput<'i, Token = Token, Span = Span>, 217 246 { 218 - just(Token::KwIf).map_with(|_, extra| Spanned { 247 + just(Token::KwFor).map_with(|_, extra| Spanned { 219 248 span: extra.span(), 220 - inner: Keyword::If, 249 + inner: Keyword::For, 221 250 }) 222 251 } 223 - fn kw_else<'i, I>() -> impl RParser<'i, I, Keyword> 252 + 253 + fn kw_match<'i, I>() -> impl SParser<'i, I, Keyword> 224 254 where 225 255 I: ValueInput<'i, Token = Token, Span = Span>, 226 256 { 227 - just(Token::KwElse).map_with(|_, extra| Spanned { 257 + just(Token::KwMatch).map_with(|_, extra| Spanned { 228 258 span: extra.span(), 229 - inner: Keyword::Else, 259 + inner: Keyword::Match, 230 260 }) 231 261 } 232 - fn kw_do<'i, I>() -> impl RParser<'i, I, Keyword> 262 + fn kw_if<'i, I>() -> impl SParser<'i, I, Keyword> 233 263 where 234 264 I: ValueInput<'i, Token = Token, Span = Span>, 235 265 { 236 - just(Token::KwDo).map_with(|_, extra| Spanned { 266 + just(Token::KwIf).map_with(|_, extra| Spanned { 237 267 span: extra.span(), 238 - inner: Keyword::Do, 268 + inner: Keyword::If, 239 269 }) 240 270 } 241 - fn kw_ret<'i, I>() -> impl RParser<'i, I, Keyword> 271 + fn kw_else<'i, I>() -> impl SParser<'i, I, Keyword> 242 272 where 243 273 I: ValueInput<'i, Token = Token, Span = Span>, 244 274 { 245 - just(Token::KwReturn).map_with(|_, extra| Spanned { 275 + just(Token::KwElse).map_with(|_, extra| Spanned { 246 276 span: extra.span(), 247 - inner: Keyword::Return, 277 + inner: Keyword::Else, 248 278 }) 249 279 } 250 - fn kw_cast<'i, I>() -> impl RParser<'i, I, Keyword> 280 + 281 + fn kw_ret<'i, I>() -> impl SParser<'i, I, Keyword> 251 282 where 252 283 I: ValueInput<'i, Token = Token, Span = Span>, 253 284 { 254 - just(Token::KwCast).map_with(|_, extra| Spanned { 285 + just(Token::KwReturn).map_with(|_, extra| Spanned { 255 286 span: extra.span(), 256 - inner: Keyword::Cast, 287 + inner: Keyword::Return, 257 288 }) 258 289 } 259 - fn kw_proc<'i, I>() -> impl RParser<'i, I, Keyword> 290 + 291 + fn kw_func<'i, I>() -> impl SParser<'i, I, Keyword> 260 292 where 261 293 I: ValueInput<'i, Token = Token, Span = Span>, 262 294 { 263 - just(Token::KwProc).map_with(|_, extra| Spanned { 295 + just(Token::KwFunc).map_with(|_, extra| Spanned { 264 296 span: extra.span(), 265 - inner: Keyword::Proc, 297 + inner: Keyword::Func, 266 298 }) 267 299 } 268 300 269 - fn kw_const<'i, I>() -> impl RParser<'i, I, Keyword> 301 + fn kw_const<'i, I>() -> impl SParser<'i, I, Keyword> 270 302 where 271 303 I: ValueInput<'i, Token = Token, Span = Span>, 272 304 { ··· 276 308 }) 277 309 } 278 310 279 - fn kw_static<'i, I>() -> impl RParser<'i, I, Keyword> 311 + fn kw_static<'i, I>() -> impl SParser<'i, I, Keyword> 280 312 where 281 313 I: ValueInput<'i, Token = Token, Span = Span>, 282 314 { ··· 286 318 }) 287 319 } 288 320 289 - fn kw_struct<'i, I>() -> impl RParser<'i, I, Keyword> 321 + fn kw_struct<'i, I>() -> impl SParser<'i, I, Keyword> 290 322 where 291 323 I: ValueInput<'i, Token = Token, Span = Span>, 292 324 { ··· 296 328 }) 297 329 } 298 330 299 - fn op_and<'i, I>() -> impl RParser<'i, I, Punctuation> 331 + fn kw_impl<'i, I>() -> impl SParser<'i, I, Keyword> 332 + where 333 + I: ValueInput<'i, Token = Token, Span = Span>, 334 + { 335 + just(Token::KwImpl).map_with(|_, extra| Spanned { 336 + span: extra.span(), 337 + inner: Keyword::Impl, 338 + }) 339 + } 340 + 341 + fn op_and<'i, I>() -> impl SParser<'i, I, Punctuation> 300 342 where 301 343 I: ValueInput<'i, Token = Token, Span = Span>, 302 344 { ··· 306 348 }) 307 349 } 308 350 309 - fn op_mul<'i, I>() -> impl RParser<'i, I, Punctuation> 351 + fn op_mul<'i, I>() -> impl SParser<'i, I, Punctuation> 310 352 where 311 353 I: ValueInput<'i, Token = Token, Span = Span>, 312 354 { ··· 316 358 }) 317 359 } 318 360 319 - fn op_div<'i, I>() -> impl RParser<'i, I, Punctuation> 361 + fn op_div<'i, I>() -> impl SParser<'i, I, Punctuation> 320 362 where 321 363 I: ValueInput<'i, Token = Token, Span = Span>, 322 364 { ··· 326 368 }) 327 369 } 328 370 329 - fn op_plus<'i, I>() -> impl RParser<'i, I, Punctuation> 371 + fn op_plus<'i, I>() -> impl SParser<'i, I, Punctuation> 330 372 where 331 373 I: ValueInput<'i, Token = Token, Span = Span>, 332 374 { ··· 336 378 }) 337 379 } 338 380 339 - fn op_minus<'i, I>() -> impl RParser<'i, I, Punctuation> 381 + fn op_minus<'i, I>() -> impl SParser<'i, I, Punctuation> 340 382 where 341 383 I: ValueInput<'i, Token = Token, Span = Span>, 342 384 { ··· 346 388 }) 347 389 } 348 390 349 - fn op_assign<'i, I>() -> impl RParser<'i, I, Punctuation> 391 + fn op_assign<'i, I>() -> impl SParser<'i, I, Punctuation> 350 392 where 351 393 I: ValueInput<'i, Token = Token, Span = Span>, 352 394 { ··· 356 398 }) 357 399 } 358 400 359 - fn op_eq<'i, I>() -> impl RParser<'i, I, Punctuation> 401 + fn op_eq<'i, I>() -> impl SParser<'i, I, Punctuation> 360 402 where 361 403 I: ValueInput<'i, Token = Token, Span = Span>, 362 404 { ··· 366 408 }) 367 409 } 368 410 369 - fn op_dot<'i, I>() -> impl RParser<'i, I, Punctuation> 411 + fn op_dot<'i, I>() -> impl SParser<'i, I, Punctuation> 370 412 where 371 413 I: ValueInput<'i, Token = Token, Span = Span>, 372 414 { ··· 376 418 }) 377 419 } 378 420 379 - fn fat_arrow<'i, I>() -> impl RParser<'i, I, Punctuation> 421 + fn fat_arrow<'i, I>() -> impl SParser<'i, I, Punctuation> 380 422 where 381 423 I: ValueInput<'i, Token = Token, Span = Span>, 382 424 { ··· 386 428 }) 387 429 } 388 430 389 - fn word<'i, I>() -> impl RParser<'i, I, Word> 431 + fn word<'i, I>() -> impl SParser<'i, I, Word> 390 432 where 391 433 I: ValueInput<'i, Token = Token, Span = Span>, 392 434 { ··· 395 437 } 396 438 } 397 439 398 - fn word_expr<'i, I>() -> impl RParser<'i, I, Expr> 440 + fn word_expr<'i, I>() -> impl SParser<'i, I, Expr> 399 441 where 400 442 I: ValueInput<'i, Token = Token, Span = Span>, 401 443 { ··· 404 446 } 405 447 } 406 448 407 - fn path<'i, I>() -> impl RParser<'i, I, ItemPathBuf> 449 + fn path<'i, I>() -> impl SParser<'i, I, ItemPathBuf> 408 450 where 409 451 I: ValueInput<'i, Token = Token, Span = Span>, 410 452 { ··· 420 462 }) 421 463 } 422 464 423 - fn path_expr<'i, I>() -> impl RParser<'i, I, Expr> 465 + fn path_expr<'i, I>() -> impl SParser<'i, I, Expr> 424 466 where 425 467 I: ValueInput<'i, Token = Token, Span = Span>, 426 468 { ··· 436 478 }) 437 479 } 438 480 439 - fn separator<'i, I>() -> impl RParser<'i, I, Punctuation> 481 + fn separator<'i, I>() -> impl SParser<'i, I, Punctuation> 440 482 where 441 483 I: ValueInput<'i, Token = Token, Span = Span>, 442 484 { ··· 446 488 }) 447 489 } 448 490 449 - fn read_expr<'i, I>() -> impl RParser<'i, I, Expr> 491 + fn const_<'i: 'd, 'd, I>(expr: impl SParser<'i, I, Expr>) -> impl SParser<'i, I, Const> 492 + where 493 + I: ValueInput<'i, Token = Token, Span = Span>, 494 + { 495 + kw_const() 496 + .then(word()) 497 + .then(type_ascription()) 498 + .then(op_assign()) 499 + .then(expr) 500 + .map_with( 501 + |((((const_, name), signature), assign), expr), span| Spanned { 502 + span: span.span(), 503 + inner: Const { 504 + const_, 505 + name, 506 + signature, 507 + assign, 508 + expr, 509 + }, 510 + }, 511 + ) 512 + } 513 + 514 + fn const_toplevel<'i, I>() -> impl SParser<'i, I, TopLevel> 515 + where 516 + I: ValueInput<'i, Token = Token, Span = Span>, 517 + { 518 + const_(expr()).map(|Spanned { span, inner }| Spanned { 519 + span, 520 + inner: TopLevel::Const(inner), 521 + }) 522 + } 523 + 524 + fn const_expr<'i, I>(expr: impl SParser<'i, I, Expr>) -> impl SParser<'i, I, Expr> 450 525 where 451 526 I: ValueInput<'i, Token = Token, Span = Span>, 452 527 { 453 - select! { 454 - t @ Token::OpMul = span => Spanned { span: span.span(), inner: t }, 455 - } 456 - .then(ty()) 457 - .map_with(|(read, ty), span| Spanned { 458 - span: span.span(), 459 - inner: Expr::Read(Read { read, ty }), 528 + const_(expr).map(|Spanned { span, inner }| Spanned { 529 + span, 530 + inner: Expr::Const(Box::new(inner)), 460 531 }) 461 532 } 462 533 463 - fn static_<'i, I>() -> impl RParser<'i, I, Static> 534 + fn static_<'i, I>(expr: impl SParser<'i, I, Expr>) -> impl SParser<'i, I, Static> 464 535 where 465 536 I: ValueInput<'i, Token = Token, Span = Span>, 466 537 { 467 538 kw_static() 468 539 .then(word()) 469 - .then(separator()) 470 - .then(ty()) 471 - .map_with(|(((static_, name), sep), ty), span| Spanned { 472 - span: span.span(), 473 - inner: Static { 474 - static_, 475 - name, 476 - sep, 477 - ty, 540 + .then(type_ascription()) 541 + .then(op_assign()) 542 + .then(expr) 543 + .map_with( 544 + |((((static_, name), signature), assign), expr), span| Spanned { 545 + span: span.span(), 546 + inner: Static { 547 + static_, 548 + name, 549 + signature, 550 + assign, 551 + expr, 552 + }, 478 553 }, 479 - }) 554 + ) 480 555 } 481 556 482 - fn static_toplevel<'i, I>() -> impl RParser<'i, I, TopLevel> 557 + fn static_toplevel<'i, I>() -> impl SParser<'i, I, TopLevel> 483 558 where 484 559 I: ValueInput<'i, Token = Token, Span = Span>, 485 560 { 486 - static_().map(|Spanned { span, inner }| Spanned { 561 + static_(expr()).map(|Spanned { span, inner }| Spanned { 487 562 span, 488 - inner: TopLevel::Var(inner), 563 + inner: TopLevel::Static(inner), 489 564 }) 490 565 } 491 566 492 - fn static_expr<'i, I>() -> impl RParser<'i, I, Expr> 567 + fn static_expr<'i, I>(expr: impl SParser<'i, I, Expr>) -> impl SParser<'i, I, Expr> 493 568 where 494 569 I: ValueInput<'i, Token = Token, Span = Span>, 495 570 { 496 - static_().map(|Spanned { span, inner }| Spanned { 571 + static_(expr).map(|Spanned { span, inner }| Spanned { 497 572 span, 498 - inner: Expr::Static(inner), 573 + inner: Expr::Static(Box::new(inner)), 499 574 }) 500 575 } 501 576 502 - fn expr<'i: 'd, 'd, I>() -> impl RParser<'i, I, Expr> 577 + fn expr<'i: 'd, 'd, I>() -> impl SParser<'i, I, Expr> 503 578 where 504 579 I: ValueInput<'i, Token = Token, Span = Span>, 505 580 { 506 581 recursive(|expr| { 507 582 let while_ = kw_while() 508 583 .then(expr.clone()) 509 - .then(kw_do().map_with(|d, e| { 510 - e.state().push(0); 511 - d 512 - })) 513 584 .then(body(expr.clone())) 514 - .map_with(|(((while_, cond), do_), body), span| Spanned { 585 + .map_with(|((while_, cond), body), span| Spanned { 515 586 span: span.span(), 516 - inner: Expr::While(Box::new(While { 517 - while_, 518 - cond, 519 - do_, 520 - body, 521 - })), 587 + inner: Expr::While(Box::new(While { while_, cond, body })), 522 588 }); 523 589 524 - let lie = kw_else() 525 - .then(expr.clone().repeated().collect()) 590 + let lie = indent_same() 591 + .ignore_then(kw_else()) 592 + .then(body(expr.clone())) 526 593 .map(|(else_, body)| Else { else_, body }); 527 594 528 595 let if_ = kw_if() 529 - .then(expr.clone().repeated().collect()) 596 + .then(expr.clone()) 597 + .then(body(expr.clone())) 530 598 .then(lie.or_not()) 531 - .map_with(|((if_, truth), lie), span| Spanned { 532 - span: span.span(), 533 - inner: Expr::If(If { if_, truth, lie }), 599 + .map_with(|(((if_, cond), truth), lie), extra| Spanned { 600 + span: extra.span(), 601 + inner: Expr::If(Box::new(If { 602 + if_, 603 + cond, 604 + truth, 605 + lie, 606 + })), 534 607 }); 535 608 536 - let cast = kw_cast().then(ty()).map_with(|(cast, ty), span| Spanned { 537 - span: span.span(), 538 - inner: Expr::Cast(Cast { cast, ty }), 539 - }); 540 - 541 - let pat = choice((literal_expr(), word_expr(), path_expr())); 609 + let pat = choice((literal_expr(expr.clone()), word_expr(), path_expr())); 542 610 let match_branch = pat 543 611 .clone() 544 612 .then(fat_arrow()) 545 - .then(choice(( 546 - expr.clone().map(|e| vec![e]), 547 - kw_do().ignore_then(body(expr.clone())), 548 - ))) 613 + .then(choice((expr.clone().map(|e| vec![e]), body(expr.clone())))) 549 614 .map_with(|((pat, fat_arrow), body), extra| Spanned { 550 615 span: extra.span(), 551 616 inner: MatchBranch { ··· 558 623 559 624 let match_ = kw_match() 560 625 .then(expr.clone()) 561 - .then(kw_do()) 562 - .then( 563 - indent_increase().ignore_with_ctx( 564 - match_branch 565 - .separated_by( 566 - select(|x, e| match x { 567 - Token::Indent(level) if level == *dbg! {e.ctx()} => Some(()), 568 - _ => None, 569 - }) 570 - .labelled("matching indent level"), 571 - ) 572 - .collect(), 573 - ), 574 - ) 575 - .map_with(|(((match_, expr), do_), branches), extra| Spanned { 626 + .then(body(match_branch)) 627 + .map_with(|((match_, expr), branches), extra| Spanned { 576 628 span: extra.span(), 577 629 inner: Expr::Cond(Box::new(Match { 578 630 match_, 579 631 expr, 580 - do_, 581 632 branches, 582 633 })), 583 634 }); 584 635 585 - let ret_expr = kw_ret().map(|Spanned { span, inner }| Spanned { 586 - span, 587 - inner: Expr::Keyword(inner), 588 - }); 636 + let ret_expr = kw_ret() 637 + .then(expr.clone().or_not()) 638 + .map_with(|(return_, expr), extra| Spanned { 639 + span: extra.span(), 640 + inner: Expr::Return(Box::new(Return { return_, expr })), 641 + }); 589 642 590 643 let let_ = kw_let() 591 644 .then(word()) 645 + .then(type_ascription().or_not()) 592 646 .then(op_assign()) 593 647 .then(expr.clone()) 594 - .map_with(|(((let_, pat), assign), body), extra| Spanned { 648 + .map_with( 649 + |((((let_, pat), ascription), assign), body), extra| Spanned { 650 + span: extra.span(), 651 + inner: Expr::Let(Box::new(Let { 652 + let_, 653 + pat, 654 + ascription, 655 + assign, 656 + body, 657 + })), 658 + }, 659 + ); 660 + 661 + let lambda = kw_lambda() 662 + .then( 663 + word() 664 + .then(type_ascription().or_not()) 665 + .map(|(name, ascription)| LambdaArg { name, ascription }) 666 + .separated_by(just(Token::Comma)) 667 + .collect::<Vec<_>>(), 668 + ) 669 + .then(choice((expr.clone().map(|e| vec![e]), body(expr.clone())))) 670 + .map_with(|((lambda, arglist), body), extra| Spanned { 595 671 span: extra.span(), 596 - inner: Expr::Let(Box::new(Let { 597 - let_, 598 - pat, 599 - assign, 672 + inner: Expr::Lambda(Lambda { 673 + lambda, 674 + arglist, 600 675 body, 601 - })), 676 + }), 602 677 }); 603 678 604 679 let call = lparen() 605 - .then(expr.separated_by(just(Token::Comma)).collect::<Vec<_>>()) 606 - .then(rparen()); 680 + .then( 681 + select! { Token::Indent(_) => () } 682 + .or_not() 683 + .ignore_then(expr.clone()) 684 + .separated_by(just(Token::Comma)) 685 + .allow_trailing() 686 + .collect::<Vec<_>>(), 687 + ) 688 + .then( 689 + select! { Token::Indent(_) => () } 690 + .or_not() 691 + .ignore_then(rparen()), 692 + ); 607 693 608 694 choice(( 609 - literal_expr(), 610 - static_expr(), 695 + literal_expr(expr.clone()), 696 + static_expr(expr.clone()), 697 + const_expr(expr), 611 698 path_expr(), 612 699 word_expr(), 613 - read_expr(), 614 700 let_, 701 + lambda, 615 702 while_, 616 703 if_, 617 704 match_, 618 - cast, 619 705 ret_expr, 620 706 )) 621 707 .pratt(( ··· 663 749 inner: Expr::Assign(Box::new(Binary { left, op, right })), 664 750 } 665 751 }), 666 - pratt::postfix(6, op_dot::<I>().then(word()), |expr, (op, field), span| { 667 - Spanned { 752 + pratt::postfix( 753 + 6, 754 + select! { Token::Indent(_) => () } 755 + .or_not() 756 + .ignore_then(op_dot::<I>().then(word())), 757 + |expr, (op, field), span| Spanned { 668 758 span, 669 759 inner: Expr::FieldAccess(Box::new(FieldAccess { 670 760 reciever: expr, 671 761 access: op, 672 762 field, 673 763 })), 674 - } 675 - }), 764 + }, 765 + ), 676 766 pratt::postfix(6, call, |callee, ((lparen, args), rparen), span| Spanned { 677 767 span, 678 768 inner: Expr::Call(Box::new(Call { ··· 688 778 } 689 779 690 780 fn indent_increase<'i, I>( 691 - ) -> impl Parser<'i, I, usize, extra::Full<Rich<'i, Token, Span>, Vec<usize>, usize>> + Clone 781 + ) -> impl Parser<'i, I, usize, extra::Full<Rich<'i, Token, Span>, (), usize>> + Clone 692 782 where 693 783 I: ValueInput<'i, Token = Token, Span = Span>, 694 784 { 695 785 select(|x, e| match x { 696 - Token::Indent(level) if level > *e.ctx() => { 697 - if level > *e.ctx() { 698 - Some(level) 699 - } else { 700 - None 701 - } 702 - } 786 + Token::Indent(level) if level > *e.ctx() => Some(level), 703 787 _ => None, 704 788 }) 705 - .labelled("Indent increase") 789 + .labelled("indent increase") 706 790 } 707 791 708 - fn proc_signature<'i, I>() -> impl RParser<'i, I, ProcSignature> 792 + fn indent_same<'i, I>( 793 + ) -> impl Parser<'i, I, (), extra::Full<Rich<'i, Token, Span>, (), usize>> + Clone 709 794 where 710 795 I: ValueInput<'i, Token = Token, Span = Span>, 711 796 { 712 - ty().repeated() 797 + select(|x, e| match x { 798 + Token::Indent(level) if level == *e.ctx() => Some(()), 799 + _ => None, 800 + }) 801 + .labelled("same indent") 802 + } 803 + fn func_signature<'i, I>() -> impl NParser<'i, I, FuncSignature> 804 + where 805 + I: ValueInput<'i, Token = Token, Span = Span>, 806 + { 807 + name_type_pair() 808 + .separated_by(just(Token::Comma)) 713 809 .collect::<Vec<_>>() 714 810 .then( 715 - separator() 716 - .then(ty().repeated().at_least(1).collect::<Vec<_>>()) 811 + fat_arrow() 812 + .then(ty()) 813 + .map(|(arrow, ty)| SignatureReturn { arrow, ty }) 717 814 .or_not(), 718 815 ) 719 - .map_with(|(ins, maybe_outs), span| { 720 - let (sep, outs) = if let Some((sep, outs)) = maybe_outs { 721 - (Some(sep), Some(outs)) 722 - } else { 723 - (None, None) 724 - }; 725 - Spanned { 726 - span: span.span(), 727 - inner: ProcSignature { ins, sep, outs }, 728 - } 729 - }) 816 + .map(|(ins, return_)| FuncSignature { ins, return_ }) 730 817 } 731 818 732 - fn lbracket<'i, I>() -> impl RParser<'i, I, Punctuation> 819 + fn lbracket<'i, I>() -> impl SParser<'i, I, Punctuation> 733 820 where 734 821 I: ValueInput<'i, Token = Token, Span = Span>, 735 822 { ··· 738 825 inner: Punctuation::LBracket, 739 826 }) 740 827 } 741 - fn rbracket<'i, I>() -> impl RParser<'i, I, Punctuation> 828 + fn rbracket<'i, I>() -> impl SParser<'i, I, Punctuation> 742 829 where 743 830 I: ValueInput<'i, Token = Token, Span = Span>, 744 831 { ··· 748 835 }) 749 836 } 750 837 751 - fn lparen<'i, I>() -> impl RParser<'i, I, Punctuation> 838 + fn lparen<'i, I>() -> impl SParser<'i, I, Punctuation> 752 839 where 753 840 I: ValueInput<'i, Token = Token, Span = Span>, 754 841 { ··· 757 844 inner: Punctuation::RParen, 758 845 }) 759 846 } 760 - fn rparen<'i, I>() -> impl RParser<'i, I, Punctuation> 847 + fn rparen<'i, I>() -> impl SParser<'i, I, Punctuation> 761 848 where 762 849 I: ValueInput<'i, Token = Token, Span = Span>, 763 850 { ··· 767 854 }) 768 855 } 769 856 770 - fn generics<'i, I>() -> impl RParser<'i, I, Generics> 857 + fn generics<'i, I>() -> impl NParser<'i, I, Generics> 771 858 where 772 859 I: ValueInput<'i, Token = Token, Span = Span>, 773 860 { 774 861 lbracket() 775 - .then(word().repeated().at_least(1).collect::<Vec<_>>()) 862 + .then( 863 + word() 864 + .then( 865 + separator() 866 + .then(path().separated_by(just(Token::OpPlus)).collect()) 867 + .map(|(sep, constraint)| Constraint { sep, constraint }) 868 + .or_not(), 869 + ) 870 + .map(|(ty, constraint)| Generic { ty, constraint }) 871 + .separated_by(just(Token::Comma)) 872 + .collect::<Vec<_>>(), 873 + ) 776 874 .then(rbracket()) 777 - .map_with(|((left_bracket, tys), right_bracket), span| Spanned { 778 - span: span.span(), 779 - inner: Generics { 780 - left_bracket, 781 - tys, 782 - right_bracket, 783 - }, 875 + .map(|((left_bracket, tys), right_bracket)| Generics { 876 + left_bracket, 877 + tys, 878 + right_bracket, 784 879 }) 785 880 } 786 881 787 - fn body<'i: 'd, 'd, I>( 788 - expr: impl RParser<'i, I, Expr>, 789 - ) -> impl Parser<'i, I, Vec<Spanned<Expr>>, extra::Full<Rich<'i, Token, Span>, Vec<usize>, usize>> + Clone 882 + fn body<'i: 'd, 'd, I, O>( 883 + expr: impl Parser<'i, I, Spanned<O>, extra::Full<Rich<'i, Token, Span>, (), usize>> + Clone, 884 + ) -> impl Parser<'i, I, Vec<Spanned<O>>, extra::Full<Rich<'i, Token, Span>, (), usize>> + Clone 885 + where 886 + I: ValueInput<'i, Token = Token, Span = Span>, 887 + { 888 + indent_increase().ignore_with_ctx(expr.clone().separated_by(indent_same()).collect::<Vec<_>>()) 889 + } 890 + 891 + fn body_unspanned<'i: 'd, 'd, I, O>( 892 + expr: impl Parser<'i, I, O, extra::Full<Rich<'i, Token, Span>, (), usize>> + Clone, 893 + ) -> impl Parser<'i, I, Vec<O>, extra::Full<Rich<'i, Token, Span>, (), usize>> + Clone 790 894 where 791 895 I: ValueInput<'i, Token = Token, Span = Span>, 792 896 { 793 - expr.clone() 794 - .then( 795 - select(|x, extra| match x { 796 - Token::Indent(level) if level == *extra.ctx() => Some(()), 797 - _ => None, 798 - }) 799 - .ignore_then(expr) 800 - .repeated() 801 - .collect::<Vec<_>>(), 802 - ) 803 - .map(|(first, mut rest)| { 804 - rest.insert(0, first); 805 - rest 806 - }) 897 + indent_increase().ignore_with_ctx(expr.clone().separated_by(indent_same()).collect::<Vec<_>>()) 807 898 } 808 899 809 - fn proc<'i: 'd, 'd, I>() -> impl RParser<'i, I, TopLevel> 900 + fn func<'i: 'd, 'd, I>() -> impl SParser<'i, I, Func> 810 901 where 811 902 I: ValueInput<'i, Token = Token, Span = Span>, 812 903 { 813 - kw_proc() 904 + kw_func() 814 905 .then(generics().or_not()) 815 906 .then(word()) 816 - .then(proc_signature()) 817 - .then(kw_do()) 818 - .then(indent_increase().ignore_with_ctx(body(expr()))) 907 + .then(func_signature()) 908 + .then(body(expr())) 819 909 .map_with( 820 - |(((((proc, generics), name), signature), do_), body), span| Spanned { 910 + |((((func, generics), name), signature), body), span| Spanned { 821 911 span: span.span(), 822 - inner: TopLevel::Proc(Proc { 823 - proc, 912 + inner: Func { 913 + func, 824 914 generics, 825 915 name, 826 916 signature, 827 - do_, 828 917 body, 829 - }), 918 + }, 830 919 }, 831 920 ) 832 921 } 833 922 834 - fn const_signature<'i, I>() -> impl RParser<'i, I, ConstSignature> 923 + fn type_ascription<'i, I>( 924 + ) -> impl Parser<'i, I, TypeAscription, extra::Full<Rich<'i, Token, Span>, (), usize>> + Clone 835 925 where 836 926 I: ValueInput<'i, Token = Token, Span = Span>, 837 927 { 838 928 separator() 839 - .then(ty().repeated().at_least(1).collect()) 840 - .map_with(|(sep, tys), span| Spanned { 841 - span: span.span(), 842 - inner: ConstSignature { sep, tys }, 843 - }) 929 + .then(ty()) 930 + .map(|(sep, ty)| TypeAscription { sep, ty }) 844 931 } 845 932 846 - fn const_<'i: 'd, 'd, I>() -> impl RParser<'i, I, TopLevel> 933 + fn name_type_pair<'i, I>() -> impl NParser<'i, I, NameTypePair> 847 934 where 848 935 I: ValueInput<'i, Token = Token, Span = Span>, 849 936 { 850 - kw_const() 851 - .then(word()) 852 - .then(const_signature()) 853 - .then(kw_do()) 854 - .then(expr().repeated().collect()) 855 - .map_with(|((((const_, name), signature), do_), body), span| Spanned { 856 - span: span.span(), 857 - inner: TopLevel::Const(Const { 858 - const_, 859 - name, 860 - signature, 861 - do_, 862 - body, 863 - }), 864 - }) 937 + word() 938 + .then(type_ascription()) 939 + .map(|(name, TypeAscription { sep, ty })| NameTypePair { name, sep, ty }) 865 940 } 866 941 867 - fn name_type_pair<'i, I>() -> impl RParser<'i, I, NameTypePair> 942 + fn field_value_pair<'i, I>(expr: impl SParser<'i, I, Expr>) -> impl NParser<'i, I, FieldValuePair> 868 943 where 869 944 I: ValueInput<'i, Token = Token, Span = Span>, 870 945 { 871 946 word() 872 947 .then(separator()) 873 - .then(ty()) 874 - .map_with(|((name, sep), ty), span| Spanned { 875 - span: span.span(), 876 - inner: NameTypePair { name, sep, ty }, 877 - }) 948 + .then(expr) 949 + .map(|((name, sep), expr)| FieldValuePair { name, sep, expr }) 878 950 } 879 951 880 - fn struct_<'i, I>() -> impl RParser<'i, I, TopLevel> 952 + fn struct_<'i, I>() -> impl SParser<'i, I, TopLevel> 881 953 where 882 954 I: ValueInput<'i, Token = Token, Span = Span>, 883 955 { 884 956 kw_struct() 885 957 .then(generics().or_not()) 886 958 .then(word()) 887 - .then(kw_do()) 888 - .then(name_type_pair().repeated().collect()) 889 - .map_with(|((((struct_, generics), name), do_), body), span| Spanned { 959 + .then(body_unspanned(name_type_pair())) 960 + .map_with(|(((struct_, generics), name), body), span| Spanned { 890 961 span: span.span(), 891 962 inner: TopLevel::Struct(Struct { 892 963 struct_, 893 964 generics, 894 965 name, 895 - do_, 966 + body, 967 + }), 968 + }) 969 + } 970 + 971 + fn inherent_impl<'i, I>() -> impl SParser<'i, I, TopLevel> 972 + where 973 + I: ValueInput<'i, Token = Token, Span = Span>, 974 + { 975 + kw_impl() 976 + .then(generics().or_not()) 977 + .then(ty()) 978 + .then(body_unspanned(func().map(|f| f.map(ImplLevel::Func)))) 979 + .map_with(|(((impl_, generics), ty), body), span| Spanned { 980 + span: span.span(), 981 + inner: TopLevel::InherentImpl(InherentImpl { 982 + impl_, 983 + generics, 984 + ty, 896 985 body, 897 986 }), 898 987 }) 899 988 } 900 989 901 - fn module<'i, I>() -> impl RParser<'i, I, TopLevel> 990 + fn trait_impl<'i, I>() -> impl SParser<'i, I, TopLevel> 991 + where 992 + I: ValueInput<'i, Token = Token, Span = Span>, 993 + { 994 + kw_impl() 995 + .then(generics().or_not()) 996 + .then(path()) 997 + .then(kw_for()) 998 + .then(ty()) 999 + .then(body_unspanned(func().map(|f| f.map(ImplLevel::Func)))) 1000 + .map_with( 1001 + |(((((impl_, generics), trait_), for_), ty), body), span| Spanned { 1002 + span: span.span(), 1003 + inner: TopLevel::TraitImpl(TraitImpl { 1004 + impl_, 1005 + generics, 1006 + trait_, 1007 + for_, 1008 + ty, 1009 + body, 1010 + }), 1011 + }, 1012 + ) 1013 + } 1014 + 1015 + fn module<'i, I>() -> impl SParser<'i, I, TopLevel> 902 1016 where 903 1017 I: ValueInput<'i, Token = Token, Span = Span>, 904 1018 { ··· 910 1024 }) 911 1025 } 912 1026 913 - fn use_<'i, I>() -> impl RParser<'i, I, TopLevel> 1027 + fn use_<'i, I>() -> impl SParser<'i, I, TopLevel> 914 1028 where 915 1029 I: ValueInput<'i, Token = Token, Span = Span>, 916 1030 { ··· 928 1042 } 929 1043 930 1044 pub fn file<'i: 'd, 'd, I>( 931 - ) -> impl Parser<'i, I, Module, extra::Full<Rich<'i, Token, Span>, Vec<usize>, usize>> + 'd 1045 + ) -> impl Parser<'i, I, Module, extra::Full<Rich<'i, Token, Span>, (), usize>> + 'd 932 1046 where 933 1047 I: ValueInput<'i, Token = Token, Span = Span>, 934 1048 { 935 1049 choice(( 936 1050 module(), 937 - proc(), 938 - const_(), 1051 + func().map(|f| f.map(TopLevel::Func)), 1052 + inherent_impl(), 1053 + trait_impl(), 1054 + const_toplevel(), 939 1055 static_toplevel(), 940 1056 struct_(), 941 1057 use_(), ··· 947 1063 .or(end().to(vec![])) 948 1064 .map(|items| { 949 1065 let mut module = Module { 950 - procs: Vec::new(), 1066 + funcs: Vec::new(), 951 1067 consts: Vec::new(), 952 - vars: Vec::new(), 1068 + statics: Vec::new(), 953 1069 structs: Vec::new(), 1070 + inherent_impls: Vec::new(), 1071 + trait_impls: Vec::new(), 954 1072 modules: Vec::new(), 955 1073 uses: Vec::new(), 956 1074 }; 957 1075 958 1076 for item in items { 959 1077 match item.inner { 960 - TopLevel::Proc(p) => module.procs.push(Spanned { 1078 + TopLevel::Func(p) => module.funcs.push(Spanned { 961 1079 span: item.span, 962 1080 inner: p, 963 1081 }), ··· 965 1083 span: item.span, 966 1084 inner: c, 967 1085 }), 968 - TopLevel::Var(v) => module.vars.push(Spanned { 1086 + TopLevel::Static(v) => module.statics.push(Spanned { 969 1087 span: item.span, 970 1088 inner: v, 971 1089 }), ··· 973 1091 span: item.span, 974 1092 inner: s, 975 1093 }), 1094 + TopLevel::InherentImpl(i) => module.inherent_impls.push(Spanned { 1095 + span: item.span, 1096 + inner: i, 1097 + }), 1098 + TopLevel::TraitImpl(i) => module.trait_impls.push(Spanned { 1099 + span: item.span, 1100 + inner: i, 1101 + }), 976 1102 TopLevel::Use(u) => module.uses.push(Spanned { 977 1103 span: item.span, 978 1104 inner: u, ··· 986 1112 987 1113 module 988 1114 }) 989 - .with_state(Vec::new()) 990 1115 }
+1 -1
spanner/src/lib.rs
··· 1 1 use std::{ 2 2 fmt::Debug, 3 - ops::{Deref, DerefMut, Range}, 3 + ops::Range, 4 4 path::{Path, PathBuf}, 5 5 }; 6 6
+45 -3
test.sl
··· 1 - proc fib i64 : i64 do 1 + module foo 2 + use bar from foo 3 + 4 + struct [T] FooBar 5 + foo: T 6 + 7 + impl FooBar[i32] 8 + func method self: Self => i32 9 + self.foo + 1 10 + 11 + impl[T: Copy] Frobnify for FooBar[T] 12 + func frob self: &Self => T 13 + self.foo 14 + 15 + func foo i: i64, j: u69 => i64 16 + static BAR: i64 = 69 17 + const FOO: i64 = 42 18 + let goo: i64 = 66 2 19 0 3 - match i do 20 + let foo = match i 4 21 1 => 2 5 22 3 => 4 6 - 5 23 + 5 => 24 + 6 25 + 7 26 + 8 27 + if foo == 9 28 + 10 29 + else 30 + 11 31 + while false 32 + 12 33 + let lam = lambda x, y, z: i32 y 34 + lam(6, 6, 6) 35 + alpha.beta.delta() 36 + sigma 37 + .zeta 38 + .eta( 39 + looongloong, 40 + verylong, 41 + a.fucking.expression, 42 + a.fucking 43 + .multiline 44 + .expression 45 + ) 46 + let bar = Something 47 + a: 1 48 + b: "hello"