fnotation 0.11.2

A simple lower-house syntax for programming language experimentation
Documentation
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use crate::token::{self, *};
use crate::types::*;

use bumpalo::collections::Vec;
use bumpalo::Bump;
use std::cell::Cell;
use std::collections::HashMap;
use tattle::{declare_error, Loc, Reporter};

macro_rules! error {
    ($p:expr, $m:expr, $msg:literal) => {{
        $p.error($m, None, format!($msg))
    }};
    ($p:expr, $m:expr, $msg:literal, $($arg:expr),+) => {{
        $p.error($m, None, format!($msg, $($arg),+))
    }};
}

macro_rules! error_at {
    ($p:expr, $m:expr, $l:expr, $msg:literal) => {{
        $p.error($m, Some($l), format!($msg))
    }};
    ($p:expr, $m:expr, $l:expr, $msg:literal, $($arg:expr),+) => {{
        $p.error($m, Some($l), format!($msg, $($arg),+))
    }};
}

declare_error!(SYNTAX_ERROR, "syntax", "an error during the parsing phase");

pub type Marker = usize;

pub struct Parser<'a> {
    src: &'a str,
    reporter: Reporter,
    tokens: std::vec::Vec<Token>,
    arena: &'a Bump,
    prectable: HashMap<String, Prec>,
    pos: Cell<usize>,
    gas: Cell<usize>,
}

impl<'a> Parser<'a> {
    pub fn new(
        src: &'a str,
        reporter: Reporter,
        prectable: HashMap<String, Prec>,
        tokens: std::vec::Vec<Token>,
        arena: &'a Bump,
    ) -> Self {
        Parser {
            src,
            reporter,
            tokens,
            arena,
            prectable,
            pos: Cell::new(0),
            gas: Cell::new(256),
        }
    }

    pub fn cur(&self) -> Kind {
        if self.gas.get() == 0 {
            println!("probable infinite loop in grammar, stuck after parsing:");
            for tok in self.tokens[0..self.pos.get()].iter() {
                print!("{} ", tok);
            }
            println!();
            panic!();
        }

        self.gas.set(self.gas.get() - 1);
        if self.pos.get() >= self.tokens.len() {
            token::EOF
        } else {
            self.tokens[self.pos.get()].kind
        }
    }

    pub fn no_preceding_whitespace(&self) -> bool {
        if self.pos.get() >= self.tokens.len() {
            false
        } else {
            !self.tokens[self.pos.get()].preceding_whitespace
        }
    }

    pub fn advance(&self) {
        self.gas.set(256);
        self.pos.set(self.pos.get() + 1);
    }

    pub fn open(&self) -> Marker {
        self.tokens[self.pos.get().min(self.tokens.len() - 1)]
            .loc
            .start
    }

    fn closing_pos(&self) -> Marker {
        let prev = self.pos.get().min(self.tokens.len()) - 1;
        self.tokens[prev].loc.end
    }

    pub fn loc_from(&self, m: Marker) -> Loc {
        let n = self.closing_pos();
        Loc::new(n.min(m), n)
    }

    pub fn close(&self, m: Marker, node: FNtn0<'a>) -> &'a FNtn<'a> {
        self.arena.alloc(FNtn::L(self.loc_from(m), node))
    }

    pub fn close_with(&self, m: Marker, e: Marker, node: FNtn0<'a>) -> &'a FNtn<'a> {
        self.arena.alloc(FNtn::L(Loc::new(m, e), node))
    }

    pub fn new_vec<T>(&self) -> Vec<'a, T> {
        Vec::new_in(self.arena)
    }

    pub fn slice(&self) -> &'a str {
        if self.pos.get() >= self.tokens.len() {
            ""
        } else {
            self.tokens[self.pos.get()].loc.slice(self.src)
        }
    }

    pub fn error(&self, m: Marker, loc: Option<Loc>, message: String) -> &'a FNtn<'a> {
        let loc = loc.unwrap_or_else(|| self.loc_from(m));
        self.reporter.error(loc, SYNTAX_ERROR, message);
        self.close(m, Error)
    }

    pub fn prec(&self, op: &str) -> Option<Prec> {
        self.prectable.get(op).copied()
    }

    pub fn at(&self, token: Kind) -> bool {
        self.cur() == token
    }

    /// Only use in situation we are sure it won't
    /// cause an infinite loop.
    pub fn cur_free(&self) -> Kind {
        if self.pos.get() >= self.tokens.len() {
            token::EOF
        } else {
            self.tokens[self.pos.get()].kind
        }
    }

    pub fn at_free(&self, token: Kind) -> bool {
        self.cur_free() == token
    }

    pub fn eat_free(&self, m: Marker, kind: Kind) -> Result<(), &'a FNtn<'a>> {
        let cur = self.cur_free();
        if cur == kind {
            self.advance();
            Ok(())
        } else {
            Err(error_at!(
                self,
                m,
                self.cur_loc(),
                "unexpected token {:?}, expected {:?}",
                cur,
                kind
            ))
        }
    }

    pub fn at_any(&self, tokens: &[Kind]) -> bool {
        tokens.contains(&self.cur())
    }

    pub fn cur_loc(&self) -> Loc {
        if self.pos.get() >= self.tokens.len() {
            Loc::new(self.src.len(), self.src.len() + 1)
        } else {
            self.tokens[self.pos.get()].loc
        }
    }

    pub fn eat(&self, m: Marker, kind: Kind) -> Result<(), &'a FNtn<'a>> {
        let cur = self.cur();
        if cur == kind {
            self.advance();
            Ok(())
        } else {
            Err(error_at!(
                self,
                m,
                self.cur_loc(),
                "unexpected token {:?}, expected {:?}",
                cur,
                kind
            ))
        }
    }

    pub fn open_with(&self, kind: Kind) -> Marker {
        assert!(self.at(kind));
        let m = self.open();
        self.advance();
        m
    }

    pub fn advance_close(&self, m: Marker, node: FNtn0<'a>) -> &'a FNtn<'a> {
        self.advance();
        self.close(m, node)
    }

    pub fn slice_when(&self, m: Marker, kind: Kind) -> Result<&'a str, &'a FNtn<'a>> {
        let s = self.slice();
        self.eat(m, kind).map(|_| s)
    }
}

