Skip to main content

lisette_syntax/
lib.rs

1pub mod ast;
2pub mod attributes;
3pub mod containment;
4mod display;
5pub mod go_names;
6pub mod imports;
7pub mod lex;
8pub mod parse;
9pub mod program;
10pub mod types;
11
12pub use ecow::EcoString;
13pub use parse::ParseError;
14
15pub const ENTRY_MODULE_ID: &str = "_entry_";
16pub const ROOT_IMPORT: &str = "root";
17
18pub type AstBuildResult = parse::ParseResult;
19
20#[cfg(target_pointer_width = "64")]
21mod size_assertions {
22    use std::mem::size_of;
23    const _: () = assert!(size_of::<super::ast::Expression>() == 288);
24    const _: () = assert!(size_of::<super::ast::Pattern>() == 152);
25    const _: () = assert!(size_of::<super::types::Type>() == 40);
26    const _: () = assert!(size_of::<super::ast::Span>() == 12);
27}
28
29const MAX_SOURCE_BYTES: usize = 10 * 1024 * 1024; // 10 MiB
30
31pub fn build_ast(source: &str, file_id: u32) -> AstBuildResult {
32    if source.len() > MAX_SOURCE_BYTES {
33        return parse::ParseResult {
34            ast: vec![],
35            errors: vec![
36                ParseError::new(
37                    "File too large",
38                    ast::Span::new(file_id, 0, 0),
39                    format!(
40                        "file is {} bytes, maximum is {} bytes",
41                        source.len(),
42                        MAX_SOURCE_BYTES,
43                    ),
44                )
45                .with_parse_code("file_too_large"),
46            ],
47            file_comment: None,
48            truncated: true,
49        };
50    }
51
52    let parse_result = parse::Parser::lex_and_parse_file(source, file_id);
53    if parse_result.failed() {
54        return parse::ParseResult {
55            ast: vec![],
56            errors: parse_result.errors,
57            file_comment: None,
58            truncated: true,
59        };
60    }
61
62    parse_result
63}
64
65#[cfg(test)]
66mod tests {
67    use super::ast::{BinaryOperator, Expression};
68
69    fn contains_pipeline(expression: &Expression) -> bool {
70        matches!(
71            expression,
72            Expression::Binary {
73                operator: BinaryOperator::Pipeline,
74                ..
75            }
76        ) || expression.children().into_iter().any(contains_pipeline)
77    }
78
79    #[test]
80    fn build_ast_preserves_pipeline_expression() {
81        let result = super::build_ast("fn test() { value |> transform }", 0);
82
83        assert!(result.errors.is_empty());
84        assert!(result.ast.iter().any(contains_pipeline));
85    }
86}