[870221]: lexlib / SubStyles.h  Maximize  Restore  History

Download this file

178 lines (149 with data), 4.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Scintilla source code edit control
/** @file SubStyles.h
** Manage substyles for a lexer.
**/
// Copyright 2012 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.
#ifndef SUBSTYLES_H
#define SUBSTYLES_H
#ifdef SCI_NAMESPACE
namespace Scintilla {
#endif
class WordClassifier {
int baseStyle;
int firstStyle;
int lenStyles;
std::map<std::string, int> wordToStyle;
public:
WordClassifier(int baseStyle_) : baseStyle(baseStyle_), firstStyle(0), lenStyles(0) {
}
void Allocate(int firstStyle_, int lenStyles_) {
firstStyle = firstStyle_;
lenStyles = lenStyles_;
wordToStyle.clear();
}
int Base() const {
return baseStyle;
}
int Start() const {
return firstStyle;
}
int Length() const {
return lenStyles;
}
void Clear() {
firstStyle = 0;
lenStyles = 0;
wordToStyle.clear();
}
int ValueFor(const std::string &s) const {
std::map<std::string, int>::const_iterator it = wordToStyle.find(s);
if (it != wordToStyle.end())
return it->second;
else
return -1;
}
bool IncludesStyle(int style) const {
return (style >= firstStyle) && (style < (firstStyle + lenStyles));
}
void SetIdentifiers(int style, const char *identifiers) {
while (*identifiers) {
const char *cpSpace = identifiers;
while (*cpSpace && !(*cpSpace == ' ' || *cpSpace == '\t' || *cpSpace == '\r' || *cpSpace == '\n'))
cpSpace++;
if (cpSpace > identifiers) {
std::string word(identifiers, cpSpace - identifiers);
wordToStyle[word] = style;
}
identifiers = cpSpace;
if (*identifiers)
identifiers++;
}
}
};
class SubStyles {
int classifications;
const char *baseStyles;
int styleFirst;
int stylesAvailable;
int secondaryDistance;
int allocated;
std::vector<WordClassifier> classifiers;
int BlockFromBaseStyle(int baseStyle) const {
for (int b=0; b < classifications; b++) {
if (baseStyle == baseStyles[b])
return b;
}
return -1;
}
int BlockFromStyle(int style) const {
int b = 0;
for (std::vector<WordClassifier>::const_iterator it=classifiers.begin(); it != classifiers.end(); ++it) {
if (it->IncludesStyle(style))
return b;
b++;
}
return -1;
}
public:
SubStyles(const char *baseStyles_, int styleFirst_, int stylesAvailable_, int secondaryDistance_) :
classifications(0),
baseStyles(baseStyles_),
styleFirst(styleFirst_),
stylesAvailable(stylesAvailable_),
secondaryDistance(secondaryDistance_),
allocated(0) {
while (baseStyles[classifications]) {
classifiers.push_back(WordClassifier(baseStyles[classifications]));
classifications++;
}
}
int Allocate(int styleBase, int numberStyles) {
int block = BlockFromBaseStyle(styleBase);
if (block >= 0) {
if ((allocated + numberStyles) > stylesAvailable)
return -1;
int startBlock = styleFirst + allocated;
allocated += numberStyles;
classifiers[block].Allocate(startBlock, numberStyles);
return startBlock;
} else {
return -1;
}
}
int Start(int styleBase) {
int block = BlockFromBaseStyle(styleBase);
return (block >= 0) ? classifiers[block].Start() : -1;
}
int Length(int styleBase) {
int block = BlockFromBaseStyle(styleBase);
return (block >= 0) ? classifiers[block].Length() : 0;
}
int BaseStyle(int subStyle) const {
int block = BlockFromStyle(subStyle);
if (block >= 0)
return classifiers[block].Base();
else
return subStyle;
}
int DistanceToSecondaryStyles() const {
return secondaryDistance;
}
void SetIdentifiers(int style, const char *identifiers) {
int block = BlockFromStyle(style);
if (block >= 0)
classifiers[block].SetIdentifiers(style, identifiers);
}
void Free() {
allocated = 0;
for (std::vector<WordClassifier>::iterator it=classifiers.begin(); it != classifiers.end(); ++it)
it->Clear();
}
const WordClassifier &Classifier(int baseStyle) const {
return classifiers[BlockFromBaseStyle(baseStyle)];
}
};
#ifdef SCI_NAMESPACE
}
#endif
#endif

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks