Crate laps_regex
source ·Expand description
laps_regex
Tools for generating NFAs, DFAs and state-transition tables from regular expressions.
This library is built for crate laps
.
Example: Matching UTF-8 Strings
use laps_regex::re::{RegexBuilder, CharsMatcher};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
enum Token {
Keyword,
Identifier,
Number,
}
let matcher: CharsMatcher<_> = RegexBuilder::new()
.add("if|else|while", Token::Keyword)
.add("[_a-zA-Z][_a-zA-Z0-9]*", Token::Identifier)
.add("[0-9]|[1-9][0-9]+", Token::Number)
.build()
.unwrap();
assert_eq!(matcher.is_str_match("if"), Some(&Token::Keyword));
assert_eq!(matcher.is_str_match("while1"), Some(&Token::Identifier));
assert_eq!(matcher.is_str_match("42"), Some(&Token::Number));
assert_eq!(matcher.is_str_match("?"), None);
Example: Matching Bytes
use laps_regex::re::{RegexBuilder, BytesMatcher};
let matcher: BytesMatcher<_> = RegexBuilder::new()
.add("hello|hi", 0)
.add("goodbye|bye", 1)
.build_bytes()
.unwrap();
assert_eq!(matcher.is_match("hello".as_bytes()), Some(&0));
assert_eq!(matcher.is_match(&[0x62, 0x79, 0x65]), Some(&1));
Modules
- Deterministic finite automaton (
DFA
) related implementations. - Finite automaton representations.
- Mid-level intermediate representation (
Mir
) of regular expressions. - Nondeterministic finite automaton (
NFA
) related implementations. - User interfaces for building and matching regular expressions.
- State-transition table (
StateTransTable
) related implementations.