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(unsigned int startPos
, int 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];
44 styler
.StartAt(startPos
);
46 StyleContext
sc(startPos
, length
, initStyle
, styler
);
48 for (; sc
.More(); sc
.Forward()) {
50 if (sc
.state
== SCE_POWERSHELL_COMMENT
) {
52 sc
.SetState(SCE_POWERSHELL_DEFAULT
);
54 } else if (sc
.state
== SCE_POWERSHELL_COMMENTSTREAM
) {
55 if (sc
.ch
== '>' && sc
.chPrev
== '#') {
56 sc
.ForwardSetState(SCE_POWERSHELL_DEFAULT
);
58 } else if (sc
.state
== SCE_POWERSHELL_STRING
) {
59 // This is a doubles quotes string
61 sc
.ForwardSetState(SCE_POWERSHELL_DEFAULT
);
63 } else if (sc
.state
== SCE_POWERSHELL_CHARACTER
) {
64 // This is a single quote string
66 sc
.ForwardSetState(SCE_POWERSHELL_DEFAULT
);
68 } else if (sc
.state
== SCE_POWERSHELL_NUMBER
) {
69 if (!IsADigit(sc
.ch
)) {
70 sc
.SetState(SCE_POWERSHELL_DEFAULT
);
72 } else if (sc
.state
== SCE_POWERSHELL_VARIABLE
) {
73 if (!IsAWordChar(sc
.ch
)) {
74 sc
.SetState(SCE_POWERSHELL_DEFAULT
);
76 } else if (sc
.state
== SCE_POWERSHELL_OPERATOR
) {
77 if (!isoperator(static_cast<char>(sc
.ch
))) {
78 sc
.SetState(SCE_POWERSHELL_DEFAULT
);
80 } else if (sc
.state
== SCE_POWERSHELL_IDENTIFIER
) {
81 if (!IsAWordChar(sc
.ch
)) {
83 sc
.GetCurrentLowered(s
, sizeof(s
));
85 if (keywords
.InList(s
)) {
86 sc
.ChangeState(SCE_POWERSHELL_KEYWORD
);
87 } else if (keywords2
.InList(s
)) {
88 sc
.ChangeState(SCE_POWERSHELL_CMDLET
);
89 } else if (keywords3
.InList(s
)) {
90 sc
.ChangeState(SCE_POWERSHELL_ALIAS
);
91 } else if (keywords4
.InList(s
)) {
92 sc
.ChangeState(SCE_POWERSHELL_FUNCTION
);
93 } else if (keywords5
.InList(s
)) {
94 sc
.ChangeState(SCE_POWERSHELL_USER1
);
96 sc
.SetState(SCE_POWERSHELL_DEFAULT
);
100 // Determine if a new state should be entered.
101 if (sc
.state
== SCE_POWERSHELL_DEFAULT
) {
103 sc
.SetState(SCE_POWERSHELL_COMMENT
);
104 } else if (sc
.ch
== '<' && sc
.chNext
== '#') {
105 sc
.SetState(SCE_POWERSHELL_COMMENTSTREAM
);
106 } else if (sc
.ch
== '\"') {
107 sc
.SetState(SCE_POWERSHELL_STRING
);
108 } else if (sc
.ch
== '\'') {
109 sc
.SetState(SCE_POWERSHELL_CHARACTER
);
110 } else if (sc
.ch
== '$') {
111 sc
.SetState(SCE_POWERSHELL_VARIABLE
);
112 } else if (IsADigit(sc
.ch
) || (sc
.ch
== '.' && IsADigit(sc
.chNext
))) {
113 sc
.SetState(SCE_POWERSHELL_NUMBER
);
114 } else if (isoperator(static_cast<char>(sc
.ch
))) {
115 sc
.SetState(SCE_POWERSHELL_OPERATOR
);
116 } else if (IsAWordChar(sc
.ch
)) {
117 sc
.SetState(SCE_POWERSHELL_IDENTIFIER
);
124 // Store both the current line's fold level and the next lines in the
125 // level store to make it easy to pick up with each increment
126 // and to make it possible to fiddle the current level for "} else {".
127 static void FoldPowerShellDoc(unsigned int startPos
, int length
, int initStyle
,
128 WordList
*[], Accessor
&styler
) {
129 bool foldComment
= styler
.GetPropertyInt("fold.comment") != 0;
130 bool foldCompact
= styler
.GetPropertyInt("fold.compact", 1) != 0;
131 bool foldAtElse
= styler
.GetPropertyInt("fold.at.else", 0) != 0;
132 unsigned int endPos
= startPos
+ length
;
133 int visibleChars
= 0;
134 int lineCurrent
= styler
.GetLine(startPos
);
135 int levelCurrent
= SC_FOLDLEVELBASE
;
137 levelCurrent
= styler
.LevelAt(lineCurrent
-1) >> 16;
138 int levelMinCurrent
= levelCurrent
;
139 int levelNext
= levelCurrent
;
140 char chNext
= styler
[startPos
];
141 int styleNext
= styler
.StyleAt(startPos
);
142 int style
= initStyle
;
143 for (unsigned int i
= startPos
; i
< endPos
; i
++) {
145 chNext
= styler
.SafeGetCharAt(i
+ 1);
146 int stylePrev
= style
;
148 styleNext
= styler
.StyleAt(i
+ 1);
149 bool atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
150 if (style
== SCE_POWERSHELL_OPERATOR
) {
152 // Measure the minimum before a '{' to allow
153 // folding on "} else {"
154 if (levelMinCurrent
> levelNext
) {
155 levelMinCurrent
= levelNext
;
158 } else if (ch
== '}') {
161 } else if (foldComment
&& style
== SCE_POWERSHELL_COMMENTSTREAM
) {
162 if (stylePrev
!= SCE_POWERSHELL_COMMENTSTREAM
) {
164 } else if (styleNext
!= SCE_POWERSHELL_COMMENTSTREAM
) {
170 if (atEOL
|| (i
== endPos
-1)) {
171 int levelUse
= levelCurrent
;
173 levelUse
= levelMinCurrent
;
175 int lev
= levelUse
| levelNext
<< 16;
176 if (visibleChars
== 0 && foldCompact
)
177 lev
|= SC_FOLDLEVELWHITEFLAG
;
178 if (levelUse
< levelNext
)
179 lev
|= SC_FOLDLEVELHEADERFLAG
;
180 if (lev
!= styler
.LevelAt(lineCurrent
)) {
181 styler
.SetLevel(lineCurrent
, lev
);
184 levelCurrent
= levelNext
;
185 levelMinCurrent
= levelCurrent
;
191 static const char * const powershellWordLists
[] = {
200 LexerModule
lmPowerShell(SCLEX_POWERSHELL
, ColourisePowerShellDoc
, "powershell", FoldPowerShellDoc
, powershellWordLists
);