]> git.saurik.com Git - wxWidgets.git/blame - src/stc/scintilla/lexers/LexPowerShell.cxx
Add missing WXK constants for the control keys
[wxWidgets.git] / src / stc / scintilla / lexers / LexPowerShell.cxx
CommitLineData
9e96e16f
RD
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>
9e96e16f
RD
10#include <stdio.h>
11#include <stdarg.h>
1dcf666d
RD
12#include <assert.h>
13#include <ctype.h>
9e96e16f 14
1dcf666d
RD
15#include "ILexer.h"
16#include "Scintilla.h"
17#include "SciLexer.h"
9e96e16f 18
1dcf666d
RD
19#include "WordList.h"
20#include "LexAccessor.h"
9e96e16f
RD
21#include "Accessor.h"
22#include "StyleContext.h"
1dcf666d
RD
23#include "CharacterSet.h"
24#include "LexerModule.h"
9e96e16f
RD
25
26#ifdef SCI_NAMESPACE
27using namespace Scintilla;
28#endif
29
30// Extended to accept accented characters
31static inline bool IsAWordChar(int ch) {
1dcf666d 32 return ch >= 0x80 || isalnum(ch) || ch == '-' || ch == '_';
9e96e16f
RD
33}
34
35static void ColourisePowerShellDoc(unsigned int startPos, int length, int initStyle,
36 WordList *keywordlists[], Accessor &styler) {
37
38 WordList &keywords = *keywordlists[0];
39 WordList &keywords2 = *keywordlists[1];
40 WordList &keywords3 = *keywordlists[2];
1dcf666d
RD
41 WordList &keywords4 = *keywordlists[3];
42 WordList &keywords5 = *keywordlists[4];
9e96e16f
RD
43
44 styler.StartAt(startPos);
45
46 StyleContext sc(startPos, length, initStyle, styler);
47
48 for (; sc.More(); sc.Forward()) {
49
50 if (sc.state == SCE_POWERSHELL_COMMENT) {
51 if (sc.atLineEnd) {
52 sc.SetState(SCE_POWERSHELL_DEFAULT);
53 }
1dcf666d
RD
54 } else if (sc.state == SCE_POWERSHELL_COMMENTSTREAM) {
55 if (sc.ch == '>' && sc.chPrev == '#') {
56 sc.ForwardSetState(SCE_POWERSHELL_DEFAULT);
57 }
9e96e16f
RD
58 } else if (sc.state == SCE_POWERSHELL_STRING) {
59 // This is a doubles quotes string
60 if (sc.ch == '\"') {
61 sc.ForwardSetState(SCE_POWERSHELL_DEFAULT);
62 }
63 } else if (sc.state == SCE_POWERSHELL_CHARACTER) {
64 // This is a single quote string
65 if (sc.ch == '\'') {
66 sc.ForwardSetState(SCE_POWERSHELL_DEFAULT);
67 }
68 } else if (sc.state == SCE_POWERSHELL_NUMBER) {
69 if (!IsADigit(sc.ch)) {
70 sc.SetState(SCE_POWERSHELL_DEFAULT);
71 }
72 } else if (sc.state == SCE_POWERSHELL_VARIABLE) {
73 if (!IsAWordChar(sc.ch)) {
74 sc.SetState(SCE_POWERSHELL_DEFAULT);
75 }
76 } else if (sc.state == SCE_POWERSHELL_OPERATOR) {
77 if (!isoperator(static_cast<char>(sc.ch))) {
78 sc.SetState(SCE_POWERSHELL_DEFAULT);
79 }
80 } else if (sc.state == SCE_POWERSHELL_IDENTIFIER) {
81 if (!IsAWordChar(sc.ch)) {
82 char s[100];
83 sc.GetCurrentLowered(s, sizeof(s));
84
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);
1dcf666d
RD
91 } else if (keywords4.InList(s)) {
92 sc.ChangeState(SCE_POWERSHELL_FUNCTION);
93 } else if (keywords5.InList(s)) {
94 sc.ChangeState(SCE_POWERSHELL_USER1);
9e96e16f
RD
95 }
96 sc.SetState(SCE_POWERSHELL_DEFAULT);
97 }
98 }
99
100 // Determine if a new state should be entered.
101 if (sc.state == SCE_POWERSHELL_DEFAULT) {
102 if (sc.ch == '#') {
103 sc.SetState(SCE_POWERSHELL_COMMENT);
1dcf666d
RD
104 } else if (sc.ch == '<' && sc.chNext == '#') {
105 sc.SetState(SCE_POWERSHELL_COMMENTSTREAM);
9e96e16f
RD
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);
118 }
119 }
120 }
121 sc.Complete();
122}
123
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 {".
1dcf666d 127static void FoldPowerShellDoc(unsigned int startPos, int length, int initStyle,
9e96e16f 128 WordList *[], Accessor &styler) {
1dcf666d 129 bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
9e96e16f
RD
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;
136 if (lineCurrent > 0)
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);
1dcf666d 142 int style = initStyle;
9e96e16f
RD
143 for (unsigned int i = startPos; i < endPos; i++) {
144 char ch = chNext;
145 chNext = styler.SafeGetCharAt(i + 1);
1dcf666d
RD
146 int stylePrev = style;
147 style = styleNext;
9e96e16f
RD
148 styleNext = styler.StyleAt(i + 1);
149 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
150 if (style == SCE_POWERSHELL_OPERATOR) {
151 if (ch == '{') {
152 // Measure the minimum before a '{' to allow
153 // folding on "} else {"
154 if (levelMinCurrent > levelNext) {
155 levelMinCurrent = levelNext;
156 }
157 levelNext++;
158 } else if (ch == '}') {
159 levelNext--;
160 }
1dcf666d
RD
161 } else if (foldComment && style == SCE_POWERSHELL_COMMENTSTREAM) {
162 if (stylePrev != SCE_POWERSHELL_COMMENTSTREAM) {
163 levelNext++;
164 } else if (styleNext != SCE_POWERSHELL_COMMENTSTREAM) {
165 levelNext--;
166 }
9e96e16f
RD
167 }
168 if (!IsASpace(ch))
169 visibleChars++;
170 if (atEOL || (i == endPos-1)) {
171 int levelUse = levelCurrent;
172 if (foldAtElse) {
173 levelUse = levelMinCurrent;
174 }
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);
182 }
183 lineCurrent++;
184 levelCurrent = levelNext;
185 levelMinCurrent = levelCurrent;
186 visibleChars = 0;
187 }
188 }
189}
190
191static const char * const powershellWordLists[] = {
192 "Commands",
193 "Cmdlets",
194 "Aliases",
1dcf666d
RD
195 "Functions",
196 "User1",
9e96e16f
RD
197 0
198};
199
200LexerModule lmPowerShell(SCLEX_POWERSHELL, ColourisePowerShellDoc, "powershell", FoldPowerShellDoc, powershellWordLists);
201