1pub mod analysis;
2mod ast;
3pub mod ast_json;
4pub mod builtin_signatures;
5pub mod const_eval;
6pub mod diagnostic;
7pub mod diagnostic_codes;
8pub mod harness_methods;
9mod parser;
10pub mod stdlib_metadata;
11pub mod typechecker;
12pub mod visit;
13
14pub use ast::*;
15pub use diagnostic_codes::{
16 Category as DiagnosticCodeCategory, Code as DiagnosticCode, ParseRepairSafetyError, Repair,
17 RepairId, RepairSafety, RepairTemplate, REPAIR_REGISTRY,
18};
19pub use parser::*;
20pub use stdlib_metadata::{parse_for_span as parse_stdlib_metadata, StdlibMetadata};
21pub use typechecker::{
22 block_definitely_exits, format_type, stmt_definitely_exits, DiagnosticDetails,
23 DiagnosticSeverity, InlayHintInfo, TypeChecker, TypeDiagnostic,
24};
25
26pub use builtin_signatures::install_builtin_signatures;
27
28pub fn is_known_builtin(name: &str) -> bool {
30 builtin_signatures::is_builtin(name)
31}
32
33pub fn known_builtin_names() -> impl Iterator<Item = &'static str> {
36 builtin_signatures::iter_builtin_names()
37}
38
39pub fn known_builtin_metadata() -> impl Iterator<Item = builtin_signatures::BuiltinMetadata> {
40 builtin_signatures::iter_builtin_metadata()
41}
42
43#[derive(Debug)]
46pub enum PipelineError {
47 Lex(harn_lexer::LexerError),
48 Parse(ParserError),
49 TypeCheck(Box<TypeDiagnostic>),
52}
53
54impl std::fmt::Display for PipelineError {
55 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56 match self {
57 PipelineError::Lex(e) => e.fmt(f),
58 PipelineError::Parse(e) => e.fmt(f),
59 PipelineError::TypeCheck(diag) => write!(f, "type error: {}", diag.message),
60 }
61 }
62}
63
64impl std::error::Error for PipelineError {}
65
66impl From<harn_lexer::LexerError> for PipelineError {
67 fn from(e: harn_lexer::LexerError) -> Self {
68 PipelineError::Lex(e)
69 }
70}
71
72impl From<ParserError> for PipelineError {
73 fn from(e: ParserError) -> Self {
74 PipelineError::Parse(e)
75 }
76}
77
78impl PipelineError {
79 pub fn span(&self) -> Option<&harn_lexer::Span> {
81 match self {
82 PipelineError::Lex(e) => match e {
83 harn_lexer::LexerError::UnexpectedCharacter(_, span)
84 | harn_lexer::LexerError::UnterminatedString(span)
85 | harn_lexer::LexerError::UnterminatedBlockComment(span) => Some(span),
86 },
87 PipelineError::Parse(e) => match e {
88 ParserError::Unexpected { span, .. } => Some(span),
89 ParserError::UnexpectedEof { span, .. } => Some(span),
90 },
91 PipelineError::TypeCheck(diag) => diag.span.as_ref(),
92 }
93 }
94}
95
96pub fn parse_source(source: &str) -> Result<Vec<SNode>, PipelineError> {
98 let mut lexer = harn_lexer::Lexer::new(source);
99 let tokens = lexer.tokenize()?;
100 let mut parser = Parser::new(tokens);
101 Ok(parser.parse()?)
102}
103
104pub fn check_source(source: &str) -> Result<(Vec<SNode>, Vec<TypeDiagnostic>), PipelineError> {
107 let program = parse_source(source)?;
108 let diagnostics = TypeChecker::new().check_with_source(&program, source);
109 Ok((program, diagnostics))
110}
111
112pub fn check_source_strict(source: &str) -> Result<Vec<SNode>, PipelineError> {
114 let (program, diagnostics) = check_source(source)?;
115 for diag in &diagnostics {
116 if diag.severity == DiagnosticSeverity::Error {
117 return Err(PipelineError::TypeCheck(Box::new(diag.clone())));
118 }
119 }
120 Ok(program)
121}
122
123#[cfg(test)]
124mod pipeline_tests {
125 use super::*;
126
127 #[test]
128 fn parse_source_valid() {
129 let program = parse_source("let x = 1").unwrap();
130 assert!(!program.is_empty());
131 }
132
133 #[test]
134 fn parse_source_lex_error() {
135 let err = parse_source("let x = `").unwrap_err();
136 assert!(matches!(err, PipelineError::Lex(_)));
137 assert!(err.span().is_some());
138 assert!(err.to_string().contains("Unexpected character"));
139 }
140
141 #[test]
142 fn parse_source_parse_error() {
143 let err = parse_source("let = 1").unwrap_err();
144 assert!(matches!(err, PipelineError::Parse(_)));
145 assert!(err.span().is_some());
146 }
147
148 #[test]
149 fn check_source_returns_diagnostics() {
150 let (program, _diagnostics) = check_source("let x = 1").unwrap();
151 assert!(!program.is_empty());
152 }
153
154 #[test]
155 fn check_source_strict_passes_valid_code() {
156 let program = check_source_strict("let x = 1\nlog(x)").unwrap();
157 assert!(!program.is_empty());
158 }
159
160 #[test]
161 fn check_source_strict_catches_lex_error() {
162 let err = check_source_strict("`").unwrap_err();
163 assert!(matches!(err, PipelineError::Lex(_)));
164 }
165
166 #[test]
167 fn pipeline_error_display_is_informative() {
168 let err = parse_source("`").unwrap_err();
169 let msg = err.to_string();
170 assert!(!msg.is_empty());
171 assert!(msg.contains('`') || msg.contains("Unexpected"));
172 }
173
174 #[test]
175 fn pipeline_error_size_is_bounded() {
176 assert!(
178 std::mem::size_of::<PipelineError>() <= 96,
179 "PipelineError grew to {} bytes — consider boxing large variants",
180 std::mem::size_of::<PipelineError>()
181 );
182 }
183}