aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/parse.rs
blob: c5259820a2875e37ca2a727a351b6eab7b736d36 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use std::fmt;

use crate::frontend::lex::*;
use crate::frontend::ast::*;
use crate::frontend::ast::Binding::*;
use crate::frontend::ast::Control::*;
use crate::frontend::ast::Pattern::*;
use Token::*;
use Literal::*;
use Punctuation::*;

type Input = std::iter::Peekable<std::vec::IntoIter<Token>>;

#[derive(Clone, Copy)]
struct State {
    depth: usize,
    step: usize
}

impl State {
    fn indent(&self) -> State {
        State { depth: self.depth + self.step, step: self.step }
    }
    fn dedent(&self) -> State {
        State { depth: self.depth - self.step, step: self.step }
    }
}

/// Convert a basic TokenStream into an AbstractSyntaxTree
pub fn astify(input: TokenStream, name: &str) -> Result<Expr> {
    let mut input = input.into_iter().peekable();
    let body = parse_body(&mut input, State { depth: 0, step: 0 })?;
    Ok(Expr::Binding(Module{ id: name.to_string(), body }))
}

/// Parse a series of Exprs, for ex. the body of a function.
fn parse_body(input: &mut Input, state: State) -> Result<Vec<Expr>> {
    let mut res = Vec::new();
    while input.peek() == Some(&Indent(state.depth)) {
        input.next();
        res.push(parse_expr(input, state)?);
    }
    Ok(res)
}

/// Expr ::= Let | Var | Const | Func | Type |
///          Mod | Import | Block | Static |
///          For | While | Loop | If | When | Try | Match
fn parse_expr(input: &mut Input, state: State) -> Result<Expr> {
    use Keyword::*;
    match input.next() {
        Some(Key(word)) => match word {
            Pub => {
                match input.next() {
                    Some(Key(word)) => match word {
                        Const => parse_const(input, state, true),
                        Func => parse_funcdecl(input, state, true),
                        Type => parse_typedecl(input, state, true),
                        Mod => parse_mod(input, state, true),
                        _ => return Err("unrecognized keyword following pub".into()),
                    }
                    Some(_) => return Err("unrecognized thing following pub".into()),
                    None => return Err("end of input".into()),
                }
            },
            Let => parse_let(input, state),
            Var => parse_var(input, state),
            Const => parse_const(input, state, false),
            Func => parse_funcdecl(input, state, false),
            Type => parse_typedecl(input, state, false),
            Mod => parse_mod(input, state, false),
            From => parse_import(input, state, true),
            Import => parse_import(input, state, false),
            Block => parse_block(input, state),
            Static => parse_static(input, state),
            For => parse_for(input, state),
            While => parse_while(input, state),
            Loop => parse_loop(input, state),
            If => parse_if(input, state),
            When => parse_when(input, state),
            Try => parse_try(input, state),
            Match => parse_match(input, state),
            _ => return Err("invalid keyword starting expression".into()),
        },
        _ => todo!(), // what can i do with this?? match line here
    }
}

