oak_core/lexer/cache.rs
1use crate::{
2 Language,
3 lexer::{LexOutput, Token},
4};
5
6/// Cache trait for lexical results.
7///
8/// This trait defines the interface for caching and accessing lexical analysis results.
9/// It provides methods for storing and retrieving token information from previous
10/// lexical analysis operations.
11#[allow(unused_variables)]
12pub trait LexerCache<L: Language> {
13 /// Sets the lexed output in the cache.
14 ///
15 /// # Arguments
16 ///
17 /// * `output` - The output from lexical analysis, including tokens and diagnostics
18 fn set_lex_output(&mut self, output: LexOutput<L>);
19
20 /// Gets a token from the cache by index.
21 ///
22 /// # Arguments
23 ///
24 /// * `index` - The index of the token to retrieve
25 ///
26 /// # Returns
27 ///
28 /// An `Option<Token<L::TokenType>>` containing the token if it exists,
29 /// or `None` if the index is out of bounds or no tokens are cached
30 fn get_token(&self, index: usize) -> Option<Token<L::TokenType>>;
31
32 /// Gets the total number of tokens in the cache.
33 ///
34 /// # Returns
35 ///
36 /// The number of cached tokens, or 0 if no tokens are cached
37 fn count_tokens(&self) -> usize;
38
39 /// Checks if the cache contains any tokens.
40 ///
41 /// # Returns
42 ///
43 /// `true` if the cache contains tokens, `false` otherwise
44 fn has_tokens(&self) -> bool;
45
46 /// Gets all cached tokens as a slice.
47 ///
48 /// # Returns
49 ///
50 /// An optional slice of tokens if available.
51 fn get_tokens(&self) -> Option<&[Token<L::TokenType>]> {
52 None
53 }
54}
55
56impl<'a, L: Language, C: LexerCache<L> + ?Sized> LexerCache<L> for &'a mut C {
57 fn set_lex_output(&mut self, output: LexOutput<L>) {
58 (**self).set_lex_output(output)
59 }
60
61 fn get_token(&self, index: usize) -> Option<Token<L::TokenType>> {
62 (**self).get_token(index)
63 }
64
65 fn count_tokens(&self) -> usize {
66 (**self).count_tokens()
67 }
68
69 fn has_tokens(&self) -> bool {
70 (**self).has_tokens()
71 }
72
73 fn get_tokens(&self) -> Option<&[Token<L::TokenType>]> {
74 (**self).get_tokens()
75 }
76}
77
78/// A no-op implementation of `LexerCache`.
79#[derive(Debug, Clone, Copy, Default)]
80pub struct NoLexerCache;
81
82impl<L: Language> LexerCache<L> for NoLexerCache {
83 fn set_lex_output(&mut self, _output: LexOutput<L>) {}
84
85 fn get_token(&self, _index: usize) -> Option<Token<L::TokenType>> {
86 None
87 }
88
89 fn count_tokens(&self) -> usize {
90 0
91 }
92
93 fn has_tokens(&self) -> bool {
94 false
95 }
96}