adze 0.8.0

Define tree-sitter grammars alongside Rust logic with AST-first parsing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! Minimal Tree-sitter compatibility shims (edits, points, language wrapper).
#![cfg_attr(feature = "strict_docs", allow(missing_docs))]

//! Tree-sitter compatibility API
//!
//! This module provides a compatibility layer that mimics the Tree-sitter API,
//! allowing existing Tree-sitter code to work with adze with minimal changes.

use crate::parser_v4::{ParseNode, Parser as CoreParser};
use crate::pure_incremental::Edit as CoreEdit;
use crate::pure_parser;
use adze_glr_core::ParseTable;
use adze_ir::Grammar;
use std::sync::Arc;

/// An owned tree representation for ts_compat layer.
/// This provides the interface expected by ts_compat::Tree without lifetime constraints.
#[derive(Clone, Debug)]
pub(crate) struct OwnedCoreTree {
    /// The root parse node
    pub root: ParseNode,
    /// Source text that was parsed
    pub source: Vec<u8>,
    /// Number of parse errors
    pub error_count: usize,
}

impl OwnedCoreTree {
    /// Get the root symbol ID
    pub(crate) fn root_kind(&self) -> u16 {
        self.root.symbol.0
    }

    /// Get the error count
    pub(crate) fn error_count(&self) -> usize {
        self.error_count
    }
}

/// A position in a document, identified by row and column.
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct Point {
    pub row: u32,
    pub column: u32,
}

impl From<(u32, u32)> for Point {
    fn from((row, column): (u32, u32)) -> Self {
        Point { row, column }
    }
}

impl From<Point> for (u32, u32) {
    fn from(p: Point) -> Self {
        (p.row, p.column)
    }
}

/// An edit to a document.
#[derive(Clone, Debug, Default)]
pub struct InputEdit {
    pub start_byte: usize,
    pub old_end_byte: usize,
    pub new_end_byte: usize,
    pub start_position: Point,
    pub old_end_position: Point,
    pub new_end_position: Point,
}

impl From<InputEdit> for CoreEdit {
    fn from(e: InputEdit) -> Self {
        CoreEdit {
            start_byte: e.start_byte,
            old_end_byte: e.old_end_byte,
            new_end_byte: e.new_end_byte,
            start_point: pure_parser::Point {
                row: e.start_position.row,
                column: e.start_position.column,
            },
            old_end_point: pure_parser::Point {
                row: e.old_end_position.row,
                column: e.old_end_position.column,
            },
            new_end_point: pure_parser::Point {
                row: e.new_end_position.row,
                column: e.new_end_position.column,
            },
        }
    }
}

/// A language definition containing grammar and parse tables.
#[derive(Clone, Debug)]
pub struct Language {
    pub name: String,
    pub grammar: Grammar,
    pub table: ParseTable,
}

impl Language {
    pub fn new(name: impl Into<String>, grammar: Grammar, table: ParseTable) -> Self {
        Self {
            name: name.into(),
            grammar,
            table,
        }
    }
}

/// A parser that can parse source code using a language.
pub struct Parser {
    core: Option<CoreParser>,
    lang: Option<Arc<Language>>,
}

impl Parser {
    /// Create a new parser.
    pub fn new() -> Self {
        Self {
            core: None,
            lang: None,
        }
    }

    /// Set the language for this parser.
    pub fn set_language(&mut self, lang: Arc<Language>) -> Result<(), String> {
        self.lang = Some(Arc::clone(&lang));
        self.core = Some(CoreParser::new(
            lang.grammar.clone(),
            lang.table.clone(),
            lang.name.clone(),
        ));
        Ok(())
    }

    /// Parse source code, optionally reusing an old tree for incremental parsing.
    ///
    /// Note: Incremental parsing is currently disabled and falls back to fresh parsing
    /// for consistency. The `old` parameter is accepted for API compatibility but ignored.
    pub fn parse(&mut self, source: &str, _old: Option<&Tree>) -> Option<Tree> {
        let core_parser = self.core.as_mut()?;
        let lang = self.lang.as_ref()?;

        // Use parse_tree() which returns an owned ParseNode
        match core_parser.parse_tree(source) {
            Ok(root) => Some(Tree {
                core: OwnedCoreTree {
                    root,
                    source: source.as_bytes().to_vec(),
                    error_count: 0, // TODO: track error count properly
                },
                last_edit: None,
                language: lang.clone(),
            }),
            Err(_) => None,
        }
    }

