mpw-shell/mpw-regex.h
Kelvin Sherlock ce1a36eba5 add regular expression support for evaluate, if, etc.
eg:

evaluate abc =~ /(abc)®4/ # sets the '@4' environment variable.
evaluate abc !~ /[aA]bc/

MPW regular expressions are converted to c++11 std::regex regular expressions and evaluated.
Sadly, the // regular expression syntax interferes with unix-paths (if the / count is odd).  quoting or ∂-escaping the /s is therefore necessary.

file globbing is not yet implemented.
2022-11-22 17:22:49 -05:00

49 lines
1.1 KiB
C++

#ifndef __mpw_regex_h__
#define __mpw_regex_h__
#include "environment.h"
#include <string>
#include <regex>
class mpw_regex {
public:
mpw_regex(const std::string &s, bool slash);
mpw_regex(const mpw_regex &) = default;
// mpw_regex(mpw_regex &&) = default;
~mpw_regex() = default;
mpw_regex &operator=(const mpw_regex &) = default;
// mpw_regex &operator=(mpw_regex &&) = default;
bool match(const std::string &, class Environment &);
bool match(const std::string &);
static bool is_glob(const std::string &s);
private:
typedef std::string::const_iterator iterator;
void convert_re(const std::string &, bool slash);
iterator convert_re(iterator iter, iterator end, std::string &accumulator, unsigned char term);
iterator convert_re_repeat(iterator iter, iterator end, std::string &accumulator);
iterator convert_re_set(iterator iter, iterator end, std::string &accumulator);
iterator convert_re_capture(iterator iter, iterator end, std::string &accumulator);
std::regex re;
std::string key;
int capture_map[10] = {}; // map mpw capture number to ecma group
int num_captures = 0;
};
#endif