#parse #fsm #token #lex #finite-state-machine #state-machine

ram

A library that helps to parse source code with finite state machines

1 stable release

8.0.0 Nov 26, 2022
7.0.3 Dec 5, 2019
6.0.1 May 1, 2019
5.0.0 Jan 25, 2016
4.0.0 Dec 11, 2015

#1648 in Algorithms

28 downloads per month

Apache-2.0

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