Skip to main content

oak_core/parser/
mod.rs

1#![doc = include_str!("readme.md")]
2
3/// Pratt parser implementation for operator precedence parsing.
4pub mod pratt;
5/// Parser memory pool management.
6pub mod session;
7/// Internal parser state and checkpointing.
8pub mod state;
9
10pub use self::{
11    pratt::{Associativity, OperatorInfo, Pratt, PrattParser, binary, postfix, unary},
12    session::{ParseCache, ParseSession},
13    state::{ParserState, deep_clone_node},
14};
15
16pub use triomphe::Arc;
17
18pub use crate::{
19    Language, Lexer,
20    errors::{OakDiagnostics, OakError},
21    source::{Source, TextEdit},
22    tree::GreenNode,
23};
24
25/// The output of a parsing operation, containing the result and diagnostics.
26pub type ParseOutput<'a, L: Language> = OakDiagnostics<&'a GreenNode<'a, L>>;
27
28/// Core parser trait that defines how to run the parser.
29pub trait Parser<L: Language + Send + Sync>
30where
31    L::ElementType: From<L::TokenType>,
32{
33    /// The core parsing entry point.
34    ///
35    /// This method orchestrates the parsing process using the provided cache.
36    /// It should handle incremental reuse automatically if the cache contains a previous tree.
37    ///
38    /// # Arguments
39    /// * `text` - The source text
40    /// * `edits` - Edits applied to the source since the last parse
41    /// * `cache` - The cache for resources and incremental reuse
42    fn parse<'a, S: Source + ?Sized>(&self, text: &'a S, edits: &[TextEdit], cache: &'a mut impl ParseCache<L>) -> ParseOutput<'a, L>;
43}
44
45/// Standalone parsing function that coordinates lexing and parsing.
46///
47/// This is a convenience function for performing a complete parse (lexing + parsing)
48/// in one call.
49pub fn parse<'a, L, P, Lex, S>(parser: &P, _lexer: &Lex, text: &'a S, edits: &[TextEdit], cache: &'a mut impl ParseCache<L>) -> ParseOutput<'a, L>
50where
51    L: Language + Send + Sync,
52    L::ElementType: From<L::TokenType>,
53    P: Parser<L>,
54    Lex: Lexer<L>,
55    S: Source + ?Sized,
56{
57    parser.parse(text, edits, cache)
58}
59
60/// Standalone parsing function that performs a complete parse without incremental reuse.
61///
62/// This is a convenience function for parsing a source from scratch.
63pub fn parse_one_pass<'a, L, P, S>(parser: &P, text: &'a S, cache: &'a mut impl ParseCache<L>) -> ParseOutput<'a, L>
64where
65    L: Language + Send + Sync,
66    L::ElementType: From<L::TokenType>,
67    P: Parser<L>,
68    S: Source + ?Sized,
69{
70    parser.parse(text, &[], cache)
71}
72
73/// This function handles the boilerplate of preparing the cache, ensuring lexing is performed,
74/// setting up the parser state, and committing the result.
75pub fn parse_with_lexer<'a, L, S, Lex>(lexer: &Lex, text: &'a S, edits: &[TextEdit], cache: &'a mut impl ParseCache<L>, run: impl FnOnce(&mut ParserState<'a, L, S>) -> Result<&'a GreenNode<'a, L>, OakError>) -> ParseOutput<'a, L>
76where
77    L: Language + Send + Sync,
78    L::ElementType: From<L::TokenType>,
79    S: Source + ?Sized,
80    Lex: Lexer<L>,
81{
82    // 2. Get Lexing Result (Auto-lex if missing)
83    let lex_out = match cache.lex_output().cloned() {
84        Some(out) => out,
85        None => {
86            let out = lexer.lex(text, edits, cache);
87            cache.set_lex_output(out.clone());
88            out
89        }
90    };
91
92    let capacity_hint = cache.old_tree().map(|old| old.children.len().max(1024)).unwrap_or(1024);
93
94    // 3. Initialize Parser State
95    // Safety: We transmute the arena and old tree to 'a to satisfy the borrow checker.
96    // The ParseCache guarantees that the arena and old tree live long enough.
97    let arena: &'a crate::memory::arena::SyntaxArena = unsafe { std::mem::transmute(cache.arena()) };
98    let mut st = ParserState::new(arena, lex_out, text, capacity_hint);
99
100    if let Some(old) = cache.old_tree() {
101        let old: &'a GreenNode<'a, L> = unsafe { std::mem::transmute(old) };
102        st.set_incremental(old, edits);
103    }
104
105    // 4. Run Parser Logic
106    let result = run(&mut st);
107    let output = st.finish(result);
108
109    // 5. Commit Generation
110    if let Ok(root) = output.result {
111        cache.commit_generation(root)
112    }
113
114    output
115}