1 | // Scintilla source code edit control |
2 | /** @file LexDMAP.cxx |
3 | ** Lexer for MSC Nastran DMAP. |
4 | ** Written by Mark Robinson, based on the Fortran lexer by Chuan-jian Shen, Last changed Aug. 2013 |
5 | **/ |
6 | // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org> |
7 | // The License.txt file describes the conditions under which this software may be distributed. |
8 | /***************************************/ |
9 | #include <stdlib.h> |
10 | #include <string.h> |
11 | #include <stdio.h> |
12 | #include <stdarg.h> |
13 | #include <assert.h> |
14 | #include <ctype.h> |
15 | |
16 | #include <string> |
17 | #include <string_view> |
18 | /***************************************/ |
19 | #include "ILexer.h" |
20 | #include "Scintilla.h" |
21 | #include "SciLexer.h" |
22 | |
23 | #include "WordList.h" |
24 | #include "LexAccessor.h" |
25 | #include "Accessor.h" |
26 | #include "StyleContext.h" |
27 | #include "CharacterSet.h" |
28 | #include "LexerModule.h" |
29 | /***************************************/ |
30 | |
31 | using namespace Lexilla; |
32 | |
33 | /***********************************************/ |
34 | static inline bool IsAWordChar(const int ch) { |
35 | return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '%'); |
36 | } |
37 | /**********************************************/ |
38 | static inline bool IsAWordStart(const int ch) { |
39 | return (ch < 0x80) && (isalnum(ch)); |
40 | } |
41 | /***************************************/ |
42 | static void ColouriseDMAPDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, |
43 | WordList *keywordlists[], Accessor &styler) { |
44 | WordList &keywords = *keywordlists[0]; |
45 | WordList &keywords2 = *keywordlists[1]; |
46 | WordList &keywords3 = *keywordlists[2]; |
47 | /***************************************/ |
48 | Sci_Position posLineStart = 0, numNonBlank = 0; |
49 | Sci_Position endPos = startPos + length; |
50 | /***************************************/ |
51 | // backtrack to the nearest keyword |
52 | while ((startPos > 1) && (styler.StyleAt(startPos) != SCE_DMAP_WORD)) { |
53 | startPos--; |
54 | } |
55 | startPos = styler.LineStart(styler.GetLine(startPos)); |
56 | initStyle = styler.StyleAt(startPos - 1); |
57 | StyleContext sc(startPos, endPos-startPos, initStyle, styler); |
58 | /***************************************/ |
59 | for (; sc.More(); sc.Forward()) { |
60 | // remember the start position of the line |
61 | if (sc.atLineStart) { |
62 | posLineStart = sc.currentPos; |
63 | numNonBlank = 0; |
64 | sc.SetState(SCE_DMAP_DEFAULT); |
65 | } |
66 | if (!IsASpaceOrTab(sc.ch)) numNonBlank ++; |
67 | /***********************************************/ |
68 | // Handle data appearing after column 72; it is ignored |
69 | Sci_Position toLineStart = sc.currentPos - posLineStart; |
70 | if (toLineStart >= 72 || sc.ch == '$') { |
71 | sc.SetState(SCE_DMAP_COMMENT); |
72 | while (!sc.atLineEnd && sc.More()) sc.Forward(); // Until line end |
73 | continue; |
74 | } |
75 | /***************************************/ |
76 | // Determine if the current state should terminate. |
77 | if (sc.state == SCE_DMAP_OPERATOR) { |
78 | sc.SetState(SCE_DMAP_DEFAULT); |
79 | } else if (sc.state == SCE_DMAP_NUMBER) { |
80 | if (!(IsAWordChar(sc.ch) || sc.ch=='\'' || sc.ch=='\"' || sc.ch=='.')) { |
81 | sc.SetState(SCE_DMAP_DEFAULT); |
82 | } |
83 | } else if (sc.state == SCE_DMAP_IDENTIFIER) { |
84 | if (!IsAWordChar(sc.ch) || (sc.ch == '%')) { |
85 | char s[100]; |
86 | sc.GetCurrentLowered(s, sizeof(s)); |
87 | if (keywords.InList(s)) { |
88 | sc.ChangeState(SCE_DMAP_WORD); |
89 | } else if (keywords2.InList(s)) { |
90 | sc.ChangeState(SCE_DMAP_WORD2); |
91 | } else if (keywords3.InList(s)) { |
92 | sc.ChangeState(SCE_DMAP_WORD3); |
93 | } |
94 | sc.SetState(SCE_DMAP_DEFAULT); |
95 | } |
96 | } else if (sc.state == SCE_DMAP_COMMENT) { |
97 | if (sc.ch == '\r' || sc.ch == '\n') { |
98 | sc.SetState(SCE_DMAP_DEFAULT); |
99 | } |
100 | } else if (sc.state == SCE_DMAP_STRING1) { |
101 | if (sc.ch == '\'') { |
102 | if (sc.chNext == '\'') { |
103 | sc.Forward(); |
104 | } else { |
105 | sc.ForwardSetState(SCE_DMAP_DEFAULT); |
106 | } |
107 | } else if (sc.atLineEnd) { |
108 | sc.ChangeState(SCE_DMAP_STRINGEOL); |
109 | sc.ForwardSetState(SCE_DMAP_DEFAULT); |
110 | } |
111 | } else if (sc.state == SCE_DMAP_STRING2) { |
112 | if (sc.atLineEnd) { |
113 | sc.ChangeState(SCE_DMAP_STRINGEOL); |
114 | sc.ForwardSetState(SCE_DMAP_DEFAULT); |
115 | } else if (sc.ch == '\"') { |
116 | if (sc.chNext == '\"') { |
117 | sc.Forward(); |
118 | } else { |
119 | sc.ForwardSetState(SCE_DMAP_DEFAULT); |
120 | } |
121 | } |
122 | } |
123 | /***************************************/ |
124 | // Determine if a new state should be entered. |
125 | if (sc.state == SCE_DMAP_DEFAULT) { |
126 | if (sc.ch == '$') { |
127 | sc.SetState(SCE_DMAP_COMMENT); |
128 | } else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext)) || (sc.ch == '-' && IsADigit(sc.chNext))) { |
129 | sc.SetState(SCE_F_NUMBER); |
130 | } else if (IsAWordStart(sc.ch)) { |
131 | sc.SetState(SCE_DMAP_IDENTIFIER); |
132 | } else if (sc.ch == '\"') { |
133 | sc.SetState(SCE_DMAP_STRING2); |
134 | } else if (sc.ch == '\'') { |
135 | sc.SetState(SCE_DMAP_STRING1); |
136 | } else if (isoperator(static_cast<char>(sc.ch))) { |
137 | sc.SetState(SCE_DMAP_OPERATOR); |
138 | } |
139 | } |
140 | } |
141 | sc.Complete(); |
142 | } |
143 | /***************************************/ |
144 | // To determine the folding level depending on keywords |
145 | static int classifyFoldPointDMAP(const char* s, const char* prevWord) { |
146 | int lev = 0; |
147 | if ((strcmp(prevWord, "else" ) == 0 && strcmp(s, "if" ) == 0) || strcmp(s, "enddo" ) == 0 || strcmp(s, "endif" ) == 0) { |
148 | lev = -1; |
149 | } else if ((strcmp(prevWord, "do" ) == 0 && strcmp(s, "while" ) == 0) || strcmp(s, "then" ) == 0) { |
150 | lev = 1; |
151 | } |
152 | return lev; |
153 | } |
154 | // Folding the code |
155 | static void FoldDMAPDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, |
156 | WordList *[], Accessor &styler) { |
157 | // |
158 | // bool foldComment = styler.GetPropertyInt("fold.comment") != 0; |
159 | // Do not know how to fold the comment at the moment. |
160 | // |
161 | bool foldCompact = styler.GetPropertyInt("fold.compact" , 1) != 0; |
162 | Sci_PositionU endPos = startPos + length; |
163 | int visibleChars = 0; |
164 | Sci_Position lineCurrent = styler.GetLine(startPos); |
165 | int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK; |
166 | int levelCurrent = levelPrev; |
167 | char chNext = styler[startPos]; |
168 | int styleNext = styler.StyleAt(startPos); |
169 | int style = initStyle; |
170 | /***************************************/ |
171 | Sci_Position lastStart = 0; |
172 | char prevWord[32] = "" ; |
173 | /***************************************/ |
174 | for (Sci_PositionU i = startPos; i < endPos; i++) { |
175 | char ch = chNext; |
176 | chNext = styler.SafeGetCharAt(i + 1); |
177 | int stylePrev = style; |
178 | style = styleNext; |
179 | styleNext = styler.StyleAt(i + 1); |
180 | bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n'); |
181 | // |
182 | if ((stylePrev == SCE_DMAP_DEFAULT || stylePrev == SCE_DMAP_OPERATOR || stylePrev == SCE_DMAP_COMMENT) && (style == SCE_DMAP_WORD)) { |
183 | // Store last word and label start point. |
184 | lastStart = i; |
185 | } |
186 | /***************************************/ |
187 | if (style == SCE_DMAP_WORD) { |
188 | if(iswordchar(ch) && !iswordchar(chNext)) { |
189 | char s[32]; |
190 | Sci_PositionU k; |
191 | for(k=0; (k<31 ) && (k<i-lastStart+1 ); k++) { |
192 | s[k] = static_cast<char>(tolower(styler[lastStart+k])); |
193 | } |
194 | s[k] = '\0'; |
195 | levelCurrent += classifyFoldPointDMAP(s, prevWord); |
196 | strcpy(prevWord, s); |
197 | } |
198 | } |
199 | if (atEOL) { |
200 | int lev = levelPrev; |
201 | if (visibleChars == 0 && foldCompact) |
202 | lev |= SC_FOLDLEVELWHITEFLAG; |
203 | if ((levelCurrent > levelPrev) && (visibleChars > 0)) |
204 | lev |= SC_FOLDLEVELHEADERFLAG; |
205 | if (lev != styler.LevelAt(lineCurrent)) { |
206 | styler.SetLevel(lineCurrent, lev); |
207 | } |
208 | lineCurrent++; |
209 | levelPrev = levelCurrent; |
210 | visibleChars = 0; |
211 | strcpy(prevWord, "" ); |
212 | } |
213 | /***************************************/ |
214 | if (!isspacechar(ch)) visibleChars++; |
215 | } |
216 | /***************************************/ |
217 | // Fill in the real level of the next line, keeping the current flags as they will be filled in later |
218 | int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK; |
219 | styler.SetLevel(lineCurrent, levelPrev | flagsNext); |
220 | } |
221 | /***************************************/ |
222 | static const char * const DMAPWordLists[] = { |
223 | "Primary keywords and identifiers" , |
224 | "Intrinsic functions" , |
225 | "Extended and user defined functions" , |
226 | 0, |
227 | }; |
228 | /***************************************/ |
229 | LexerModule lmDMAP(SCLEX_DMAP, ColouriseDMAPDoc, "DMAP" , FoldDMAPDoc, DMAPWordLists); |
230 | |