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