| Crates.io | carbon-parser |
| lib.rs | carbon-parser |
| version | 0.1.2 |
| created_at | 2025-11-11 20:03:01.063736+00 |
| updated_at | 2025-11-16 03:27:57.734032+00 |
| description | A parser for Google's Carbon programming language, supporting basic syntax including function, variable, and type declarations. |
| homepage | |
| repository | https://github.com/tajmaha1/CarbonParserRust.git |
| max_upload_size | |
| id | 1928115 |
| size | 64,267 |
A parser for Google's Carbon programming language, written in Rust using the Pest library.
Carbon Parser is a tool for syntactic analysis of Carbon language code. The parser processes Carbon source code and builds an Abstract Syntax Tree (AST), which can be used for:
The parser supports the following Carbon constructs:
fn FunctionName(param: Type) -> ReturnType { ... }var variable_name: Type = value;i32, f64, bool, String)//) and multi-line (/* */)program = { SOI ~ (function_decl | var_decl)* ~ EOI }
function_decl = { "fn" ~ identifier ~ "(" ~ parameter_list? ~ ")" ~ ("->" ~ type_name)? ~ block }
var_decl = { "var" ~ identifier ~ ":" ~ type_name ~ ("=" ~ expression)? ~ ";" }
parameter_list = { parameter ~ ("," ~ parameter)* }
parameter = { identifier ~ ":" ~ type_name }
block = { "{" ~ statement* ~ "}" }
statement = { var_decl | expression ~ ";" | return_stmt }
expression = { literal | identifier | binary_expr | function_call }
The parsing result is a Pairs<Rule> from Pest, which represents the parse tree. This tree can be:
Program
├── FunctionDecl*
│ ├── Identifier (function name)
│ ├── ParameterList
│ │ └── Parameter* (name: type)
│ ├── ReturnType (optional)
│ └── Block
│ └── Statement*
└── VarDecl*
├── Identifier (variable name)
├── TypeName
└── Expression (optional)
cargo install carbon-parser
Or clone the repository:
git clone https://github.com/tajmaha1/carbon-parser
cd carbon-parser
cargo build --release
# Parse a file
carbon-parser parse input.carbon
# Show help
carbon-parser help
# Show version
carbon-parser --version
use carbon_parser::{parse_carbon, CarbonParser};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let source_code = r#"
fn main() -> i32 {
var x: i32 = 42;
return x;
}
"#;
let parse_tree = parse_carbon(source_code)?;
println!("Parsing successful!");
Ok(())
}
make test
make fmt
make clippy
make run
make pre-commit
The project contains unit tests for each grammar rule:
Run tests: cargo test
Full documentation is available at docs.rs/carbon-parser
Local documentation:
cargo doc --open
This project is distributed under the dual MIT/Apache-2.0 license.
[Daniil Cherniavskyi] - [rembo9028@gmail.com]
Pull requests are welcome. For significant changes, please open an issue first to discuss.