1#ifndef TEXTMATELIB_GRAMMAR_H
2#define TEXTMATELIB_GRAMMAR_H
10#include "basicScopesAttributeProvider.h"
21class AttributedScopeStack;
23struct TokenTypeMatcher;
29 std::vector<std::string> scopes;
31 IToken() : startIndex(0), endIndex(0) {}
35struct ITokenizeLineResult {
36 std::vector<IToken> tokens;
37 StateStack* ruleStack;
40 ITokenizeLineResult() : ruleStack(nullptr), stoppedEarly(false) {}
44struct ITokenizeLineResult2 {
45 std::vector<uint32_t> tokens;
46 StateStack* ruleStack;
49 ITokenizeLineResult2() : ruleStack(nullptr), stoppedEarly(false) {}
55 virtual ~IGrammar() {}
57 virtual ITokenizeLineResult tokenizeLine(
58 const std::string& lineText,
59 StateStack* prevState,
63 virtual ITokenizeLineResult2 tokenizeLine2(
64 const std::string& lineText,
65 StateStack* prevState,
72 std::string debugSelector;
73 Matcher<std::vector<std::string>> matcher;
82struct TokenTypeMatcher {
83 Matcher<std::vector<std::string>> matcher;
90class BalancedBracketSelectors {
92 std::vector<Matcher<std::vector<std::string>>> _balancedBracketMatchers;
93 std::vector<Matcher<std::vector<std::string>>> _unbalancedBracketMatchers;
97 BalancedBracketSelectors(
98 const std::vector<std::string>& balancedBracketSelectors,
99 const std::vector<std::string>& unbalancedBracketSelectors
102 bool matchesAlways()
const;
103 bool matchesNever()
const;
104 bool match(
const std::vector<std::string>& scopes)
const;
108class AttributedScopeStack {
110 AttributedScopeStack* parent;
114 AttributedScopeStack(
115 AttributedScopeStack* parent_,
119 ~AttributedScopeStack();
121 static AttributedScopeStack* createRoot(
122 const std::string& scopeName,
126 static AttributedScopeStack* createRootAndLookUpScopeName(
127 const std::string& scopeName,
132 AttributedScopeStack* push(
134 const std::string& scopeName
137 AttributedScopeStack* pushAttributed(
138 const std::string& scopePath,
142 std::vector<std::string> getScopeNames()
const;
144 static bool equals(AttributedScopeStack* a, AttributedScopeStack* b);
147 static AttributedScopeStack* _pushAttributed(
148 AttributedScopeStack* target,
149 const std::string& scopeName,
155class StateStackImpl :
public StateStack {
161 static StateStackImpl* NULL_STATE;
163 StateStackImpl* parent;
165 bool beginRuleCapturedEOL;
166 std::string* endRule;
167 AttributedScopeStack* nameScopesList;
168 AttributedScopeStack* contentNameScopesList;
171 StateStackImpl* parent_,
175 bool beginRuleCapturedEOL_,
176 const std::string* endRule_,
177 AttributedScopeStack* nameScopesList_,
178 AttributedScopeStack* contentNameScopesList_
185 int getDepth()
const override {
return depth; }
186 StateStack* clone()
override;
187 bool equals(StateStack* other)
override;
192 StateStackImpl* push(
196 bool beginRuleCapturedEOL,
197 const std::string* endRule,
198 AttributedScopeStack* nameScopesList,
199 AttributedScopeStack* contentNameScopesList
202 StateStackImpl* pop();
203 StateStackImpl* safePop();
206 int getEnterPos()
const {
return _enterPos; }
207 int getAnchorPos()
const {
return _anchorPos; }
208 Rule* getRule(Grammar* grammar);
211 StateStackImpl* withContentNameScopesList(AttributedScopeStack* contentNameScopesList);
212 StateStackImpl* withEndRule(
const std::string& endRule);
215 bool hasSameRuleAs(StateStackImpl* other);
217 std::string toString()
const;
235class StackNodeArena {
237 std::vector<StateStackImpl*> stacks;
238 std::vector<AttributedScopeStack*> scopes;
245 void sweepKeeping(
const std::vector<StateStackImpl*>& roots);
251StackNodeArena* tmlGetActiveArena();
252void tmlSetActiveArena(StackNodeArena* arena);
257 bool _emitBinaryTokens;
258 std::string _lineText;
259 std::vector<TokenTypeMatcher> _tokenTypeMatchers;
260 BalancedBracketSelectors* _balancedBracketSelectors;
262 std::vector<IToken> _tokens;
263 std::vector<uint32_t> _binaryTokens;
264 int _lastTokenEndIndex;
268 bool emitBinaryTokens,
269 const std::string& lineText,
270 const std::vector<TokenTypeMatcher>& tokenTypeMatchers,
271 BalancedBracketSelectors* balancedBracketSelectors
274 void produce(StateStackImpl* stack,
int endIndex);
275 void produceFromScopes(AttributedScopeStack* scopesList,
int endIndex);
277 std::vector<IToken> getResult(StateStackImpl* stack,
int lineLength);
278 std::vector<uint32_t> getBinaryResult(StateStackImpl* stack,
int lineLength);
282class Grammar :
public IGrammar,
public IRuleFactoryHelper,
public IOnigLib {
287 std::vector<Rule*> _ruleId2desc;
288 std::map<std::string, IRawGrammar*> _includedGrammars;
289 IGrammarRepository* _grammarRepository;
290 IThemeProvider* _themeProvider;
291 IRawGrammar* _grammar;
292 std::vector<Injection>* _injections;
293 BasicScopeAttributesProvider* _basicScopeAttributesProvider;
294 std::vector<TokenTypeMatcher> _tokenTypeMatchers;
298 BalancedBracketSelectors* balancedBracketSelectors;
302 IRawGrammar* grammar,
306 BalancedBracketSelectors* balancedBracketSelectors_,
307 IGrammarRepository* grammarRepository,
308 IThemeProvider* themeProvider,
316 IThemeProvider* getThemeProvider()
const {
return _themeProvider; }
318 size_t getRuleCount()
const {
return _ruleId2desc.size(); }
321 OnigScanner* createOnigScanner(
const std::vector<std::string>& sources)
override;
322 OnigString* createOnigString(
const std::string& str)
override;
325 Rule* getRule(RuleId ruleId)
override;
326 RuleId registerRule(Rule* rule)
override;
329 RuleId allocateRuleId()
override;
330 void setRule(RuleId ruleId, Rule* rule)
override;
333 IRawGrammar* getExternalGrammar(
const std::string& scopeName, IRawRepository* repository)
override;
336 BasicScopeAttributes getMetadataForScope(
const std::string& scope);
339 std::vector<Injection> getInjections();
342 ITokenizeLineResult tokenizeLine(
343 const std::string& lineText,
344 StateStack* prevState,
348 ITokenizeLineResult2 tokenizeLine2(
349 const std::string& lineText,
350 StateStack* prevState,
355 ScopeName getScopeName()
const {
return _rootScopeName; }
358 std::vector<Injection> _collectInjections();
360 struct TokenizeResult {
362 LineTokens* lineTokens;
363 StateStackImpl* ruleStack;
367 TokenizeResult _tokenize(
368 const std::string& lineText,
369 StateStackImpl* prevState,
370 bool emitBinaryTokens,
376Grammar* createGrammar(
378 IRawGrammar* grammar,
382 BalancedBracketSelectors* balancedBracketSelectors,
383 IGrammarRepository* grammarRepository,
384 IThemeProvider* themeProvider,
389IRawGrammar* initGrammar(IRawGrammar* grammar, IRawRule* base);
std::string ScopeName
Semantic name identifying a scope (e.g., "source.javascript", "comment.line")
std::map< std::string, int > EmbeddedLanguagesMap
Map from embedded language name to token type ID.
std::map< std::string, StandardTokenType > TokenTypeMap
Map from scope pattern to standard token type.
RuleId ruleIdFromNumber(int id)
Convert an integer to a RuleId.
StandardTokenType
Standard TextMate token type for syntax classification.
@ Other
Not a recognized standard type.
int32_t EncodedTokenAttributes
Compact 32-bit encoding of a token's attributes.
Core type definitions and interfaces for TextMateLib.