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:
- Lexical Analysis: Input text is tokenized into identifiers, keywords, and literals.
- Syntax Analysis: Tokens are processed according to the grammatical rules of Carbon.
- 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:
Or clone the repository:
Usage:
CLI
# Parse a file
# Show help
# Show version
As a Library:
use ;
Development:
Running Tests:
Code Formatting:
Linting:
Running the Program:
Before Committing:
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:
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.