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;
223 bool _emitBinaryTokens;
224 std::string _lineText;
225 std::vector<TokenTypeMatcher> _tokenTypeMatchers;
226 BalancedBracketSelectors* _balancedBracketSelectors;
228 std::vector<IToken> _tokens;
229 std::vector<uint32_t> _binaryTokens;
230 int _lastTokenEndIndex;
234 bool emitBinaryTokens,
235 const std::string& lineText,
236 const std::vector<TokenTypeMatcher>& tokenTypeMatchers,
237 BalancedBracketSelectors* balancedBracketSelectors
240 void produce(StateStackImpl* stack,
int endIndex);
241 void produceFromScopes(AttributedScopeStack* scopesList,
int endIndex);
243 std::vector<IToken> getResult(StateStackImpl* stack,
int lineLength);
244 std::vector<uint32_t> getBinaryResult(StateStackImpl* stack,
int lineLength);
248class Grammar :
public IGrammar,
public IRuleFactoryHelper,
public IOnigLib {
253 std::vector<Rule*> _ruleId2desc;
254 std::map<std::string, IRawGrammar*> _includedGrammars;
255 IGrammarRepository* _grammarRepository;
256 IThemeProvider* _themeProvider;
257 IRawGrammar* _grammar;
258 std::vector<Injection>* _injections;
259 BasicScopeAttributesProvider* _basicScopeAttributesProvider;
260 std::vector<TokenTypeMatcher> _tokenTypeMatchers;
264 BalancedBracketSelectors* balancedBracketSelectors;
268 IRawGrammar* grammar,
272 BalancedBracketSelectors* balancedBracketSelectors_,
273 IGrammarRepository* grammarRepository,
274 IThemeProvider* themeProvider,
282 IThemeProvider* getThemeProvider()
const {
return _themeProvider; }
284 size_t getRuleCount()
const {
return _ruleId2desc.size(); }
287 OnigScanner* createOnigScanner(
const std::vector<std::string>& sources)
override;
288 OnigString* createOnigString(
const std::string& str)
override;
291 Rule* getRule(RuleId ruleId)
override;
292 RuleId registerRule(Rule* rule)
override;
295 RuleId allocateRuleId()
override;
296 void setRule(RuleId ruleId, Rule* rule)
override;
299 IRawGrammar* getExternalGrammar(
const std::string& scopeName, IRawRepository* repository)
override;
302 BasicScopeAttributes getMetadataForScope(
const std::string& scope);
305 std::vector<Injection> getInjections();
308 ITokenizeLineResult tokenizeLine(
309 const std::string& lineText,
310 StateStack* prevState,
314 ITokenizeLineResult2 tokenizeLine2(
315 const std::string& lineText,
316 StateStack* prevState,
321 ScopeName getScopeName()
const {
return _rootScopeName; }
324 std::vector<Injection> _collectInjections();
326 struct TokenizeResult {
328 LineTokens* lineTokens;
329 StateStackImpl* ruleStack;
333 TokenizeResult _tokenize(
334 const std::string& lineText,
335 StateStackImpl* prevState,
336 bool emitBinaryTokens,
342Grammar* createGrammar(
344 IRawGrammar* grammar,
348 BalancedBracketSelectors* balancedBracketSelectors,
349 IGrammarRepository* grammarRepository,
350 IThemeProvider* themeProvider,
355IRawGrammar* 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.