meta-language 1.0.0

A self-describing links-network core for lossless language representation
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//! The Rust frontend: reads a Rust program into the surface AST.
//!
//! It reads the subset of Rust the core can represent faithfully: modules
//! with `use` of crate items, enums with unit and tuple variants (`Box<T>`
//! fields are the recursive occurrences of a value type), free functions over
//! machine integers, `bool` and `String`, expression bodies with `let`, `if`,
//! `match` and `format!`, and a `main` made of `println!`, `let` and
//! `assert!`/`assert_eq!` statements. Everything else is rejected with a
//! precise obligation.
//!
//! Mirrors `js/src/translation/rust.js`.

use std::collections::HashMap;

use super::aborts::rust_macro_message;
use super::diagnostics::{Result, TranslationError, type_error, unsupported};
use super::lexer::{Token, TokenCursor, TokenKind, describe, tokenize};
use super::surface::{
    BinaryOp, Flavor, Rounding, SComparison, SCtor, SData, SEffect, SExpr, SField, SFn, SItem,
    SMain, SModule, SNode, SParam, SPattern, SPatternNode, SProgram, SProp, SPropNode, SRow,
    ShowStyle, UnaryOp,
};
use super::types::{BOOL, STRING, Type, UNIT, rust_fixed_type};
use super::{Language, Span};

mod expressions;
mod items;

const ACCEPTED_DERIVES: [&str; 8] = [
    "Clone",
    "Copy",
    "Debug",
    "PartialEq",
    "Eq",
    "Hash",
    "PartialOrd",
    "Ord",
];
const ACCEPTED_LINTS: [&str; 6] = ["allow", "warn", "deny", "expect", "must_use", "inline"];
const RESERVED_ITEMS: [&str; 10] = [
    "impl",
    "trait",
    "struct",
    "union",
    "static",
    "type",
    "extern",
    "macro_rules",
    "async",
    "unsafe",
];
const UNSUPPORTED_TYPES: [&str; 11] = [
    "str", "char", "f32", "f64", "Vec", "Option", "Result", "Rc", "Arc", "HashMap", "Self",
];

fn comparison_op(value: &str) -> Option<BinaryOp> {
    Some(match value {
        "==" => BinaryOp::Eq,
        "!=" => BinaryOp::Ne,
        "<" => BinaryOp::Lt,
        "<=" => BinaryOp::Le,
        ">" => BinaryOp::Gt,
        ">=" => BinaryOp::Ge,
        _ => return None,
    })
}

fn additive_op(value: &str) -> Option<BinaryOp> {
    Some(match value {
        "+" => BinaryOp::Add,
        "-" => BinaryOp::Sub,
        _ => return None,
    })
}

fn multiplicative_op(value: &str) -> Option<BinaryOp> {
    Some(match value {
        "*" => BinaryOp::Mul,
        "/" => BinaryOp::Div,
        "%" => BinaryOp::Rem,
        _ => return None,
    })
}

/// Reads a Rust program.
///
/// # Errors
/// On syntax errors and on constructs outside the portable core.
pub fn parse_rust(source: &str) -> Result<SProgram> {
    let tokens = tokenize(source, Language::Rust)?.tokens;
    RustParser::new(tokens).file()
}

/// Where a sequence of items ends: at the end of the file or at a module's `}`.
#[derive(Clone, Copy)]
enum ItemsEnd {
    File,
    Module,
}

/// Parsing context an expression inherits from its position.
#[derive(Clone, Copy, Default)]
struct Options {
    /// The expression starts a statement, so a leading `if`/`match` ends there.
    statement: bool,
    /// `{` opens a block, not a struct literal (`if`/`match` heads).
    no_struct: bool,
}

/// A `name @ pattern` binding, bound around the arm's body.
struct Alias {
    name: String,
    value: SExpr,
}

/// A parsed `let` statement.
struct Binding {
    name: String,
    ty: Option<Type>,
    value: SExpr,
    span: Option<Span>,
}

struct RustParser {
    cursor: TokenCursor,
    /// `use` aliases, per module path.
    aliases: HashMap<String, HashMap<String, Vec<String>>>,
    module_path: Vec<String>,
    main: Option<SMain>,
}

impl RustParser {
    fn new(tokens: Vec<Token>) -> Self {
        Self {
            cursor: TokenCursor::new(tokens, Language::Rust),
            aliases: HashMap::new(),
            module_path: Vec::new(),
            main: None,
        }
    }

    fn peek(&self) -> Token {
        self.cursor.peek().clone()
    }

    fn fail(message: &str, token: &Token) -> TranslationError {
        TranslationError::syntax(
            format!("{message} but found {}", describe(token)),
            Some(token.span()),
        )
    }
}

