1#![doc = include_str!("readme.md")]
2
3pub mod pratt;
5pub mod session;
7pub mod state;
9
10pub use self::{
11 pratt::{Associativity, OperatorInfo, Pratt, PrattParser, binary, postfix, unary},
12 session::{ParseCache, ParseSession},
13 state::ParserState,
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
25pub type ParseOutput<'a, L: Language> = OakDiagnostics<&'a GreenNode<'a, L>>;
27
28pub trait Parser<L: Language + Send + Sync + 'static> {
30 fn parse<'a, S: Source + ?Sized>(&self, text: &'a S, edits: &[TextEdit], cache: &'a mut impl ParseCache<L>) -> ParseOutput<'a, L>;
40}
41
42pub 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>
47where
48 L: Language + Send + Sync + 'static,
49 P: Parser<L>,
50 Lex: Lexer<L>,
51 S: Source + ?Sized,
52{
53 parser.parse(text, edits, cache)
54}
55
56pub fn parse_one_pass<'a, L, P, S>(parser: &P, text: &'a S, cache: &'a mut impl ParseCache<L>) -> ParseOutput<'a, L>
60where
61 L: Language + Send + Sync + 'static,
62 P: Parser<L>,
63 S: Source + ?Sized,
64{
65 parser.parse(text, &[], cache)
66}
67
68pub 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>
73where
74 L: Language + Send + Sync + 'static,
75 S: Source + ?Sized,
76 Lex: Lexer<L>,
77{
78 cache.prepare_generation();
80
81 let lex_out = match cache.lex_output() {
83 Some(out) => out.clone(),
84 None => {
85 let out = lexer.lex(text, edits, cache);
86 cache.set_lex_output(out.clone());
87 out
88 }
89 };
90
91 let capacity_hint = cache.old_tree().map(|old| old.children.len().max(1024)).unwrap_or(1024);
92
93 let arena: &'a crate::memory::arena::SyntaxArena = unsafe { std::mem::transmute(cache.arena()) };
97 let mut st = ParserState::new(arena, lex_out, text, capacity_hint);
98
99 if let Some(old) = cache.old_tree() {
100 let old: &'a GreenNode<'a, L> = unsafe { std::mem::transmute(old) };
101 st.set_incremental(old, edits);
102 }
103
104 let result = run(&mut st);
106 let output = st.finish(result);
107
108 if let Ok(root) = output.result {
110 cache.commit_generation(root);
111 }
112
113 output
114}