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.
18 #include "StyleContext.h"
20 #include "Scintilla.h"
24 using namespace Scintilla
;
27 // Extended to accept accented characters
28 static inline bool IsAWordChar(int ch
) {
29 return ch
>= 0x80 || isalnum(ch
) || ch
== '-';
32 static void ColourisePowerShellDoc(unsigned int startPos
, int length
, int initStyle
,
33 WordList
*keywordlists
[], Accessor
&styler
) {
35 WordList
&keywords
= *keywordlists
[0];
36 WordList
&keywords2
= *keywordlists
[1];
37 WordList
&keywords3
= *keywordlists
[2];
39 styler
.StartAt(startPos
);
41 StyleContext
sc(startPos
, length
, initStyle
, styler
);
43 for (; sc
.More(); sc
.Forward()) {
45 if (sc
.state
== SCE_POWERSHELL_COMMENT
) {
47 sc
.SetState(SCE_POWERSHELL_DEFAULT
);
49 } else if (sc
.state
== SCE_POWERSHELL_STRING
) {
50 // This is a doubles quotes string
52 sc
.ForwardSetState(SCE_POWERSHELL_DEFAULT
);
54 } else if (sc
.state
== SCE_POWERSHELL_CHARACTER
) {
55 // This is a single quote string
57 sc
.ForwardSetState(SCE_POWERSHELL_DEFAULT
);
59 } else if (sc
.state
== SCE_POWERSHELL_NUMBER
) {
60 if (!IsADigit(sc
.ch
)) {
61 sc
.SetState(SCE_POWERSHELL_DEFAULT
);
63 } else if (sc
.state
== SCE_POWERSHELL_VARIABLE
) {
64 if (!IsAWordChar(sc
.ch
)) {
65 sc
.SetState(SCE_POWERSHELL_DEFAULT
);
67 } else if (sc
.state
== SCE_POWERSHELL_OPERATOR
) {
68 if (!isoperator(static_cast<char>(sc
.ch
))) {
69 sc
.SetState(SCE_POWERSHELL_DEFAULT
);
71 } else if (sc
.state
== SCE_POWERSHELL_IDENTIFIER
) {
72 if (!IsAWordChar(sc
.ch
)) {
74 sc
.GetCurrentLowered(s
, sizeof(s
));
76 if (keywords
.InList(s
)) {
77 sc
.ChangeState(SCE_POWERSHELL_KEYWORD
);
78 } else if (keywords2
.InList(s
)) {
79 sc
.ChangeState(SCE_POWERSHELL_CMDLET
);
80 } else if (keywords3
.InList(s
)) {
81 sc
.ChangeState(SCE_POWERSHELL_ALIAS
);
83 sc
.SetState(SCE_POWERSHELL_DEFAULT
);
87 // Determine if a new state should be entered.
88 if (sc
.state
== SCE_POWERSHELL_DEFAULT
) {
90 sc
.SetState(SCE_POWERSHELL_COMMENT
);
91 } else if (sc
.ch
== '\"') {
92 sc
.SetState(SCE_POWERSHELL_STRING
);
93 } else if (sc
.ch
== '\'') {
94 sc
.SetState(SCE_POWERSHELL_CHARACTER
);
95 } else if (sc
.ch
== '$') {
96 sc
.SetState(SCE_POWERSHELL_VARIABLE
);
97 } else if (IsADigit(sc
.ch
) || (sc
.ch
== '.' && IsADigit(sc
.chNext
))) {
98 sc
.SetState(SCE_POWERSHELL_NUMBER
);
99 } else if (isoperator(static_cast<char>(sc
.ch
))) {
100 sc
.SetState(SCE_POWERSHELL_OPERATOR
);
101 } else if (IsAWordChar(sc
.ch
)) {
102 sc
.SetState(SCE_POWERSHELL_IDENTIFIER
);
109 // Store both the current line's fold level and the next lines in the
110 // level store to make it easy to pick up with each increment
111 // and to make it possible to fiddle the current level for "} else {".
112 static void FoldPowerShellDoc(unsigned int startPos
, int length
, int,
113 WordList
*[], Accessor
&styler
) {
114 bool foldCompact
= styler
.GetPropertyInt("fold.compact", 1) != 0;
115 bool foldAtElse
= styler
.GetPropertyInt("fold.at.else", 0) != 0;
116 unsigned int endPos
= startPos
+ length
;
117 int visibleChars
= 0;
118 int lineCurrent
= styler
.GetLine(startPos
);
119 int levelCurrent
= SC_FOLDLEVELBASE
;
121 levelCurrent
= styler
.LevelAt(lineCurrent
-1) >> 16;
122 int levelMinCurrent
= levelCurrent
;
123 int levelNext
= levelCurrent
;
124 char chNext
= styler
[startPos
];
125 int styleNext
= styler
.StyleAt(startPos
);
126 for (unsigned int i
= startPos
; i
< endPos
; i
++) {
128 chNext
= styler
.SafeGetCharAt(i
+ 1);
129 int style
= styleNext
;
130 styleNext
= styler
.StyleAt(i
+ 1);
131 bool atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
132 if (style
== SCE_POWERSHELL_OPERATOR
) {
134 // Measure the minimum before a '{' to allow
135 // folding on "} else {"
136 if (levelMinCurrent
> levelNext
) {
137 levelMinCurrent
= levelNext
;
140 } else if (ch
== '}') {
146 if (atEOL
|| (i
== endPos
-1)) {
147 int levelUse
= levelCurrent
;
149 levelUse
= levelMinCurrent
;
151 int lev
= levelUse
| levelNext
<< 16;
152 if (visibleChars
== 0 && foldCompact
)
153 lev
|= SC_FOLDLEVELWHITEFLAG
;
154 if (levelUse
< levelNext
)
155 lev
|= SC_FOLDLEVELHEADERFLAG
;
156 if (lev
!= styler
.LevelAt(lineCurrent
)) {
157 styler
.SetLevel(lineCurrent
, lev
);
160 levelCurrent
= levelNext
;
161 levelMinCurrent
= levelCurrent
;
167 static const char * const powershellWordLists
[] = {
174 LexerModule
lmPowerShell(SCLEX_POWERSHELL
, ColourisePowerShellDoc
, "powershell", FoldPowerShellDoc
, powershellWordLists
);