| 1 |
// Scintilla source code edit control |
| 2 |
/** @file SubStyles.h |
| 3 |
** Manage substyles for a lexer. |
| 4 |
**/ |
| 5 |
// Copyright 2012 by Neil Hodgson <[email protected]> |
| 6 |
// The License.txt file describes the conditions under which this software may be distributed. |
| 7 |
|
| 8 |
#ifndef SUBSTYLES_H |
| 9 |
#define SUBSTYLES_H |
| 10 |
|
| 11 |
namespace Lexilla { |
| 12 |
|
| 13 |
class WordClassifier { |
| 14 |
int baseStyle; |
| 15 |
int firstStyle; |
| 16 |
int lenStyles; |
| 17 |
using WordStyleMap = std::map<std::string, int, std::less<>>; |
| 18 |
WordStyleMap wordToStyle; |
| 19 |
|
| 20 |
public: |
| 21 |
|
| 22 |
explicit WordClassifier(int baseStyle_) : baseStyle(baseStyle_), firstStyle(0), lenStyles(0) { |
| 23 |
} |
| 24 |
|
| 25 |
void Allocate(int firstStyle_, int lenStyles_) noexcept { |
| 26 |
firstStyle = firstStyle_; |
| 27 |
lenStyles = lenStyles_; |
| 28 |
wordToStyle.clear(); |
| 29 |
} |
| 30 |
|
| 31 |
int Base() const noexcept { |
| 32 |
return baseStyle; |
| 33 |
} |
| 34 |
|
| 35 |
int Start() const noexcept { |
| 36 |
return firstStyle; |
| 37 |
} |
| 38 |
|
| 39 |
int Last() const noexcept { |
| 40 |
return firstStyle + lenStyles - 1; |
| 41 |
} |
| 42 |
|
| 43 |
int Length() const noexcept { |
| 44 |
return lenStyles; |
| 45 |
} |
| 46 |
|
| 47 |
void Clear() noexcept { |
| 48 |
firstStyle = 0; |
| 49 |
lenStyles = 0; |
| 50 |
wordToStyle.clear(); |
| 51 |
} |
| 52 |
|
| 53 |
int ValueFor(std::string_view s) const { |
| 54 |
WordStyleMap::const_iterator const it = wordToStyle.find(s); |
| 55 |
if (it != wordToStyle.end()) |
| 56 |
return it->second; |
| 57 |
else |
| 58 |
return -1; |
| 59 |
} |
| 60 |
|
| 61 |
bool IncludesStyle(int style) const noexcept { |
| 62 |
return (style >= firstStyle) && (style < (firstStyle + lenStyles)); |
| 63 |
} |
| 64 |
|
| 65 |
void RemoveStyle(int style) noexcept { |
| 66 |
WordStyleMap::iterator it = wordToStyle.begin(); |
| 67 |
while (it != wordToStyle.end()) { |
| 68 |
if (it->second == style) { |
| 69 |
it = wordToStyle.erase(it); |
| 70 |
} else { |
| 71 |
++it; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
void SetIdentifiers(int style, const char *identifiers, bool lowerCase) { |
| 77 |
RemoveStyle(style); |
| 78 |
if (!identifiers) |
| 79 |
return; |
| 80 |
while (*identifiers) { |
| 81 |
const char *cpSpace = identifiers; |
| 82 |
while (*cpSpace && !(*cpSpace == ' ' || *cpSpace == '\t' || *cpSpace == '\r' || *cpSpace == '\n')) |
| 83 |
cpSpace++; |
| 84 |
if (cpSpace > identifiers) { |
| 85 |
std::string word(identifiers, cpSpace - identifiers); |
| 86 |
if (lowerCase) { |
| 87 |
for (char &ch : word) { |
| 88 |
ch = MakeLowerCase(ch); |
| 89 |
} |
| 90 |
} |
| 91 |
wordToStyle[word] = style; |
| 92 |
} |
| 93 |
identifiers = cpSpace; |
| 94 |
if (*identifiers) |
| 95 |
identifiers++; |
| 96 |
} |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
// This is the common configuration: 64 sub-styles allocated from 128 to 191 |
| 101 |
constexpr int SubStylesFirst = 0x80; |
| 102 |
constexpr int SubStylesAvailable = 0x40; |
| 103 |
|
| 104 |
class SubStyles { |
| 105 |
int classifications; |
| 106 |
const char *baseStyles; |
| 107 |
int styleFirst; |
| 108 |
int stylesAvailable; |
| 109 |
int secondaryDistance; |
| 110 |
int allocated; |
| 111 |
std::vector<WordClassifier> classifiers; |
| 112 |
|
| 113 |
int BlockFromBaseStyle(int baseStyle) const noexcept { |
| 114 |
for (int b=0; b < classifications; b++) { |
| 115 |
if (baseStyle == baseStyles[b]) |
| 116 |
return b; |
| 117 |
} |
| 118 |
return -1; |
| 119 |
} |
| 120 |
|
| 121 |
int BlockFromStyle(int style) const noexcept { |
| 122 |
int b = 0; |
| 123 |
for (const WordClassifier &wc : classifiers) { |
| 124 |
if (wc.IncludesStyle(style)) |
| 125 |
return b; |
| 126 |
b++; |
| 127 |
} |
| 128 |
return -1; |
| 129 |
} |
| 130 |
|
| 131 |
public: |
| 132 |
|
| 133 |
SubStyles(const char *baseStyles_, int styleFirst_=SubStylesFirst, int stylesAvailable_=SubStylesAvailable, int secondaryDistance_=0) : |
| 134 |
classifications(0), |
| 135 |
baseStyles(baseStyles_), |
| 136 |
styleFirst(styleFirst_), |
| 137 |
stylesAvailable(stylesAvailable_), |
| 138 |
secondaryDistance(secondaryDistance_), |
| 139 |
allocated(0) { |
| 140 |
while (baseStyles[classifications]) { |
| 141 |
classifiers.push_back(WordClassifier(baseStyles[classifications])); |
| 142 |
classifications++; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
int Allocate(int styleBase, int numberStyles) noexcept { |
| 147 |
const int block = BlockFromBaseStyle(styleBase); |
| 148 |
if (block >= 0) { |
| 149 |
if ((allocated + numberStyles) > stylesAvailable) |
| 150 |
return -1; |
| 151 |
const int startBlock = styleFirst + allocated; |
| 152 |
allocated += numberStyles; |
| 153 |
classifiers[block].Allocate(startBlock, numberStyles); |
| 154 |
return startBlock; |
| 155 |
} else { |
| 156 |
return -1; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
int Start(int styleBase) noexcept { |
| 161 |
const int block = BlockFromBaseStyle(styleBase); |
| 162 |
return (block >= 0) ? classifiers[block].Start() : -1; |
| 163 |
} |
| 164 |
|
| 165 |
int Length(int styleBase) noexcept { |
| 166 |
const int block = BlockFromBaseStyle(styleBase); |
| 167 |
return (block >= 0) ? classifiers[block].Length() : 0; |
| 168 |
} |
| 169 |
|
| 170 |
int BaseStyle(int subStyle) const noexcept { |
| 171 |
const int block = BlockFromStyle(subStyle); |
| 172 |
if (block >= 0) |
| 173 |
return classifiers[block].Base(); |
| 174 |
else |
| 175 |
return subStyle; |
| 176 |
} |
| 177 |
|
| 178 |
int DistanceToSecondaryStyles() const noexcept { |
| 179 |
return secondaryDistance; |
| 180 |
} |
| 181 |
|
| 182 |
int FirstAllocated() const noexcept { |
| 183 |
int start = 257; |
| 184 |
for (const WordClassifier &wc : classifiers) { |
| 185 |
if ((wc.Length() > 0) && (start > wc.Start())) |
| 186 |
start = wc.Start(); |
| 187 |
} |
| 188 |
return (start < 256) ? start : -1; |
| 189 |
} |
| 190 |
|
| 191 |
int LastAllocated() const noexcept { |
| 192 |
int last = -1; |
| 193 |
for (const WordClassifier &wc : classifiers) { |
| 194 |
if ((wc.Length() > 0) && (last < wc.Last())) |
| 195 |
last = wc.Last(); |
| 196 |
} |
| 197 |
return last; |
| 198 |
} |
| 199 |
|
| 200 |
void SetIdentifiers(int style, const char *identifiers, bool lowerCase=false) { |
| 201 |
const int block = BlockFromStyle(style); |
| 202 |
if (block >= 0) |
| 203 |
classifiers[block].SetIdentifiers(style, identifiers, lowerCase); |
| 204 |
} |
| 205 |
|
| 206 |
void Free() noexcept { |
| 207 |
allocated = 0; |
| 208 |
for (WordClassifier &wc : classifiers) { |
| 209 |
wc.Clear(); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
const WordClassifier &Classifier(int baseStyle) const noexcept { |
| 214 |
const int block = BlockFromBaseStyle(baseStyle); |
| 215 |
return classifiers[block >= 0 ? block : 0]; |
| 216 |
} |
| 217 |
}; |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
#endif |