1 // Scintilla source code edit control
5 // Copyright 2002 by Sergey Koshcheyev <sergey.k@seznam.cz>
6 // The License.txt file describes the conditions under which this software may be distributed.
18 #include "Scintilla.h"
22 #include "LexAccessor.h"
24 #include "StyleContext.h"
25 #include "CharacterSet.h"
26 #include "LexerModule.h"
29 using namespace Scintilla
;
36 static void ColouriseDocument(
37 Sci_PositionU startPos
,
40 WordList
*keywordlists
[],
43 static const char * const adaWordListDesc
[] = {
48 LexerModule
lmAda(SCLEX_ADA
, ColouriseDocument
, "ada", NULL
, adaWordListDesc
);
54 // Functions that have apostropheStartsAttribute as a parameter set it according to whether
55 // an apostrophe encountered after processing the current token will start an attribute or
56 // a character literal.
57 static void ColouriseCharacter(StyleContext
& sc
, bool& apostropheStartsAttribute
);
58 static void ColouriseComment(StyleContext
& sc
, bool& apostropheStartsAttribute
);
59 static void ColouriseContext(StyleContext
& sc
, char chEnd
, int stateEOL
);
60 static void ColouriseDelimiter(StyleContext
& sc
, bool& apostropheStartsAttribute
);
61 static void ColouriseLabel(StyleContext
& sc
, WordList
& keywords
, bool& apostropheStartsAttribute
);
62 static void ColouriseNumber(StyleContext
& sc
, bool& apostropheStartsAttribute
);
63 static void ColouriseString(StyleContext
& sc
, bool& apostropheStartsAttribute
);
64 static void ColouriseWhiteSpace(StyleContext
& sc
, bool& apostropheStartsAttribute
);
65 static void ColouriseWord(StyleContext
& sc
, WordList
& keywords
, bool& apostropheStartsAttribute
);
67 static inline bool IsDelimiterCharacter(int ch
);
68 static inline bool IsSeparatorOrDelimiterCharacter(int ch
);
69 static bool IsValidIdentifier(const std::string
& identifier
);
70 static bool IsValidNumber(const std::string
& number
);
71 static inline bool IsWordStartCharacter(int ch
);
72 static inline bool IsWordCharacter(int ch
);
74 static void ColouriseCharacter(StyleContext
& sc
, bool& apostropheStartsAttribute
) {
75 apostropheStartsAttribute
= true;
77 sc
.SetState(SCE_ADA_CHARACTER
);
79 // Skip the apostrophe and one more character (so that '' is shown as non-terminated and '''
80 // is handled correctly)
84 ColouriseContext(sc
, '\'', SCE_ADA_CHARACTEREOL
);
87 static void ColouriseContext(StyleContext
& sc
, char chEnd
, int stateEOL
) {
88 while (!sc
.atLineEnd
&& !sc
.Match(chEnd
)) {
93 sc
.ForwardSetState(SCE_ADA_DEFAULT
);
95 sc
.ChangeState(stateEOL
);
99 static void ColouriseComment(StyleContext
& sc
, bool& /*apostropheStartsAttribute*/) {
100 // Apostrophe meaning is not changed, but the parameter is present for uniformity
102 sc
.SetState(SCE_ADA_COMMENTLINE
);
104 while (!sc
.atLineEnd
) {
109 static void ColouriseDelimiter(StyleContext
& sc
, bool& apostropheStartsAttribute
) {
110 apostropheStartsAttribute
= sc
.Match (')');
111 sc
.SetState(SCE_ADA_DELIMITER
);
112 sc
.ForwardSetState(SCE_ADA_DEFAULT
);
115 static void ColouriseLabel(StyleContext
& sc
, WordList
& keywords
, bool& apostropheStartsAttribute
) {
116 apostropheStartsAttribute
= false;
118 sc
.SetState(SCE_ADA_LABEL
);
124 std::string identifier
;
126 while (!sc
.atLineEnd
&& !IsSeparatorOrDelimiterCharacter(sc
.ch
)) {
127 identifier
+= static_cast<char>(tolower(sc
.ch
));
132 if (sc
.Match('>', '>')) {
136 sc
.ChangeState(SCE_ADA_ILLEGAL
);
139 // If the name is an invalid identifier or a keyword, then make it invalid label
140 if (!IsValidIdentifier(identifier
) || keywords
.InList(identifier
.c_str())) {
141 sc
.ChangeState(SCE_ADA_ILLEGAL
);
144 sc
.SetState(SCE_ADA_DEFAULT
);
148 static void ColouriseNumber(StyleContext
& sc
, bool& apostropheStartsAttribute
) {
149 apostropheStartsAttribute
= true;
152 sc
.SetState(SCE_ADA_NUMBER
);
154 // Get all characters up to a delimiter or a separator, including points, but excluding
155 // double points (ranges).
156 while (!IsSeparatorOrDelimiterCharacter(sc
.ch
) || (sc
.ch
== '.' && sc
.chNext
!= '.')) {
157 number
+= static_cast<char>(sc
.ch
);
161 // Special case: exponent with sign
162 if ((sc
.chPrev
== 'e' || sc
.chPrev
== 'E') &&
163 (sc
.ch
== '+' || sc
.ch
== '-')) {
164 number
+= static_cast<char>(sc
.ch
);
167 while (!IsSeparatorOrDelimiterCharacter(sc
.ch
)) {
168 number
+= static_cast<char>(sc
.ch
);
173 if (!IsValidNumber(number
)) {
174 sc
.ChangeState(SCE_ADA_ILLEGAL
);
177 sc
.SetState(SCE_ADA_DEFAULT
);
180 static void ColouriseString(StyleContext
& sc
, bool& apostropheStartsAttribute
) {
181 apostropheStartsAttribute
= true;
183 sc
.SetState(SCE_ADA_STRING
);
186 ColouriseContext(sc
, '"', SCE_ADA_STRINGEOL
);
189 static void ColouriseWhiteSpace(StyleContext
& sc
, bool& /*apostropheStartsAttribute*/) {
190 // Apostrophe meaning is not changed, but the parameter is present for uniformity
191 sc
.SetState(SCE_ADA_DEFAULT
);
192 sc
.ForwardSetState(SCE_ADA_DEFAULT
);
195 static void ColouriseWord(StyleContext
& sc
, WordList
& keywords
, bool& apostropheStartsAttribute
) {
196 apostropheStartsAttribute
= true;
197 sc
.SetState(SCE_ADA_IDENTIFIER
);
201 while (!sc
.atLineEnd
&& !IsSeparatorOrDelimiterCharacter(sc
.ch
)) {
202 word
+= static_cast<char>(tolower(sc
.ch
));
206 if (!IsValidIdentifier(word
)) {
207 sc
.ChangeState(SCE_ADA_ILLEGAL
);
209 } else if (keywords
.InList(word
.c_str())) {
210 sc
.ChangeState(SCE_ADA_WORD
);
213 apostropheStartsAttribute
= false;
217 sc
.SetState(SCE_ADA_DEFAULT
);
224 static void ColouriseDocument(
225 Sci_PositionU startPos
,
228 WordList
*keywordlists
[],
230 WordList
&keywords
= *keywordlists
[0];
232 StyleContext
sc(startPos
, length
, initStyle
, styler
);
234 Sci_Position lineCurrent
= styler
.GetLine(startPos
);
235 bool apostropheStartsAttribute
= (styler
.GetLineState(lineCurrent
) & 1) != 0;
239 // Go to the next line
243 // Remember the line state for future incremental lexing
244 styler
.SetLineState(lineCurrent
, apostropheStartsAttribute
);
246 // Don't continue any styles on the next line
247 sc
.SetState(SCE_ADA_DEFAULT
);
251 if (sc
.Match('-', '-')) {
252 ColouriseComment(sc
, apostropheStartsAttribute
);
255 } else if (sc
.Match('"')) {
256 ColouriseString(sc
, apostropheStartsAttribute
);
259 } else if (sc
.Match('\'') && !apostropheStartsAttribute
) {
260 ColouriseCharacter(sc
, apostropheStartsAttribute
);
263 } else if (sc
.Match('<', '<')) {
264 ColouriseLabel(sc
, keywords
, apostropheStartsAttribute
);
267 } else if (IsASpace(sc
.ch
)) {
268 ColouriseWhiteSpace(sc
, apostropheStartsAttribute
);
271 } else if (IsDelimiterCharacter(sc
.ch
)) {
272 ColouriseDelimiter(sc
, apostropheStartsAttribute
);
275 } else if (IsADigit(sc
.ch
) || sc
.ch
== '#') {
276 ColouriseNumber(sc
, apostropheStartsAttribute
);
278 // Keywords or identifiers
280 ColouriseWord(sc
, keywords
, apostropheStartsAttribute
);
287 static inline bool IsDelimiterCharacter(int ch
) {
311 static inline bool IsSeparatorOrDelimiterCharacter(int ch
) {
312 return IsASpace(ch
) || IsDelimiterCharacter(ch
);
315 static bool IsValidIdentifier(const std::string
& identifier
) {
316 // First character can't be '_', so initialize the flag to true
317 bool lastWasUnderscore
= true;
319 size_t length
= identifier
.length();
321 // Zero-length identifiers are not valid (these can occur inside labels)
326 // Check for valid character at the start
327 if (!IsWordStartCharacter(identifier
[0])) {
331 // Check for only valid characters and no double underscores
332 for (size_t i
= 0; i
< length
; i
++) {
333 if (!IsWordCharacter(identifier
[i
]) ||
334 (identifier
[i
] == '_' && lastWasUnderscore
)) {
337 lastWasUnderscore
= identifier
[i
] == '_';
340 // Check for underscore at the end
341 if (lastWasUnderscore
== true) {
349 static bool IsValidNumber(const std::string
& number
) {
350 size_t hashPos
= number
.find("#");
351 bool seenDot
= false;
354 size_t length
= number
.length();
357 return false; // Just in case
360 if (hashPos
== std::string::npos
) {
361 bool canBeSpecial
= false;
363 for (; i
< length
; i
++) {
364 if (number
[i
] == '_') {
368 canBeSpecial
= false;
369 } else if (number
[i
] == '.') {
370 if (!canBeSpecial
|| seenDot
) {
373 canBeSpecial
= false;
375 } else if (IsADigit(number
[i
])) {
386 bool canBeSpecial
= false;
390 for (; i
< length
; i
++) {
395 canBeSpecial
= false;
396 } else if (IsADigit(ch
)) {
397 base
= base
* 10 + (ch
- '0');
401 } else if (ch
== '#' && canBeSpecial
) {
413 i
++; // Skip over '#'
416 canBeSpecial
= false;
418 for (; i
< length
; i
++) {
419 int ch
= tolower(number
[i
]);
425 canBeSpecial
= false;
427 } else if (ch
== '.') {
428 if (!canBeSpecial
|| seenDot
) {
431 canBeSpecial
= false;
434 } else if (IsADigit(ch
)) {
435 if (ch
- '0' >= base
) {
440 } else if (ch
>= 'a' && ch
<= 'f') {
441 if (ch
- 'a' + 10 >= base
) {
446 } else if (ch
== '#' && canBeSpecial
) {
461 // Exponent (optional)
463 if (number
[i
] != 'e' && number
[i
] != 'E')
466 i
++; // Move past 'E'
472 if (number
[i
] == '+')
474 else if (number
[i
] == '-') {
478 return false; // Integer literals should not have negative exponents
486 bool canBeSpecial
= false;
488 for (; i
< length
; i
++) {
489 if (number
[i
] == '_') {
493 canBeSpecial
= false;
494 } else if (IsADigit(number
[i
])) {
505 // if i == length, number was parsed successfully.
509 static inline bool IsWordCharacter(int ch
) {
510 return IsWordStartCharacter(ch
) || IsADigit(ch
);
513 static inline bool IsWordStartCharacter(int ch
) {
514 return (IsASCII(ch
) && isalpha(ch
)) || ch
== '_';