1#ifndef TEXTMATELIB_RULE_H
2#define TEXTMATELIB_RULE_H
16class RegExpSourceList;
22 virtual ~IRuleRegistry() {}
23 virtual Rule* getRule(RuleId ruleId) = 0;
24 virtual RuleId registerRule(Rule* rule) = 0;
28class IGrammarRegistry {
30 virtual ~IGrammarRegistry() {}
31 virtual IRawGrammar* getExternalGrammar(
const std::string& scopeName, IRawRepository* repository) = 0;
35class IRuleFactoryHelper :
public IRuleRegistry,
public IGrammarRegistry {
37 virtual ~IRuleFactoryHelper() {}
40 virtual RuleId allocateRuleId() = 0;
41 virtual void setRule(RuleId ruleId, Rule* rule) = 0;
45struct ICompilePatternsResult {
46 std::vector<RuleId> patterns;
47 bool hasMissingPatterns;
49 ICompilePatternsResult() : hasMissingPatterns(false) {}
56 std::vector<RuleId> rules;
65class RegExpSourceList {
67 std::vector<RegexSource*> _items;
69 CompiledRule* _cached;
70 CompiledRule* _anchorCache_A0_G0;
71 CompiledRule* _anchorCache_A0_G1;
72 CompiledRule* _anchorCache_A1_G0;
73 CompiledRule* _anchorCache_A1_G1;
79 void push(RegexSource* item);
80 void unshift(RegexSource* item);
81 void setSource(
int index,
const std::string& newSource);
84 CompiledRule* compile(IOnigLib* onigLib);
85 CompiledRule* compileAG(IOnigLib* onigLib,
bool allowA,
bool allowG);
97 bool _nameIsCapturing;
99 bool _contentNameIsCapturing;
100 std::string* _contentName;
103 Rule(ILocation* location_, RuleId id_,
104 const std::string* name,
const std::string* contentName);
107 virtual void dispose() = 0;
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;
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;
124class CaptureRule :
public Rule {
126 RuleId retokenizeCapturedWithRuleId;
128 CaptureRule(ILocation* location_, RuleId id_,
129 const std::string* name,
const std::string* contentName,
130 RuleId retokenizeCapturedWithRuleId_);
132 void dispose()
override;
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;
143class MatchRule :
public Rule {
146 RegExpSourceList* _cachedCompiledPatterns;
149 std::vector<CaptureRule*> captures;
151 MatchRule(ILocation* location_, RuleId id_,
152 const std::string* name,
const std::string& match,
153 const std::vector<CaptureRule*>& captures_);
156 void dispose()
override;
158 std::string getDebugMatchRegExp()
const;
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;
168 RegExpSourceList* _getCachedCompiledPatterns(IRuleRegistry* grammar);
172class IncludeOnlyRule :
public Rule {
174 RegExpSourceList* _cachedCompiledPatterns;
177 bool hasMissingPatterns;
178 std::vector<RuleId> patterns;
180 IncludeOnlyRule(ILocation* location_, RuleId id_,
181 const std::string* name,
const std::string* contentName,
182 const ICompilePatternsResult& patterns_);
184 void dispose()
override;
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;
194 RegExpSourceList* _getCachedCompiledPatterns(IRuleRegistry* grammar);
198class BeginEndRule :
public Rule {
202 RegExpSourceList* _cachedCompiledPatterns;
205 std::vector<CaptureRule*> beginCaptures;
206 bool endHasBackReferences;
207 std::vector<CaptureRule*> endCaptures;
208 bool applyEndPatternLast;
209 bool hasMissingPatterns;
210 std::vector<RuleId> patterns;
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_);
219 void dispose()
override;
221 std::string getDebugBeginRegExp()
const;
222 std::string getDebugEndRegExp()
const;
223 std::string getEndWithResolvedBackReferences(
const std::string& lineText,
224 const std::vector<IOnigCaptureIndex>& captureIndices);
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;
234 RegExpSourceList* _getCachedCompiledPatterns(IRuleRegistry* grammar,
const std::string& endRegexSource);
238class BeginWhileRule :
public Rule {
242 RegExpSourceList* _cachedCompiledPatterns;
243 RegExpSourceList* _cachedCompiledWhilePatterns;
246 std::vector<CaptureRule*> beginCaptures;
247 std::vector<CaptureRule*> whileCaptures;
248 bool whileHasBackReferences;
249 bool hasMissingPatterns;
250 std::vector<RuleId> patterns;
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_);
259 void dispose()
override;
261 std::string getDebugBeginRegExp()
const;
262 std::string getDebugWhileRegExp()
const;
263 std::string getWhileWithResolvedBackReferences(
const std::string& lineText,
264 const std::vector<IOnigCaptureIndex>& captureIndices);
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;
273 CompiledRule* compileWhile(IOnigLib* onigLib,
const std::string* endRegexSource);
274 CompiledRule* compileWhileAG(IOnigLib* onigLib,
const std::string* endRegexSource,
275 bool allowA,
bool allowG);
278 RegExpSourceList* _getCachedCompiledPatterns(IRuleRegistry* grammar);
279 RegExpSourceList* _getCachedCompiledWhilePatterns(IOnigLib* onigLib,
const std::string& whileRegexSource);
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);
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);
Core type definitions and interfaces for TextMateLib.