1 stable release
8.0.0 | Nov 26, 2022 |
---|---|
7.0.3 |
|
6.0.1 |
|
5.0.0 |
|
4.0.0 |
|
#1648 in Algorithms
28 downloads per month
13KB
290 lines
Rustastic Automaton
ram
allows you to ease the creation of a language lexer based on finite state machines.
ram
is made available free of charge. You can support its development through Liberapay 💪
Usage
Add ram
as a dependency in Cargo.toml:
[dependencies]
ram = "8.0"
See examples and docs at https://docs.rs/ram.
License
The source code is released under the Apache 2.0 license.
lib.rs
:
This library makes it easy to create finite state machines to tokenize strings.
Here's the simplest automaton you can make with it, it simply finds EOF:
use ram::Automaton;
enum TokenType {
End,
}
// Create the FSM (2 states, 0 or 1) that will parse the source code
let mut am = Automaton::new(0, 1);
// When the FSM hits the end of the source, go to state 1, the final state
am.find_end(TokenType::End as i32, 0, 1);
// Run the FSM with an empty string as the source code
let source_code = format!("");
let runner = am.run(source_code);
assert_eq!(runner.tokens.len(), 1);
assert!(runner.completed());
// With a non-empty string, the result is not complete
let source_code = format!("Invalid entry");
let runner = am.run(source_code);
assert_eq!(runner.tokens.len(), 0);
assert!(!runner.completed());
Run cargo run --example let-it-be-42
to see a more complete example.
Dependencies
~2–3MB
~54K SLoC