    /// Get the current language.
    pub fn language(&self) -> Option<&Arc<Language>> {
        self.lang.as_ref()
    }
}

impl Default for Parser {
    fn default() -> Self {
        Self::new()
    }
}

/// A parsed syntax tree.
#[derive(Clone, Debug)]
pub struct Tree {
    pub(crate) core: OwnedCoreTree,
    pub(crate) last_edit: Option<CoreEdit>,
    pub(crate) language: Arc<Language>,
}

impl Tree {
    /// Apply an edit to this tree.
    pub fn edit(&mut self, edit: &InputEdit) {
        let core_edit = CoreEdit::from(edit.clone());
        // Store the edit for later incremental parsing
        // Note: parser_v4::Tree doesn't have apply_edit, edits are tracked separately
        self.last_edit = Some(core_edit);
    }

    /// Get the root node of this tree.
    pub fn root_node(&self) -> Node<'_> {
        Node::new(self, 0)
    }

    /// Get the root kind as a string.
    pub fn root_kind(&self) -> &str {
        let sym = self.core.root_kind();
        // Try direct rule name mapping first
        if let Some(name) = self
            .language
            .grammar
            .rule_names
            .get(&adze_ir::SymbolId(sym))
        {
            return name.as_str();
        }
        // Fallback: if index_to_symbol is populated, prefer that
        if let Some(name) = self
            .language
            .table
            .index_to_symbol
            .get(sym as usize)
            .and_then(|sid| self.language.grammar.rule_names.get(sid))
        {
            return name.as_str();
        }
        "unknown"
    }

    /// Get the number of errors in this tree.
    pub fn error_count(&self) -> usize {
        self.core.error_count()
    }

    /// Check if the tree has errors.
    pub fn has_errors(&self) -> bool {
        self.error_count() > 0
    }
}

/// A node in a syntax tree.
///
/// Note: Current implementation represents the root node only, as parser_v4
/// does not expose detailed tree structure. Node metadata is inferred from
/// the tree's overall properties and position within the source.
#[derive(Debug, Clone)]
pub struct Node<'a> {
    tree: &'a Tree,
    /// Index/position within the tree (root = 0)
    index: usize,
    /// Cached byte range for this node (start, end)
    byte_range: Option<(usize, usize)>,
    /// Cached position range for this node
    position_range: Option<(Point, Point)>,
}

impl<'a> Node<'a> {
    /// Create a new node with computed metadata
    fn new(tree: &'a Tree, index: usize) -> Self {
        let (byte_range, position_range) = Self::compute_ranges(tree, index);
        Self {
            tree,
            index,
            byte_range,
            position_range,
        }
    }

    /// Compute byte and position ranges for this node based on its index
    #[allow(clippy::type_complexity)]
    fn compute_ranges(
        tree: &Tree,
        index: usize,
    ) -> (Option<(usize, usize)>, Option<(Point, Point)>) {
        if index == 0 {
            // Root node covers the entire source
            let byte_end = tree.core.source.len();
            let end_position = Self::byte_to_point(&tree.core.source, byte_end);
            (
                Some((0, byte_end)),
                Some((Point { row: 0, column: 0 }, end_position)),
            )
        } else {
            // Non-root nodes: In current implementation, no children are exposed
            // Return None to indicate this node doesn't have valid ranges
            (None, None)
        }
    }

    /// Convert byte position to Point (row, column)
    fn byte_to_point(source: &[u8], byte_pos: usize) -> Point {
        let mut row = 0;
        let mut column = 0;

        for (i, &byte) in source.iter().enumerate() {
            if i >= byte_pos {
                break;
            }
            if byte == b'\n' {
                row += 1;
                column = 0;
            } else {
                column += 1;
            }
        }

        Point { row, column }
    }

    /// Get the kind of this node as a string.
    pub fn kind(&self) -> &str {
        if self.index == 0 {
            // Root node - return the actual root kind
            self.tree.root_kind()
        } else {
            // Non-root nodes are not exposed by current parser_v4 implementation
            "unknown"
        }
    }

