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