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