/// `format!("{} and {name}", a)`: `{}` takes the next argument, `{name}` a
/// variable in scope, `{{`/`}}` are braces; every value is rendered with
/// Display, which for integers is its decimal text.
fn format(args: Vec<SExpr>, token: &Token) -> Result<SExpr> {
    let mut args = args.into_iter();
    let template = args.next();
    let values: Vec<SExpr> = args.collect();
    let Some(SExpr {
        node: SNode::Str { value: source },
        span: template_span,
        ..
    }) = template
    else {
        return Err(unsupported(
            &format!("{}!", token.value),
            "the format string must be a literal",
            span(token, token),
        ));
    };
    let chars: Vec<char> = source.chars().collect();
    let mut pieces: Vec<SExpr> = Vec::new();
    let mut text = String::new();
    let mut next = 0;
    let mut index = 0;
    while index < chars.len() {
        let char = chars[index];
        let following = chars.get(index + 1).copied();
        if char == '{' && following == Some('{') {
            text.push('{');
            index += 2;
            continue;
        }
        if char == '}' && following == Some('}') {
            text.push('}');
            index += 2;
            continue;
        }
        if char == '}' {
            return Err(TranslationError::syntax(
                "unmatched } in format string",
                template_span,
            ));
        }
        if char != '{' {
            text.push(char);
            index += 1;
            continue;
        }
        let Some(end) = chars[index..]
            .iter()
            .position(|&candidate| candidate == '}')
            .map(|offset| index + offset)
        else {
            return Err(TranslationError::syntax(
                "unclosed { in format string",
                template_span,
            ));
        };
        let spec: String = chars[index + 1..end].iter().collect();
        if spec.contains(':') {
            return Err(unsupported(
                &format!("format spec {{{spec}}}"),
                "only plain {} Display formatting is portable",
                template_span,
            ));
        }
        let value = if spec.is_empty() {
            let value = values.get(next).cloned();
            next += 1;
            value.ok_or_else(|| {
                TranslationError::syntax("format string has more {} than arguments", template_span)
            })?
        } else if spec.bytes().all(|byte| byte.is_ascii_digit()) {
            // A position too large for an index names no argument either.
            let position = spec.parse::<usize>().ok();
            let value = position.and_then(|position| values.get(position).cloned());
            let (Some(position), Some(value)) = (position, value) else {
                return Err(TranslationError::syntax(
                    format!("format argument {spec} is missing"),
                    template_span,
                ));
            };
            next = next.max(position + 1);
            value
        } else if is_identifier(&spec) {
            SExpr::new(SNode::Name { path: vec![spec] }, template_span)
        } else {
            return Err(unsupported(
                &format!("format argument {{{spec}}}"),
                "outside the portable core",
                template_span,
            ));
        };
        if !text.is_empty() {
            pieces.push(SExpr::new(
                SNode::Str {
                    value: std::mem::take(&mut text),
                },
                template_span,
            ));
        }
        let value_span = value.span;
        pieces.push(SExpr::new(
            SNode::Show {
                arg: Box::new(value),
                style: ShowStyle::Rust,
            },
            value_span,
        ));
        index = end + 1;
    }
    if next < values.len() {
        return Err(TranslationError::syntax(
            "format string has fewer {} than arguments",
            template_span,
        ));
    }
    if !text.is_empty() || pieces.is_empty() {
        pieces.push(SExpr::new(SNode::Str { value: text }, template_span));
    }
    let mut pieces = pieces.into_iter();
    let first = pieces
        .next()
        .unwrap_or_else(|| unreachable!("a piece was pushed"));
    Ok(pieces.fold(first, |left, right| {
        SExpr::new(
            SNode::Binary {
                op: BinaryOp::Concat,
                left: Box::new(left),
                right: Box::new(right),
                rounding: None,
            },
            template_span,
        )
    }))
}

/// `/^[A-Za-z_][A-Za-z0-9_]*$/`.
fn is_identifier(text: &str) -> bool {
    let mut bytes = text.bytes();
    bytes
        .next()
        .is_some_and(|first| first.is_ascii_alphabetic() || first == b'_')
        && bytes.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
}

fn method_call(
    receiver: SExpr,
    method: &Token,
    args: Vec<SExpr>,
    range: Option<Span>,
) -> Result<SExpr> {
    let expect = |count: usize| {
        if args.len() == count {
            Ok(())
        } else {
            Err(type_error(
                format!(
                    "{} expects {count} arguments but got {}",
                    method.value,
                    args.len()
                ),
                range,
            ))
        }
    };
    match method.value.as_str() {
        "clone" | "to_owned" => {
            expect(0)?;
            Ok(receiver)
        }
        "to_string" => {
            expect(0)?;
            Ok(SExpr::new(
                SNode::ToString {
                    arg: Box::new(receiver),
                },
                range,
            ))
        }
        "div_euclid" | "rem_euclid" => {
            expect(1)?;
            let op = if method.value == "div_euclid" {
                BinaryOp::Div
            } else {
                BinaryOp::Rem
            };
            let right = args
                .into_iter()
                .next()
                .unwrap_or_else(|| unreachable!("one argument"));
            Ok(SExpr::new(
                SNode::Binary {
                    op,
                    left: Box::new(receiver),
                    right: Box::new(right),
                    rounding: Some(Rounding::Euclid),
                },
                range,
            ))
        }
        _ => Err(unsupported(
            &format!("method {}", method.value),
            "outside the portable core",
            range,
        )),
    }
}

