devalang_core/core/parser/driver/
parser.rs

1use crate::core::{
2    lexer::token::{Token, TokenKind},
3    parser::statement::Statement,
4    store::global::GlobalStore,
5};
6use devalang_types::Value;
7
8#[derive(Debug, Clone, PartialEq)]
9pub struct Parser {
10    pub resolve_modules: bool,
11    pub tokens: Vec<Token>,
12    pub token_index: usize,
13    pub current_module: String,
14    pub previous: Option<Token>,
15}
16
17impl Default for Parser {
18    fn default() -> Self {
19        Self::new()
20    }
21}
22
23impl Parser {
24    pub fn new() -> Self {
25        Parser {
26            resolve_modules: false,
27            tokens: Vec::new(),
28            token_index: 0,
29            current_module: String::new(),
30            previous: None,
31        }
32    }
33
34    pub fn set_current_module(&mut self, module_path: String) {
35        self.current_module = module_path;
36    }
37
38    pub fn advance(&mut self) -> Option<&Token> {
39        crate::core::parser::driver::cursor::advance_impl(self)
40    }
41
42    pub fn peek_is(&self, expected: &str) -> bool {
43        crate::core::parser::driver::cursor::peek_is_impl(self, expected)
44    }
45
46    pub fn peek_nth(&self, n: usize) -> Option<&Token> {
47        crate::core::parser::driver::cursor::peek_nth_impl(self, n)
48    }
49
50    pub fn peek_nth_kind(&self, n: usize) -> Option<TokenKind> {
51        crate::core::parser::driver::cursor::peek_nth_kind_impl(self, n)
52    }
53
54    pub fn advance_if(&mut self, kind: TokenKind) -> bool {
55        crate::core::parser::driver::cursor::advance_if_impl(self, kind)
56    }
57
58    pub fn match_token(&mut self, kind: TokenKind) -> bool {
59        crate::core::parser::driver::cursor::match_token_impl(self, kind)
60    }
61
62    pub fn previous_clone(&self) -> Option<Token> {
63        crate::core::parser::driver::cursor::previous_clone_impl(self)
64    }
65
66    pub fn parse_block(
67        &self,
68        tokens: Vec<Token>,
69        global_store: &mut GlobalStore,
70    ) -> Vec<Statement> {
71        crate::core::parser::driver::block::parse_block(self, tokens, global_store)
72    }
73
74    pub fn parse_tokens(
75        &mut self,
76        tokens: Vec<Token>,
77        global_store: &mut GlobalStore,
78    ) -> Vec<Statement> {
79        crate::core::parser::driver::driver_impl::parse_tokens_impl(self, tokens, global_store)
80    }
81
82    pub fn check_token(&self, kind: TokenKind) -> bool {
83        crate::core::parser::driver::cursor::peek_impl(self).is_some_and(|t| t.kind == kind)
84    }
85
86    pub fn peek_kind(&self) -> Option<TokenKind> {
87        crate::core::parser::driver::cursor::peek_impl(self).map(|t| t.kind.clone())
88    }
89
90    pub fn parse_map_value(&mut self) -> Option<Value> {
91        // Delegated to parse_map.rs
92        crate::core::parser::driver::parse_map::parse_map_value(self)
93    }
94
95    // Parse an array value like [1, 2, 3] or ["a", b]
96    pub fn parse_array_value(&mut self) -> Option<Value> {
97        // delegated to parse_array.rs
98        crate::core::parser::driver::parse_array::parse_array_value(self)
99    }
100
101    pub fn peek(&self) -> Option<&Token> {
102        self.tokens.get(self.token_index)
103    }
104
105    pub fn peek_clone(&self) -> Option<Token> {
106        self.tokens.get(self.token_index).cloned()
107    }
108
109    pub fn expect(&mut self, kind: TokenKind) -> Result<&Token, String> {
110        let tok = self.advance().ok_or("Unexpected end of input")?;
111        if tok.kind == kind {
112            Ok(tok)
113        } else {
114            Err(format!("Expected {:?}, got {:?}", kind, tok.kind))
115        }
116    }
117
118    pub fn collect_block_tokens(&mut self, base_indent: usize) -> Vec<Token> {
119        crate::core::parser::driver::block::collect_block_tokens(self, base_indent)
120    }
121
122    pub fn collect_until<F>(&mut self, condition: F) -> Vec<Token>
123    where
124        F: Fn(&Token) -> bool,
125    {
126        crate::core::parser::driver::block::collect_until(self, condition)
127    }
128
129    pub fn is_eof(&self) -> bool {
130        self.token_index >= self.tokens.len()
131    }
132
133    pub fn parse_block_until_next_else(
134        &mut self,
135        base_indent: usize,
136        global_store: &mut GlobalStore,
137    ) -> Vec<Statement> {
138        crate::core::parser::driver::block::parse_block_until_next_else(
139            self,
140            base_indent,
141            global_store,
142        )
143    }
144
145    pub fn parse_condition_until_colon(&mut self) -> Option<Value> {
146        crate::core::parser::driver::driver_impl::parse_condition_until_colon_impl(self)
147    }
148
149    pub fn parse_block_until_else_or_dedent(
150        &mut self,
151        base_indent: usize,
152        global_store: &mut GlobalStore,
153    ) -> Vec<Statement> {
154        crate::core::parser::driver::block::parse_block_until_else_or_dedent(
155            self,
156            base_indent,
157            global_store,
158        )
159    }
160}