pine_sema/lib.rs
1//! Semantic analysis for Pine Script — a static pre-check that runs after
2//! parsing and before execution.
3//!
4//! # Example
5//!
6//! ```
7//! use pine_ast::Program;
8//! use pine_lexer::Lexer;
9//! use pine_parser::Parser;
10//!
11//! let src = "x = clse + 1\n"; // typo: `clse`
12//! let tokens = Lexer::new(src).tokenize().unwrap();
13//! let program = Program::new(Parser::new(tokens).parse().unwrap());
14//!
15//! use std::collections::HashMap;
16//! use pine_interpreter::{DefaultPineOutput, Value};
17//!
18//! // The built-ins the runtime registers; here just `close`.
19//! let mut builtins: HashMap<String, Value<DefaultPineOutput>> = HashMap::new();
20//! builtins.insert("close".to_string(), Value::Na);
21//!
22//! let errors = pine_sema::analyze(&program, &builtins);
23//! assert_eq!(errors.len(), 1);
24//! assert_eq!(errors[0].rule, "undeclared-variable");
25//! ```
26
27mod analyzer;
28mod scope;
29
30pub use analyzer::Analyzer;
31pub use pine_diagnostics::{Diagnostic, Severity};
32pub use scope::SymbolKind;
33
34use pine_ast::Program;
35use pine_interpreter::{PineOutput, Value};
36use std::collections::HashMap;
37
38/// Run semantic analysis over a parsed program and return every error found.
39/// An empty result means the program passed all implemented semantic checks.
40///
41/// `builtins` is the runtime's registered built-ins (from
42/// `pine_builtins::register_namespace_objects` plus the per-bar variables) — the
43/// names that resolve without a user declaration. It is taken as the full value
44/// map so later passes can inspect the objects' types.
45pub fn analyze<O: PineOutput>(
46 program: &Program,
47 builtins: &HashMap<String, Value<O>>,
48) -> Vec<Diagnostic> {
49 Analyzer::new(builtins).analyze(program)
50}