Skip to main content

lisette_syntax/
lib.rs

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