/// An imported path from the crate root, so it means the same wherever it is used.
fn absolute(segments: &[String], path: &[String], range: Option<Span>) -> Result<Vec<String>> {
    let crate_root = || vec!["crate".to_owned()];
    if segments[0] == "crate" {
        return Ok(segments.to_vec());
    }
    if segments[0] == "self" {
        let mut result = crate_root();
        result.extend_from_slice(path);
        result.extend_from_slice(&segments[1..]);
        return Ok(result);
    }
    let up = segments
        .iter()
        .take_while(|segment| *segment == "super")
        .count();
    if up > path.len() {
        return Err(type_error("super beyond crate root", range));
    }
    let mut result = crate_root();
    result.extend_from_slice(&path[..path.len() - up]);
    result.extend_from_slice(&segments[up..]);
    Ok(result)
}

fn prop_of(expr: SExpr) -> SProp {
    let node = match expr.node {
        SNode::Binary {
            op:
                op @ (BinaryOp::Eq
                | BinaryOp::Ne
                | BinaryOp::Lt
                | BinaryOp::Le
                | BinaryOp::Gt
                | BinaryOp::Ge),
            left,
            right,
            ..
        } => {
            let comparison = SComparison {
                left: *left,
                right: *right,
                reference: false,
                same_value: false,
            };
            match op {
                BinaryOp::Eq => SPropNode::Eq(comparison),
                BinaryOp::Ne => SPropNode::Ne(comparison),
                BinaryOp::Lt => SPropNode::Lt(comparison),
                BinaryOp::Le => SPropNode::Le(comparison),
                BinaryOp::Gt => SPropNode::Gt(comparison),
                _ => SPropNode::Ge(comparison),
            }
        }
        SNode::Binary {
            op: BinaryOp::And,
            left,
            right,
            ..
        } => SPropNode::And {
            left: Box::new(prop_of(*left)),
            right: Box::new(prop_of(*right)),
        },
        SNode::Binary {
            op: BinaryOp::Or,
            left,
            right,
            ..
        } => SPropNode::Or {
            left: Box::new(prop_of(*left)),
            right: Box::new(prop_of(*right)),
        },
        SNode::Unary {
            op: UnaryOp::Not,
            arg,
        } => SPropNode::Not {
            arg: Box::new(prop_of(*arg)),
        },
        node => SPropNode::Bool {
            expr: SExpr { node, ..expr },
        },
    };
    SProp { node, span: None }
}

/// The value a pattern matched, for `name @ p`.
fn pattern_value(pattern: &SPattern, range: Option<Span>) -> Result<SExpr> {
    let node = match &pattern.node {
        SPatternNode::BindOrCtor { name } => SNode::Name {
            path: vec![name.clone()],
        },
        SPatternNode::NumLit { value, negative } => SNode::Num {
            value: value.clone(),
            ty: None,
            negative: *negative,
            unit: false,
        },
        SPatternNode::BoolLit { value, .. } => SNode::Bool { value: *value },
        SPatternNode::Ctor { path, args } if args.is_empty() => SNode::Name { path: path.clone() },
        SPatternNode::Ctor { path, args } => SNode::App {
            func: Box::new(SExpr::new(SNode::Name { path: path.clone() }, range)),
            args: args
                .iter()
                .map(|arg| pattern_value(arg, range))
                .collect::<Result<_>>()?,
        },
        _ => {
            return Err(unsupported(
                "@ binding",
                "the aliased pattern must bind every field",
                range,
            ));
        }
    };
    Ok(SExpr::new(node, range))
}

const fn is_if_or_match(expr: &SExpr) -> bool {
    matches!(expr.node, SNode::If { .. } | SNode::Match { .. })
}

fn operator(token: &Token, table: fn(&str) -> Option<BinaryOp>) -> Option<BinaryOp> {
    if token.kind == TokenKind::Punct {
        table(&token.value)
    } else {
        None
    }
}

fn binary(op: BinaryOp, left: SExpr, right: SExpr, token: &Token) -> SExpr {
    let range = joined(left.span, right.span, token);
    SExpr::new(
        SNode::Binary {
            op,
            left: Box::new(left),
            right: Box::new(right),
            rounding: None,
        },
        range,
    )
}

#[allow(clippy::unnecessary_wraps)] // nodes and errors take an optional span
fn joined(left: Option<Span>, right: Option<Span>, token: &Token) -> Option<Span> {
    Some(Span::new(
        left.map_or(token.start, |left| left.start),
        right.map_or(token.end, |right| right.end),
    ))
}

#[allow(clippy::unnecessary_wraps)] // nodes and errors take an optional span
fn span(from: &Token, to: &Token) -> Option<Span> {
    let to_start = if to.kind == TokenKind::Eof {
        from.end
    } else {
        to.start
    };
    Some(Span::new(from.start, from.end.max(to_start)))
}