TextMateLib 1.0
Modern C++ implementation of the TextMate syntax highlighting engine
Loading...
Searching...
No Matches
rawGrammar.h
1#ifndef TEXTMATELIB_RAW_GRAMMAR_H
2#define TEXTMATELIB_RAW_GRAMMAR_H
3
4#include "types.h"
5#include <string>
6#include <vector>
7#include <map>
8#include <memory>
9
10namespace tml {
11
12// ILocation interface
13struct ILocation {
14 std::string filename;
15 int line;
16 int charPos;
17
18 ILocation() : line(0), charPos(0) {}
19 ILocation(const std::string& fname, int l, int c)
20 : filename(fname), line(l), charPos(c) {}
21};
22
23// ILocatable interface
24struct ILocatable {
25 ILocation* tmlLocation;
26
27 ILocatable() : tmlLocation(nullptr) {}
28 ~ILocatable() {
29 delete tmlLocation;
30 }
31};
32
33// Forward declarations
34struct IRawRule;
35struct IRawRepository;
36struct IRawCaptures;
37
38// IRawCaptures interface
39struct IRawCapturesMap {
40 std::map<std::string, IRawRule*> captures;
41
42 IRawCapturesMap() {}
43 ~IRawCapturesMap();
44};
45
46struct IRawCaptures : public IRawCapturesMap, public ILocatable {
47 IRawCaptures() {}
48};
49
50// IRawRule interface
51struct IRawRule : public ILocatable {
52 RuleId* id; // Not part of spec, used internally
53
54 IncludeString* include;
55
56 ScopeName* name;
57 ScopeName* contentName;
58
59 RegExpString* match;
60 IRawCaptures* captures;
61
62 RegExpString* begin;
63 IRawCaptures* beginCaptures;
64
65 RegExpString* end;
66 IRawCaptures* endCaptures;
67
68 RegExpString* whilePattern; // 'while' is a C++ keyword, so using whilePattern
69 IRawCaptures* whileCaptures;
70
71 std::vector<IRawRule*>* patterns;
72
73 IRawRepository* repository;
74
75 bool* applyEndPatternLast;
76
77 IRawRule()
78 : id(nullptr), include(nullptr), name(nullptr), contentName(nullptr),
79 match(nullptr), captures(nullptr), begin(nullptr), beginCaptures(nullptr),
80 end(nullptr), endCaptures(nullptr), whilePattern(nullptr), whileCaptures(nullptr),
81 patterns(nullptr), repository(nullptr), applyEndPatternLast(nullptr) {}
82
83 ~IRawRule();
84};
85
86// IRawRepository interface
87struct IRawRepositoryMap {
88 std::map<std::string, IRawRule*> rules;
89 IRawRule* selfRule; // $self
90 IRawRule* baseRule; // $base
91
92 IRawRepositoryMap() : selfRule(nullptr), baseRule(nullptr) {}
93 ~IRawRepositoryMap();
94
95 // Helper method to get a rule by name (handles $self, $base, and regular rules)
96 IRawRule* getRule(const std::string& name) const;
97};
98
99struct IRawRepository : public IRawRepositoryMap, public ILocatable {
100 IRawRepository() {}
101};
102
103// IRawGrammar interface
104struct IRawGrammar : public IRawRule {
105 ScopeName scopeName;
106
107 std::map<std::string, IRawRule*>* injections;
108 std::string* injectionSelector;
109
110 std::vector<std::string>* fileTypes;
111 // name and repository are inherited from IRawRule
112 std::string* firstLineMatch;
113
114 IRawGrammar()
115 : IRawRule(), injections(nullptr), injectionSelector(nullptr),
116 fileTypes(nullptr), firstLineMatch(nullptr) {
117 }
118
119 ~IRawGrammar();
120};
121
122// Helper function to delete pointer if not null
123template<typename T>
124void deleteIfNotNull(T*& ptr) {
125 if (ptr != nullptr) {
126 delete ptr;
127 ptr = nullptr;
128 }
129}
130
131} // namespace tml
132
133#endif // TEXTMATELIB_RAW_GRAMMAR_H
std::string RegExpString
Regular expression pattern as a string.
Definition types.h:37
std::string ScopeName
Semantic name identifying a scope (e.g., "source.javascript", "comment.line")
Definition types.h:20
std::string IncludeString
String referencing another grammar to include (scope name or file path)
Definition types.h:33
Core type definitions and interfaces for TextMateLib.