TextMateLib 1.0
Modern C++ implementation of the TextMate syntax highlighting engine
Loading...
Searching...
No Matches
rule.h
1#ifndef TEXTMATELIB_RULE_H
2#define TEXTMATELIB_RULE_H
3
4#include "types.h"
5#include "utils.h"
6#include "onigLib.h"
7#include "rawGrammar.h"
8#include <string>
9#include <vector>
10#include <memory>
11
12namespace tml {
13
14// Forward declarations
15class Rule;
16class RegExpSourceList;
17class CompiledRule;
18
19// IRuleRegistry interface
20class IRuleRegistry {
21public:
22 virtual ~IRuleRegistry() {}
23 virtual Rule* getRule(RuleId ruleId) = 0;
24 virtual RuleId registerRule(Rule* rule) = 0;
25};
26
27// IGrammarRegistry interface
28class IGrammarRegistry {
29public:
30 virtual ~IGrammarRegistry() {}
31 virtual IRawGrammar* getExternalGrammar(const std::string& scopeName, IRawRepository* repository) = 0;
32};
33
34// IRuleFactoryHelper interface
35class IRuleFactoryHelper : public IRuleRegistry, public IGrammarRegistry {
36public:
37 virtual ~IRuleFactoryHelper() {}
38
39 // New methods for proper rule registration
40 virtual RuleId allocateRuleId() = 0;
41 virtual void setRule(RuleId ruleId, Rule* rule) = 0;
42};
43
44// ICompilePatternsResult structure
45struct ICompilePatternsResult {
46 std::vector<RuleId> patterns;
47 bool hasMissingPatterns;
48
49 ICompilePatternsResult() : hasMissingPatterns(false) {}
50};
51
52// CompiledRule class
53class CompiledRule {
54public:
55 OnigScanner* scanner;
56 std::vector<RuleId> rules;
57
58 CompiledRule();
59 ~CompiledRule();
60
61 void dispose();
62};
63
64// RegExpSourceList class
65class RegExpSourceList {
66private:
67 std::vector<RegexSource*> _items;
68 bool _hasAnchors;
69 CompiledRule* _cached;
70 CompiledRule* _anchorCache_A0_G0;
71 CompiledRule* _anchorCache_A0_G1;
72 CompiledRule* _anchorCache_A1_G0;
73 CompiledRule* _anchorCache_A1_G1;
74
75public:
76 RegExpSourceList();
77 ~RegExpSourceList();
78
79 void push(RegexSource* item);
80 void unshift(RegexSource* item);
81 void setSource(int index, const std::string& newSource);
82 int length() const;
83
84 CompiledRule* compile(IOnigLib* onigLib);
85 CompiledRule* compileAG(IOnigLib* onigLib, bool allowA, bool allowG);
86
87 void dispose();
88};
89
90// Abstract Rule base class
91class Rule {
92public:
93 ILocation* location;
94 RuleId id;
95
96protected:
97 bool _nameIsCapturing;
98 std::string* _name;
99 bool _contentNameIsCapturing;
100 std::string* _contentName;
101
102public:
103 Rule(ILocation* location_, RuleId id_,
104 const std::string* name, const std::string* contentName);
105 virtual ~Rule();
106
107 virtual void dispose() = 0;
108
109 std::string getDebugName() const;
110 std::string* getName(const std::string* lineText,
111 const std::vector<IOnigCaptureIndex>* captureIndices) const;
112 std::string* getContentName(const std::string& lineText,
113 const std::vector<IOnigCaptureIndex>& captureIndices) const;
114
115 virtual void collectPatterns(IRuleRegistry* grammar, RegExpSourceList* out) = 0;
116 virtual CompiledRule* compile(IRuleRegistry* grammar, IOnigLib* onigLib,
117 const std::string* endRegexSource) = 0;
118 virtual CompiledRule* compileAG(IRuleRegistry* grammar, IOnigLib* onigLib,
119 const std::string* endRegexSource,
120 bool allowA, bool allowG) = 0;
121};
122
123// CaptureRule class
124class CaptureRule : public Rule {
125public:
126 RuleId retokenizeCapturedWithRuleId;
127
128 CaptureRule(ILocation* location_, RuleId id_,
129 const std::string* name, const std::string* contentName,
130 RuleId retokenizeCapturedWithRuleId_);
131
132 void dispose() override;
133
134 void collectPatterns(IRuleRegistry* grammar, RegExpSourceList* out) override;
135 CompiledRule* compile(IRuleRegistry* grammar, IOnigLib* onigLib,
136 const std::string* endRegexSource) override;
137 CompiledRule* compileAG(IRuleRegistry* grammar, IOnigLib* onigLib,
138 const std::string* endRegexSource,
139 bool allowA, bool allowG) override;
140};
141
142// MatchRule class
143class MatchRule : public Rule {
144private:
145 RegexSource _match;
146 RegExpSourceList* _cachedCompiledPatterns;
147
148public:
149 std::vector<CaptureRule*> captures;
150
151 MatchRule(ILocation* location_, RuleId id_,
152 const std::string* name, const std::string& match,
153 const std::vector<CaptureRule*>& captures_);
154 ~MatchRule();
155
156 void dispose() override;
157
158 std::string getDebugMatchRegExp() const;
159
160 void collectPatterns(IRuleRegistry* grammar, RegExpSourceList* out) override;
161 CompiledRule* compile(IRuleRegistry* grammar, IOnigLib* onigLib,
162 const std::string* endRegexSource) override;
163 CompiledRule* compileAG(IRuleRegistry* grammar, IOnigLib* onigLib,
164 const std::string* endRegexSource,
165 bool allowA, bool allowG) override;
166
167private:
168 RegExpSourceList* _getCachedCompiledPatterns(IRuleRegistry* grammar);
169};
170
171// IncludeOnlyRule class
172class IncludeOnlyRule : public Rule {
173private:
174 RegExpSourceList* _cachedCompiledPatterns;
175
176public:
177 bool hasMissingPatterns;
178 std::vector<RuleId> patterns;
179
180 IncludeOnlyRule(ILocation* location_, RuleId id_,
181 const std::string* name, const std::string* contentName,
182 const ICompilePatternsResult& patterns_);
183
184 void dispose() override;
185
186 void collectPatterns(IRuleRegistry* grammar, RegExpSourceList* out) override;
187 CompiledRule* compile(IRuleRegistry* grammar, IOnigLib* onigLib,
188 const std::string* endRegexSource) override;
189 CompiledRule* compileAG(IRuleRegistry* grammar, IOnigLib* onigLib,
190 const std::string* endRegexSource,
191 bool allowA, bool allowG) override;
192
193private:
194 RegExpSourceList* _getCachedCompiledPatterns(IRuleRegistry* grammar);
195};
196
197// BeginEndRule class
198class BeginEndRule : public Rule {
199private:
200 RegexSource _begin;
201 RegexSource _end;
202 RegExpSourceList* _cachedCompiledPatterns;
203
204public:
205 std::vector<CaptureRule*> beginCaptures;
206 bool endHasBackReferences;
207 std::vector<CaptureRule*> endCaptures;
208 bool applyEndPatternLast;
209 bool hasMissingPatterns;
210 std::vector<RuleId> patterns;
211
212 BeginEndRule(ILocation* location_, RuleId id_,
213 const std::string* name, const std::string* contentName,
214 const std::string& begin, const std::vector<CaptureRule*>& beginCaptures_,
215 const std::string& end, const std::vector<CaptureRule*>& endCaptures_,
216 bool applyEndPatternLast_, const ICompilePatternsResult& patterns_);
217 ~BeginEndRule();
218
219 void dispose() override;
220
221 std::string getDebugBeginRegExp() const;
222 std::string getDebugEndRegExp() const;
223 std::string getEndWithResolvedBackReferences(const std::string& lineText,
224 const std::vector<IOnigCaptureIndex>& captureIndices);
225
226 void collectPatterns(IRuleRegistry* grammar, RegExpSourceList* out) override;
227 CompiledRule* compile(IRuleRegistry* grammar, IOnigLib* onigLib,
228 const std::string* endRegexSource) override;
229 CompiledRule* compileAG(IRuleRegistry* grammar, IOnigLib* onigLib,
230 const std::string* endRegexSource,
231 bool allowA, bool allowG) override;
232
233private:
234 RegExpSourceList* _getCachedCompiledPatterns(IRuleRegistry* grammar, const std::string& endRegexSource);
235};
236
237// BeginWhileRule class
238class BeginWhileRule : public Rule {
239private:
240 RegexSource _begin;
241 RegexSource _while;
242 RegExpSourceList* _cachedCompiledPatterns;
243 RegExpSourceList* _cachedCompiledWhilePatterns;
244
245public:
246 std::vector<CaptureRule*> beginCaptures;
247 std::vector<CaptureRule*> whileCaptures;
248 bool whileHasBackReferences;
249 bool hasMissingPatterns;
250 std::vector<RuleId> patterns;
251
252 BeginWhileRule(ILocation* location_, RuleId id_,
253 const std::string* name, const std::string* contentName,
254 const std::string& begin, const std::vector<CaptureRule*>& beginCaptures_,
255 const std::string& whilePattern, const std::vector<CaptureRule*>& whileCaptures_,
256 const ICompilePatternsResult& patterns_);
257 ~BeginWhileRule();
258
259 void dispose() override;
260
261 std::string getDebugBeginRegExp() const;
262 std::string getDebugWhileRegExp() const;
263 std::string getWhileWithResolvedBackReferences(const std::string& lineText,
264 const std::vector<IOnigCaptureIndex>& captureIndices);
265
266 void collectPatterns(IRuleRegistry* grammar, RegExpSourceList* out) override;
267 CompiledRule* compile(IRuleRegistry* grammar, IOnigLib* onigLib,
268 const std::string* endRegexSource) override;
269 CompiledRule* compileAG(IRuleRegistry* grammar, IOnigLib* onigLib,
270 const std::string* endRegexSource,
271 bool allowA, bool allowG) override;
272
273 CompiledRule* compileWhile(IOnigLib* onigLib, const std::string* endRegexSource);
274 CompiledRule* compileWhileAG(IOnigLib* onigLib, const std::string* endRegexSource,
275 bool allowA, bool allowG);
276
277private:
278 RegExpSourceList* _getCachedCompiledPatterns(IRuleRegistry* grammar);
279 RegExpSourceList* _getCachedCompiledWhilePatterns(IOnigLib* onigLib, const std::string& whileRegexSource);
280};
281
282// RuleFactory class
283class RuleFactory {
284public:
285 static RuleId getCompiledRuleId(IRawRule* desc, IRuleFactoryHelper* helper, IRawRepository* repository);
286 static Rule* createCaptureRule(IRuleFactoryHelper* helper, ILocation* location,
287 const std::string* name, const std::string* contentName,
288 RuleId retokenizeCapturedWithRuleId);
289
290private:
291 static std::vector<CaptureRule*> _compileCaptures(IRawCaptures* captures,
292 IRuleFactoryHelper* helper,
293 IRawRepository* repository);
294 static ICompilePatternsResult _compilePatterns(std::vector<IRawRule*>* patterns,
295 IRuleFactoryHelper* helper,
296 IRawRepository* repository);
297};
298
299} // namespace tml
300
301#endif // TEXTMATELIB_RULE_H
Core type definitions and interfaces for TextMateLib.