]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/LexAda.cxx
0d8fb9d5dd7148fd42b792ca6927e3dd4595684e
[wxWidgets.git] / contrib / src / stc / scintilla / src / LexAda.cxx
1 // SciTE - Scintilla based Text Editor
2 // LexAda.cxx - lexer for Ada95
3 // by Tahir Karaca <tahir@bigfoot.de>
4 // The License.txt file describes the conditions under which this software may be distributed.
5
6 #include <stdlib.h>
7 #include <string.h>
8 #include <ctype.h>
9 #include <stdio.h>
10 #include <stdarg.h>
11
12 #include "Platform.h"
13
14 #include "PropSet.h"
15 #include "Accessor.h"
16 #include "KeyWords.h"
17 #include "Scintilla.h"
18 #include "SciLexer.h"
19
20 inline void classifyWordAda(unsigned int start, unsigned int end,
21 WordList &keywords, Accessor &styler) {
22
23 static const unsigned KEWORD_LEN_MAX = 30;
24
25 char wordLower[KEWORD_LEN_MAX + 1];
26 unsigned i;
27 for(i = 0; ( i < KEWORD_LEN_MAX ) && ( i < end - start + 1 ); i++) {
28 wordLower[i] = static_cast<char>(tolower(styler[start + i]));
29 }
30 wordLower[i] = '\0';
31
32 // int levelChange = 0;
33 char chAttr = SCE_ADA_IDENTIFIER;
34 if (keywords.InList(wordLower)) {
35 chAttr = SCE_ADA_WORD;
36
37 // Folding doesn't work this way since the semantics of some keywords depends
38 // on the current context.
39 // E.g. - "cond1 and THEN cond2" <-> "if ... THEN ..."
40 // - "procedure X IS ... end X;" <-> "procedure X IS new Y;"
41 // if (strcmp(wordLower, "is") == 0 || strcmp(wordLower, "then") == 0)
42 // levelChange=1;
43 // else if (strcmp(wordLower, "end") == 0)
44 // levelChange=-1;
45 }
46 styler.ColourTo(end, chAttr);
47
48 // return levelChange;
49 }
50
51
52 static inline bool isAdaOperator(char ch) {
53
54 if (ch == '&' || ch == '\'' || ch == '(' || ch == ')' ||
55 ch == '*' || ch == '+' || ch == ',' || ch == '-' ||
56 ch == '.' || ch == '/' || ch == ':' || ch == ';' ||
57 ch == '<' || ch == '=' || ch == '>')
58 return true;
59 return false;
60 }
61
62
63 inline void styleTokenBegin(char beginChar, unsigned int pos, int &state,
64 Accessor &styler) {
65
66 if (isalpha(beginChar)) {
67 styler.ColourTo(pos-1, state);
68 state = SCE_ADA_IDENTIFIER;
69 } else if (isdigit(beginChar)) {
70 styler.ColourTo(pos-1, state);
71 state = SCE_ADA_NUMBER;
72 } else if (beginChar == '-' && styler.SafeGetCharAt(pos + 1) == '-') {
73 styler.ColourTo(pos-1, state);
74 state = SCE_ADA_COMMENT;
75 } else if (beginChar == '\"') {
76 styler.ColourTo(pos-1, state);
77 state = SCE_ADA_STRING;
78 } else if (beginChar == '\'' && styler.SafeGetCharAt(pos + 2) == '\'') {
79 styler.ColourTo(pos-1, state);
80 state = SCE_ADA_CHARACTER;
81 } else if (isAdaOperator(beginChar)) {
82 styler.ColourTo(pos-1, state);
83 styler.ColourTo(pos, SCE_ADA_OPERATOR);
84 }
85 }
86
87
88 static void ColouriseAdaDoc(unsigned int startPos, int length, int initStyle,
89 WordList *keywordlists[], Accessor &styler) {
90
91 WordList &keywords = *keywordlists[0];
92
93 styler.StartAt(startPos);
94
95 // bool fold = styler.GetPropertyInt("fold");
96 // int lineCurrent = styler.GetLine(startPos);
97 // int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
98 // int levelCurrent = levelPrev;
99
100 int state = initStyle;
101 if (state == SCE_ADA_STRINGEOL) // Does not leak onto next line
102 state = SCE_ADA_DEFAULT;
103 char chNext = styler[startPos];
104 const unsigned int lengthDoc = startPos + length;
105 //int visibleChars = 0;
106 styler.StartSegment(startPos);
107 for (unsigned int i = startPos; i < lengthDoc; i++) {
108 char ch = chNext;
109 chNext = styler.SafeGetCharAt(i + 1);
110
111 if ((ch == '\r' && chNext != '\n') || (ch == '\n')) {
112 // Trigger on CR only (Mac style) or either on LF from CR+LF (Dos/Win) or on LF alone (Unix)
113 // Avoid triggering two times on Dos/Win
114 if (state == SCE_ADA_STRINGEOL) {
115 styler.ColourTo(i, state);
116 state = SCE_ADA_DEFAULT;
117 }
118 // if (fold) {
119 // int lev = levelPrev;
120 // if (visibleChars == 0)
121 // lev |= SC_FOLDLEVELWHITEFLAG;
122 // if ((levelCurrent > levelPrev) && (visibleChars > 0))
123 // lev |= SC_FOLDLEVELHEADERFLAG;
124 // styler.SetLevel(lineCurrent, lev);
125 // lineCurrent++;
126 // levelPrev = levelCurrent;
127 // }
128 //visibleChars = 0;
129 }
130 //if (!isspacechar(ch))
131 // visibleChars++;
132
133 if (styler.IsLeadByte(ch)) {
134 chNext = styler.SafeGetCharAt(i + 2);
135 i += 1;
136 continue;
137 }
138
139 if (state == SCE_ADA_DEFAULT) {
140 styleTokenBegin(ch, i, state, styler);
141 } else if (state == SCE_ADA_IDENTIFIER) {
142 if (!iswordchar(ch)) {
143 classifyWordAda(styler.GetStartSegment(),
144 i - 1,
145 keywords,
146 styler);
147 state = SCE_ADA_DEFAULT;
148 styleTokenBegin(ch, i, state, styler);
149 }
150 } else if (state == SCE_ADA_COMMENT) {
151 if (ch == '\r' || ch == '\n') {
152 styler.ColourTo(i-1, state);
153 state = SCE_ADA_DEFAULT;
154 }
155 } else if (state == SCE_ADA_STRING) {
156 if (ch == '"' ) {
157 if( chNext == '"' ) {
158 i++;
159 chNext = styler.SafeGetCharAt(i + 1);
160 } else {
161 styler.ColourTo(i, state);
162 state = SCE_ADA_DEFAULT;
163 }
164 } else if (chNext == '\r' || chNext == '\n') {
165 styler.ColourTo(i-1, SCE_ADA_STRINGEOL);
166 state = SCE_ADA_STRINGEOL;
167 }
168 } else if (state == SCE_ADA_CHARACTER) {
169 if (ch == '\r' || ch == '\n') {
170 styler.ColourTo(i-1, SCE_ADA_STRINGEOL);
171 state = SCE_ADA_STRINGEOL;
172 } else if (ch == '\'' && styler.SafeGetCharAt(i - 2) == '\'') {
173 styler.ColourTo(i, state);
174 state = SCE_ADA_DEFAULT;
175 }
176 } else if (state == SCE_ADA_NUMBER) {
177 if ( !( isdigit(ch) || ch == '.' || ch == '_' || ch == '#'
178 || ch == 'A' || ch == 'B' || ch == 'C' || ch == 'D'
179 || ch == 'E' || ch == 'F'
180 || ch == 'a' || ch == 'b' || ch == 'c' || ch == 'd'
181 || ch == 'e' || ch == 'f' ) ) {
182 styler.ColourTo(i-1, SCE_ADA_NUMBER);
183 state = SCE_ADA_DEFAULT;
184 styleTokenBegin(ch, i, state, styler);
185 }
186 }
187
188 }
189 styler.ColourTo(lengthDoc - 1, state);
190
191 // // Fill in the real level of the next line, keeping the current flags as they will be filled in later
192 // if (fold) {
193 // int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
194 // styler.SetLevel(lineCurrent, levelPrev | flagsNext);
195 // }
196 }
197
198 LexerModule lmAda(SCLEX_ADA, ColouriseAdaDoc, "ada");