Skip to main content

Crate oak_c

Crate oak_c 

Source
Expand description

ยง๐Ÿ› ๏ธ C Parser Developer Guide

This guide is designed to help you quickly get started with developing and integrating oak-c.

ยง๐Ÿšฆ Quick Start

ยงBasic Parsing Example

The following is a standard workflow for parsing a C function:

use oak_c::{CParser, SourceText, CLanguage};

fn main() {
    // 1. Prepare source code
    let code = r#"
        #include <stdio.h>
        
        int main() {
            printf("Hello, Oak!\n");
            return 0;
        }
    "#;
    let source = SourceText::new(code);

    // 2. Initialize parser
    let config = CLanguage::new();
    let parser = CParser::new(&config);

    // 3. Execute parsing
    let result = parser.parse(&source);

    // 4. Handle results
    if result.is_success() {
        println!("Parsing successful! AST node count: {}", result.node_count());
    } else {
        eprintln!("Errors found during parsing.");
    }
}

ยง๐Ÿ” Core API Usage

ยง1. Syntax Tree Traversal

After a successful parse, you can use the built-in visitor pattern or manually traverse the Green/Red Tree to extract C-specific constructs like function definitions, struct members, or preprocessor directives.

ยง2. Incremental Parsing

No need to re-parse the entire translation unit when small changes occur:

// Assuming you have an old parse result 'old_result' and new source text 'new_source'
let new_result = parser.reparse(&new_source, &old_result);

ยง3. Diagnostics

oak-c provides rich error contexts specifically tailored for C developers:

for diag in result.diagnostics() {
    println!("[{}:{}] {}", diag.line, diag.column, diag.message);
}

ยง๐Ÿ—๏ธ Architecture Overview

  • Lexer: Tokenizes C source text into a stream of tokens, handling keywords, operators, and literals.
  • Parser: Syntax analyzer based on the Pratt parsing algorithm to handle complex C expression precedence and operator associativity.
  • AST: A strongly-typed syntax abstraction layer designed for downstream systems analysis tools.

ยง๐Ÿ”— Advanced Resources

  • Full Examples: Check the examples/ folder in the project root.
  • API Documentation: Run cargo doc --open for detailed type definitions.
  • Test Cases: See tests/ for handling of various C edge cases and standards. C support for the Oak language framework.

Re-exportsยง

pub use lexer::token_type::CTokenType;
pub use parser::CParser;
pub use parser::element_type::CElementType;

Modulesยง

ast
AST module.
builder
Builder module.
language
Type definition module. Language configuration module.
lexer
Lexer module.
lsp
C Lsp
parser
Parser module.

Enumsยง

LanguageCategory
Represents the broad category a language belongs to.

Traitsยง

ElementType
Element type definitions for nodes in the parsed tree.
Language
Language definition trait that coordinates all language-related types and behaviors.
TokenType
Token type definitions for tokens in the parsing system.