Skip to main content

Crate oak_objective_c

Crate oak_objective_c 

Source
Expand description

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

Objective-c support for the Oak language framework.

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

ยง๐Ÿšฆ Quick Start

Add the dependency to your Cargo.toml:

[dependencies]
oak-objective-c = { path = "..." }

ยงBasic Parsing Example

The following is a standard workflow for parsing modern Objective-C with properties, categories, and blocks:

use oak_objective_c::{Parser, SourceText};

fn main() {
    // 1. Prepare source code
    let code = r#"
        #import <Foundation/Foundation.h>

        @interface User : NSObject
        @property (nonatomic, copy) NSString *name;
        - (void)greetWithCompletion:(void (^)(NSString *))completion;
        @end

        @implementation User
        - (void)greetWithCompletion:(void (^)(NSString *))completion {
            NSLog(@"Hello, %@", self.name);
            if (completion) {
                completion(@"Greeting finished");
            }
        }
        @end
    "#;
    let source = SourceText::new(code);

    // 2. Initialize parser
    let parser = Parser::new();

    // 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 Objective-C specific constructs like interface/implementation blocks, property declarations, method definitions, or block expressions.

ยง2. Incremental Parsing

No need to re-parse a massive Objective-C file 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-objective-c provides rich error contexts specifically tailored for Objective-C developers, handling complex scenarios like missing @end keywords, malformed message expressions, or invalid block syntax:

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

ยง๐Ÿ—๏ธ Architecture Overview

  • Lexer: Tokenizes Objective-C source text into a stream of tokens, including support for @-prefixed keywords, message selectors, and C-style literals.
  • Parser: Syntax analyzer based on the Pratt parsing algorithm to handle Objective-Cโ€™s unique message passing syntax, block structures, and mixed C/C++ integration.
  • AST: A strongly-typed syntax abstraction layer designed for high-performance Objective-C analysis tools, static analyzers, and IDEs.

ยง๐Ÿ”— 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 Objective-C standards and edge cases.

Re-exportsยง

pub use crate::ast::ObjectiveCRoot;
pub use crate::builder::ObjectiveCBuilder;
pub use crate::language::ObjectiveCLanguage;
pub use crate::lexer::ObjectiveCLexer;
pub use crate::parser::ObjectiveCParser;
pub use crate::lsp::highlighter::HighlightKind;
pub use crate::lsp::highlighter::Highlighter;
pub use crate::lsp::highlighter::ObjectiveCHighlighter;
pub use crate::lsp::ObjectiveCLanguageService;
pub use lexer::token_type::ObjectiveCTokenType;
pub use parser::element_type::ObjectiveCElementType;

Modulesยง

ast
AST module.
builder
Builder module.
language
Language definitions.
lexer
Lexer module.
lsp
LSP module.
mcp
MCP module.
parser
Parser module.