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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
// CFscintilla.cpp
// this file is part of Context Free
// ---------------------
// Copyright (C) 2018 John Horigan - john@glyphic.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// John Horigan can be contacted at john@glyphic.com or at
// John Horigan, 1209 Villa St., Mountain View, CA 94041-1123, USA
//
//
#include "CFscintilla.h"
#include <cctype>
#include <string>
#include <cstring>
#include <set>
#include <vector>
std::vector<const char*> CFscintilla::AutoComplete = {
"ARCREL", "ARCTO", "background", "bitand", "bitleft", "bitnot", "bitor", "bitright", "bitxor", "blend",
"CF::Align", "CF::AllowOverlap", "CF::Alpha", "CF::ArcCW", "CF::ArcLarge", "CF::Background",
"CF::BevelJoin", "CF::BorderDynamic", "CF::BorderFixed", "CF::ButtCap", "CF::cm", "CF::cmm", "CF::Color",
"CF::ColorDepth", "CF::Continuous", "CF::Cyclic", "CF::Dihedral", "CF::EvenOdd", "CF::Frame",
"CF::FrameTime", "CF::Impure", "CF::IsoWidth", "CF::MaxNatural", "CF::MaxShapes",
"CF::MinimumSize", "CF::MiterJoin",
"CF::Normal", "CF::Clear", "CF::Xor", "CF::Plus", "CF::Multiply", "CF::Screen", "CF::Overlay",
"CF::Darken", "CF::Lighten", "CF::ColorDodge", "CF::ColorBurn", "CF::HardLight", "CF::SoftLight",
"CF::Difference", "CF::Exclusion",
"CF::p11g", "CF::p11m", "CF::p1m1", "CF::p2", "CF::p2mg", "CF::p2mm", "CF::p3", "CF::p3m1", "CF::p31m",
"CF::p4", "CF::p4g", "CF::p4m", "CF::p6", "CF::p6m", "CF::pg", "CF::pgg", "CF::pm", "CF::pmg", "CF::pmm",
"CF::RoundCap", "CF::RoundJoin", "CF::Size", "CF::SquareCap", "CF::StartShape", "CF::Symmetry", "CF::Tile", "CF::Time",
"CIRCLE", "CLOSEPOLY", "CURVEREL", "CURVETO", "FILL", "import", "include", "LINEREL", "LINETO", "MOVEREL", "MOVETO", "SQUARE", "STROKE", "TRIANGLE",
"rand", "rand::cauchy", "rand::chisquared", "rand::exponential", "rand::extremeV",
"rand::fisherF", "rand::gamma", "rand::lognormal", "rand::normal", "rand::studentT", "rand::weibull",
"randint", "randint::bernoulli", "randint::binomial", "randint::discrete",
"randint::geometric", "randint::negbinomial", "randint::poisson", "rand_static", "startshape",
"floor", "ceiling", "infinity", "factorial", "isNatural", "divides", "div",
"dot", "cross", "hsb2rgb", "rgb2hsb"
};
static std::size_t isUTF8op(const char* text)
{
static const char* utf8ops[6] = {
"\xe2\x80\xa6", "\xc2\xb1", "\xe2\x89\xa4", "\xe2\x89\xa5", "\xe2\x89\xa0", "\xe2\x88\x9e"
};
for (int op = 0; op < 6; ++op) {
auto len = std::strlen(utf8ops[op]);
if (std::strncmp(text, utf8ops[op], len) == 0)
return len;
}
return 0;
}
CFscintilla::Style
CFscintilla::StyleLine(std::size_t length, const char* text, char* styles, Style initStyle)
{
const unsigned char* utext = reinterpret_cast<const unsigned char*>(text);
Style state = initStyle;
std::size_t idStart = 0;
bool afterColon = false;
bool decimalPointFound = false;
int digits = 0;
for (std::size_t i = 0; i < length;) {
switch (state) {
// break == style current character and move on to next
// continue == restart current character with a new state
// It's always safe to read text[i+1] because of null termination
case StyleDefault:
if (std::isspace(utext[i])) {
styles[i] = StyleDefault;
break;
}
if (text[i] == '.' && text[i+1] == '.') {
styles[i] = styles[i+1] = StyleSymbol;
++i;
break;
}
if ((text[i] == '.' && std::isdigit(utext[i+1])) || std::isdigit(utext[i])) {
decimalPointFound = false;
digits = 0;
state = StyleNumber;
continue;
}
if (std::size_t sz = isUTF8op(text + i)) {
for (std::size_t j = 0; j < sz; ++j)
styles[i+j] = StyleSymbol;
i += sz;
continue; // don't break, i is already incremented
}
if ((utext[i] & 0x80) || std::isalpha(utext[i]) || text[i] == '_') {
idStart = i; afterColon = false;
state = StyleIdentifier;
continue;
}
if (text[i] == '#' || (text[i] == '/' && text[i+1] == '/')) {
// Style rest of line but leave EOL with default style
// to prevent wrapping of comment style
while (i < length && text[i] != '\n' && text[i] != '\r')
styles[i++] = StyleComment;
continue;
}
if (text[i] == '/' && text[i+1] == '*') {
state = StyleComment;
styles[i] = styles[i+1] = StyleComment;
++i;
break;
}
if (text[i] == '"') {
state = StyleString;
styles[i] = StyleString;
break;
}
if (strchr("<>[]{}()+-*/|=^&@:;,!", utext[i])) {
styles[i] = StyleSymbol;
break;
}
styles[i] = StyleDefault;
break;
case StyleComment:
styles[i] = StyleComment;
if (text[i] == '*' && text[i+1] == '/') {
styles[++i] = StyleComment;
state = StyleDefault;
}
break;
case StyleString:
if (text[i] == '"') {
styles[i] = StyleString;
state = StyleDefault;
break;
}
if (text[i] == '\r' || text[i] == '\n') {
// EOL must be default styled to prevent string style wrapping
state = StyleDefault;
continue;
}
styles[i] = StyleString;
break;
case StyleNumber:
if (std::isdigit(utext[i])) {
++digits;
styles[i] = StyleNumber;
break;
}
if (text[i] == '.') {
if (decimalPointFound || text[i+1] == '.') {
// This is the start of another number or the .. operator
state = StyleDefault;
continue;
}
styles[i] = StyleNumber;
decimalPointFound = true;
break;
}
if ((text[i] == 'e' || text[i] == 'E') && (digits > 0)) {
if ((text[i+1] == '+' || text[i+1] == '-') && (i + 2 < length) &&
std::isdigit(utext[i+2]))
{
styles[i] = styles[i+1] = StyleNumber;
i += 2; // style the e/E and +/-
} else if (std::isdigit(utext[i+1])) {
styles[i] = StyleNumber;
++i; // style the e/E
} else {
state = StyleDefault;
continue; // e/E is not part of number
}
// style the exponent
while (i < length && std::isdigit(utext[i]))
styles[i++] = StyleNumber;
state = StyleDefault;
continue; // don't break, i is already incremented
}
state = StyleDefault;
continue;
case StyleIdentifier:
if (std::isdigit(utext[i]) && !afterColon) {
styles[i] = StyleIdentifier;
break;
}
if (text[i] == ':' && text[i+1] == ':') {
styles[i] = styles[i+1] = StyleIdentifier;
++i;
afterColon = true;
break;
}
afterColon = false;
if (((utext[i] & 0x80) && !isUTF8op(text + i)) ||
std::isalpha(utext[i]) || text[i] == '_')
{
styles[i] = StyleIdentifier;
break;
}
if (i + 5 <= length && std::strncmp(text + i, ".cfdg", 5) == 0) {
// identifier will be restyled as a string
i += 5;
}
// Done with current identifier. Check if it is actually a
// keyword, builtin, or cfdg file (string).
StyleId(i - idStart, text + idStart, styles + idStart);
state = StyleDefault;
continue;
default:
// Should never happen, but the warnings are annoying
styles[i] = state;
break;
}
++i; // skipped if we continue
}
// If line ended w/o EOL and on an identifier then restyle the identifier
if (state == StyleIdentifier)
StyleId(length - idStart, text + idStart, styles + idStart);
// Comment style is the only style that wraps
return state == StyleComment ? StyleComment : StyleDefault;
}
void
CFscintilla::StyleId(std::size_t length, const char* text, char* styles)
{
using stringSet = std::set<std::string>;
static const stringSet keywords = {
"a", "alpha",
"b", "background", "blend", "brightness",
"case", "clone",
"else",
"f", "finally", "flip",
"h", "hue",
"if", "import", "include",
"let", "loop",
"path",
"r", "rotate", "rule",
"s", "saturation", "sat", "size", "skew", "startshape", "shape",
"tile", "time", "timescale", "trans", "transform",
"x", "x1", "x2",
"y", "y1", "y2",
"z"
};
static const stringSet builtins = {
"cos", "sin", "tan", "cot", "acos", "asin", "atan", "acot", "cosh", "sinh", "tanh", "acosh", "asinh", "atanh", "log", "log10",
"sqrt", "exp", "abs", "floor", "ceiling", "infinity", "factorial", "sg", "isNatural", "bitnot", "bitor", "bitand", "bitxor",
"bitleft", "bitright", "atan2", "mod", "divides", "div", "dot", "cross", "hsb2rgb", "rgb2hsb", "vec", "min", "max", "ftime",
"frame", "rand_static", "rand", "rand::exponential", "rand::gamma", "rand::weibull", "rand::extremeV",
"rand::normal", "rand::lognormal", "rand::chisquared", "rand::cauchy", "rand::fisherF",
"rand::studentT", "randint", "randint::bernoulli", "randint::binomial", "randint::negbinomial",
"randint::poisson", "randint::discrete", "randint::geometric",
"CIRCLE", "SQUARE", "TRIANGLE", "FILL", "STROKE",
"MOVETO", "LINETO", "ARCTO", "CURVETO", "MOVEREL", "LINEREL", "ARCREL", "CURVEREL", "CLOSEPOLY",
"CF::Normal", "CF::Clear", "CF::Xor" "CF::Plus", "CF::Multiply", "CF::Screen", "CF::Overlay",
"CF::Darken", "CF::Lighten", "CF::ColorDodge", "CF::ColorBurn", "CF::HardLight", "CF::SoftLight",
"CF::Difference", "CF::Exclusion",
"CF::None", "CF::MiterJoin", "CF::RoundJoin", "CF::BevelJoin", "CF::ButtCap", "CF::RoundCap",
"CF::SquareCap", "CF::ArcCW", "CF::ArcLarge", "CF::Continuous", "CF::Align", "CF::EvenOdd",
"CF::IsoWidth", "CF::Cyclic", "CF::Dihedral", "CF::p11g", "CF::p11m", "CF::p1m1", "CF::p2",
"CF::p2mg", "CF::p2mm", "CF::pm", "CF::pg", "CF::cm", "CF::pmm", "CF::pmg", "CF::pgg", "CF::cmm",
"CF::p4", "CF::p4m", "CF::p4g", "CF::p3", "CF::p3m1", "CF::p31m", "CF::p6", "CF::p6m",
"CF::AllowOverlap", "CF::Alpha", "CF::Background", "CF::BorderDynamic", "CF::BorderFixed",
"CF::Color", "CF::ColorDepth", "CF::Frame", "CF::FrameTime", "CF::Impure", "CF::MaxNatural",
"CF::MaxShapes", "CF::MinimumSize", "CF::Size", "CF::StartShape", "CF::Symmetry",
"CF::Tile", "CF::Time"
};
Style state = StyleIdentifier;
std::string temp{text, length};
if (length > 5 && std::strncmp(text + (length - 5), ".cfdg", 5) == 0)
state = StyleString;
else if (keywords.count(temp))
state = StyleKeywords;
else if (builtins.count(temp))
state = StyleBuiltins;
if (state != StyleIdentifier)
for (std::size_t i = 0; i < length; ++i)
styles[i] = state;
}
void
CFscintilla::StyleLines(SciFnDirect directFunction, sptr_t sciptr, Sci_Position startLine, Sci_Position endLine)
{
Sci_Position startPos = (Sci_Position)directFunction(sciptr, SCI_POSITIONFROMLINE, startLine, 0);
CFscintilla::Style state = Style::StyleDefault;
if (startLine > 0 &&
static_cast<Style>(directFunction(sciptr, SCI_GETSTYLEAT, startPos - 1, 0) == Style::StyleComment))
{
state = Style::StyleComment;
}
std::vector<char> text, styles;
directFunction(sciptr, SCI_STARTSTYLING, startPos, 0);
for (Sci_Position i = startLine; i <= endLine; ++i) {
Sci_Position length = (Sci_Position)directFunction(sciptr, SCI_LINELENGTH, i, 0);
if (text.size() < (size_t)(length + 1))
text.resize(length + 1);
if (styles.size() < (size_t)length)
styles.resize(length);
directFunction(sciptr, SCI_GETLINE, i, (sptr_t)text.data());
state = StyleLine(length, text.data(), styles.data(), state);
directFunction(sciptr, SCI_SETSTYLINGEX, length, (sptr_t)styles.data());
}
}
|