Debian Codegraph @

scintilla/lexilla/lexers/LexR.cxx

raw
1 // Scintilla source code edit control
2 /** @file LexR.cxx
3 ** Lexer for R, S, SPlus Statistics Program (Heavily derived from CPP Lexer).
4 **
5 **/
6 // Copyright 1998-2002 by Neil Hodgson <[email protected]>
7 // The License.txt file describes the conditions under which this software may be distributed.
8
9 #include <cassert>
10 #include <cctype>
11
12 #include <string>
13 #include <string_view>
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 Lexilla;
27
28 namespace {
29
30 inline bool IsAWordChar(int ch) noexcept {
31 return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_');
32 }
33
34 inline bool IsAWordStart(int ch) noexcept {
35 return (ch < 0x80) && (isalnum(ch) || ch == '_');
36 }
37
38 constexpr bool IsAnOperator(int ch) noexcept {
39 // '.' left out as it is used to make up numbers
40 if (ch == '-' || ch == '+' || ch == '!' || ch == '~' ||
41 ch == '?' || ch == ':' || ch == '*' || ch == '/' ||
42 ch == '^' || ch == '<' || ch == '>' || ch == '=' ||
43 ch == '&' || ch == '|' || ch == '$' || ch == '(' ||
44 ch == ')' || ch == '}' || ch == '{' || ch == '[' ||
45 ch == ']')
46 return true;
47 return false;
48 }
49
50 constexpr bool IsOctalOrHex(int ch, bool hex) noexcept {
51 return IsAnOctalDigit(ch) || (hex && IsAHeXDigit(ch));
52 }
53
54 // https://search.r-project.org/R/refmans/base/html/Quotes.html
55 struct EscapeSequence {
56 int outerState = SCE_R_DEFAULT;
57 int digitsLeft = 0;
58 bool hex = false;
59 bool brace = false;
60
61 // highlight any character as escape sequence, unrecognized escape sequence is syntax error.
62 void resetEscapeState(int state, int chNext) noexcept {
63 outerState = state;
64 digitsLeft = 1;
65 hex = true;
66 brace = false;
67 if (chNext == 'x') {
68 digitsLeft = 3;
69 } else if (chNext == 'u') {
70 digitsLeft = 5;
71 } else if (chNext == 'U') {
72 digitsLeft = 9;
73 } else if (IsAnOctalDigit(chNext)) {
74 digitsLeft = 3;
75 hex = false;
76 }
77 }
78 bool atEscapeEnd(int ch) noexcept {
79 --digitsLeft;
80 return digitsLeft <= 0 || !IsOctalOrHex(ch, hex);
81 }
82 };
83
84 int CheckRawString(LexAccessor &styler, Sci_Position pos, int &dashCount) {
85 dashCount = 0;
86 while (true) {
87 const char ch = styler.SafeGetCharAt(pos++);
88 switch (ch) {
89 case '-':
90 ++dashCount;
91 break;
92 case '(':
93 return ')';
94 case '[':
95 return ']';
96 case '{':
97 return '}';
98 default:
99 dashCount = 0;
100 return 0;
101 }
102 }
103 }
104
105 void ColouriseRDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[],
106 Accessor &styler) {
107
108 const WordList &keywords = *keywordlists[0];
109 const WordList &keywords2 = *keywordlists[1];
110 const WordList &keywords3 = *keywordlists[2];
111 // state for raw string
112 int matchingDelimiter = 0;
113 int dashCount = 0;
114
115 // property lexer.r.escape.sequence
116 // Set to 1 to enable highlighting of escape sequences in strings.
117 const bool escapeSequence = styler.GetPropertyInt("lexer.r.escape.sequence", 0) != 0;
118 EscapeSequence escapeSeq;
119
120 StyleContext sc(startPos, length, initStyle, styler);
121 if (sc.currentLine > 0) {
122 const int lineState = styler.GetLineState(sc.currentLine - 1);
123 matchingDelimiter = lineState & 0xff;
124 dashCount = lineState >> 8;
125 }
126
127 while (sc.More()) {
128 // Determine if the current state should terminate.
129 switch (sc.state) {
130 case SCE_R_OPERATOR:
131 sc.SetState(SCE_R_DEFAULT);
132 break;
133
134 case SCE_R_NUMBER:
135 // https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Literal-constants
136 if (AnyOf(sc.ch, 'e', 'E', 'p', 'P') && (IsADigit(sc.chNext) || sc.chNext == '+' || sc.chNext == '-')) {
137 sc.Forward(); // exponent part
138 } else if (!(IsAHeXDigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext)))) {
139 if (AnyOf(sc.ch, 'L', 'i')) {
140 sc.Forward(); // integer and complex qualifier
141 }
142 sc.SetState(SCE_R_DEFAULT);
143 }
144 break;
145
146 case SCE_R_IDENTIFIER:
147 if (!IsAWordChar(sc.ch)) {
148 char s[100];
149 sc.GetCurrent(s, sizeof(s));
150 if (keywords.InList(s)) {
151 sc.ChangeState(SCE_R_KWORD);
152 } else if (keywords2.InList(s)) {
153 sc.ChangeState(SCE_R_BASEKWORD);
154 } else if (keywords3.InList(s)) {
155 sc.ChangeState(SCE_R_OTHERKWORD);
156 }
157 sc.SetState(SCE_R_DEFAULT);
158 }
159 break;
160
161 case SCE_R_COMMENT:
162 if (sc.MatchLineEnd()) {
163 sc.SetState(SCE_R_DEFAULT);
164 }
165 break;
166
167 case SCE_R_STRING:
168 case SCE_R_STRING2:
169 case SCE_R_BACKTICKS:
170 if (sc.ch == '\\') {
171 if (escapeSequence) {
172 escapeSeq.resetEscapeState(sc.state, sc.chNext);
173 sc.SetState(SCE_R_ESCAPESEQUENCE);
174 sc.Forward();
175 if (sc.chNext == '{' && AnyOf(sc.ch, 'u', 'U')) {
176 escapeSeq.brace = true;
177 sc.Forward();
178 } else if (sc.MatchLineEnd()) {
179 // don't highlight line ending as escape sequence:
180 // escapeSeq.outerState is lost when editing on next line.
181 sc.SetState(escapeSeq.outerState);
182 }
183 } else {
184 sc.Forward(); // Skip all characters after the backslash
185 }
186 } else if ((sc.state == SCE_R_STRING && sc.ch == '\"')
187 || (sc.state == SCE_R_STRING2 && sc.ch == '\'')
188 || (sc.state == SCE_R_BACKTICKS && sc.ch == '`')) {
189 sc.ForwardSetState(SCE_R_DEFAULT);
190 }
191 break;
192
193 case SCE_R_ESCAPESEQUENCE:
194 if (escapeSeq.atEscapeEnd(sc.ch)) {
195 if (escapeSeq.brace && sc.ch == '}') {
196 sc.Forward();
197 }
198 sc.SetState(escapeSeq.outerState);
199 continue;
200 }
201 break;
202
203 case SCE_R_RAWSTRING:
204 case SCE_R_RAWSTRING2:
205 while (sc.ch == matchingDelimiter) {
206 sc.Forward();
207 int count = dashCount;
208 while (count != 0 && sc.ch == '-') {
209 --count;
210 sc.Forward();
211 }
212 if (count == 0 && sc.ch == ((sc.state == SCE_R_RAWSTRING) ? '\"' : '\'')) {
213 matchingDelimiter = 0;
214 dashCount = 0;
215 sc.ForwardSetState(SCE_R_DEFAULT);
216 break;
217 }
218 }
219 break;
220
221 case SCE_R_INFIX:
222 if (sc.ch == '%') {
223 sc.ForwardSetState(SCE_R_DEFAULT);
224 } else if (sc.atLineEnd) {
225 sc.ChangeState(SCE_R_INFIXEOL);
226 }
227 break;
228
229 case SCE_R_INFIXEOL:
230 if (sc.atLineStart) {
231 sc.SetState(SCE_R_DEFAULT);
232 }
233 break;
234 }
235
236 // Determine if a new state should be entered.
237 if (sc.state == SCE_R_DEFAULT) {
238 if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
239 sc.SetState(SCE_R_NUMBER);
240 if (sc.ch == '0' && AnyOf(sc.chNext, 'x', 'X')) {
241 sc.Forward();
242 }
243 } else if (AnyOf(sc.ch, 'r', 'R') && AnyOf(sc.chNext, '\"', '\'')) {
244 const int chNext = sc.chNext;
245 matchingDelimiter = CheckRawString(styler, sc.currentPos + 2, dashCount);
246 if (matchingDelimiter) {
247 sc.SetState((chNext == '\"') ? SCE_R_RAWSTRING : SCE_R_RAWSTRING2);
248 sc.Forward(dashCount + 2);
249 } else {
250 // syntax error
251 sc.SetState(SCE_R_IDENTIFIER);
252 sc.ForwardSetState((chNext == '\"') ? SCE_R_STRING : SCE_R_STRING2);
253 }
254 } else if (IsAWordStart(sc.ch) ) {
255 sc.SetState(SCE_R_IDENTIFIER);
256 } else if (sc.Match('#')) {
257 sc.SetState(SCE_R_COMMENT);
258 } else if (sc.ch == '\"') {
259 sc.SetState(SCE_R_STRING);
260 } else if (sc.ch == '%') {
261 sc.SetState(SCE_R_INFIX);
262 } else if (sc.ch == '\'') {
263 sc.SetState(SCE_R_STRING2);
264 } else if (sc.ch == '`') {
265 sc.SetState(SCE_R_BACKTICKS);
266 } else if (IsAnOperator(sc.ch)) {
267 sc.SetState(SCE_R_OPERATOR);
268 }
269 }
270
271 if (sc.atLineEnd) {
272 const int lineState = matchingDelimiter | (dashCount << 8);
273 styler.SetLineState(sc.currentLine, lineState);
274 }
275 sc.Forward();
276 }
277 sc.Complete();
278 }
279
280 // Store both the current line's fold level and the next lines in the
281 // level store to make it easy to pick up with each increment
282 // and to make it possible to fiddle the current level for "} else {".
283 void FoldRDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[],
284 Accessor &styler) {
285 const bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
286 const bool foldAtElse = styler.GetPropertyInt("fold.at.else", 0) != 0;
287 const Sci_PositionU endPos = startPos + length;
288 int visibleChars = 0;
289 Sci_Position lineCurrent = styler.GetLine(startPos);
290 int levelCurrent = SC_FOLDLEVELBASE;
291 if (lineCurrent > 0)
292 levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
293 int levelMinCurrent = levelCurrent;
294 int levelNext = levelCurrent;
295 char chNext = styler[startPos];
296 int styleNext = styler.StyleAt(startPos);
297 for (Sci_PositionU i = startPos; i < endPos; i++) {
298 const char ch = chNext;
299 chNext = styler.SafeGetCharAt(i + 1);
300 const int style = styleNext;
301 styleNext = styler.StyleAt(i + 1);
302 const bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
303 if (style == SCE_R_OPERATOR) {
304 if (ch == '{') {
305 // Measure the minimum before a '{' to allow
306 // folding on "} else {"
307 if (levelMinCurrent > levelNext) {
308 levelMinCurrent = levelNext;
309 }
310 levelNext++;
311 } else if (ch == '}') {
312 levelNext--;
313 }
314 }
315 if (atEOL) {
316 int levelUse = levelCurrent;
317 if (foldAtElse) {
318 levelUse = levelMinCurrent;
319 }
320 int lev = levelUse | levelNext << 16;
321 if (visibleChars == 0 && foldCompact)
322 lev |= SC_FOLDLEVELWHITEFLAG;
323 if (levelUse < levelNext)
324 lev |= SC_FOLDLEVELHEADERFLAG;
325 if (lev != styler.LevelAt(lineCurrent)) {
326 styler.SetLevel(lineCurrent, lev);
327 }
328 lineCurrent++;
329 levelCurrent = levelNext;
330 levelMinCurrent = levelCurrent;
331 visibleChars = 0;
332 }
333 if (!isspacechar(ch))
334 visibleChars++;
335 }
336 }
337
338
339 const char * const RWordLists[] = {
340 "Language Keywords",
341 "Base / Default package function",
342 "Other Package Functions",
343 "Unused",
344 "Unused",
345 nullptr,
346 };
347
348 }
349
350 extern const LexerModule lmR(SCLEX_R, ColouriseRDoc, "r", FoldRDoc, RWordLists);