1// Scintilla source code edit control
2/** @file LexPowerShell.cxx
3 ** Lexer for PowerShell scripts.
4 **/
5// Copyright 2008 by Tim Gerundt <tim@gerundt.de>
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
29using namespace Lexilla;
30
31// Extended to accept accented characters
32static inline bool IsAWordChar(int ch) {
33 return ch >= 0x80 || isalnum(ch) || ch == '-' || ch == '_';
34}
35
36static void ColourisePowerShellDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
37 WordList *keywordlists[], Accessor &styler) {
38
39 WordList &keywords = *keywordlists[0];
40 WordList &keywords2 = *keywordlists[1];
41 WordList &keywords3 = *keywordlists[2];
42 WordList &keywords4 = *keywordlists[3];
43 WordList &keywords5 = *keywordlists[4];
44 WordList &keywords6 = *keywordlists[5];
45
46 styler.StartAt(startPos);
47
48 StyleContext sc(startPos, length, initStyle, styler);
49
50 for (; sc.More(); sc.Forward()) {
51
52 if (sc.state == SCE_POWERSHELL_COMMENT) {
53 if (sc.atLineEnd) {
54 sc.SetState(SCE_POWERSHELL_DEFAULT);
55 }
56 } else if (sc.state == SCE_POWERSHELL_COMMENTSTREAM) {
57 if (sc.atLineStart) {
58 while (IsASpaceOrTab(sc.ch)) {
59 sc.Forward();
60 }
61 if (sc.ch == '.' && IsAWordChar(sc.chNext)) {
62 sc.SetState(SCE_POWERSHELL_COMMENTDOCKEYWORD);
63 }
64 }
65 if (sc.ch == '>' && sc.chPrev == '#') {
66 sc.ForwardSetState(SCE_POWERSHELL_DEFAULT);
67 }
68 } else if (sc.state == SCE_POWERSHELL_COMMENTDOCKEYWORD) {
69 if (!IsAWordChar(sc.ch)) {
70 char s[100];
71 sc.GetCurrentLowered(s, sizeof(s));
72 if (!keywords6.InList(s + 1)) {
73 sc.ChangeState(SCE_POWERSHELL_COMMENTSTREAM);
74 }
75 sc.SetState(SCE_POWERSHELL_COMMENTSTREAM);
76 }
77 } else if (sc.state == SCE_POWERSHELL_STRING) {
78 // This is a doubles quotes string
79 if (sc.ch == '\"') {
80 sc.ForwardSetState(SCE_POWERSHELL_DEFAULT);
81 } else if (sc.ch == '`') {
82 sc.Forward(); // skip next escaped character
83 }
84 } else if (sc.state == SCE_POWERSHELL_CHARACTER) {
85 // This is a single quote string
86 if (sc.ch == '\'') {
87 sc.ForwardSetState(SCE_POWERSHELL_DEFAULT);
88 } else if (sc.ch == '`') {
89 sc.Forward(); // skip next escaped character
90 }
91 } else if (sc.state == SCE_POWERSHELL_HERE_STRING) {
92 // This is a doubles quotes here-string
93 if (sc.atLineStart && sc.ch == '\"' && sc.chNext == '@') {
94 sc.Forward(2);
95 sc.SetState(SCE_POWERSHELL_DEFAULT);
96 }
97 } else if (sc.state == SCE_POWERSHELL_HERE_CHARACTER) {
98 // This is a single quote here-string
99 if (sc.atLineStart && sc.ch == '\'' && sc.chNext == '@') {
100 sc.Forward(2);
101 sc.SetState(SCE_POWERSHELL_DEFAULT);
102 }
103 } else if (sc.state == SCE_POWERSHELL_NUMBER) {
104 if (!IsADigit(sc.ch)) {
105 sc.SetState(SCE_POWERSHELL_DEFAULT);
106 }
107 } else if (sc.state == SCE_POWERSHELL_VARIABLE) {
108 if (!IsAWordChar(sc.ch)) {
109 sc.SetState(SCE_POWERSHELL_DEFAULT);
110 }
111 } else if (sc.state == SCE_POWERSHELL_OPERATOR) {
112 if (!isoperator(static_cast<char>(sc.ch))) {
113 sc.SetState(SCE_POWERSHELL_DEFAULT);
114 }
115 } else if (sc.state == SCE_POWERSHELL_IDENTIFIER) {
116 if (!IsAWordChar(sc.ch)) {
117 char s[100];
118 sc.GetCurrentLowered(s, sizeof(s));
119
120 if (keywords.InList(s)) {
121 sc.ChangeState(SCE_POWERSHELL_KEYWORD);
122 } else if (keywords2.InList(s)) {
123 sc.ChangeState(SCE_POWERSHELL_CMDLET);
124 } else if (keywords3.InList(s)) {
125 sc.ChangeState(SCE_POWERSHELL_ALIAS);
126 } else if (keywords4.InList(s)) {
127 sc.ChangeState(SCE_POWERSHELL_FUNCTION);
128 } else if (keywords5.InList(s)) {
129 sc.ChangeState(SCE_POWERSHELL_USER1);
130 }
131 sc.SetState(SCE_POWERSHELL_DEFAULT);
132 }
133 }
134
135 // Determine if a new state should be entered.
136 if (sc.state == SCE_POWERSHELL_DEFAULT) {
137 if (sc.ch == '#') {
138 sc.SetState(SCE_POWERSHELL_COMMENT);
139 } else if (sc.ch == '<' && sc.chNext == '#') {
140 sc.SetState(SCE_POWERSHELL_COMMENTSTREAM);
141 } else if (sc.ch == '\"') {
142 sc.SetState(SCE_POWERSHELL_STRING);
143 } else if (sc.ch == '\'') {
144 sc.SetState(SCE_POWERSHELL_CHARACTER);
145 } else if (sc.ch == '@' && sc.chNext == '\"') {
146 sc.SetState(SCE_POWERSHELL_HERE_STRING);
147 } else if (sc.ch == '@' && sc.chNext == '\'') {
148 sc.SetState(SCE_POWERSHELL_HERE_CHARACTER);
149 } else if (sc.ch == '$') {
150 sc.SetState(SCE_POWERSHELL_VARIABLE);
151 } else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
152 sc.SetState(SCE_POWERSHELL_NUMBER);
153 } else if (isoperator(static_cast<char>(sc.ch))) {
154 sc.SetState(SCE_POWERSHELL_OPERATOR);
155 } else if (IsAWordChar(sc.ch)) {
156 sc.SetState(SCE_POWERSHELL_IDENTIFIER);
157 } else if (sc.ch == '`') {
158 sc.Forward(); // skip next escaped character
159 }
160 }
161 }
162 sc.Complete();
163}
164
165// Store both the current line's fold level and the next lines in the
166// level store to make it easy to pick up with each increment
167// and to make it possible to fiddle the current level for "} else {".
168static void FoldPowerShellDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
169 WordList *[], Accessor &styler) {
170 bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
171 bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
172 bool foldAtElse = styler.GetPropertyInt("fold.at.else", 0) != 0;
173 Sci_PositionU endPos = startPos + length;
174 int visibleChars = 0;
175 Sci_Position lineCurrent = styler.GetLine(startPos);
176 int levelCurrent = SC_FOLDLEVELBASE;
177 if (lineCurrent > 0)
178 levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
179 int levelMinCurrent = levelCurrent;
180 int levelNext = levelCurrent;
181 char chNext = styler[startPos];
182 int styleNext = styler.StyleAt(startPos);
183 int style = initStyle;
184 for (Sci_PositionU i = startPos; i < endPos; i++) {
185 char ch = chNext;
186 chNext = styler.SafeGetCharAt(i + 1);
187 int stylePrev = style;
188 style = styleNext;
189 styleNext = styler.StyleAt(i + 1);
190 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
191 if (style == SCE_POWERSHELL_OPERATOR) {
192 if (ch == '{') {
193 // Measure the minimum before a '{' to allow
194 // folding on "} else {"
195 if (levelMinCurrent > levelNext) {
196 levelMinCurrent = levelNext;
197 }
198 levelNext++;
199 } else if (ch == '}') {
200 levelNext--;
201 }
202 } else if (foldComment && style == SCE_POWERSHELL_COMMENTSTREAM) {
203 if (stylePrev != SCE_POWERSHELL_COMMENTSTREAM && stylePrev != SCE_POWERSHELL_COMMENTDOCKEYWORD) {
204 levelNext++;
205 } else if (styleNext != SCE_POWERSHELL_COMMENTSTREAM && styleNext != SCE_POWERSHELL_COMMENTDOCKEYWORD) {
206 levelNext--;
207 }
208 } else if (foldComment && style == SCE_POWERSHELL_COMMENT) {
209 if (ch == '#') {
210 Sci_PositionU j = i + 1;
211 while ((j < endPos) && IsASpaceOrTab(styler.SafeGetCharAt(j))) {
212 j++;
213 }
214 if (styler.Match(j, "region")) {
215 levelNext++;
216 } else if (styler.Match(j, "endregion")) {
217 levelNext--;
218 }
219 }
220 }
221 if (!IsASpace(ch))
222 visibleChars++;
223 if (atEOL || (i == endPos-1)) {
224 int levelUse = levelCurrent;
225 if (foldAtElse) {
226 levelUse = levelMinCurrent;
227 }
228 int lev = levelUse | levelNext << 16;
229 if (visibleChars == 0 && foldCompact)
230 lev |= SC_FOLDLEVELWHITEFLAG;
231 if (levelUse < levelNext)
232 lev |= SC_FOLDLEVELHEADERFLAG;
233 if (lev != styler.LevelAt(lineCurrent)) {
234 styler.SetLevel(lineCurrent, lev);
235 }
236 lineCurrent++;
237 levelCurrent = levelNext;
238 levelMinCurrent = levelCurrent;
239 visibleChars = 0;
240 }
241 }
242}
243
244static const char *const powershellWordLists[] = {
245 "Commands",
246 "Cmdlets",
247 "Aliases",
248 "Functions",
249 "User1",
250 "DocComment",
251 0
252};
253
254LexerModule lmPowerShell(SCLEX_POWERSHELL, ColourisePowerShellDoc, "powershell", FoldPowerShellDoc, powershellWordLists);
255
256