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