Expand description
§CGrammar
A comprehensive C language grammar parser library written in Rust, implementing the C23 standard (ISO/IEC 9899:2023).
§Language Support
This library supports all major C23 features including:
- Lexical Elements: Keywords, identifiers, constants, string literals, and punctuators
- Expressions: All expression types from primary expressions to complex compound expressions
- Declarations: Variable declarations, function declarations, and type declarations
- Statements: Control flow, loops, jumps, and compound statements
- Functions: Function definitions with parameter lists and variadic arguments
- Preprocessor: Basic preprocessing token support
- Modern C Features:
- Binary constants (
0bprefix) - Digit separators in numeric literals
_BitInttype specifiertypeofandtypeof_unqualoperators_Decimal128,_Decimal32,_Decimal64types- And many more C23 additions
- Binary constants (
§Quick Start
Add this to your Cargo.toml:
[dependencies]
cgrammar = "0.2.0"§Basic Usage
use cgrammar::*;
fn main() {
let source_code = r#"
int main() {
printf("Hello, World!\n");
return 0;
}
"#;
// Tokenize the source code
let (tokens, _) = lex(source_code, None);
// Parse into AST
let result = translation_unit().parse(tokens.as_input());
if let Some(ast) = result.output() {
println!("Successfully parsed!");
println!("{:#?}", ast);
} else {
eprintln!("Parse failed!");
// Handle errors...
}
}§License
This project is licensed under the Apache-2.0 License - see the LICENSE file for details.
Re-exports§
pub use visitor::Visitor;pub use visitor::VisitorMut;pub use parser::*;
Modules§
- parser
- Parser for C source code, producing an abstract syntax tree.
- span
- Span utilities.
- visitor
- Visitor pattern implementation for traversing the C AST.
Structs§
- Assignment
Expression - Assignment expressions (6.5.16)
- Atomic
Type Specifier - Atomic type specifiers (6.7.2.4)
- Attribute
- Attribute (6.7.12.1)
- Balanced
Token Sequence - Balanced token sequence (6.4.4.3)
- Binary
Expression - Binary expressions (6.5.14)
- Braced
Initializer - Braced initializers (6.7.10)
- Character
Constant - Character constants (6.4.4.4)
- Comma
Expression - Comma expressions (6.5.17)
- Compound
Literal - Compound literals (6.5.2.5)
- Compound
Statement - Compound statements (6.8.2)
- Conditional
Expression - Conditional expressions (6.5.15)
- Declaration
- Declarations (6.7)
- Declaration
Specifiers - Declaration specifiers (6.7)
- Designated
Initializer - Designated initializers (6.7.10)
- Designation
- Designation (6.7.10)
- Enum
Specifier - Enum specifiers (6.7.2.2)
- Enumerator
- Enumerator (6.7.2.2)
- Expression
- Expression
- Expression
Statement - Expression statements (6.8.3)
- Floating
Constant - Floating-point constants (6.4.4.2)
- Function
Definition - Function definitions (6.9.1)
- Generic
Selection - Generic selection (6.5.1.1)
- Identifier
- Identifier (6.4.2.1)
- Init
Declarator - Init declarators (6.7)
- Integer
Constant - Integer constants (6.4.4.1)
- Labeled
Statement - Labeled statements (6.8.1)
- Parameter
Declaration - Parameter declarations (6.7.6)
- Pointer
- Pointers (6.7.6)
- Specifier
Qualifier List - Specifier qualifier lists (6.7.2.1)
- State
- Parsing state.
- Statement
- Statements (6.8)
- Static
Assert Declaration - Static assert declarations (6.7.11)
- String
Literal - String literal (6.4.5)
- String
Literals - Concatenation of string literals (6.4.5)
- Struct
OrUnion Specifier - Struct or union specifiers (6.7.2.1)
- Translation
Unit - Translation units (6.9)
Enums§
- Abstract
Declarator - Abstract declarators (6.7.7)
- Alignment
Specifier - Alignment specifiers (6.7.5)
- Array
Declarator - Array declarators (6.7.6)
- Assignment
Operator - Assignment operators (6.5.16)
- Attribute
Specifier - Attribute specifiers (6.7.12.1)
- Attribute
Token - Attribute tokens (6.7.12.1)
- Balanced
Token - Balanced tokens (6.4.4.3)
- Binary
Operator - Binary operators (6.5.14)
- Block
Item - Block items (6.8.2)
- Cast
Expression - Cast expressions (6.5.4)
- Constant
- Constants (6.4.4)
- Constant
Expression - Constant expressions (6.6)
- Declaration
Kind - Declaration kinds
- Declaration
Specifier - Declaration specifiers (6.7)
- Declarator
- Declarators (6.7.6)
- Designator
- Designators (6.7.10)
- Direct
Abstract Declarator - Direct abstract declarators (6.7.7)
- Direct
Declarator - Direct declarators (6.7.6)
- Encoding
Prefix - Encoding prefixes (6.4.4.4)
- Expression
Kind - Expression kinds
- External
Declaration - External declarations (6.9)
- Floating
Suffix - Floating-point suffixes (6.4.4.2)
- ForInit
- For initialization subclause (6.8.5)
- Function
Specifier - Function specifiers (6.7.4)
- Generic
Association - Generic association (6.5.1.1)
- Initializer
- Initializers (6.7.10)
- Integer
Suffix - Integer suffixes (6.4.4.1)
- Iteration
Statement - Iteration statements (6.8.5)
- Jump
Statement - Jump statements (6.8.6)
- Label
- Labels (6.8.1)
- Member
Declaration - Member declarations (6.7.2.1)
- Member
Declarator - Member declarators (6.7.2.1)
- Parameter
Declaration Kind - Parameter declaration kinds (6.7.6)
- Parameter
Type List - Parameter type lists (6.7.6)
- Pointer
OrBlock - Pointer or block (clang extension)
- Postfix
Expression - Postfix expressions (6.5.2)
- Predefined
Constant - Predefined constants (6.4.4.5)
- Primary
Block - Primary blocks (6.8.4)
- Primary
Expression - Primary expressions (6.5.1)
- Punctuator
- Punctuators (6.4.6)
- Selection
Statement - Selection statements (6.8.4)
- Statement
Kind - Statement kinds
- Storage
Class Specifier - Storage class specifiers (6.7.1)
- Struct
OrUnion - Struct or union (6.7.2.1)
- Type
Name - Type names (6.7.7)
- Type
Qualifier - Type qualifiers (6.7.3)
- Type
Specifier - Type specifiers (6.7.2)
- Type
Specifier Qualifier - Type specifier qualifiers (6.7.2.1)
- Typeof
Specifier - typeof specifiers (6.7.2.5)
- Typeof
Specifier Argument - Unary
Expression - Unary expressions (6.5.3)
- Unary
Operator - Unary operators (6.5.3)
- Unlabeled
Statement - Unlabeled statements (6.8)
Traits§
- Parser
- A trait implemented by parsers.
Functions§
- lex
- Lexes the input source code into a balanced token sequence.