| 1 | // Scintilla source code edit control |
| 2 | /** @file LexPS.cxx |
| 3 | ** Lexer for PostScript |
| 4 | ** |
| 5 | ** Written by Nigel Hathaway <nigel@bprj.co.uk>. |
| 6 | ** The License.txt file describes the conditions under which this software may be distributed. |
| 7 | **/ |
| 8 | |
| 9 | // Previous releases of this lexer included support for marking token starts with |
| 10 | // a style byte indicator. This was used by the wxGhostscript IDE/debugger. |
| 11 | // Style byte indicators were removed in version 3.4.3. |
| 12 | // Anyone wanting to restore this functionality for wxGhostscript using 'modern' |
| 13 | // indicators can examine the earlier source in the Mercurial repository. |
| 14 | |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdarg.h> |
| 19 | #include <assert.h> |
| 20 | #include <ctype.h> |
| 21 | |
| 22 | #include <string> |
| 23 | #include <string_view> |
| 24 | |
| 25 | #include "ILexer.h" |
| 26 | #include "Scintilla.h" |
| 27 | #include "SciLexer.h" |
| 28 | |
| 29 | #include "WordList.h" |
| 30 | #include "LexAccessor.h" |
| 31 | #include "Accessor.h" |
| 32 | #include "StyleContext.h" |
| 33 | #include "CharacterSet.h" |
| 34 | #include "LexerModule.h" |
| 35 | |
| 36 | using namespace Lexilla; |
| 37 | |
| 38 | static inline bool IsASelfDelimitingChar(const int ch) { |
| 39 | return (ch == '[' || ch == ']' || ch == '{' || ch == '}' || |
| 40 | ch == '/' || ch == '<' || ch == '>' || |
| 41 | ch == '(' || ch == ')' || ch == '%'); |
| 42 | } |
| 43 | |
| 44 | static inline bool IsAWhitespaceChar(const int ch) { |
| 45 | return (ch == ' ' || ch == '\t' || ch == '\r' || |
| 46 | ch == '\n' || ch == '\f' || ch == '\0'); |
| 47 | } |
| 48 | |
| 49 | static bool IsABaseNDigit(const int ch, const int base) { |
| 50 | int maxdig = '9'; |
| 51 | int letterext = -1; |
| 52 | |
| 53 | if (base <= 10) |
| 54 | maxdig = '0' + base - 1; |
| 55 | else |
| 56 | letterext = base - 11; |
| 57 | |
| 58 | return ((ch >= '0' && ch <= maxdig) || |
| 59 | (ch >= 'A' && ch <= ('A' + letterext)) || |
| 60 | (ch >= 'a' && ch <= ('a' + letterext))); |
| 61 | } |
| 62 | |
| 63 | static inline bool IsABase85Char(const int ch) { |
| 64 | return ((ch >= '!' && ch <= 'u') || ch == 'z'); |
| 65 | } |
| 66 | |
| 67 | static void ColourisePSDoc( |
| 68 | Sci_PositionU startPos, |
| 69 | Sci_Position length, |
| 70 | int initStyle, |
| 71 | WordList *keywordlists[], |
| 72 | Accessor &styler) { |
| 73 | |
| 74 | WordList &keywords1 = *keywordlists[0]; |
| 75 | WordList &keywords2 = *keywordlists[1]; |
| 76 | WordList &keywords3 = *keywordlists[2]; |
| 77 | WordList &keywords4 = *keywordlists[3]; |
| 78 | WordList &keywords5 = *keywordlists[4]; |
| 79 | |
| 80 | StyleContext sc(startPos, length, initStyle, styler); |
| 81 | |
| 82 | int pslevel = styler.GetPropertyInt("ps.level" , 3); |
| 83 | Sci_Position lineCurrent = styler.GetLine(startPos); |
| 84 | int nestTextCurrent = 0; |
| 85 | if (lineCurrent > 0 && initStyle == SCE_PS_TEXT) |
| 86 | nestTextCurrent = styler.GetLineState(lineCurrent - 1); |
| 87 | int numRadix = 0; |
| 88 | bool numHasPoint = false; |
| 89 | bool numHasExponent = false; |
| 90 | bool numHasSign = false; |
| 91 | |
| 92 | for (; sc.More(); sc.Forward()) { |
| 93 | if (sc.atLineStart) |
| 94 | lineCurrent = styler.GetLine(sc.currentPos); |
| 95 | |
| 96 | // Determine if the current state should terminate. |
| 97 | if (sc.state == SCE_PS_COMMENT || sc.state == SCE_PS_DSC_VALUE) { |
| 98 | if (sc.atLineEnd) { |
| 99 | sc.SetState(SCE_C_DEFAULT); |
| 100 | } |
| 101 | } else if (sc.state == SCE_PS_DSC_COMMENT) { |
| 102 | if (sc.ch == ':') { |
| 103 | sc.Forward(); |
| 104 | if (!sc.atLineEnd) |
| 105 | sc.SetState(SCE_PS_DSC_VALUE); |
| 106 | else |
| 107 | sc.SetState(SCE_C_DEFAULT); |
| 108 | } else if (sc.atLineEnd) { |
| 109 | sc.SetState(SCE_C_DEFAULT); |
| 110 | } else if (IsAWhitespaceChar(sc.ch) && sc.ch != '\r') { |
| 111 | sc.ChangeState(SCE_PS_COMMENT); |
| 112 | } |
| 113 | } else if (sc.state == SCE_PS_NUMBER) { |
| 114 | if (IsASelfDelimitingChar(sc.ch) || IsAWhitespaceChar(sc.ch)) { |
| 115 | if ((sc.chPrev == '+' || sc.chPrev == '-' || |
| 116 | sc.chPrev == 'E' || sc.chPrev == 'e') && numRadix == 0) |
| 117 | sc.ChangeState(SCE_PS_NAME); |
| 118 | sc.SetState(SCE_C_DEFAULT); |
| 119 | } else if (sc.ch == '#') { |
| 120 | if (numHasPoint || numHasExponent || numHasSign || numRadix != 0) { |
| 121 | sc.ChangeState(SCE_PS_NAME); |
| 122 | } else { |
| 123 | char szradix[5]; |
| 124 | sc.GetCurrent(szradix, 4); |
| 125 | numRadix = atoi(szradix); |
| 126 | if (numRadix < 2 || numRadix > 36) |
| 127 | sc.ChangeState(SCE_PS_NAME); |
| 128 | } |
| 129 | } else if ((sc.ch == 'E' || sc.ch == 'e') && numRadix == 0) { |
| 130 | if (numHasExponent) { |
| 131 | sc.ChangeState(SCE_PS_NAME); |
| 132 | } else { |
| 133 | numHasExponent = true; |
| 134 | if (sc.chNext == '+' || sc.chNext == '-') |
| 135 | sc.Forward(); |
| 136 | } |
| 137 | } else if (sc.ch == '.') { |
| 138 | if (numHasPoint || numHasExponent || numRadix != 0) { |
| 139 | sc.ChangeState(SCE_PS_NAME); |
| 140 | } else { |
| 141 | numHasPoint = true; |
| 142 | } |
| 143 | } else if (numRadix == 0) { |
| 144 | if (!IsABaseNDigit(sc.ch, 10)) |
| 145 | sc.ChangeState(SCE_PS_NAME); |
| 146 | } else { |
| 147 | if (!IsABaseNDigit(sc.ch, numRadix)) |
| 148 | sc.ChangeState(SCE_PS_NAME); |
| 149 | } |
| 150 | } else if (sc.state == SCE_PS_NAME || sc.state == SCE_PS_KEYWORD) { |
| 151 | if (IsASelfDelimitingChar(sc.ch) || IsAWhitespaceChar(sc.ch)) { |
| 152 | char s[100]; |
| 153 | sc.GetCurrent(s, sizeof(s)); |
| 154 | if ((pslevel >= 1 && keywords1.InList(s)) || |
| 155 | (pslevel >= 2 && keywords2.InList(s)) || |
| 156 | (pslevel >= 3 && keywords3.InList(s)) || |
| 157 | keywords4.InList(s) || keywords5.InList(s)) { |
| 158 | sc.ChangeState(SCE_PS_KEYWORD); |
| 159 | } |
| 160 | sc.SetState(SCE_C_DEFAULT); |
| 161 | } |
| 162 | } else if (sc.state == SCE_PS_LITERAL || sc.state == SCE_PS_IMMEVAL) { |
| 163 | if (IsASelfDelimitingChar(sc.ch) || IsAWhitespaceChar(sc.ch)) |
| 164 | sc.SetState(SCE_C_DEFAULT); |
| 165 | } else if (sc.state == SCE_PS_PAREN_ARRAY || sc.state == SCE_PS_PAREN_DICT || |
| 166 | sc.state == SCE_PS_PAREN_PROC) { |
| 167 | sc.SetState(SCE_C_DEFAULT); |
| 168 | } else if (sc.state == SCE_PS_TEXT) { |
| 169 | if (sc.ch == '(') { |
| 170 | nestTextCurrent++; |
| 171 | } else if (sc.ch == ')') { |
| 172 | if (--nestTextCurrent == 0) |
| 173 | sc.ForwardSetState(SCE_PS_DEFAULT); |
| 174 | } else if (sc.ch == '\\') { |
| 175 | sc.Forward(); |
| 176 | } |
| 177 | } else if (sc.state == SCE_PS_HEXSTRING) { |
| 178 | if (sc.ch == '>') { |
| 179 | sc.ForwardSetState(SCE_PS_DEFAULT); |
| 180 | } else if (!IsABaseNDigit(sc.ch, 16) && !IsAWhitespaceChar(sc.ch)) { |
| 181 | sc.SetState(SCE_PS_HEXSTRING); |
| 182 | styler.ColourTo(sc.currentPos, SCE_PS_BADSTRINGCHAR); |
| 183 | } |
| 184 | } else if (sc.state == SCE_PS_BASE85STRING) { |
| 185 | if (sc.Match('~', '>')) { |
| 186 | sc.Forward(); |
| 187 | sc.ForwardSetState(SCE_PS_DEFAULT); |
| 188 | } else if (!IsABase85Char(sc.ch) && !IsAWhitespaceChar(sc.ch)) { |
| 189 | sc.SetState(SCE_PS_BASE85STRING); |
| 190 | styler.ColourTo(sc.currentPos, SCE_PS_BADSTRINGCHAR); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // Determine if a new state should be entered. |
| 195 | if (sc.state == SCE_C_DEFAULT) { |
| 196 | |
| 197 | if (sc.ch == '[' || sc.ch == ']') { |
| 198 | sc.SetState(SCE_PS_PAREN_ARRAY); |
| 199 | } else if (sc.ch == '{' || sc.ch == '}') { |
| 200 | sc.SetState(SCE_PS_PAREN_PROC); |
| 201 | } else if (sc.ch == '/') { |
| 202 | if (sc.chNext == '/') { |
| 203 | sc.SetState(SCE_PS_IMMEVAL); |
| 204 | sc.Forward(); |
| 205 | } else { |
| 206 | sc.SetState(SCE_PS_LITERAL); |
| 207 | } |
| 208 | } else if (sc.ch == '<') { |
| 209 | if (sc.chNext == '<') { |
| 210 | sc.SetState(SCE_PS_PAREN_DICT); |
| 211 | sc.Forward(); |
| 212 | } else if (sc.chNext == '~') { |
| 213 | sc.SetState(SCE_PS_BASE85STRING); |
| 214 | sc.Forward(); |
| 215 | } else { |
| 216 | sc.SetState(SCE_PS_HEXSTRING); |
| 217 | } |
| 218 | } else if (sc.ch == '>' && sc.chNext == '>') { |
| 219 | sc.SetState(SCE_PS_PAREN_DICT); |
| 220 | sc.Forward(); |
| 221 | } else if (sc.ch == '>' || sc.ch == ')') { |
| 222 | sc.SetState(SCE_C_DEFAULT); |
| 223 | styler.ColourTo(sc.currentPos, SCE_PS_BADSTRINGCHAR); |
| 224 | } else if (sc.ch == '(') { |
| 225 | sc.SetState(SCE_PS_TEXT); |
| 226 | nestTextCurrent = 1; |
| 227 | } else if (sc.ch == '%') { |
| 228 | if (sc.chNext == '%' && sc.atLineStart) { |
| 229 | sc.SetState(SCE_PS_DSC_COMMENT); |
| 230 | sc.Forward(); |
| 231 | if (sc.chNext == '+') { |
| 232 | sc.Forward(); |
| 233 | sc.ForwardSetState(SCE_PS_DSC_VALUE); |
| 234 | } |
| 235 | } else { |
| 236 | sc.SetState(SCE_PS_COMMENT); |
| 237 | } |
| 238 | } else if ((sc.ch == '+' || sc.ch == '-' || sc.ch == '.') && |
| 239 | IsABaseNDigit(sc.chNext, 10)) { |
| 240 | sc.SetState(SCE_PS_NUMBER); |
| 241 | numRadix = 0; |
| 242 | numHasPoint = (sc.ch == '.'); |
| 243 | numHasExponent = false; |
| 244 | numHasSign = (sc.ch == '+' || sc.ch == '-'); |
| 245 | } else if ((sc.ch == '+' || sc.ch == '-') && sc.chNext == '.' && |
| 246 | IsABaseNDigit(sc.GetRelative(2), 10)) { |
| 247 | sc.SetState(SCE_PS_NUMBER); |
| 248 | numRadix = 0; |
| 249 | numHasPoint = false; |
| 250 | numHasExponent = false; |
| 251 | numHasSign = true; |
| 252 | } else if (IsABaseNDigit(sc.ch, 10)) { |
| 253 | sc.SetState(SCE_PS_NUMBER); |
| 254 | numRadix = 0; |
| 255 | numHasPoint = false; |
| 256 | numHasExponent = false; |
| 257 | numHasSign = false; |
| 258 | } else if (!IsAWhitespaceChar(sc.ch)) { |
| 259 | sc.SetState(SCE_PS_NAME); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | if (sc.atLineEnd) |
| 264 | styler.SetLineState(lineCurrent, nestTextCurrent); |
| 265 | } |
| 266 | |
| 267 | sc.Complete(); |
| 268 | } |
| 269 | |
| 270 | static void FoldPSDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], |
| 271 | Accessor &styler) { |
| 272 | bool foldCompact = styler.GetPropertyInt("fold.compact" , 1) != 0; |
| 273 | bool foldAtElse = styler.GetPropertyInt("fold.at.else" , 0) != 0; |
| 274 | Sci_PositionU endPos = startPos + length; |
| 275 | int visibleChars = 0; |
| 276 | Sci_Position lineCurrent = styler.GetLine(startPos); |
| 277 | int levelCurrent = SC_FOLDLEVELBASE; |
| 278 | if (lineCurrent > 0) |
| 279 | levelCurrent = styler.LevelAt(lineCurrent-1) >> 16; |
| 280 | int levelMinCurrent = levelCurrent; |
| 281 | int levelNext = levelCurrent; |
| 282 | char chNext = styler[startPos]; |
| 283 | int styleNext = styler.StyleAt(startPos); |
| 284 | for (Sci_PositionU i = startPos; i < endPos; i++) { |
| 285 | char ch = chNext; |
| 286 | chNext = styler.SafeGetCharAt(i + 1); |
| 287 | int style = styleNext; |
| 288 | styleNext = styler.StyleAt(i + 1); |
| 289 | bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n'); //mac?? |
| 290 | if ((style & 31) == SCE_PS_PAREN_PROC) { |
| 291 | if (ch == '{') { |
| 292 | // Measure the minimum before a '{' to allow |
| 293 | // folding on "} {" |
| 294 | if (levelMinCurrent > levelNext) { |
| 295 | levelMinCurrent = levelNext; |
| 296 | } |
| 297 | levelNext++; |
| 298 | } else if (ch == '}') { |
| 299 | levelNext--; |
| 300 | } |
| 301 | } |
| 302 | if (atEOL) { |
| 303 | int levelUse = levelCurrent; |
| 304 | if (foldAtElse) { |
| 305 | levelUse = levelMinCurrent; |
| 306 | } |
| 307 | int lev = levelUse | levelNext << 16; |
| 308 | if (visibleChars == 0 && foldCompact) |
| 309 | lev |= SC_FOLDLEVELWHITEFLAG; |
| 310 | if (levelUse < levelNext) |
| 311 | lev |= SC_FOLDLEVELHEADERFLAG; |
| 312 | if (lev != styler.LevelAt(lineCurrent)) { |
| 313 | styler.SetLevel(lineCurrent, lev); |
| 314 | } |
| 315 | lineCurrent++; |
| 316 | levelCurrent = levelNext; |
| 317 | levelMinCurrent = levelCurrent; |
| 318 | visibleChars = 0; |
| 319 | } |
| 320 | if (!isspacechar(ch)) |
| 321 | visibleChars++; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | static const char * const psWordListDesc[] = { |
| 326 | "PS Level 1 operators" , |
| 327 | "PS Level 2 operators" , |
| 328 | "PS Level 3 operators" , |
| 329 | "RIP-specific operators" , |
| 330 | "User-defined operators" , |
| 331 | 0 |
| 332 | }; |
| 333 | |
| 334 | LexerModule lmPS(SCLEX_PS, ColourisePSDoc, "ps" , FoldPSDoc, psWordListDesc); |
| 335 | |