Skip to main content

Crate oak_go

Crate oak_go 

Source
Expand description

Β§πŸ› οΈ Go Parser Developer Guide

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

§🚦 Quick Start

Β§Basic Parsing Example

The following is a standard workflow for parsing a Go file:

use oak_go::{GoParser, SourceText, GoLanguage};

fn main() {
    // 1. Prepare source code
    let code = r#"
        package main
        
        import "fmt"
        
        func main() {
            fmt.Println("Hello from Oak Go!")
        }
    "#;
    let source = SourceText::new(code);

    // 2. Initialize parser
    let config = GoLanguage::new();
    let parser = GoParser::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 Go constructs like package declarations, struct methods, or channel operations.

Β§2. Incremental Parsing

No need to re-parse the entire 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-go provides rich error contexts specifically tailored for Go developers:

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

Β§πŸ—οΈ Architecture Overview

  • Lexer: Tokenizes Go source text into a stream of tokens, handling keywords, operators, and literals with support for Go’s unique syntax rules.
  • Parser: Syntax analyzer based on the Pratt parsing algorithm to handle Go’s expression precedence and structural declarations.
  • AST: A strongly-typed syntax abstraction layer designed for building high-performance Go analysis tools 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 Go edge cases and language versions. Go support for the Oak language framework.

Re-exportsΒ§

pub use crate::ast::GoRoot;
pub use builder::GoBuilder;
pub use language::GoLanguage;
pub use lexer::GoLexer;
pub use parser::GoParser;
pub use crate::lsp::highlighter::GoHighlighter;
pub use crate::lsp::GoLanguageService;
pub use crate::lsp::formatter::GoFormatter;
pub use crate::mcp::serve_go_mcp;
pub use lexer::token_type::GoTokenType;
pub use parser::element_type::GoElementType;

ModulesΒ§

ast
AST module.
builder
Builder module. Go θ―­θ¨€ζž„ε»Ίε™¨
language
Kind definition module. Language configuration module.
lexer
Lexer module.
lsp
LSP module.
mcp
MCP module.
parser
Parser module.