| 1 |
// Scintilla source code edit control |
| 2 |
/** @file LexPowerShell.cxx |
| 3 |
** Lexer for PowerShell scripts. |
| 4 |
**/ |
| 5 |
// Copyright 2008 by Tim Gerundt <[email protected]> |
| 6 |
// The License.txt file describes the conditions under which this software may be distributed. |
| 7 |
|
| 8 |
#include <stdlib.h> |
| 9 |
#include <string.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdarg.h> |
| 12 |
#include <assert.h> |
| 13 |
#include <ctype.h> |
| 14 |
|
| 15 |
#include <string> |
| 16 |
#include <string_view> |
| 17 |
|
| 18 |
#include "ILexer.h" |
| 19 |
#include "Scintilla.h" |
| 20 |
#include "SciLexer.h" |
| 21 |
|
| 22 |
#include "WordList.h" |
| 23 |
#include "LexAccessor.h" |
| 24 |
#include "Accessor.h" |
| 25 |
#include "StyleContext.h" |
| 26 |
#include "CharacterSet.h" |
| 27 |
#include "LexerModule.h" |
| 28 |
|
| 29 |
using namespace Lexilla; |
| 30 |
|
| 31 |
// Extended to accept accented characters |
| 32 |
static inline bool IsAWordChar(int ch) noexcept { |
| 33 |
return ch >= 0x80 || isalnum(ch) || ch == '-' || ch == '_'; |
| 34 |
} |
| 35 |
|
| 36 |
static bool IsNumericLiteral(int chPrev, int ch, int chNext) { |
| 37 |
// integers |
| 38 |
if (ch >= '0' && ch <= '9') { |
| 39 |
return true; |
| 40 |
} |
| 41 |
// hex 0x or a-f |
| 42 |
if ((ch == 'x' && chPrev == '0') || (ch >= 'a' && ch <= 'f')) { |
| 43 |
return true; |
| 44 |
} |
| 45 |
// decimal point |
| 46 |
if (ch == '.' && chNext != '.') { |
| 47 |
return true; |
| 48 |
} |
| 49 |
// optional -/+ sign after exponent |
| 50 |
if ((ch == '+' || ch == '-') && chPrev == 'e') { |
| 51 |
return true; |
| 52 |
} |
| 53 |
// suffix |
| 54 |
switch (ch) { |
| 55 |
//case 'b': see hex |
| 56 |
case 'g': |
| 57 |
case 'k': |
| 58 |
case 'l': |
| 59 |
case 'm': |
| 60 |
case 'n': |
| 61 |
case 'p': |
| 62 |
case 's': |
| 63 |
case 't': |
| 64 |
case 'u': |
| 65 |
case 'y': |
| 66 |
return true; |
| 67 |
} |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
static void ColourisePowerShellDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, |
| 72 |
WordList *keywordlists[], Accessor &styler) { |
| 73 |
|
| 74 |
const WordList &keywords = *keywordlists[0]; |
| 75 |
const WordList &keywords2 = *keywordlists[1]; |
| 76 |
const WordList &keywords3 = *keywordlists[2]; |
| 77 |
const WordList &keywords4 = *keywordlists[3]; |
| 78 |
const WordList &keywords5 = *keywordlists[4]; |
| 79 |
const WordList &keywords6 = *keywordlists[5]; |
| 80 |
|
| 81 |
styler.StartAt(startPos); |
| 82 |
|
| 83 |
StyleContext sc(startPos, length, initStyle, styler); |
| 84 |
|
| 85 |
for (; sc.More(); sc.Forward()) { |
| 86 |
|
| 87 |
if (sc.state == SCE_POWERSHELL_COMMENT) { |
| 88 |
if (sc.MatchLineEnd()) { |
| 89 |
sc.SetState(SCE_POWERSHELL_DEFAULT); |
| 90 |
} |
| 91 |
} else if (sc.state == SCE_POWERSHELL_COMMENTSTREAM) { |
| 92 |
if (sc.atLineStart) { |
| 93 |
while (IsASpaceOrTab(sc.ch)) { |
| 94 |
sc.Forward(); |
| 95 |
} |
| 96 |
if (sc.ch == '.' && IsAWordChar(sc.chNext)) { |
| 97 |
sc.SetState(SCE_POWERSHELL_COMMENTDOCKEYWORD); |
| 98 |
} |
| 99 |
} |
| 100 |
if (sc.ch == '>' && sc.chPrev == '#') { |
| 101 |
sc.ForwardSetState(SCE_POWERSHELL_DEFAULT); |
| 102 |
} |
| 103 |
} else if (sc.state == SCE_POWERSHELL_COMMENTDOCKEYWORD) { |
| 104 |
if (!IsAWordChar(sc.ch)) { |
| 105 |
char s[100]; |
| 106 |
sc.GetCurrentLowered(s, sizeof(s)); |
| 107 |
if (!keywords6.InList(s + 1)) { |
| 108 |
sc.ChangeState(SCE_POWERSHELL_COMMENTSTREAM); |
| 109 |
} |
| 110 |
sc.SetState(SCE_POWERSHELL_COMMENTSTREAM); |
| 111 |
} |
| 112 |
} else if (sc.state == SCE_POWERSHELL_STRING) { |
| 113 |
// This is a doubles quotes string |
| 114 |
if (sc.ch == '\"') { |
| 115 |
sc.ForwardSetState(SCE_POWERSHELL_DEFAULT); |
| 116 |
} else if (sc.ch == '`') { |
| 117 |
sc.Forward(); // skip next escaped character |
| 118 |
} |
| 119 |
} else if (sc.state == SCE_POWERSHELL_CHARACTER) { |
| 120 |
// This is a single quote string |
| 121 |
if (sc.ch == '\'') { |
| 122 |
sc.ForwardSetState(SCE_POWERSHELL_DEFAULT); |
| 123 |
} |
| 124 |
} else if (sc.state == SCE_POWERSHELL_HERE_STRING) { |
| 125 |
// This is a doubles quotes here-string |
| 126 |
if (sc.atLineStart && sc.ch == '\"' && sc.chNext == '@') { |
| 127 |
sc.Forward(2); |
| 128 |
sc.SetState(SCE_POWERSHELL_DEFAULT); |
| 129 |
} |
| 130 |
} else if (sc.state == SCE_POWERSHELL_HERE_CHARACTER) { |
| 131 |
// This is a single quote here-string |
| 132 |
if (sc.atLineStart && sc.ch == '\'' && sc.chNext == '@') { |
| 133 |
sc.Forward(2); |
| 134 |
sc.SetState(SCE_POWERSHELL_DEFAULT); |
| 135 |
} |
| 136 |
} else if (sc.state == SCE_POWERSHELL_NUMBER) { |
| 137 |
if (!IsNumericLiteral(MakeLowerCase(sc.chPrev), |
| 138 |
MakeLowerCase(sc.ch), |
| 139 |
MakeLowerCase(sc.chNext))) { |
| 140 |
if (sc.MatchLineEnd() || IsASpaceOrTab(sc.ch) || isoperator(sc.ch)) { |
| 141 |
sc.SetState(SCE_POWERSHELL_DEFAULT); |
| 142 |
} else { |
| 143 |
sc.ChangeState(SCE_POWERSHELL_IDENTIFIER); |
| 144 |
} |
| 145 |
} |
| 146 |
} else if (sc.state == SCE_POWERSHELL_VARIABLE) { |
| 147 |
if (!IsAWordChar(sc.ch)) { |
| 148 |
sc.SetState(SCE_POWERSHELL_DEFAULT); |
| 149 |
} |
| 150 |
} else if (sc.state == SCE_POWERSHELL_OPERATOR) { |
| 151 |
if (!isoperator(sc.ch)) { |
| 152 |
sc.SetState(SCE_POWERSHELL_DEFAULT); |
| 153 |
} |
| 154 |
} else if (sc.state == SCE_POWERSHELL_IDENTIFIER) { |
| 155 |
if (!IsAWordChar(sc.ch)) { |
| 156 |
char s[100]; |
| 157 |
sc.GetCurrentLowered(s, sizeof(s)); |
| 158 |
|
| 159 |
if (keywords.InList(s)) { |
| 160 |
sc.ChangeState(SCE_POWERSHELL_KEYWORD); |
| 161 |
} else if (keywords2.InList(s)) { |
| 162 |
sc.ChangeState(SCE_POWERSHELL_CMDLET); |
| 163 |
} else if (keywords3.InList(s)) { |
| 164 |
sc.ChangeState(SCE_POWERSHELL_ALIAS); |
| 165 |
} else if (keywords4.InList(s)) { |
| 166 |
sc.ChangeState(SCE_POWERSHELL_FUNCTION); |
| 167 |
} else if (keywords5.InList(s)) { |
| 168 |
sc.ChangeState(SCE_POWERSHELL_USER1); |
| 169 |
} |
| 170 |
sc.SetState(SCE_POWERSHELL_DEFAULT); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
// Determine if a new state should be entered. |
| 175 |
if (sc.state == SCE_POWERSHELL_DEFAULT) { |
| 176 |
if (sc.ch == '#') { |
| 177 |
sc.SetState(SCE_POWERSHELL_COMMENT); |
| 178 |
} else if (sc.ch == '<' && sc.chNext == '#') { |
| 179 |
sc.SetState(SCE_POWERSHELL_COMMENTSTREAM); |
| 180 |
} else if (sc.ch == '\"') { |
| 181 |
sc.SetState(SCE_POWERSHELL_STRING); |
| 182 |
} else if (sc.ch == '\'') { |
| 183 |
sc.SetState(SCE_POWERSHELL_CHARACTER); |
| 184 |
} else if (sc.ch == '@' && sc.chNext == '\"') { |
| 185 |
sc.SetState(SCE_POWERSHELL_HERE_STRING); |
| 186 |
} else if (sc.ch == '@' && sc.chNext == '\'') { |
| 187 |
sc.SetState(SCE_POWERSHELL_HERE_CHARACTER); |
| 188 |
} else if (sc.ch == '$') { |
| 189 |
sc.SetState(SCE_POWERSHELL_VARIABLE); |
| 190 |
} else if (IsADigit(sc.ch) || (sc.chPrev != '.' && sc.ch == '.' && IsADigit(sc.chNext))) { |
| 191 |
sc.SetState(SCE_POWERSHELL_NUMBER); |
| 192 |
} else if (isoperator(sc.ch)) { |
| 193 |
sc.SetState(SCE_POWERSHELL_OPERATOR); |
| 194 |
} else if (IsAWordChar(sc.ch)) { |
| 195 |
sc.SetState(SCE_POWERSHELL_IDENTIFIER); |
| 196 |
} else if (sc.ch == '`') { |
| 197 |
sc.Forward(); // skip next escaped character |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
sc.Complete(); |
| 202 |
} |
| 203 |
|
| 204 |
// Store both the current line's fold level and the next lines in the |
| 205 |
// level store to make it easy to pick up with each increment |
| 206 |
// and to make it possible to fiddle the current level for "} else {". |
| 207 |
static void FoldPowerShellDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, |
| 208 |
WordList *[], Accessor &styler) { |
| 209 |
const bool foldComment = styler.GetPropertyInt("fold.comment") != 0; |
| 210 |
const bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0; |
| 211 |
const bool foldAtElse = styler.GetPropertyInt("fold.at.else", 0) != 0; |
| 212 |
const Sci_PositionU endPos = startPos + length; |
| 213 |
int visibleChars = 0; |
| 214 |
Sci_Position lineCurrent = styler.GetLine(startPos); |
| 215 |
int levelCurrent = SC_FOLDLEVELBASE; |
| 216 |
if (lineCurrent > 0) |
| 217 |
levelCurrent = styler.LevelAt(lineCurrent-1) >> 16; |
| 218 |
int levelMinCurrent = levelCurrent; |
| 219 |
int levelNext = levelCurrent; |
| 220 |
char chNext = styler[startPos]; |
| 221 |
int styleNext = styler.StyleAt(startPos); |
| 222 |
int style = initStyle; |
| 223 |
for (Sci_PositionU i = startPos; i < endPos; i++) { |
| 224 |
const char ch = chNext; |
| 225 |
chNext = styler.SafeGetCharAt(i + 1); |
| 226 |
const int stylePrev = style; |
| 227 |
style = styleNext; |
| 228 |
styleNext = styler.StyleAt(i + 1); |
| 229 |
const bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n'); |
| 230 |
if (style == SCE_POWERSHELL_OPERATOR) { |
| 231 |
if (ch == '{') { |
| 232 |
// Measure the minimum before a '{' to allow |
| 233 |
// folding on "} else {" |
| 234 |
if (levelMinCurrent > levelNext) { |
| 235 |
levelMinCurrent = levelNext; |
| 236 |
} |
| 237 |
levelNext++; |
| 238 |
} else if (ch == '}') { |
| 239 |
levelNext--; |
| 240 |
} |
| 241 |
} else if (foldComment && style == SCE_POWERSHELL_COMMENTSTREAM) { |
| 242 |
if (stylePrev != SCE_POWERSHELL_COMMENTSTREAM && stylePrev != SCE_POWERSHELL_COMMENTDOCKEYWORD) { |
| 243 |
levelNext++; |
| 244 |
} else if (styleNext != SCE_POWERSHELL_COMMENTSTREAM && styleNext != SCE_POWERSHELL_COMMENTDOCKEYWORD) { |
| 245 |
levelNext--; |
| 246 |
} |
| 247 |
} else if (foldComment && style == SCE_POWERSHELL_COMMENT) { |
| 248 |
if (ch == '#') { |
| 249 |
Sci_PositionU j = i + 1; |
| 250 |
while ((j < endPos) && IsASpaceOrTab(styler.SafeGetCharAt(j))) { |
| 251 |
j++; |
| 252 |
} |
| 253 |
if (styler.Match(j, "region")) { |
| 254 |
levelNext++; |
| 255 |
} else if (styler.Match(j, "endregion")) { |
| 256 |
levelNext--; |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
if (!IsASpace(ch)) |
| 261 |
visibleChars++; |
| 262 |
if (atEOL || (i == endPos-1)) { |
| 263 |
int levelUse = levelCurrent; |
| 264 |
if (foldAtElse) { |
| 265 |
levelUse = levelMinCurrent; |
| 266 |
} |
| 267 |
int lev = levelUse | levelNext << 16; |
| 268 |
if (visibleChars == 0 && foldCompact) |
| 269 |
lev |= SC_FOLDLEVELWHITEFLAG; |
| 270 |
if (levelUse < levelNext) |
| 271 |
lev |= SC_FOLDLEVELHEADERFLAG; |
| 272 |
if (lev != styler.LevelAt(lineCurrent)) { |
| 273 |
styler.SetLevel(lineCurrent, lev); |
| 274 |
} |
| 275 |
lineCurrent++; |
| 276 |
levelCurrent = levelNext; |
| 277 |
levelMinCurrent = levelCurrent; |
| 278 |
visibleChars = 0; |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
static const char *const powershellWordLists[] = { |
| 284 |
"Commands", |
| 285 |
"Cmdlets", |
| 286 |
"Aliases", |
| 287 |
"Functions", |
| 288 |
"User1", |
| 289 |
"DocComment", |
| 290 |
0 |
| 291 |
}; |
| 292 |
|
| 293 |
extern const LexerModule lmPowerShell(SCLEX_POWERSHELL, ColourisePowerShellDoc, "powershell", FoldPowerShellDoc, powershellWordLists); |
| 294 |
|