1 // Scintilla source code edit control
2 /** @file LexPowerShell.cxx
3 ** Lexer for PowerShell scripts.
5 // Copyright 2008 by Tim Gerundt <tim@gerundt.de>
6 // The License.txt file describes the conditions under which this software may be distributed.
16 #include "Scintilla.h"
20 #include "LexAccessor.h"
22 #include "StyleContext.h"
23 #include "CharacterSet.h"
24 #include "LexerModule.h"
27 using namespace Scintilla
;
30 // Extended to accept accented characters
31 static inline bool IsAWordChar(int ch
) {
32 return ch
>= 0x80 || isalnum(ch
) || ch
== '-' || ch
== '_';
35 static void ColourisePowerShellDoc(Sci_PositionU startPos
, Sci_Position length
, int initStyle
,
36 WordList
*keywordlists
[], Accessor
&styler
) {
38 WordList
&keywords
= *keywordlists
[0];
39 WordList
&keywords2
= *keywordlists
[1];
40 WordList
&keywords3
= *keywordlists
[2];
41 WordList
&keywords4
= *keywordlists
[3];
42 WordList
&keywords5
= *keywordlists
[4];
43 WordList
&keywords6
= *keywordlists
[5];
45 styler
.StartAt(startPos
);
47 StyleContext
sc(startPos
, length
, initStyle
, styler
);
49 for (; sc
.More(); sc
.Forward()) {
51 if (sc
.state
== SCE_POWERSHELL_COMMENT
) {
53 sc
.SetState(SCE_POWERSHELL_DEFAULT
);
55 } else if (sc
.state
== SCE_POWERSHELL_COMMENTSTREAM
) {
57 while(IsASpaceOrTab(sc
.ch
)) {
60 if (sc
.ch
== '.' && IsAWordChar(sc
.chNext
)) {
61 sc
.SetState(SCE_POWERSHELL_COMMENTDOCKEYWORD
);
64 if (sc
.ch
== '>' && sc
.chPrev
== '#') {
65 sc
.ForwardSetState(SCE_POWERSHELL_DEFAULT
);
67 } else if (sc
.state
== SCE_POWERSHELL_COMMENTDOCKEYWORD
) {
68 if(!IsAWordChar(sc
.ch
)) {
70 sc
.GetCurrentLowered(s
, sizeof(s
));
71 if (!keywords6
.InList(s
+ 1)) {
72 sc
.ChangeState(SCE_POWERSHELL_COMMENTSTREAM
);
74 sc
.SetState(SCE_POWERSHELL_COMMENTSTREAM
);
76 } else if (sc
.state
== SCE_POWERSHELL_STRING
) {
77 // This is a doubles quotes string
79 sc
.ForwardSetState(SCE_POWERSHELL_DEFAULT
);
81 } else if (sc
.state
== SCE_POWERSHELL_CHARACTER
) {
82 // This is a single quote string
84 sc
.ForwardSetState(SCE_POWERSHELL_DEFAULT
);
86 } else if (sc
.state
== SCE_POWERSHELL_HERE_STRING
) {
87 // This is a doubles quotes here-string
88 if (sc
.atLineStart
&& sc
.ch
== '\"' && sc
.chNext
== '@') {
90 sc
.SetState(SCE_POWERSHELL_DEFAULT
);
92 } else if (sc
.state
== SCE_POWERSHELL_HERE_CHARACTER
) {
93 // This is a single quote here-string
94 if (sc
.atLineStart
&& sc
.ch
== '\'' && sc
.chNext
== '@') {
96 sc
.SetState(SCE_POWERSHELL_DEFAULT
);
98 } else if (sc
.state
== SCE_POWERSHELL_NUMBER
) {
99 if (!IsADigit(sc
.ch
)) {
100 sc
.SetState(SCE_POWERSHELL_DEFAULT
);
102 } else if (sc
.state
== SCE_POWERSHELL_VARIABLE
) {
103 if (!IsAWordChar(sc
.ch
)) {
104 sc
.SetState(SCE_POWERSHELL_DEFAULT
);
106 } else if (sc
.state
== SCE_POWERSHELL_OPERATOR
) {
107 if (!isoperator(static_cast<char>(sc
.ch
))) {
108 sc
.SetState(SCE_POWERSHELL_DEFAULT
);
110 } else if (sc
.state
== SCE_POWERSHELL_IDENTIFIER
) {
111 if (!IsAWordChar(sc
.ch
)) {
113 sc
.GetCurrentLowered(s
, sizeof(s
));
115 if (keywords
.InList(s
)) {
116 sc
.ChangeState(SCE_POWERSHELL_KEYWORD
);
117 } else if (keywords2
.InList(s
)) {
118 sc
.ChangeState(SCE_POWERSHELL_CMDLET
);
119 } else if (keywords3
.InList(s
)) {
120 sc
.ChangeState(SCE_POWERSHELL_ALIAS
);
121 } else if (keywords4
.InList(s
)) {
122 sc
.ChangeState(SCE_POWERSHELL_FUNCTION
);
123 } else if (keywords5
.InList(s
)) {
124 sc
.ChangeState(SCE_POWERSHELL_USER1
);
126 sc
.SetState(SCE_POWERSHELL_DEFAULT
);
130 // Determine if a new state should be entered.
131 if (sc
.state
== SCE_POWERSHELL_DEFAULT
) {
133 sc
.SetState(SCE_POWERSHELL_COMMENT
);
134 } else if (sc
.ch
== '<' && sc
.chNext
== '#') {
135 sc
.SetState(SCE_POWERSHELL_COMMENTSTREAM
);
136 } else if (sc
.ch
== '\"') {
137 sc
.SetState(SCE_POWERSHELL_STRING
);
138 } else if (sc
.ch
== '\'') {
139 sc
.SetState(SCE_POWERSHELL_CHARACTER
);
140 } else if (sc
.ch
== '@' && sc
.chNext
== '\"') {
141 sc
.SetState(SCE_POWERSHELL_HERE_STRING
);
142 } else if (sc
.ch
== '@' && sc
.chNext
== '\'') {
143 sc
.SetState(SCE_POWERSHELL_HERE_CHARACTER
);
144 } else if (sc
.ch
== '$') {
145 sc
.SetState(SCE_POWERSHELL_VARIABLE
);
146 } else if (IsADigit(sc
.ch
) || (sc
.ch
== '.' && IsADigit(sc
.chNext
))) {
147 sc
.SetState(SCE_POWERSHELL_NUMBER
);
148 } else if (isoperator(static_cast<char>(sc
.ch
))) {
149 sc
.SetState(SCE_POWERSHELL_OPERATOR
);
150 } else if (IsAWordChar(sc
.ch
)) {
151 sc
.SetState(SCE_POWERSHELL_IDENTIFIER
);
158 // Store both the current line's fold level and the next lines in the
159 // level store to make it easy to pick up with each increment
160 // and to make it possible to fiddle the current level for "} else {".
161 static void FoldPowerShellDoc(Sci_PositionU startPos
, Sci_Position length
, int initStyle
,
162 WordList
*[], Accessor
&styler
) {
163 bool foldComment
= styler
.GetPropertyInt("fold.comment") != 0;
164 bool foldCompact
= styler
.GetPropertyInt("fold.compact", 1) != 0;
165 bool foldAtElse
= styler
.GetPropertyInt("fold.at.else", 0) != 0;
166 Sci_PositionU endPos
= startPos
+ length
;
167 int visibleChars
= 0;
168 Sci_Position lineCurrent
= styler
.GetLine(startPos
);
169 int levelCurrent
= SC_FOLDLEVELBASE
;
171 levelCurrent
= styler
.LevelAt(lineCurrent
-1) >> 16;
172 int levelMinCurrent
= levelCurrent
;
173 int levelNext
= levelCurrent
;
174 char chNext
= styler
[startPos
];
175 int styleNext
= styler
.StyleAt(startPos
);
176 int style
= initStyle
;
177 for (Sci_PositionU i
= startPos
; i
< endPos
; i
++) {
179 chNext
= styler
.SafeGetCharAt(i
+ 1);
180 int stylePrev
= style
;
182 styleNext
= styler
.StyleAt(i
+ 1);
183 bool atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
184 if (style
== SCE_POWERSHELL_OPERATOR
) {
186 // Measure the minimum before a '{' to allow
187 // folding on "} else {"
188 if (levelMinCurrent
> levelNext
) {
189 levelMinCurrent
= levelNext
;
192 } else if (ch
== '}') {
195 } else if (foldComment
&& style
== SCE_POWERSHELL_COMMENTSTREAM
) {
196 if (stylePrev
!= SCE_POWERSHELL_COMMENTSTREAM
&& stylePrev
!= SCE_POWERSHELL_COMMENTDOCKEYWORD
) {
198 } else if (styleNext
!= SCE_POWERSHELL_COMMENTSTREAM
&& styleNext
!= SCE_POWERSHELL_COMMENTDOCKEYWORD
) {
201 } else if (foldComment
&& style
== SCE_POWERSHELL_COMMENT
) {
203 Sci_PositionU j
= i
+ 1;
204 while ((j
< endPos
) && IsASpaceOrTab(styler
.SafeGetCharAt(j
))) {
207 if (styler
.Match(j
, "region")) {
209 } else if (styler
.Match(j
, "endregion")) {
216 if (atEOL
|| (i
== endPos
-1)) {
217 int levelUse
= levelCurrent
;
219 levelUse
= levelMinCurrent
;
221 int lev
= levelUse
| levelNext
<< 16;
222 if (visibleChars
== 0 && foldCompact
)
223 lev
|= SC_FOLDLEVELWHITEFLAG
;
224 if (levelUse
< levelNext
)
225 lev
|= SC_FOLDLEVELHEADERFLAG
;
226 if (lev
!= styler
.LevelAt(lineCurrent
)) {
227 styler
.SetLevel(lineCurrent
, lev
);
230 levelCurrent
= levelNext
;
231 levelMinCurrent
= levelCurrent
;
237 static const char * const powershellWordLists
[] = {
247 LexerModule
lmPowerShell(SCLEX_POWERSHELL
, ColourisePowerShellDoc
, "powershell", FoldPowerShellDoc
, powershellWordLists
);