pub type PResult<'a> = Result<&'a FNtn<'a>, &'a FNtn<'a>>;

pub fn get<'a>(r: PResult<'a>) -> &'a FNtn<'a> {
    r.unwrap_or_else(|s| s)
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Assoc {
    Left,
    Right,
    Non,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Prec {
    tightness: usize,
    assoc: Assoc,
}

impl Prec {
    pub const fn lassoc(tightness: usize) -> Self {
        Prec {
            tightness,
            assoc: Assoc::Left,
        }
    }

    pub const fn rassoc(tightness: usize) -> Self {
        Prec {
            tightness,
            assoc: Assoc::Right,
        }
    }

    pub const fn nonassoc(tightness: usize) -> Self {
        Prec {
            tightness,
            assoc: Assoc::Non,
        }
    }

    fn loosest() -> Self {
        Prec {
            tightness: 0,
            assoc: Assoc::Non,
        }
    }
}

impl PartialOrd for Prec {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        use std::cmp::Ordering::*;
        use Assoc::*;
        match self.tightness.cmp(&other.tightness) {
            Less => Some(Less),
            Equal => match (self.assoc, other.assoc) {
                (Left, Left) => Some(Greater),
                (Right, Right) => Some(Less),
                _ => None,
            },
            Greater => Some(Greater),
        }
    }
}

#[derive(Clone, Copy, PartialEq)]
pub struct BinOp<'a> {
    pub expr: &'a FNtn<'a>,
    pub prec: Prec,
}

impl<'a> BinOp<'a> {
    fn start(&self) -> usize {
        self.expr.loc().start
    }

    fn app(&self, x: &'a FNtn<'a>, y: &'a FNtn<'a>) -> FNtn0<'a> {
        App2(self.expr, x, y)
    }
}

impl<'a> PartialOrd for BinOp<'a> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.prec.partial_cmp(&other.prec)
    }
}

enum PrecState<'a> {
    Complete(&'a FNtn<'a>),
    HalfApp(&'a FNtn<'a>, BinOp<'a>),
    Error(usize), // start position of the error
}

pub struct TermStack<'a> {
    states: Vec<'a, PrecState<'a>>,
    start: usize,
}

impl<'a> TermStack<'a> {
    pub fn new(start: usize, p: &Parser<'a>) -> Self {
        TermStack {
            start,
            states: p.new_vec(),
        }
    }

    pub fn push_term(&mut self, p: &Parser<'a>, i: &'a FNtn<'a>) {
        use PrecState::*;
        match self.states.pop() {
            None => self.states.push(Complete(i)),
            Some(x) => match x {
                Complete(f) => self
                    .states
                    .push(Complete(p.close(f.loc().start, App1(f, i)))),
                HalfApp(l, op) => {
                    self.states.push(HalfApp(l, op));
                    self.states.push(Complete(i));
                }
                Error(i) => self.states.push(Error(i)),
            },
        }
    }

    pub fn collect_for(&mut self, p: &Parser<'a>, prec: Prec, mut i: &'a FNtn<'a>) -> &'a FNtn<'a> {
        use PrecState::*;
        while let Some(x) = self.states.pop() {
            match x {
                Complete(_j) => {
                    // this should never happen
                    panic!()
                }
                HalfApp(l, op) => {
                    if op.prec > prec {
                        i = p.close_with(l.loc().start, i.loc().end, op.app(l, i))
                    } else {
                        self.states.push(HalfApp(l, op));
                        return i;
                    }
                }
                Error(u) => {
                    self.states.push(Error(u));
                    return i;
                }
            }
        }
        i
    }

    pub fn push_binop(&mut self, p: &Parser<'a>, op: BinOp<'a>) -> Result<(), &'a FNtn<'a>> {
        use PrecState::*;
        match self.states.pop() {
            None => Err(error!(p, op.start(), "expected term before binary op"))?,
            Some(x) => match x {
                Complete(i) => {
                    let i = self.collect_for(p, op.prec, i);
                    self.states.push(HalfApp(i, op))
                }
                HalfApp(i, _prevop) => {
                    error!(p, i.loc().start, "expected term after binary op");
                    self.states.push(Error(i.loc().start))
                }
                Error(i) => self.states.push(Error(i)),
            },
        }
        Ok(())
    }

    pub fn finish(mut self, p: &Parser<'a>) -> &'a FNtn<'a> {
        use PrecState::*;
        if self.states.is_empty() {
            return error!(p, self.start, "uncompleted term");
        }
        let i = match self.states.pop().unwrap() {
            Complete(i) => self.collect_for(p, Prec::loosest(), i),
            _ => {
                return error!(p, self.start, "uncompleted term");
            }
        };
        if !self.states.is_empty() {
            return error!(p, self.start, "uncompleted term");
        } else {
            i
        }
    }
}