carbon-parser

Crates.iocarbon-parser
lib.rscarbon-parser
version0.1.2
created_at2025-11-11 20:03:01.063736+00
updated_at2025-11-16 03:27:57.734032+00
descriptionA parser for Google's Carbon programming language, supporting basic syntax including function, variable, and type declarations.
homepage
repositoryhttps://github.com/tajmaha1/CarbonParserRust.git
max_upload_size
id1928115
size64,267
megatr4n (tajmaha1)

documentation

README

Carbon Parser

A parser for Google's Carbon programming language, written in Rust using the Pest library.

Description:

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:

  • Static code analysis.
  • Creating compilers and interpreters.
  • Code formatting tools.
  • IDE integration and syntax highlighting.
  • Code analysis and refactoring.

Technical Description of the Parsing Process:

Parsing Stages:

  1. Lexical Analysis: Input text is tokenized into identifiers, keywords, and literals.
  2. Syntax Analysis: Tokens are processed according to the grammatical rules of Carbon.
  3. AST Construction: A tree-like structure representing the program's syntax is created.

What is parsed:

The parser supports the following Carbon constructs:

  • Function Declarations: fn FunctionName(param: Type) -> ReturnType { ... }
  • Variable Declarations: var variable_name: Type = value;
  • Data Types: basic types (i32, f64, bool, String)
  • Expressions: arithmetic operations, function calls, literals
  • Comments: single-line (//) and multi-line (/* */)

Grammar Rules:

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 }

Using the Results:

The parsing result is a Pairs<Rule> from Pest, which represents the parse tree. This tree can be:

  • Transformed into a typed AST for further processing.
  • Used for syntax validation.
  • Converted to other formats (JSON, XML).
  • Applied for code analysis and metrics.

Grammar Diagram:

Program
├── FunctionDecl*
│   ├── Identifier (function name)
│   ├── ParameterList
│   │   └── Parameter* (name: type)
│   ├── ReturnType (optional)
│   └── Block
│       └── Statement*
└── VarDecl*
    ├── Identifier (variable name)
    ├── TypeName
    └── Expression (optional)

Installation:

cargo install carbon-parser

Or clone the repository:

git clone https://github.com/tajmaha1/carbon-parser
cd carbon-parser
cargo build --release

Usage:

CLI

# Parse a file
carbon-parser parse input.carbon

# Show help
carbon-parser help

# Show version
carbon-parser --version

As a Library:

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(())
}

Development:

Running Tests:

make test

Code Formatting:

make fmt

Linting:

make clippy

Running the Program:

make run

Before Committing:

make pre-commit

Testing:

The project contains unit tests for each grammar rule:

  • Tests for function declarations.
  • Tests for variable declarations.
  • Tests for expressions.
  • Tests for types.
  • Tests for comments.

Run tests: cargo test

Documentation:

Full documentation is available at docs.rs/carbon-parser

Local documentation:

cargo doc --open

License:

This project is distributed under the dual MIT/Apache-2.0 license.

Author:

[Daniil Cherniavskyi] - [rembo9028@gmail.com]

Contribution:

Pull requests are welcome. For significant changes, please open an issue first to discuss.

Commit count: 0

cargo fmt