Skip to main content

Crate oak_fsharp

Crate oak_fsharp 

Source
Expand description

ยง๐Ÿ› ๏ธ oak-fsharp Developer Guide

Fsharp support for the Oak language framework.

Welcome to the internal implementation of the F# parser. This module provides the core logic for tokenizing and parsing F# source code into a high-fidelity syntax tree.

ยง๐Ÿ“ฆ Core Components

  • Lexer: Tokenizes F# source code, handling indentation-sensitive syntax, computation expressions, and active patterns with full fidelity.
  • Parser: Implements the F# grammar, producing a Green Tree that represents the concrete syntax.
  • AST: Provides a type-safe Red Tree facade for easy traversal and analysis.
  • Language: Defines the FSharpLanguage configuration and integration with the Oak framework.

ยง๐Ÿš€ Usage Example

ยงBasic Parsing

use oak_fsharp::{FSharpParser, SourceText, FSharpLanguage};

fn parse_fsharp_code(code: &str) {
    let source = SourceText::new(code);
    let config = FSharpLanguage::new();
    let parser = FSharpParser::new(&config);
    let result = parser.parse(&source);

    if result.is_success() {
        let root = result.root();
        println!("AST Root: {:?}", root);
    }
}

ยงIncremental Parsing

use oak_fsharp::{FSharpParser, SourceText, FSharpLanguage};

fn incremental_update(old_code: &str, new_code: &str) {
    let config = FSharpLanguage::new();
    let mut parser = FSharpParser::new(&config);
    
    // Initial parse
    let initial_source = SourceText::new(old_code);
    let _ = parser.parse(&initial_source);
    
    // Incremental update
    let updated_source = SourceText::new(new_code);
    let result = parser.parse(&updated_source);
    
    if result.is_success() {
        println!("Incremental parse completed successfully.");
    }
}

ยง๐Ÿ” Diagnostics

The parser provides detailed diagnostics for syntax errors, including error ranges and helpful messages.

let result = parser.parse(&source);
for diagnostic in result.diagnostics() {
    println!("Error at {:?}: {}", diagnostic.range(), diagnostic.message());
}

Re-exportsยง

pub use ast::FSharpRoot;
pub use builder::FSharpBuilder;
pub use language::FSharpLanguage;
pub use lexer::FSharpLexer;
pub use lexer::token_type::FSharpTokenType;
pub use parser::FSharpParser;
pub use parser::element_type::FSharpElementType;
pub use crate::lsp::FsharpLanguageService;

Modulesยง

ast
AST module AST module.
builder
Builder module
language
Language definition and configuration for F#.
lexer
Lexer implementation for F#.
lsp
LSP module
parser
Parser module

Functionsยง

parse
Parses F# source code into a FSharpRoot AST.