Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
unilang_parser
A high-performance, spec-compliant parser for the Unilang CLI instruction syntax. This crate transforms CLI-like instruction strings into structured GenericInstruction objects, enabling developers to build sophisticated command-line interfaces with consistent parsing behavior.
Why unilang_parser?
Building robust CLI parsers from scratch is complex and error-prone. The unilang_parser solves this by providing:
- 🎯 Consistent Syntax: Follows the formal Unilang specification for predictable parsing behavior
- ⚡ High Performance: Leverages
strs_toolsfor efficient tokenization with minimal allocations - 🔧 Flexible Configuration: Customizable parsing rules through
UnilangParserOptions - 📍 Precise Error Reporting: Detailed error messages with exact source locations
- 🌐 Universal Design: Works across CLI, GUI, TUI, and Web API modalities
- 🚫
no_stdSupport: Can be used in embedded and resource-constrained environments
Key Features
Core Parsing Capabilities
- Command Paths: Single and multi-segment paths (
cmd,namespace.command,deep.nested.path) - Arguments: Both positional (
arg1 arg2) and named (key::value) arguments - Quoting & Escaping: Handles quoted strings (
"value",'value') with escape sequences (\",\\,\n, etc.) - Help Operator: Built-in support for
?help requests - Multiple Instructions: Parse command sequences separated by
;;
Advanced Features
- Configurable Parsing: Control duplicate argument handling, positional vs named argument order
- Location-Aware Errors:
ParseErrorwithErrorKindand preciseSourceLocationinformation - Robust Error Handling: Comprehensive error categorization for better user experience
- Memory Efficient: Built on
strs_toolsfor optimal performance
Installation
Add to your Cargo.toml:
[]
= "0.2"
For no_std environments:
[]
= { = "0.2", = false, = ["no_std"] }
Quick Start
use ;
Running Examples
The examples/ directory contains comprehensive, runnable examples demonstrating all parser features:
# Run the basic usage example
# Run specific feature examples
Each example file includes:
- Clear documentation of what it demonstrates
- Practical, real-world usage scenarios
- Detailed comments explaining the code
- Expected output and behavior
Comprehensive Examples
1. Basic Command Parsing
use ;
let parser = new;
// Simple command
let cmd = parser.parse_single_instruction?;
assert_eq!;
// Command with positional arguments
let cmd = parser.parse_single_instruction?;
assert_eq!;
assert_eq!;
2. Named Arguments and Quoting
use ;
let parser = new;
// Named arguments with quoting
let cmd = parser.parse_single_instruction?;
println!;
println!;
3. Complex Argument Patterns
use ;
let parser = new;
// Mixed positional and named arguments
let cmd = parser.parse_single_instruction?;
assert_eq!; // positional
assert_eq!;
assert_eq!;
4. Multiple Instructions
use ;
let parser = new;
// Parse command sequence
let instructions = parser.parse_multiple_instructions?;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
5. Help Operator Usage
use ;
let parser = new;
// Command help
let cmd = parser.parse_single_instruction?;
assert!;
// Contextual help with arguments
let cmd = parser.parse_single_instruction?;
assert!;
assert_eq!;
6. Advanced Escaping and Quoting
use ;
let parser = new;
// Complex escaping scenarios
let cmd = parser.parse_single_instruction?;
// The parser handles escape sequences
assert_eq!;
assert_eq!;
7. Error Handling and Diagnostics
use ;
let parser = new;
// Handle parsing errors
match parser.parse_single_instruction
8. Custom Parser Configuration
use ;
// Configure strict parsing rules
let options = UnilangParserOptions ;
let parser = new;
// This will error due to duplicate arguments
let result = parser.parse_single_instruction;
assert!;
9. Integration with Command Frameworks
use ;
// Example: Converting to your application's command structure
let parser = new;
let instruction = parser.parse_single_instruction?;
let app_cmd = convert_instruction;
println!;
10. Performance Optimization Patterns
use ;
// Reuse parser instance for better performance
let parser = new;
let commands = vec!;
for cmd_str in commands
API Reference
Core Types
Parser: Main parsing engineGenericInstruction: Parsed instruction with command path and argumentsUnilangParserOptions: Configuration for parsing behaviorParseError: Detailed error information with source locationArgument: Individual argument representation
Key Methods
Parser::new(options): Create parser with configurationparse_single_instruction(input): Parse one commandparse_multiple_instructions(input): Parse;;-separated commands
Integration with the Unilang Ecosystem
This parser is part of the larger Unilang framework:
unilang: Core framework for building multi-modal command interfacesunilang_meta: Procedural macros for compile-time command definitionsunilang_parser(this crate): Dedicated instruction parsing
The parser outputs GenericInstruction objects that are consumed by the unilang framework for semantic analysis and execution.
Performance Characteristics
- Zero-copy parsing where possible using string slices
- Minimal allocations through efficient use of
strs_tools - Linear time complexity O(n) relative to input length
- Suitable for real-time applications with microsecond parsing times
Error Categories
The parser provides detailed error classification:
InvalidCommandPath: Malformed command pathsInvalidArgument: Malformed argument syntaxUnterminatedQuotedString: Missing closing quotesInvalidEscapeSequence: Malformed escape sequencesDuplicateNamedArgument: Duplicate argument names (when configured)PositionalAfterNamed: Positional args after named (when configured)
Specification Compliance
This parser implements the official Unilang CLI syntax specification, ensuring consistent behavior across all Unilang-based applications. See spec.md for complete syntax rules and grammar.
Examples Directory
All code examples shown in this README are available as complete, runnable programs in the examples/ directory:
| Example File | Description | Key Features Demonstrated |
|---|---|---|
unilang_parser_basic.rs |
Comprehensive basic usage | Parser creation, single/multiple instructions, argument access |
01_basic_command_parsing.rs |
Simple command parsing | Command paths, positional arguments |
02_named_arguments_quoting.rs |
Named arguments | Named args with ::, single/double quotes |
03_complex_argument_patterns.rs |
Mixed argument types | Positional + named args, flag-like arguments |
04_multiple_instructions.rs |
Command sequences | ;; separated commands, workflow patterns |
05_help_operator_usage.rs |
Help requests | ? operator, contextual help |
06_advanced_escaping_quoting.rs |
Complex strings | Escape sequences, regex patterns, JSON content |
07_error_handling_diagnostics.rs |
Error handling | Error types, location info, diagnostics |
08_custom_parser_configuration.rs |
Parser configuration | Strict vs permissive parsing options |
09_integration_command_frameworks.rs |
Framework integration | Command dispatch, validation, aliasing |
10_performance_optimization_patterns.rs |
Performance optimization | Instance reuse, batch processing, streaming |
To run any example:
To run all examples:
for; do
done
Contributing
We welcome contributions! Please see our contribution guidelines for details on:
- Reporting bugs
- Suggesting features
- Submitting pull requests
- Code style guidelines
License
Licensed under the MIT License.