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