/// Let ::= 'let' Pattern Annotation? '=' Expr
fn parse_let(input: &mut Input, state: State) -> Result<Expr> {
    let id = parse_pattern(input, state)?;
    let mut kind = None;
    if let Some(Sep(Colon)) = input.peek() {
        input.next();
        kind = Some(parse_typedesc(input, state)?);
    }
    if input.next() != Some(Sep(Equals)) {
        return Err("= not following binding".into())
    }
    let value = Box::new(parse_expr(input, state)?);
    Ok(Expr::Binding(Let { id, kind, value }))
}
/// Var ::= 'var' Pattern Annotation? ('=' Expr)?
fn parse_var(input: &mut Input, state: State) -> Result<Expr> {
    let id = parse_pattern(input, state)?;
    let mut kind = None;
    if let Some(Sep(Colon)) = input.peek() {
        input.next();
        kind = Some(parse_typedesc(input, state)?);
    }
    let mut value = None;
    if input.next() != Some(Sep(Equals)) {
        value = Some(Box::new(parse_expr(input, state)?));
    }
    Ok(Expr::Binding(Var { id, kind, value }))
}
// Const ::= 'pub'? 'const' Pattern Annotation? '=' Expr
fn parse_const(input: &mut Input, state: State, public: bool) -> Result<Expr> {
    let id = parse_pattern(input, state)?;
    let mut kind = None;
    if let Some(Sep(Colon)) = input.peek() {
        input.next();
        kind = Some(parse_typedesc(input, state)?);
    }
    if input.next() != Some(Sep(Equals)) {
        return Err("= not following binding".into())
    }
    let value = Box::new(parse_expr(input, state)?);
    Ok(Expr::Binding(Const { public, id, kind, value }))
}
// Func ::= 'pub'? ('func' | 'proc') Ident Generics? Parameters? (':' TypeDesc) '=' Body
fn parse_funcdecl(input: &mut Input, state: State, public: bool) -> Result<Expr> { todo!() }
// TypeDecl ::= 'pub'? 'type' Pattern Generics? '=' 'distinct'? 'ref'? TypeDesc
fn parse_typedecl(input: &mut Input, state: State, public: bool) -> Result<Expr> {
    let pattern = parse_pattern(input, state)?;
    todo!()
}
// Mod ::= 'pub'? 'mod' Ident ':' Body
fn parse_mod(input: &mut Input, state: State, public: bool) -> Result<Expr> {
    match input.next() {
        Some(Word(id)) => {
            match input.next() {
                Some(Sep(Colon)) => {
                    let body = parse_body(input, state.indent())?;
                    Ok(Expr::Binding(Module { id, body }))
                },
                _ => return Err("unexpected token following mod label".into()),
            }
        },
        _ => return Err("unexpected thing following mod keyword".into()),
    }
}

// Import ::= ('from' Ident)? 'import' Ident (',' Ident)* ('as' Ident)?
fn parse_import(input: &mut Input, state: State, from_scope: bool) -> Result<Expr> {
    let mut from = None;
    if from_scope {
        match input.next() {
            Some(Word(id)) => from = Some(id),
            _ => return Err("identifier not following from keyword".into())
        }
        if input.next() != Some(Key(Keyword::Import)) {
            return Err("expected import to follow from".into())
        }
    }
    todo!()
}
// Block ::= 'block' Ident? ':' Body
fn parse_block(input: &mut Input, state: State) -> Result<Expr> { // todo: body + offset
    match input.next() {
        Some(Sep(Colon)) => {
            let id = None;
            let body = parse_body(input, state.indent())?;
            Ok(Expr::Control(Block { id, body }))
        },
        Some(Word(label)) => {
            match input.next() {
                Some(Sep(Colon)) => {
                    let id = Some(label);
                    let body = parse_body(input, state.indent())?;
                    Ok(Expr::Control(Block { id, body }))
                },
                _ => return Err("unexpected token following block label".into()),
            }
        },
        _ => return Err("unexpected thing following block keyword".into()),
    }
}
// Static ::= 'static' ':' Body
fn parse_static(input: &mut Input, state: State) -> Result<Expr> {
    if input.next() != Some(Sep(Colon)) {
        return Err("colon must follow static invocation".into());
    }
    let body = parse_body(input, state.indent())?;
    Ok(Expr::Control(Static { body }))
}

