Skip to main content

cairo_lang_parser/
db.rs

1use cairo_lang_diagnostics::{Diagnostics, DiagnosticsBuilder, Maybe, ToMaybe};
2use cairo_lang_filesystem::db::FilesGroup;
3use cairo_lang_filesystem::ids::{FileId, FileKind};
4use cairo_lang_syntax::node::ast::{Expr, StatementList, SyntaxFile};
5use cairo_lang_syntax::node::{SyntaxNode, TypedSyntaxNode};
6use salsa::Database;
7
8use crate::diagnostic::ParserDiagnostic;
9use crate::parser::Parser;
10
11#[cfg(test)]
12#[path = "db_test.rs"]
13mod db_test;
14
15/// Interface of the parser database.
16pub trait ParserGroup: Database {
17    /// Parses a file and returns its AST as a root SyntaxNode.
18    fn file_syntax<'db>(&'db self, file_id: FileId<'db>) -> Maybe<SyntaxNode<'db>> {
19        file_syntax(self.as_dyn_database(), file_id)
20    }
21
22    /// Parses a file and returns its AST as a root SyntaxFile.
23    fn file_module_syntax<'db>(&'db self, file_id: FileId<'db>) -> Maybe<SyntaxFile<'db>> {
24        file_module_syntax(self.as_dyn_database(), file_id)
25    }
26    /// Parses a file and returns its AST as an expression. Only used for inline macros expanded
27    /// code.
28    fn file_expr_syntax<'db>(&'db self, file_id: FileId<'db>) -> Maybe<Expr<'db>> {
29        file_expr_syntax(self.as_dyn_database(), file_id)
30    }
31    /// Parses a file and returns its AST as a list of statements. Only used for inline macros
32    /// expanded code.
33    fn file_statement_list_syntax<'db>(
34        &'db self,
35        file_id: FileId<'db>,
36    ) -> Maybe<StatementList<'db>> {
37        file_statement_list_syntax(self.as_dyn_database(), file_id)
38    }
39    /// Returns the parser diagnostics for this file.
40    fn file_syntax_diagnostics<'db>(
41        &'db self,
42        file_id: FileId<'db>,
43    ) -> &'db Diagnostics<'db, ParserDiagnostic<'db>> {
44        file_syntax_diagnostics(self.as_dyn_database(), file_id)
45    }
46}
47
48impl<T: Database + ?Sized> ParserGroup for T {}
49
50#[salsa::tracked]
51struct SyntaxData<'db> {
52    diagnostics: Diagnostics<'db, ParserDiagnostic<'db>>,
53    syntax: Maybe<SyntaxNode<'db>>,
54}
55
56/// Parses a file and returns the result and the generated [ParserDiagnostic].
57#[salsa::tracked(returns(ref))]
58fn file_syntax_data<'db>(db: &'db dyn Database, file_id: FileId<'db>) -> SyntaxData<'db> {
59    let mut diagnostics = DiagnosticsBuilder::default();
60    let syntax = db.file_content(file_id).to_maybe().map(|s| {
61        let green = match file_id.kind(db) {
62            FileKind::Module => Parser::parse_file_green(db, &mut diagnostics, file_id, s).0,
63            FileKind::Expr => Parser::parse_file_expr_green(db, &mut diagnostics, file_id, s).0,
64            FileKind::StatementList => {
65                Parser::parse_file_statement_list_green(db, &mut diagnostics, file_id, s).0
66            }
67        };
68        // Seed the canonical root from this file-keyed query (not a green-keyed detached
69        // constructor), so reparsing a changed file reuses the previous node ids. This keeps
70        // stable ptrs (and ids derived from them) stable across edits, which early cutoff
71        // downstream relies on. This is the *only* place a canonical root is created; every other
72        // consumer reaches it via `db.file_syntax(file_id)`.
73        SyntaxNode::new_canonical_root(db, file_id, green)
74    });
75    SyntaxData::new(db, diagnostics.build(), syntax)
76}
77
78/// Parses a file and returns its SyntaxNode.
79#[salsa::tracked]
80fn file_syntax<'db>(db: &'db dyn Database, file_id: FileId<'db>) -> Maybe<SyntaxNode<'db>> {
81    file_syntax_data(db, file_id).syntax(db)
82}
83
84/// Parses a file and returns its AST as a root SyntaxFile.
85/// Requires `file_id.kind()` to be `FileKind::Module`.
86fn file_module_syntax<'db>(db: &'db dyn Database, file_id: FileId<'db>) -> Maybe<SyntaxFile<'db>> {
87    assert_eq!(file_id.kind(db), FileKind::Module, "file_id must be a module");
88    Ok(SyntaxFile::from_syntax_node(db, file_syntax(db, file_id)?))
89}
90
91/// Parses a file and returns its AST as an expression. Only used for inline macros expanded code.
92/// Requires `file_id.kind()` to be `FileKind::Expr`.
93fn file_expr_syntax<'db>(db: &'db dyn Database, file_id: FileId<'db>) -> Maybe<Expr<'db>> {
94    assert_eq!(file_id.kind(db), FileKind::Expr, "file_id must be an expr");
95    Ok(Expr::from_syntax_node(db, file_syntax(db, file_id)?))
96}
97
98/// Parses a file and returns its AST as a list of statements. Only used for inline macros
99/// expanded code.
100/// Requires `file_id.kind()` to be `FileKind::StatementList`.
101fn file_statement_list_syntax<'db>(
102    db: &'db dyn Database,
103    file_id: FileId<'db>,
104) -> Maybe<StatementList<'db>> {
105    assert_eq!(file_id.kind(db), FileKind::StatementList, "file_id must be a statement list");
106    Ok(StatementList::from_syntax_node(db, file_syntax(db, file_id)?))
107}
108
109#[salsa::tracked(returns(ref))]
110fn file_syntax_diagnostics<'db>(
111    db: &'db dyn Database,
112    file_id: FileId<'db>,
113) -> Diagnostics<'db, ParserDiagnostic<'db>> {
114    file_syntax_data(db, file_id).diagnostics(db)
115}