oak-csharp 0.0.11

C# frontend for Oak
Documentation
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
#![doc = include_str!("readme.md")]

use crate::{ast::*, language::CSharpLanguage, lexer::token_type::CSharpTokenType, parser::CSharpElementType};
use core::range::Range;
use oak_core::{
    GreenNode, Parser,
    builder::{BuildOutput, Builder, BuilderCache},
    source::{Source, TextEdit},
    tree::red_tree::{RedNode, RedTree},
};

/// A builder that constructs a C# high-level AST from a green tree.
///
/// The `CSharpBuilder` traverses the red tree (which provides absolute spans)
/// and maps its nodes to structured `ast` types.
pub struct CSharpBuilder<'config> {
    /// The language configuration.
    language: &'config CSharpLanguage,
}

impl<'config> CSharpBuilder<'config> {
    /// Creates a new `CSharpBuilder` with the given language configuration.
    pub fn new(language: &'config CSharpLanguage) -> Self {
        Self { language }
    }

    /// Builds a `CSharpRoot` from a green node.
    fn build_root(&self, green: &GreenNode<CSharpLanguage>, source: &str) -> CSharpRoot {
        let red = RedNode::new(green, 0);
        let mut items = Vec::new();

        for child in red.children() {
            if let RedTree::Node(node) = child {
                if let Some(item) = self.build_item(node, source) {
                    items.push(item);
                }
            }
        }

        CSharpRoot { items }
    }

    /// Builds a top-level `Item` from a red node.
    fn build_item(&self, node: RedNode<CSharpLanguage>, source: &str) -> Option<Item> {
        match node.green.kind {
            CSharpElementType::NamespaceDeclaration => Some(Item::Namespace(self.build_namespace(node, source))),
            CSharpElementType::UsingDirective => Some(Item::Using(self.build_using(node, source))),
            CSharpElementType::ClassDeclaration => Some(Item::Class(self.build_class(node, source))),
            CSharpElementType::InterfaceDeclaration => Some(Item::Interface(self.build_interface(node, source))),
            CSharpElementType::StructDeclaration => Some(Item::Struct(self.build_struct(node, source))),
            CSharpElementType::EnumDeclaration => Some(Item::Enum(self.build_enum(node, source))),
            CSharpElementType::RecordDeclaration => Some(Item::Record(self.build_record(node, source))),
            CSharpElementType::DelegateDeclaration => Some(Item::Delegate(self.build_delegate(node, source))),
            _ => None,
        }
    }

