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
-
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_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
-
bool valid = true
Set to false if regex cannot be processed.
-
size_t pos = 0
Position being read in regex.
-
mutable bool dfa_ready = false
Is the dfa ready? (or does it need to be generated?)
Private Static Functions
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.
-
struct re_parent : public RegEx::re_base
Intermediate base class for RegEx components that have children (such as “and” and “or”)
-
struct re_plus : public RegEx::re_parent
Representations of one-or-more instances of a component. e.g., a+.
-
struct re_qm : public RegEx::re_parent
Representations of zero-or-one instances of a component. e.g., a?
-
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)
Public Members
-
RepeatInfo repeat
-
inline re_repeat(Ptr<re_base> c, RepeatInfo in_repeat)
-
struct re_star : public RegEx::re_parent
Representations of zero-or-more instances of a component. e.g., a*.
-
struct RepeatInfo
-
using opts_t = BitSet<NUM_SYMBOLS>