// For ::= 'for' Pattern 'in' Expr ':' Body
fn parse_for(input: &mut Input, state: State) -> Result<Expr> {
    let binding = parse_pattern(input, state)?;
    if input.next() != Some(Key(Keyword::In)) {
        return Err("expected in keyword after for pattern".into());
    }
    let range = Box::new(parse_expr(input, state)?);
    if input.next() != Some(Sep(Colon)) {
        return Err("expected colon after in expression".into());
    }
    let body = parse_body(input, state.indent())?;
    Ok(Expr::Control(For { binding, range, body }))
}
// While ::= 'while' Expr ':' Body
fn parse_while(input: &mut Input, state: State) -> Result<Expr> {
    let cond = Box::new(parse_expr(input, state)?);
    if input.next() != Some(Sep(Colon)) {
        return Err("expected colon after while keyword".into());
    }
    let body = parse_body(input, state.indent())?;
    Ok(Expr::Control(While { cond, body }))
}
// Loop ::= 'loop' ':' Body
fn parse_loop(input: &mut Input, state: State) -> Result<Expr> {
    if input.next() != Some(Sep(Colon)) {
        return Err("expected colon after loop keyword".into());
    }
    let body = parse_body(input, state.indent())?;
    Ok(Expr::Control(Loop { body }))
}

// If ::= 'if' Expr ':' Body ('elif' Expr ':' Body)* ('else' ':' Body)?
fn parse_if(input: &mut Input, state: State) -> Result<Expr> {
    let mut branches = Vec::new();
    branches.push(parse_cond_branch(input, state)?);
    while input.peek() == Some(&Key(Keyword::Elif)) {
        input.next();
        branches.push(parse_cond_branch(input, state)?);
    }
    let mut else_body = None;
    if input.peek() == Some(&Key(Keyword::Else)) {
        input.next();
        else_body = Some(parse_body(input, state.indent())?);
    }
    Ok(Expr::Control(If { branches, else_body }))
}
// When ::= 'when' Expr ':' Body ('elif' Expr ':' Body)* ('else' ':' Body)?
fn parse_when(input: &mut Input, state: State) -> Result<Expr> {
    let mut branches = Vec::new();
    branches.push(parse_cond_branch(input, state)?);
    while input.peek() == Some(&Key(Keyword::Elif)) {
        input.next();
        branches.push(parse_cond_branch(input, state)?);
    }
    let mut else_body = None;
    if input.peek() == Some(&Key(Keyword::Else)) {
        input.next();
        else_body = Some(parse_body(input, state.indent())?);
    }
    let mut body = Vec::new();
    body.push(Expr::Control(If { branches, else_body }));
    Ok(Expr::Control(Static { body }))
}
// Try ::= 'try' ':' Body ('except' Ident (',' Ident)* ':' Body) ('finally' ':' Body)?
fn parse_try(input: &mut Input, state: State) -> Result<Expr> {
    if input.next() != Some(Sep(Colon)) {
        return Err("expected colon after try keyword".into());
    }
    let body = parse_body(input, state.indent())?;
    let catches = Vec::new();
    while input.peek() == Some(&Key(Keyword::Catch)) {
        input.next();
        todo!();
    }
    let mut finally = None;
    if input.peek() == Some(&Key(Keyword::Finally)) {
        input.next();
        if input.next() != Some(Sep(Colon)) {
            return Err("expected colon after try keyword".into());
        }
        finally = Some(parse_body(input, state.indent())?);
    }
    Ok(Expr::Control(Try { body, catches, finally }))
}
// Match ::= 'match' Expr ('of' Pattern (',' Pattern)* ('where' Expr)? ':' Body)+
fn parse_match(input: &mut Input, state: State) -> Result<Expr> {
    let item = parse_pattern(input, state)?;
    let mut branches = Vec::new();
    while input.peek() == Some(&Key(Keyword::Of)) {
        input.next();
        todo!();
    }
    Ok(Expr::Control(Match { item, branches }))
}

fn parse_typedesc(input: &mut Input, state: State) -> Result<Type> { todo!() }
fn parse_pattern(input: &mut Input, state: State) -> Result<Pattern> { todo!() }
fn parse_cond_branch(input: &mut Input, state: State) -> Result<CondBranch> { todo!() }

// lex, parse, expand, compile?