oak-c 0.0.11

High-performance incremental C parser for the oak ecosystem with flexible configuration, supporting low-level system programming features.
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
use super::*;

/// Represents a canonical C type.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub enum Type {
    /// A primitive type (int, char, etc.) or a struct/union/enum.
    Base(TypeSpecifier),
    /// A pointer type.
    Pointer(Box<Type>),
    /// An array type.
    Array {
        /// The type of elements in the array.
        element_type: Box<Type>,
        /// The size of the array, if specified.
        size: Option<Box<Expression>>,
    },
    /// A function type.
    Function {
        /// The return type of the function.
        return_type: Box<Type>,
        /// The types of the parameters.
        parameters: Vec<Type>,
        /// Whether the function is variadic.
        variadic: bool,
    },
}

/// External declaration.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub enum ExternalDeclaration {
    /// Function definition.
    FunctionDefinition(FunctionDefinition),
    /// Declaration.
    Declaration(Declaration),
}

impl ExternalDeclaration {
    /// Returns the source span of the external declaration.
    pub fn span(&self) -> core::range::Range<usize> {
        match self {
            Self::FunctionDefinition(n) => n.span.clone(),
            Self::Declaration(n) => n.span.clone(),
        }
    }
}

/// Function definition.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct FunctionDefinition {
    /// Declaration specifiers (e.g., return type, storage class).
    pub declaration_specifiers: Vec<DeclarationSpecifier>,
    /// The declarator for the function.
    pub declarator: Declarator,
    /// The body of the function.
    pub compound_statement: CompoundStatement,
    /// The source span of the function definition.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: core::range::Range<usize>,
}

/// Declaration.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct Declaration {
    /// Declaration specifiers.
    pub declaration_specifiers: Vec<DeclarationSpecifier>,
    /// List of declarators being initialized.
    pub init_declarators: Vec<InitDeclarator>,
    /// The source span of the declaration.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: core::range::Range<usize>,
}

/// Declaration specifier.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub enum DeclarationSpecifier {
    /// Storage class specifier (e.g., `static`, `extern`).
    StorageClassSpecifier(StorageClassSpecifier),
    /// Type specifier (e.g., `int`, `char`).
    TypeSpecifier(TypeSpecifier),
    /// Type qualifier (e.g., `const`, `volatile`).
    TypeQualifier(TypeQualifier),
    /// Function specifier (e.g., `inline`).
    FunctionSpecifier(FunctionSpecifier),
}

impl DeclarationSpecifier {
    /// Returns the source span of the declaration specifier.
    pub fn span(&self) -> core::range::Range<usize> {
        match self {
            Self::StorageClassSpecifier(n) => n.span(),
            Self::TypeSpecifier(n) => n.span(),
            Self::TypeQualifier(n) => n.span(),
            Self::FunctionSpecifier(n) => n.span(),
        }
    }
}

