Skip to main content

lisette_syntax/
lib.rs

1pub mod ast;
2pub mod ast_folder;
3pub mod desugar;
4mod display;
5pub mod lex;
6pub mod parse;
7pub mod program;
8pub mod types;
9
10pub use ecow::EcoString;
11pub use parse::ParseError;
12
13use ast::Expression;
14
15#[derive(Debug)]
16pub struct AstBuildResult {
17    pub ast: Vec<Expression>,
18    pub errors: Vec<ParseError>,
19}
20
21impl AstBuildResult {
22    pub fn failed(&self) -> bool {
23        !self.errors.is_empty()
24    }
25}
26
27#[cfg(target_pointer_width = "64")]
28mod size_assertions {
29    use std::mem::size_of;
30    const _: () = assert!(size_of::<super::ast::Expression>() == 408);
31    const _: () = assert!(size_of::<super::types::Type>() == 80);
32    const _: () = assert!(size_of::<super::ast::Pattern>() == 152);
33    const _: () = assert!(size_of::<super::ast::Span>() == 12);
34}
35
36const MAX_SOURCE_BYTES: usize = 10 * 1024 * 1024; // 10 MiB
37
38pub fn build_ast(source: &str, file_id: u32) -> AstBuildResult {
39    if source.len() > MAX_SOURCE_BYTES {
40        return AstBuildResult {
41            ast: vec![],
42            errors: vec![
43                ParseError::new(
44                    "File too large",
45                    ast::Span::new(file_id, 0, 0),
46                    format!(
47                        "file is {} bytes, maximum is {} bytes",
48                        source.len(),
49                        MAX_SOURCE_BYTES,
50                    ),
51                )
52                .with_parse_code("file_too_large"),
53            ],
54        };
55    }
56
57    let parse_result = parse::Parser::lex_and_parse_file(source, file_id);
58    if parse_result.failed() {
59        return AstBuildResult {
60            ast: vec![],
61            errors: parse_result.errors,
62        };
63    }
64
65    let desugar_result = desugar::desugar(parse_result.ast);
66    AstBuildResult {
67        ast: desugar_result.ast,
68        errors: desugar_result.errors,
69    }
70}