Repository navigation
Expand file tree
/
Copy pathglob_program.h
More file actions
115 lines (98 loc) Β· 3.16 KB
/
Copy pathglob_program.h
File metadata and controls
115 lines (98 loc) Β· 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#ifndef SRC_GLOB_GLOB_PROGRAM_H_
#define SRC_GLOB_GLOB_PROGRAM_H_
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
#include "glob/glob_ast.h"
#include "glob/glob_unicode.h"
namespace node::glob {
struct RxNode {
enum class Kind : uint8_t {
kEmpty, // matches ''
kAssert, // zero-width start guard (see assert_* flags)
kText, // literal unit run
kAnyChar, // [^/]
kStarAny, // [^/]*? (flags below select +? and dot guards)
kClass,
kConcat,
kAlt,
kOpt, // (?:body)?
kStar, // (?:body)*
kPlus, // (?:body)+
kTwoBody, // (?:first)(?:rest)*? children = [first, rest]
kNeg, // !(..) group: guard alts + [^/]*? consume
};
Kind kind = Kind::kEmpty;
bool star_no_empty = false; // at least one unit
bool star_no_dot = false; // (?!\.) at entry
bool assert_no_dot = false; // `(?!\.)`
bool assert_no_traversal = false; // `(?!(?:^|/)\.\.?(?:$|/))`
struct CharClass {
CharSet positive;
CharSet negative;
bool has_positive = false;
bool has_negative = false;
bool negate = false;
};
CharClass klass;
PatternString text;
std::vector<uint32_t> children;
};
// One compiled path segment.
struct SegmentProgram {
enum class Tier : uint8_t {
kEmpty, // '', which matches only ''
kLiteral, // f === text
kStar, // /^\*+$/
kStarDotExt, // /^\*+([^+@!?*[(]*)$/
kStarDotStar, // /^\*+\.\*+$/
kDotStar, // /^\.\*+$/
kQmarks, // /^\?+([^+@!?*[(]*)?$/
kRegex, // generic
};
Tier tier = Tier::kRegex;
bool uflag = false; // code-point semantics + 'iu' folding
bool nocase = false; // fold case (kLiteral: only if !nocase_magic_only)
bool dot = false;
// If kLiteral: the text. If kStarDotExt/kQmarks: the extension.
PatternString literal;
PatternString literal_lower;
// kQmarks: full pattern length in UTF-16 units ($0.length).
size_t qmarks_len = 0;
// kRegex:
std::vector<RxNode> nodes;
uint32_t root = 0;
bool start_no_dot = false; // (?!\.)
bool start_no_traversal = false; // rejects '.' and '..'
bool invalid_regex = false;
};
// One lowered path part.
struct PartMatcher {
enum class Kind : uint8_t { kGlobstar, kLiteral, kProgram };
Kind kind = Kind::kProgram;
PatternString literal; // kLiteral: '===' comparison (Windows roots etc.)
// Source info
PatternString source;
SegmentProgram program;
};
struct CompiledPattern {
bool empty = false; // empty pattern: matches only ''
CompileFlags flags;
struct Row {
std::vector<PartMatcher> parts;
bool has_globstar = false;
};
std::vector<Row> rows;
// Set when the pattern does not compile
CompileError error = CompileError::kNone;
};
using CompiledPatternPtr = std::shared_ptr<CompiledPattern>;
CompiledPattern CompileProgram(ParsedPattern&& parsed,
const CompileFlags& flags);
// "Lowering", as in toLowerCase-ing
SegmentProgram LowerSegment(SegmentNode* root,
const CompileFlags& flags,
PatternView source);
} // namespace node::glob
#endif // SRC_GLOB_GLOB_PROGRAM_H_