#regex #lexer #automata #parser #generate #laps

laps_regex

Tools for generating NFAs, DFAs and state-transition tables from regular expressions

4 releases

0.1.1 Dec 24, 2023
0.1.0 Dec 17, 2023
0.0.2 Dec 10, 2023
0.0.1 Jun 17, 2023

#213 in Parser tooling


Used in 2 crates (via laps_macros)

MIT/Apache

69KB
1.5K SLoC

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(b"hello"), Some(&0));
assert_eq!(matcher.is_match(&[0x62, 0x79, 0x65]), Some(&1));

License

Copyright (C) 2022-2023 MaxXing. Licensed under either of Apache 2.0 or MIT at your option.

Dependencies

~3MB
~76K SLoC