#![warn(missing_docs)]
use std::collections::HashMap;
use boundary::{Boundary, BoundaryKind};
use char_class::{CharClass, CharGroup};
use compile::CompileState;
use error::{CompileError, ParseError};
use grapheme::Grapheme;
use options::{CompileOptions, ParseOptions};
use repetition::RegexQuantifier;
use rule::Rule;
use span::Span;
pub mod error;
pub mod features;
pub mod options;
mod alternation;
mod boundary;
mod char_class;
mod compile;
mod grapheme;
mod group;
mod literal;
mod lookaround;
mod parse;
mod range;
mod reference;
mod regex;
mod repetition;
mod rule;
mod span;
mod stmt;
mod var;
#[derive(Clone)]
#[deprecated(
since = "0.4.4",
note = "This entire crate is deprecated. Use the `pomsky` crate instead."
)]
pub struct Rulex<'i>(Rule<'i>);
impl<'i> Rulex<'i> {
pub fn parse(input: &'i str, options: ParseOptions) -> Result<Self, ParseError> {
let rule = parse::parse(input, 256)?;
rule.validate(&options)?;
Ok(Rulex(rule))
}
pub fn compile(&self, options: CompileOptions) -> Result<String, CompileError> {
let mut used_names = HashMap::new();
let mut groups_count = 0;
self.0.get_capturing_groups(&mut groups_count, &mut used_names, false)?;
let empty_span = Span::new(0, 0);
let start = Rule::Boundary(Boundary::new(BoundaryKind::Start, empty_span));
let end = Rule::Boundary(Boundary::new(BoundaryKind::End, empty_span));
let grapheme = Rule::Grapheme(Grapheme { span: empty_span });
let codepoint = Rule::CharClass(CharClass::new(CharGroup::CodePoint, empty_span));
let builtins = vec![
("Start", &start),
("End", &end),
("Grapheme", &grapheme),
("G", &grapheme),
("Codepoint", &codepoint),
("C", &codepoint),
];
let mut state = CompileState {
next_idx: 1,
used_names,
groups_count,
default_quantifier: RegexQuantifier::Greedy,
variables: builtins,
current_vars: Default::default(),
};
let compiled = self.0.comp(options, &mut state)?;
let mut buf = String::new();
compiled.codegen(&mut buf, options.flavor);
Ok(buf)
}
pub fn parse_and_compile(
input: &'i str,
parse_options: ParseOptions,
compile_options: CompileOptions,
) -> Result<String, CompileError> {
let parsed = Self::parse(input, parse_options)?;
parsed.compile(compile_options)
}
}
#[cfg(feature = "dbg")]
impl core::fmt::Debug for Rulex<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.0.fmt(f)
}
}