]> git.tdb.fi Git - libs/core.git/blob - source/strings/regex.h
Modernize noexcept specifiers
[libs/core.git] / source / strings / regex.h
1 #ifndef MSP_STRINGS_REGEX_H_
2 #define MSP_STRINGS_REGEX_H_
3
4 #include <stdexcept>
5 #include <string>
6 #include "regmatch.h"
7
8 namespace Msp {
9
10 class bad_regex: public std::logic_error
11 {
12 public:
13         bad_regex(const std::string &, const std::string &, const std::string::const_iterator &);
14
15 private:
16         std::string make_where(const std::string &, const std::string::const_iterator &);
17 };
18
19
20 /**
21 This class provides regular expression matching.  It supports a subset of
22 POSIX.2 extended regex syntax.  Character classes, equivalence classes and
23 collating elements are not supported.  Refer to the regex(7) manpage for more
24 details.
25
26 A description of the internal workings of the class follows.  You may skip this
27 if you are not a developer or otherwise interested.
28
29 The core of this class is a virtual machine running a non-deterministic finite
30 automaton (NFA).  The state of the automaton is represented by an iterator into
31 the code.  Transitions between states are represented by instructions.
32 Instructions may take multiple bytes, so a valid code iterator may not be a
33 valid NFA state.
34
35 The virtual machine may have any number of execution contexts at any one time.
36 On every cycle, each context is advanced until an input-consuming instruction
37 is encountered, keeping the input positions in sync with each other.  Execution
38 continues as long as there's at least one context remaining.
39
40 The GROUP_BEGIN and GROUP_END instructions record the begin and end offset of a
41 match group, respectively.  The GROUP_END instruction also marks the group as
42 successfully matched.  If the target group was already matched, these
43 instructions do nothing.
44
45 The JUMP instruction causes the execution iterator to be modified by an offset.
46
47 The ND_JUMP instruction causes the execution context to be split in two.  One
48 continues directly after the instruction and the other continues at an offset.
49
50 The NEGATE instruction causes the result of the next match instruction to be
51 negated.
52
53 Match instructions compare the input against a condition.  If the match
54 succeeds, execution continues at the next instruction.  If the match fails,
55 execution of that context is terminated.
56
57 The MATCH_BEGIN and MATCH_END instructions match the beginning and end of
58 the input string, respectively.  They do not consume input.
59
60 The MATCH_CHAR instruction consumes the input character and matches it against
61 a single character.  Since regexes often match sequences of printable character,
62 a match for a non-opcode character may be encoded as the character itself.
63
64 The MATCH_RANGE instruction consumes the input character and matches it against
65 an inclusive character range.
66
67 The MATCH_MASK instruction consumes the input character and matches it against
68 a bitmask.
69
70 The MATCH_ANY instruction consumes the input character and always succeeds.
71 */
72 class Regex
73 {
74 private:
75         typedef std::basic_string<unsigned char> Code;
76         typedef unsigned short Count;
77         typedef short Offset;
78         typedef unsigned short Index;
79
80         enum Instruction
81         {
82                 FIRST_INSTRUCTION_ = 0,
83
84                 JUMP,
85                 ND_JUMP,
86
87                 GROUP_BEGIN,
88                 GROUP_END,
89
90                 NEGATE,
91
92                 MATCH_BEGIN,
93                 MATCH_END,
94                 MATCH_CHAR,
95                 MATCH_RANGE,
96                 MATCH_MASK,
97                 MATCH_ANY,
98
99                 LAST_INSTRUCTION_ = 31
100         };
101
102         struct RunContext
103         {
104                 Code::const_iterator citer;
105                 std::vector<RegMatch::Group> groups;
106         };
107
108         Code code;
109         unsigned n_groups = 0;
110
111 public:
112         /** Constructs a new Regex object from a string representation. */
113         Regex(const std::string &expr);
114
115 private:
116         /** Compiles a regular expression into NFA bytecode.  , 2011The iterator will be
117         advanced to the first unused character in the string. */
118         Code compile(const std::string &expr, std::string::const_iterator &iter, unsigned &group, bool branch);
119
120         Code parse_atom(const std::string &, std::string::const_iterator &i, unsigned &);
121         Code parse_brackets(const std::string &, std::string::const_iterator &);
122         bool parse_repeat(const std::string &, std::string::const_iterator &, Count &, Count &);
123
124 public:
125         /** Matches the regex against a string.  Refer to RegMatch documentation for
126         more information on the resulting object. */
127         RegMatch match(const std::string &str) const;
128
129 private:
130         bool run(const std::string &, const std::string::const_iterator &, std::vector<RegMatch::Group> &) const;
131         bool group_compare(const RegMatch::Group &, const RegMatch::Group &) const;
132
133 public:
134         /** Returns a disassembled representation of the NFA bytecode.  For debugging
135         purposes. */
136         std::string disassemble() const;
137 private:
138         std::string disassemble_instruction(Code::const_iterator &) const;
139 };
140
141 } // namespace Msp
142
143 #endif