RegEx.hpp

Basic regular expression handler.

This file is part of Empirical, https://github.com/devosoft/Empirical Copyright (C) 2016-2024 Michigan State University MIT Software license; see doc/LICENSE.md

A fully (well, mostly) functional regular expression processor.

Special chars: | - or

  • - zero or more of previous

  • - one or more of previous ? - previous is optional ^ - match the beginning of the line (no chars) $ - match the end of the line (no chars) . - match any character except

    \d - match any digit (\D for anything EXCEPT a digit) \l - match any letter (\L for anything EXCEPT a letter) \s - match any whiteSpace (\S for anything EXCEPT whitespace) \w - match any identifier (word) char (\W for anything EXCEPT an identifier char)

Plus the following group contents (and change may translation rules) (…) - group contents “…” - ignore special characters in contents (internal quotes must be escaped) […] - character set — choose ONE character from the set. ‘^’ as first char negates contents ‘-’ indicates range UNLESS first or last. {n} - match the previous entry exactly n times. {n,} - match the previous entry at least n times. {m,n} - match the previous entry at least m, but no more than n times

Additional overloads for functions in lexer_utils.h:

static NFA to_NFA(const RegEx & regex, int stop_id=1); static DFA to_DFA(const RegEx & regex);

Todo:

Implement / to separate a regex from another regex that must follow it

Consider a separator (maybe backtick?) to divide up a regex expression; the result can be returned by each section as a vector of strings.

Consolidate most errors to a single pre-processing checker (perhaps using a lexer?)

Note

Status: BETA

Defines

INCLUDE_EMP_COMPILER_REG_EX_HPP_GUARD
class RegEx
#include <RegEx.hpp>

A basic regular expression handler.

Private Types

using opts_t = BitSet<NUM_SYMBOLS>

Private Functions

template<typename ...T>
inline void Error(T&&... args)
inline bool EnsureNext(char x)

Make sure that there is another element in the RegEx (e.g., that ‘[’ is followed by ‘]’) or else trigger an error to report the problem.

inline Ptr<re_charset> ConstructSet()

Construct a character range.

inline Ptr<re_string> ConstructString()

Construct a string, loading everything needed.

inline Ptr<re_base> ConstructSegment()

Should only be called when we know we have a single unit to produce. Build and return it.

inline RepeatInfo ReadRepeat()
inline void Generate() const

Private Members

String regex

Original string to define this RegEx.

vector<String> notes

Any warnings or errors would be provided here.

bool valid = true

Set to false if regex cannot be processed.

size_t pos = 0

Position being read in regex.

mutable DFA dfa

DFA that this RegEx translates to.

mutable bool dfa_ready = false

Is the dfa ready? (or does it need to be generated?)

Ptr<re_parent> head_ptr = nullptr

Private Static Functions

static inline NFA to_NFA(const RegEx &regex, size_t stop_id = 1)

Simple conversion of RegEx to NFA (mostly implemented in RegEx)

static inline DFA to_DFA(const RegEx &regex)

Conversion of RegEx to DFA, via NFA intermediate.

Private Static Attributes

static constexpr size_t NUM_SYMBOLS = 128

Maximum number of symbol the RegEx can handle.

struct re_base

Internal base representation of a portion of a regex.

Public Functions

inline virtual ~re_base()
inline virtual void Print(std::ostream &os) const
inline virtual Ptr<re_block> AsBlock()
inline virtual Ptr<re_charset> AsCharSet()
inline virtual Ptr<re_parent> AsParent()
inline virtual Ptr<re_string> AsString()
inline virtual size_t GetSize() const
inline virtual bool Simplify()
inline virtual void AddToNFA(NFA &nfa, size_t start, size_t stop) const
struct re_block : public RegEx::re_parent

Representation of a series of components…

Public Functions

inline void Print(std::ostream &os) const override
inline Ptr<re_block> AsBlock() override
inline bool Simplify() override
inline virtual void AddToNFA(NFA &nfa, size_t start, size_t stop) const override
struct re_charset : public RegEx::re_base

Representation of a character set e.g., [abc].

Public Functions

inline re_charset()
inline re_charset(char x, bool neg = false)
inline re_charset(const String &s, bool neg = false)
inline void Negate()
inline void Print(std::ostream &os) const override
inline Ptr<re_charset> AsCharSet() override
inline size_t GetSize() const override
inline char First() const
inline virtual void AddToNFA(NFA &nfa, size_t start, size_t stop) const override

Public Members

opts_t char_set = {}
struct re_or : public RegEx::re_parent

Representation of two options in a regex, e.g., a|b.

Public Functions

inline re_or(Ptr<re_base> l, Ptr<re_base> r)
inline void Print(std::ostream &os) const override
inline virtual void AddToNFA(NFA &nfa, size_t start, size_t stop) const override
struct re_parent : public RegEx::re_base

Intermediate base class for RegEx components that have children (such as “and” and “or”)

Public Functions

re_parent() = default
inline ~re_parent()
inline void Clear()
inline virtual void push(Ptr<re_base> x)
inline Ptr<re_base> pop()
inline size_t GetSize() const override
inline Ptr<re_parent> AsParent() override
inline bool Simplify() override

Protected Attributes

vector<Ptr<re_base>> nodes
struct re_plus : public RegEx::re_parent

Representations of one-or-more instances of a component. e.g., a+.

Public Functions

inline re_plus(Ptr<re_base> c)
inline void Print(std::ostream &os) const override
inline virtual void AddToNFA(NFA &nfa, size_t start, size_t stop) const override
struct re_qm : public RegEx::re_parent

Representations of zero-or-one instances of a component. e.g., a?

Public Functions

inline re_qm(Ptr<re_base> c)
inline void Print(std::ostream &os) const override
inline virtual void AddToNFA(NFA &nfa, size_t start, size_t stop) const override
struct re_repeat : public RegEx::re_parent

Representations of specified number of instances of a component. e.g., a{m,n}.

Public Functions

inline re_repeat(Ptr<re_base> c, RepeatInfo in_repeat)
inline void Print(std::ostream &os) const override
inline virtual void AddToNFA(NFA &nfa, size_t start, size_t stop) const override

Public Members

RepeatInfo repeat
struct re_star : public RegEx::re_parent

Representations of zero-or-more instances of a component. e.g., a*.

Public Functions

inline re_star(Ptr<re_base> c)
inline void Print(std::ostream &os) const override
inline virtual void AddToNFA(NFA &nfa, size_t start, size_t stop) const override
struct re_string : public RegEx::re_base

Representation of strings stored in a RegEx.

Public Functions

re_string() = default
inline re_string(char c)
inline re_string(const String &s)
inline void Print(std::ostream &os) const override
inline Ptr<re_string> AsString() override
inline size_t GetSize() const override
inline virtual void AddToNFA(NFA &nfa, size_t start, size_t stop) const override

Public Members

String str = {}
struct RepeatInfo

Public Members

int min_repeat = 1
int max_repeat = 1