    /// Gets a substring from the source text safely.
    fn get_text<'a>(&self, span: Range<usize>, source: &'a str) -> &'a str {
        let start = span.start;
        let end = span.end;
        if start > source.len() || end > source.len() || start > end {
            return "";
        }
        &source[start..end]
    }

    /// Extracts an identifier string from a red node.
    fn extract_identifier(&self, node: RedNode<CSharpLanguage>, source: &str) -> String {
        for child in node.children() {
            match child {
                RedTree::Leaf(leaf) => {
                    if leaf.kind == CSharpTokenType::Identifier {
                        return self.get_text(leaf.span, source).trim().to_string();
                    }
                }
                RedTree::Node(sub_node) => {
                    if sub_node.kind::<CSharpElementType>() == CSharpElementType::IdentifierName {
                        return self.get_text(sub_node.span(), source).trim().to_string();
                    }
                    let id = self.extract_identifier(sub_node, source);
                    if !id.is_empty() {
                        return id;
                    }
                }
            }
        }
        String::new()
    }

    /// Extracts a list of attributes from a red node.
    fn extract_attributes(&self, node: RedNode<CSharpLanguage>, source: &str) -> Vec<Attribute> {
        let mut attributes = Vec::new();
        for child in node.children() {
            if let RedTree::Node(attr_node) = child {
                if attr_node.green.kind == CSharpElementType::Attribute {
                    let name = self.extract_identifier(attr_node.clone(), source);
                    let span = attr_node.span();
                    // TODO: Extract arguments
                    attributes.push(Attribute { name, arguments: Vec::new(), span })
                }
            }
        }
        attributes
    }

    /// Builds a `NamespaceDeclaration` from a red node.
    fn build_namespace(&self, node: RedNode<CSharpLanguage>, source: &str) -> NamespaceDeclaration {
        let name = self.extract_identifier(node.clone(), source);
        let attributes = self.extract_attributes(node.clone(), source);
        let mut items = Vec::new();

        for child in node.children() {
            if let RedTree::Node(sub_node) = child {
                if let Some(item) = self.build_item(sub_node, source) {
                    items.push(item)
                }
            }
        }

        NamespaceDeclaration { name, attributes, items, span: node.span() }
    }

    fn build_using(&self, node: RedNode<CSharpLanguage>, source: &str) -> UsingDirective {
        let path = self.extract_identifier(node.clone(), source);
        let is_static = self.get_text(node.span(), source).contains("static");
        let is_global = self.get_text(node.span(), source).contains("global");
        UsingDirective {
            path,
            is_static,
            alias: None, // TODO
            is_global,
            span: node.span(),
        }
    }

    fn build_class(&self, node: RedNode<CSharpLanguage>, source: &str) -> ClassDeclaration {
        let name = self.extract_identifier(node.clone(), source);
        let mut members = Vec::new();
        let modifiers = self.extract_modifiers(node.clone(), source);
        let attributes = self.extract_attributes(node.clone(), source);

        for child in node.children() {
            if let RedTree::Node(sub_node) = child {
                self.collect_members(sub_node, source, &mut members)
            }
        }

        ClassDeclaration { name, attributes, modifiers, base_types: Vec::new(), type_parameters: Vec::new(), constraints: Vec::new(), members, span: node.span() }
    }

    fn build_interface(&self, node: RedNode<CSharpLanguage>, source: &str) -> InterfaceDeclaration {
        let name = self.extract_identifier(node.clone(), source);
        let modifiers = self.extract_modifiers(node.clone(), source);
        let attributes = self.extract_attributes(node.clone(), source);
        let mut members = Vec::new();
        for child in node.children() {
            if let RedTree::Node(sub_node) = child {
                self.collect_members(sub_node, source, &mut members)
            }
        }
        InterfaceDeclaration { name, attributes, modifiers, members, type_parameters: Vec::new(), span: node.span() }
    }

    fn build_struct(&self, node: RedNode<CSharpLanguage>, source: &str) -> StructDeclaration {
        let name = self.extract_identifier(node.clone(), source);
        let modifiers = self.extract_modifiers(node.clone(), source);
        let attributes = self.extract_attributes(node.clone(), source);
        let mut members = Vec::new();
        for child in node.children() {
            if let RedTree::Node(sub_node) = child {
                self.collect_members(sub_node, source, &mut members)
            }
        }
        StructDeclaration { name, attributes, modifiers, members, type_parameters: Vec::new(), span: node.span() }
    }

    fn build_enum(&self, node: RedNode<CSharpLanguage>, source: &str) -> EnumDeclaration {
        let name = self.extract_identifier(node.clone(), source);
        let modifiers = self.extract_modifiers(node.clone(), source);
        let attributes = self.extract_attributes(node.clone(), source);
        let mut members = Vec::new();
        // Handle enum members
        for child in node.children() {
            if let RedTree::Node(sub_node) = child {
                if sub_node.kind::<CSharpElementType>() == CSharpElementType::IdentifierName {
                    members.push(EnumMember { name: self.get_text(sub_node.span(), source).to_string(), attributes: Vec::new(), value: None })
                }
            }
        }
        EnumDeclaration { name, attributes, modifiers, members, span: node.span() }
    }

    fn build_record(&self, node: RedNode<CSharpLanguage>, source: &str) -> RecordDeclaration {
        let name = self.extract_identifier(node.clone(), source);
        let modifiers = self.extract_modifiers(node.clone(), source);
        let attributes = self.extract_attributes(node.clone(), source);
        let mut members = Vec::new();
        for child in node.children() {
            if let RedTree::Node(sub_node) = child {
                self.collect_members(sub_node, source, &mut members)
            }
        }
        RecordDeclaration { name, attributes, modifiers, members, type_parameters: Vec::new(), span: node.span() }
    }

    fn build_delegate(&self, node: RedNode<CSharpLanguage>, source: &str) -> DelegateDeclaration {
        let name = self.extract_identifier(node.clone(), source);
        let modifiers = self.extract_modifiers(node.clone(), source);
        let attributes = self.extract_attributes(node.clone(), source);
        DelegateDeclaration { name, attributes, modifiers, return_type: "void".to_string(), type_parameters: Vec::new(), parameters: Vec::new(), span: node.span() }
    }

    fn extract_modifiers(&self, node: RedNode<CSharpLanguage>, source: &str) -> Vec<String> {
        let mut modifiers = Vec::new();
        for child in node.children() {
            if let RedTree::Leaf(leaf) = child {
                if leaf.kind.is_keyword() {
                    let text = self.get_text(leaf.span, source).trim();
                    match text {
                        "public" | "private" | "protected" | "internal" | "static" | "readonly" | "abstract" | "virtual" | "override" | "async" | "volatile" | "sealed" | "extern" | "partial" | "new" | "unsafe" => modifiers.push(text.to_string()),
                        _ => {}
                    }
                }
            }
        }
        modifiers
    }

    fn collect_members(&self, node: RedNode<CSharpLanguage>, source: &str, members: &mut Vec<Member>) {
        match node.green.kind {
            CSharpElementType::MethodDeclaration => members.push(Member::Method(self.build_method(node, source))),
            CSharpElementType::FieldDeclaration => members.push(Member::Field(self.build_field(node, source))),
            CSharpElementType::PropertyDeclaration => members.push(Member::Property(self.build_property(node, source))),
            CSharpElementType::IndexerDeclaration => members.push(Member::Indexer(self.build_indexer(node, source))),
            CSharpElementType::EventDeclaration => members.push(Member::Event(self.build_event(node, source))),
            _ => {
                for child in node.children() {
                    if let RedTree::Node(sub_node) = child {
                        self.collect_members(sub_node, source, members)
                    }
                }
            }
        }
    }

    fn build_method(&self, node: RedNode<CSharpLanguage>, source: &str) -> MethodDeclaration {
        let name = self.extract_identifier(node.clone(), source);
        let modifiers = self.extract_modifiers(node.clone(), source);
        let attributes = self.extract_attributes(node.clone(), source);
        let is_async = modifiers.contains(&"async".to_string());

        MethodDeclaration {
            name,
            attributes,
            modifiers,
            return_type: "void".to_string(), // TODO
            type_parameters: Vec::new(),     // TODO
            parameters: Vec::new(),          // TODO
            body: self.build_body(node.clone(), source),
            is_async,
            span: node.span(),
        }
    }

    fn build_field(&self, node: RedNode<CSharpLanguage>, source: &str) -> FieldDeclaration {
        let name = self.extract_identifier(node.clone(), source);
        let attributes = self.extract_attributes(node.clone(), source);
        FieldDeclaration {
            name,
            attributes,
            r#type: "object".to_string(), // TODO
            modifiers: self.extract_modifiers(node, source),
            initializer: None, // TODO
            span: Range::default(),
        }
    }

    fn build_property(&self, node: RedNode<CSharpLanguage>, source: &str) -> PropertyDeclaration {
        let name = self.extract_identifier(node.clone(), source);
        let attributes = self.extract_attributes(node.clone(), source);
        PropertyDeclaration {
            name,
            attributes,
            r#type: "object".to_string(), // TODO
            modifiers: self.extract_modifiers(node, source),
            get_accessor: None, // TODO
            set_accessor: None, // TODO
            span: node.span(),
        }
    }

    fn build_indexer(&self, node: RedNode<CSharpLanguage>, source: &str) -> IndexerDeclaration {
        let attributes = self.extract_attributes(node.clone(), source);
        IndexerDeclaration {
            attributes,
            r#type: "object".to_string(), // TODO
            parameters: Vec::new(),       // TODO
            get_accessor: None,           // TODO
            set_accessor: None,           // TODO
            span: node.span(),
        }
    }

    fn build_event(&self, node: RedNode<CSharpLanguage>, source: &str) -> EventDeclaration {
        let name = self.extract_identifier(node.clone(), source);
        let attributes = self.extract_attributes(node.clone(), source);
        EventDeclaration {
            name,
            attributes,
            r#type: "object".to_string(), // TODO
            modifiers: self.extract_modifiers(node, source),
            span: node.span(),
        }
    }

    fn build_body(&self, node: RedNode<CSharpLanguage>, source: &str) -> Option<Vec<Statement>> {
        for child in node.children() {
            if let RedTree::Node(sub_node) = child {
                if sub_node.green.kind == CSharpElementType::Block {
                    let mut statements = Vec::new();
                    for grandchild in sub_node.children() {
                        if let RedTree::Node(grandchild_node) = grandchild {
                            if let Some(stmt) = self.build_statement(grandchild_node, source) {
                                statements.push(stmt)
                            }
                        }
                    }
                    return Some(statements);
                }
            }
        }
        None
    }

    fn build_statement(&self, node: RedNode<CSharpLanguage>, source: &str) -> Option<Statement> {
        match node.green.kind {
            CSharpElementType::ExpressionStatement => {
                for child in node.children() {
                    if let RedTree::Node(sub_node) = child {
                        if let Some(expr) = self.build_expression(sub_node, source) {
                            return Some(Statement::Expression(expr));
                        }
                    }
                }
                None
            }
            CSharpElementType::ReturnStatement => {
                for child in node.children() {
                    if let RedTree::Node(sub_node) = child {
                        if let Some(expr) = self.build_expression(sub_node, source) {
                            return Some(Statement::Return(Some(expr)));
                        }
                    }
                }
                Some(Statement::Return(None))
            }
            CSharpElementType::Block => {
                let mut statements = Vec::new();
                for child in node.children() {
                    if let RedTree::Node(sub_node) = child {
                        if let Some(stmt) = self.build_statement(sub_node, source) {
                            statements.push(stmt)
                        }
                    }
                }
                Some(Statement::Block(statements))
            }
            CSharpElementType::IfStatement => {
                // Simplified
                Some(Statement::If { condition: Expression::Literal(Literal::Boolean(true)), then_branch: Box::new(Statement::Block(Vec::new())), else_branch: None })
            }
            CSharpElementType::BreakStatement => Some(Statement::Break),
            CSharpElementType::ContinueStatement => Some(Statement::Continue),
            _ => None,
        }
    }

    fn build_expression(&self, node: RedNode<CSharpLanguage>, source: &str) -> Option<Expression> {
        match node.green.kind {
            CSharpElementType::LiteralExpression => {
                let text = self.get_text(node.span(), source).trim();
                if text == "true" {
                    Some(Expression::Literal(Literal::Boolean(true)))
                }
                else if text == "false" {
                    Some(Expression::Literal(Literal::Boolean(false)))
                }
                else if text == "null" {
                    Some(Expression::Literal(Literal::Null))
                }
                else if let Ok(n) = text.parse::<i64>() {
                    Some(Expression::Literal(Literal::Integer(n)))
                }
                else {
                    Some(Expression::Literal(Literal::String(text.to_string())))
                }
            }
            CSharpElementType::IdentifierName => Some(Expression::Identifier(self.get_text(node.span(), source).trim().to_string())),
            CSharpElementType::AwaitExpression => {
                for child in node.children() {
                    if let RedTree::Node(sub_node) = child {
                        if let Some(expr) = self.build_expression(sub_node, source) {
                            return Some(Expression::Await(Box::new(expr)));
                        }
                    }
                }
                None
            }
            _ => None,
        }
    }
}

impl<'config> Builder<CSharpLanguage> for CSharpBuilder<'config> {
    fn build<'a, S: Source + ?Sized>(&self, source: &'a S, edits: &[TextEdit], _cache: &'a mut impl BuilderCache<CSharpLanguage>) -> BuildOutput<CSharpLanguage> {
        let mut session = oak_core::parser::ParseSession::<CSharpLanguage>::default();
        let parser = crate::parser::CSharpParser::new(self.language);
        let output = parser.parse(source, edits, &mut session);

        let mut result = Err(oak_core::OakError::custom_error("Build failed"));
        if let Ok(green) = &output.result {
            let root = self.build_root(green, source.get_text_in((0..source.length()).into()).as_ref());
            result = Ok(root)
        }

        oak_core::errors::OakDiagnostics { result, diagnostics: output.diagnostics }
    }
}