oak-dart

Crates.iooak-dart
lib.rsoak-dart
version0.0.1
created_at2025-10-20 14:50:10.101358+00
updated_at2026-01-23 04:21:15.961589+00
descriptionHigh-performance incremental Dart parser for the oak ecosystem with flexible configuration, supporting cross-platform development and modern UI frameworks.
homepagehttps://github.com/ygg-lang/oaks
repositoryhttps://github.com/ygg-lang/oaks
max_upload_size
id1892037
size70,664
publisher (github:ygg-lang:publisher)

documentation

https://docs.rs/oak-dart

README

Oak Dart Parser

Crates.io Documentation

High-performance incremental Dart parser for the oak ecosystem with flexible configuration, optimized for code analysis and compilation.

🎯 Overview

Oak Dart is a robust parser for Dart, designed to handle complete Dart syntax including modern features. Built on the solid foundation of oak-core, it provides both high-level convenience and detailed AST generation for static analysis and code generation.

✨ Features

  • Complete Dart Syntax: Supports all Dart features including modern specifications
  • Full AST Generation: Generates comprehensive Abstract Syntax Trees
  • Lexer Support: Built-in tokenization with proper span information
  • Error Recovery: Graceful handling of syntax errors with detailed diagnostics

🚀 Quick Start

Basic example:

use oak_dart::{Parser, DartLanguage, SourceText};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let parser = Parser::new();
    let source = SourceText::new(r#"
import 'dart:io';

void main() {
  print('Hello, Dart!');
  
  var numbers = [1, 2, 3, 4, 5];
  numbers.forEach((number) {
    print(number * 2);
  });
}
    "#);
    
    let result = parser.parse(&source);
    println!("Parsed Dart program successfully.");
    Ok(())
}

📋 Parsing Examples

Class Parsing

use oak_dart::{Parser, DartLanguage, SourceText};

let parser = Parser::new();
let source = SourceText::new(r#"
class Calculator {
  double _result = 0.0;
  
  double add(double a, double b) {
    _result = a + b;
    return _result;
  }
  
  double subtract(double a, double b) {
    _result = a - b;
    return _result;
  }
  
  double get result => _result;
}
    "#);

let result = parser.parse(&source);
println!("Parsed Dart class successfully.");

Function Parsing

use oak_dart::{Parser, DartLanguage, SourceText};

let parser = Parser::new();
let source = SourceText::new(r#"
String greet(String name, {String greeting = 'Hello'}) {
  return '$greeting, $name!';
}

int fibonacci(int n) {
  if (n <= 1) return n;
  return fibonacci(n - 1) + fibonacci(n - 2);
}
    "#);

let result = parser.parse(&source);
println!("Parsed Dart functions successfully.");

Async/Await Parsing

use oak_dart::{Parser, DartLanguage, SourceText};

let parser = Parser::new();
let source = SourceText::new(r#"
import 'dart:convert';
import 'package:http/http.dart' as http;

Future<Map<String, dynamic>> fetchData(String url) async {
  final response = await http.get(Uri.parse(url));
  
  if (response.statusCode == 200) {
    return jsonDecode(response.body) as Map<String, dynamic>;
  } else {
    throw Exception('Failed to load data');
  }
}

void main() async {
  try {
    final data = await fetchData('https://api.example.com/data');
    print('Data loaded: $data');
  } catch (e) {
    print('Error: $e');
  }
}
    "#);

let result = parser.parse(&source);
println!("Parsed Dart async code successfully.");

🔧 Advanced Features

Token-Level Parsing

use oak_dart::{Parser, DartLanguage, SourceText};

let parser = Parser::new();
let source = SourceText::new("void main() { print('Hello'); }");
let result = parser.parse(&source);
// Token information is available in the parse result

Error Handling

use oak_dart::{Parser, DartLanguage, SourceText};

let parser = Parser::new();
let source = SourceText::new(r#"
void main() {
  print('Hello, Dart!'
  // Missing closing parenthesis
}
    "#);

let result = parser.parse(&source);
if let Err(e) = result.result {
    println!("Parse error: {:?}", e);
}

🏗️ AST Structure

The parser generates a comprehensive AST with the following main structures:

  • CompilationUnit: Root container for Dart programs
  • ClassDeclaration: Class definitions
  • FunctionDeclaration: Function declarations and definitions
  • MethodDeclaration: Method definitions within classes
  • VariableDeclaration: Variable declarations
  • Expression: Various expression types
  • Statement: Control flow and other statements

📊 Performance

  • Streaming: Parse large Dart files without loading entirely into memory
  • Incremental: Re-parse only changed sections
  • Memory Efficient: Smart AST node allocation
  • Fast Recovery: Quick error recovery for better IDE integration

🔗 Integration

Oak Dart integrates seamlessly with:

  • Compilers: Front-end for Dart compilers
  • Static Analysis Tools: Code quality and security analysis
  • IDE Support: Language server protocol compatibility
  • Code Generation: Generating code from AST

📚 Examples

Check out the examples directory for comprehensive examples:

  • Complete Dart program parsing
  • Class and function analysis
  • Code transformation
  • Integration with development workflows

🤝 Contributing

Contributions are welcome!

Please feel free to submit pull requests at the project repository or open issues.

Commit count: 80

cargo fmt