1 unstable release
Uses new Rust 2024
| 0.1.0 | Oct 15, 2025 |
|---|
#437 in Programming languages
48KB
1.5K
SLoC
Contains (ELF exe/lib, 16KB) a.out
eecee
A comprehensive Rust library for representing and pretty-printing C programming language Abstract Syntax Trees (AST). The library provides a complete type-safe representation of C syntax following the C11 standards.
Features
- Complete C AST Representation - Full coverage of C language constructs including declarations, expressions, statements, types, and more
- Pretty Printing - Built-in indented formatting for human-readable output
no_stdCompatible - Works in embedded and bare-metal environments- Memory Efficient - Uses interning to reduce memory overhead for repeated identifiers and literals
- Type Safe - Leverages Rust's type system to prevent invalid AST construction
- Well Tested - Comprehensive test suite covering all AST components
Installation
Add this to your Cargo.toml:
[dependencies]
eecee = "0.1.0"
Quick Start
use eecee::ast::*;
use internment::Intern;
// Create a simple function: int add(int a, int b) { return a + b; }
let function = FunctionDefinition::builder()
.specifiers(SpecifierList::builder()
.specifiers(vec![Specifier::Type(TypeSpecifier::Int)])
.build())
.declarator(Declarator::builder()
.pointer(None)
.direct(DirectDeclarator::Function {
declarator: Intern::new(DirectDeclarator::Identifier(
Intern::new("add".to_string())
)),
params: ParameterTypeList::builder()
.parameters(vec![
ParameterDeclaration::builder()
.specifiers(SpecifierList::builder()
.specifiers(vec![Specifier::Type(TypeSpecifier::Int)])
.build())
.declarator(Some(ParameterDeclarator::Declarator(
Declarator::builder()
.pointer(None)
.direct(DirectDeclarator::Identifier(
Intern::new("a".to_string())
))
.build()
)))
.build(),
ParameterDeclaration::builder()
.specifiers(SpecifierList::builder()
.specifiers(vec![Specifier::Type(TypeSpecifier::Int)])
.build())
.declarator(Some(ParameterDeclarator::Declarator(
Declarator::builder()
.pointer(None)
.direct(DirectDeclarator::Identifier(
Intern::new("b".to_string())
))
.build()
)))
.build(),
])
.variadic(false)
.build(),
})
.build())
.declarations(vec![])
.body(CompoundStatement::builder()
.declarations(vec![])
.statements(vec![
Statement::Return {
value: Some(Expression::Binary {
left: Intern::new(Expression::Identifier(
Intern::new("a".to_string())
)),
op: BinaryOperator::Add,
right: Intern::new(Expression::Identifier(
Intern::new("b".to_string())
)),
}),
}
])
.build())
.build();
// Print the function
println!("{}", function);
API Overview
Core Types
AST Nodes
TranslationUnit- Top-level compilation unit containing all declarationsFunctionDefinition- Complete function with signature and bodyDeclaration- Variable/type declarationsStatement- Executable statements (if, while, for, return, etc.)Expression- All expression types (binary ops, calls, literals, etc.)
Type System
TypeSpecifier- Basic and complex types (int, struct, union, enum, etc.)TypeQualifier- Type qualifiers (const, volatile, restrict, _Atomic)StorageClassSpecifier- Storage classes (static, extern, auto, register, typedef)Declarator- Variable/function declarators with pointers and dimensionsTypeName- Abstract type names for casts and sizeof
Declarations
StructOrUnionSpecifier- Struct/union definitions and forward declarationsEnumSpecifier- Enum definitions with enumeratorsInitDeclarator- Declarator with optional initializerInitializer- Expression or aggregate initializers
Statements
CompoundStatement- Block with declarations and statements- Control flow:
If,While,For,DoWhile,Switch - Jump statements:
Return,Break,Continue,Goto - Labeled statements:
Case,Default,Label
Expressions
BinaryOperator- All binary operators (+, -, *, /, ==, !=, etc.)UnaryOperator- Unary operators (++, --, !, ~, *, &, etc.)Constant- Integer, floating-point, and character constantsStringLiteral- String literals and func
Pretty Printing
The library includes a sophisticated indentation system for formatting output:
use eecee::indent_printer::{IndentContext, IndentDisplay};
let context = IndentContext::new(" "); // 4-space indentation
println!("{}", context.display(&translation_unit));
Language Support
The library aims to support C11 standards with the following features:
- ✅ Complete type system (basic types, pointers, arrays, functions)
- ✅ Struct, union, and enum declarations
- ✅ All operators (arithmetic, logical, bitwise, assignment)
- ✅ Control flow statements (if, while, for, do-while, switch)
- ✅ Jump statements (goto, break, continue, return)
- ✅ Compound literals and designated initializers
- ✅ Type qualifiers (const, volatile, restrict, _Atomic)
- ✅ Storage class specifiers (static, extern, typedef, etc.)
- ✅ Function specifiers (inline, _Noreturn)
- ✅ Alignment specifiers (_Alignas, _Alignof)
- ✅ Generic selections (_Generic)
- ✅ Static assertions (_Static_assert)
no_std Support
This library is no_std compatible by default. The library uses alloc for dynamic allocations (Vec, String).
Contributing
Contributions are welcome! Areas for contribution:
- Additional test cases for edge cases
- Performance optimizations
- Documentation improvements
- Bug fixes
References
- C11 Standard (ISO/IEC 9899:2011)
- C Grammar EBNF - Included reference grammar
Dependencies
~3.5MB
~59K SLoC