    /// Get the start byte of this node.
    pub fn start_byte(&self) -> usize {
        self.byte_range.map(|(start, _)| start).unwrap_or(0)
    }

    /// Get the end byte of this node.
    pub fn end_byte(&self) -> usize {
        self.byte_range.map(|(_, end)| end).unwrap_or(0)
    }

    /// Get the start position of this node.
    pub fn start_position(&self) -> Point {
        self.position_range
            .map(|(start, _)| start)
            .unwrap_or_default()
    }

    /// Get the end position of this node.
    pub fn end_position(&self) -> Point {
        self.position_range.map(|(_, end)| end).unwrap_or_default()
    }

    /// Get the number of children.
    pub fn child_count(&self) -> usize {
        if self.index == 0 {
            // Root node: parser_v4 doesn't expose children, but we can infer
            // that a successful parse with content has at least structure
            if !self.tree.core.source.is_empty() && self.tree.error_count() == 0 {
                // Estimate: non-trivial content likely has some structure
                // This is a heuristic since actual children aren't exposed
                0 // Conservative: return 0 until full tree structure is available
            } else {
                0
            }
        } else {
            // Non-root nodes don't exist in current implementation
            0
        }
    }

    /// Get a child by index.
    pub fn child(&self, index: usize) -> Option<Node<'a>> {
        if index < self.child_count() {
            // Current implementation doesn't expose actual children
            // Return None to indicate child access is not available
            None
        } else {
            None
        }
    }

    /// Check if this node is an error node.
    pub fn is_error(&self) -> bool {
        if self.index == 0 {
            // Root node: check if the entire tree has errors
            self.tree.error_count() > 0
        } else {
            // Non-root nodes: no specific error information available
            false
        }
    }

    /// Check if this node is missing (was expected but not found).
    pub fn is_missing(&self) -> bool {
        if self.index == 0 {
            // Root node: check if parse failed completely (empty source with errors)
            self.tree.core.source.is_empty() && self.tree.error_count() > 0
        } else {
            // Non-root nodes: no specific missing information available
            false
        }
    }

    /// Get the byte range of this node.
    pub fn byte_range(&self) -> std::ops::Range<usize> {
        let (start, end) = self.byte_range.unwrap_or((0, 0));
        start..end
    }

    /// Get the text content of this node.
    pub fn utf8_text<'b>(&self, source: &'b [u8]) -> Result<&'b str, std::str::Utf8Error> {
        let range = self.byte_range();
        let slice = source.get(range).unwrap_or(&[]);
        std::str::from_utf8(slice)
    }

    /// Get the text content of this node as a string.
    pub fn text(&self, source: &[u8]) -> String {
        self.utf8_text(source).unwrap_or("").to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use adze_glr_core::Action;
    use adze_ir::SymbolId;
    use std::collections::BTreeMap;

    fn empty_parse_table_language() -> Arc<Language> {
        Arc::new(Language::new(
            "ts_compat_empty_parse_table",
            Grammar::default(),
            ParseTable::default(),
        ))
    }

    fn accept_on_eof_language() -> Arc<Language> {
        let parse_table = ParseTable {
            symbol_to_index: BTreeMap::from([(SymbolId(0), 0)]),
            action_table: vec![vec![vec![Action::Accept]]],
            ..Default::default()
        };

        Arc::new(Language::new(
            "ts_compat_accept_on_eof",
            Grammar::default(),
            parse_table,
        ))
    }

    #[test]
    fn parse_ignores_old_tree_source() {
        let mut parser = Parser::new();
        parser.set_language(empty_parse_table_language()).unwrap();

        let old_tree = parser.parse("old", None).unwrap();
        let new_source = "incrementally updated";

        let reparsed = parser.parse(new_source, Some(&old_tree)).unwrap();
        assert_eq!(reparsed.root_node().text(new_source.as_bytes()), new_source);
        assert_eq!(reparsed.core.source, new_source.as_bytes().to_vec());
        assert_ne!(reparsed.core.source, old_tree.core.source);
    }

    #[test]
    fn parse_returns_none_on_core_parse_error() {
        let mut parser = Parser::new();
        parser.set_language(accept_on_eof_language()).unwrap();

        let tree = parser.parse("any input", None);

        assert!(tree.is_none());
    }
}