/// Storage class specifier.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub enum StorageClassSpecifier {
    /// `typedef`
    Typedef {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// `extern`
    Extern {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// `static`
    Static {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// `auto`
    Auto {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// `register`
    Register {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
}

impl StorageClassSpecifier {
    /// Returns the source span of the storage class specifier.
    pub fn span(&self) -> core::range::Range<usize> {
        match self {
            Self::Typedef { span } => span.clone(),
            Self::Extern { span } => span.clone(),
            Self::Static { span } => span.clone(),
            Self::Auto { span } => span.clone(),
            Self::Register { span } => span.clone(),
        }
    }
}

/// Type specifier.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub enum TypeSpecifier {
    /// `void`
    Void {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// `char`
    Char {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// `short`
    Short {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// `int`
    Int {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// `long`
    Long {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// `float`
    Float {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// `double`
    Double {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// `signed`
    Signed {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// `unsigned`
    Unsigned {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// `_Bool`
    Bool {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// `_Complex`
    Complex {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// `_Imaginary`
    Imaginary {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// A struct or union specifier.
    StructOrUnion(StructOrUnionSpecifier),
    /// An enum specifier.
    Enum(EnumSpecifier),
    /// A typedef name.
    TypedefName(String, #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))] core::range::Range<usize>),
}

impl TypeSpecifier {
    /// Returns the source span of the type specifier.
    pub fn span(&self) -> core::range::Range<usize> {
        match self {
            Self::Void { span } => span.clone(),
            Self::Char { span } => span.clone(),
            Self::Short { span } => span.clone(),
            Self::Int { span } => span.clone(),
            Self::Long { span } => span.clone(),
            Self::Float { span } => span.clone(),
            Self::Double { span } => span.clone(),
            Self::Signed { span } => span.clone(),
            Self::Unsigned { span } => span.clone(),
            Self::Bool { span } => span.clone(),
            Self::Complex { span } => span.clone(),
            Self::Imaginary { span } => span.clone(),
            Self::StructOrUnion(n) => n.span.clone(),
            Self::Enum(n) => n.span.clone(),
            Self::TypedefName(_, span) => span.clone(),
        }
    }
}

/// Type qualifier.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub enum TypeQualifier {
    /// `const`
    Const {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// `restrict`
    Restrict {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// `volatile`
    Volatile {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
}

impl TypeQualifier {
    /// Returns the source span of the type qualifier.
    pub fn span(&self) -> core::range::Range<usize> {
        match self {
            Self::Const { span } => span.clone(),
            Self::Restrict { span } => span.clone(),
            Self::Volatile { span } => span.clone(),
        }
    }
}

/// Function specifier.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub enum FunctionSpecifier {
    /// `inline`
    Inline {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
}

impl FunctionSpecifier {
    /// Returns the source span of the function specifier.
    pub fn span(&self) -> core::range::Range<usize> {
        match self {
            Self::Inline { span } => span.clone(),
        }
    }
}

/// Struct or union specifier.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct StructOrUnionSpecifier {
    /// Whether it's a struct or union.
    pub kind: StructOrUnion,
    /// Optional tag identifier.
    pub identifier: Option<String>,
    /// List of struct declarations.
    pub struct_declarations: Vec<StructDeclaration>,
    /// Source span.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: core::range::Range<usize>,
}

/// Struct or union keyword.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub enum StructOrUnion {
    /// `struct`
    Struct {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// `union`
    Union {
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
}

impl StructOrUnion {
    /// Returns the source span of the struct or union keyword.
    pub fn span(&self) -> core::range::Range<usize> {
        match self {
            Self::Struct { span } => span.clone(),
            Self::Union { span } => span.clone(),
        }
    }
}

/// Struct declaration.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct StructDeclaration {
    /// List of specifiers and qualifiers.
    pub specifier_qualifier_list: Vec<SpecifierQualifier>,
    /// List of struct declarators.
    pub struct_declarator_list: Vec<StructDeclarator>,
    /// Source span.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: core::range::Range<usize>,
}

/// Specifier or qualifier.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub enum SpecifierQualifier {
    /// Type specifier.
    TypeSpecifier(TypeSpecifier),
    /// Type qualifier.
    TypeQualifier(TypeQualifier),
}

impl SpecifierQualifier {
    /// Returns the source span of the specifier or qualifier.
    pub fn span(&self) -> core::range::Range<usize> {
        match self {
            Self::TypeSpecifier(n) => n.span(),
            Self::TypeQualifier(n) => n.span(),
        }
    }
}

/// Struct declarator.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct StructDeclarator {
    /// The declarator.
    pub declarator: Option<Declarator>,
    /// Optional bit-field width expression.
    pub constant_expression: Option<Expression>,
    /// Source span.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: core::range::Range<usize>,
}

/// Enum specifier.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct EnumSpecifier {
    /// Optional tag identifier.
    pub identifier: Option<String>,
    /// List of enumerators.
    pub enumerators: Vec<Enumerator>,
    /// Source span.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: core::range::Range<usize>,
}

/// Enumerator.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct Enumerator {
    /// Enumerator identifier.
    pub identifier: String,
    /// Optional constant expression value.
    pub constant_expression: Option<Expression>,
    /// Source span.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: core::range::Range<usize>,
}

/// Init declarator.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct InitDeclarator {
    /// The declarator.
    pub declarator: Declarator,
    /// Optional initializer.
    pub initializer: Option<Initializer>,
    /// Source span.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: core::range::Range<usize>,
}

/// Declarator.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct Declarator {
    /// Optional pointer prefix.
    pub pointer: Option<Pointer>,
    /// Direct declarator.
    pub direct_declarator: DirectDeclarator,
    /// Source span.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: core::range::Range<usize>,
}

/// Pointer.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct Pointer {
    /// List of type qualifiers for this pointer level.
    pub type_qualifiers: Vec<TypeQualifier>,
    /// Optional nested pointer (for `**`, etc.).
    pub pointer: Option<Box<Pointer>>,
    /// Source span.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: core::range::Range<usize>,
}

/// Direct declarator.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub enum DirectDeclarator {
    /// Identifier.
    Identifier(String, #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))] core::range::Range<usize>),
    /// Parenthesized declarator.
    Declarator(Box<Declarator>, #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))] core::range::Range<usize>),
    /// Array declarator.
    Array {
        /// The declarator being declared as an array.
        direct_declarator: Box<DirectDeclarator>,
        /// Type qualifiers inside `[]`.
        type_qualifiers: Vec<TypeQualifier>,
        /// Optional assignment expression for size.
        assignment_expression: Option<Box<Expression>>,
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// Function declarator.
    Function {
        /// The declarator being declared as a function.
        direct_declarator: Box<DirectDeclarator>,
        /// Parameter list.
        parameter_list: ParameterList,
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
}

impl DirectDeclarator {
    /// Returns the source span of the direct declarator.
    pub fn span(&self) -> core::range::Range<usize> {
        match self {
            Self::Identifier(_, span) => span.clone(),
            Self::Declarator(n, _) => n.span.clone(),
            Self::Array { span, .. } => span.clone(),
            Self::Function { span, .. } => span.clone(),
        }
    }
}

/// Parameter list.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct ParameterList {
    /// List of parameter declarations.
    pub parameter_declarations: Vec<ParameterDeclaration>,
    /// Whether the function is variadic (ends with `...`).
    pub variadic: bool,
    /// Source span.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: core::range::Range<usize>,
}

/// Parameter declaration.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct ParameterDeclaration {
    /// Declaration specifiers.
    pub declaration_specifiers: Vec<DeclarationSpecifier>,
    /// Optional declarator.
    pub declarator: Option<Declarator>,
    /// Optional abstract declarator.
    pub abstract_declarator: Option<AbstractDeclarator>,
    /// Source span.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: core::range::Range<usize>,
}

/// Abstract declarator.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct AbstractDeclarator {
    /// Optional pointer prefix.
    pub pointer: Option<Pointer>,
    /// Direct abstract declarator.
    pub direct_abstract_declarator: Option<Box<DirectAbstractDeclarator>>,
    /// Source span.
    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
    pub span: core::range::Range<usize>,
}

/// Direct abstract declarator.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub enum DirectAbstractDeclarator {
    /// Parenthesized abstract declarator.
    AbstractDeclarator(Box<AbstractDeclarator>),
    /// Array abstract declarator.
    Array {
        /// Optional direct abstract declarator.
        declarator: Option<Box<DirectAbstractDeclarator>>,
        /// Optional size expression.
        assignment_expression: Option<Box<Expression>>,
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
    /// Function abstract declarator.
    Function {
        /// Optional direct abstract declarator.
        declarator: Option<Box<DirectAbstractDeclarator>>,
        /// Parameter list.
        parameter_list: Option<ParameterList>,
        /// Source span.
        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
        span: core::range::Range<usize>,
    },
}

impl DirectAbstractDeclarator {
    /// Returns the source span of the direct abstract declarator.
    pub fn span(&self) -> core::range::Range<usize> {
        match self {
            Self::AbstractDeclarator(n) => n.span.clone(),
            Self::Array { span, .. } => span.clone(),
            Self::Function { span, .. } => span.clone(),
        }
    }
}

/// Initializer.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub enum Initializer {
    /// Assignment expression.
    AssignmentExpression(Expression),
    /// Initializer list `{ ... }`.
    InitializerList(Vec<Initializer>, #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))] core::range::Range<usize>),
}

impl Initializer {
    /// Returns the source span of the initializer.
    pub fn span(&self) -> core::range::Range<usize> {
        match self {
            Self::AssignmentExpression(n) => n.span.clone(),
            Self::InitializerList(_, span) => span.clone(),
        }
    }
}