]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexAPDL.cxx
b739e9a7cb562845ad58855044131c8eff494566
[wxWidgets.git] / src / stc / scintilla / src / LexAPDL.cxx
1
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <stdio.h>
6 #include <stdarg.h>
7
8 #include "Platform.h"
9
10 #include "PropSet.h"
11 #include "Accessor.h"
12 #include "StyleContext.h"
13 #include "KeyWords.h"
14 #include "Scintilla.h"
15 #include "SciLexer.h"
16
17
18 static inline bool IsAWordChar(const int ch) {
19 return (ch < 0x80) && (isalnum(ch) || ch == '_');
20 }
21
22 static inline bool IsAWordStart(const int ch) {
23 return (ch < 0x80) && (isalnum(ch) || ch == '/' || ch == '*');
24 }
25
26 inline bool IsABlank(unsigned int ch) {
27 return (ch == ' ') || (ch == 0x09) || (ch == 0x0b) ;
28 }
29
30
31
32 static void ColouriseAPDLDoc(unsigned int startPos, int length, int initStyle,
33 WordList *keywordlists[], Accessor &styler)
34 {
35
36 //~ FILE *fp;
37 //~ fp = fopen("myoutput.txt", "w");
38
39 WordList &commands = *keywordlists[0];
40 WordList &processors = *keywordlists[1];
41 WordList &functions = *keywordlists[2];
42
43
44 // backtrack to the beginning of the document, this may be slow for big documents.
45 initStyle = SCE_APDL_DEFAULT;
46 StyleContext sc(0, startPos+length, initStyle, styler);
47
48 // backtrack to the nearest keyword
49 //~ while ((startPos > 1) && (styler.StyleAt(startPos) != SCE_APDL_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 bool firstInLine = true;
57 bool atEOL;
58
59 for (; sc.More(); sc.Forward()) {
60
61 atEOL = (sc.ch == '\r' && sc.chNext == '\n') || (sc.ch == '\n');
62
63 //~ if (sc.ch == '\r') {
64 //~ fprintf(fp,"CR\t%d\t%d", atEOL, firstInLine);
65 //~ } else if (sc.ch == '\n') {
66 //~ fprintf(fp,"LF\t%d\t%d", atEOL, firstInLine);
67 //~ } else {
68 //~ fprintf(fp,"%c\t%d\t%d", sc.ch, atEOL, firstInLine);
69 //~ }
70
71 // Determine if the current state should terminate.
72 if (sc.state == SCE_APDL_COMMENT) {
73 //~ fprintf(fp,"\tCOMMENT");
74 if (atEOL) {
75 sc.SetState(SCE_APDL_DEFAULT);
76 }
77 } else if (sc.state == SCE_APDL_COMMENTBLOCK) {
78 //~ fprintf(fp,"\tCOMMENTBLOCK");
79 if (atEOL) {
80 if (sc.ch == '\r') {
81 sc.Forward();
82 }
83 sc.ForwardSetState(SCE_APDL_DEFAULT);
84 }
85 } else if (sc.state == SCE_APDL_NUMBER) {
86 //~ fprintf(fp,"\tNUMBER");
87 if (isdigit(sc.ch)) {
88 } else if ((sc.ch == 'e' || sc.ch == 'E') && (isdigit(sc.chNext) || sc.chNext == '+' || sc.chNext == '-')) {
89 } else if (sc.ch == '.') {
90 } else if ((sc.ch == '+' || sc.ch == '-') && (sc.chPrev == 'e' || sc.chPrev == 'E')) {
91 } else {
92 sc.SetState(SCE_APDL_DEFAULT);
93 }
94 } else if (sc.state == SCE_APDL_STRING) {
95 //~ fprintf(fp,"\tSTRING");
96 if (sc.ch == '\"') {
97 //~ sc.ForwardSetState(SCE_APDL_DEFAULT);
98 sc.Forward();
99 atEOL = (sc.ch == '\r' && sc.chNext == '\n') || (sc.ch == '\n');
100 if (atEOL) {
101 firstInLine = true;
102 }
103 sc.SetState(SCE_APDL_DEFAULT);
104 }
105 } else if (sc.state == SCE_APDL_WORD) {
106 //~ fprintf(fp,"\tWORD");
107 if (!IsAWordChar(sc.ch) || sc.ch == '%') {
108 char s[100];
109 sc.GetCurrentLowered(s, sizeof(s));
110 if (commands.InList(s) && firstInLine) {
111 if (IsABlank(sc.ch) || sc.ch == ',' || atEOL) {
112 sc.ChangeState(SCE_APDL_COMMAND);
113 }
114 if (sc.ch != '\n') {
115 firstInLine = false;
116 }
117 } else if (processors.InList(s)) {
118 if (IsABlank(sc.ch) || atEOL) {
119 sc.ChangeState(SCE_APDL_PROCESSOR);
120 while (sc.ch != '\n') {
121 sc.Forward();
122 }
123 sc.Forward();
124 }
125 } else if (functions.InList(s)) {
126 sc.ChangeState(SCE_APDL_FUNCTION);
127 if (sc.ch != '\n') {
128 firstInLine = false;
129 }
130 } else {
131 if (sc.ch != '\n') {
132 firstInLine = false;
133 }
134 }
135 sc.SetState(SCE_APDL_DEFAULT);
136 }
137 }
138
139 // Determine if a new state should be entered.
140 if (sc.state == SCE_APDL_DEFAULT) {
141 if (sc.ch == '!' && sc.chNext != '!') {
142 sc.SetState(SCE_APDL_COMMENT);
143 } else if (sc.ch == '!' && sc.chNext == '!') {
144 sc.SetState(SCE_APDL_COMMENTBLOCK);
145 } else if (IsADigit(sc.ch) && !IsAWordChar(sc.chPrev)) {
146 sc.SetState(SCE_APDL_NUMBER);
147 } else if (sc.ch == '.' && (isoperator(static_cast<char>(sc.chPrev)) ||
148 IsABlank(sc.chPrev) || sc.chPrev == '\n' || sc.chPrev == '\r')) {
149 sc.SetState(SCE_APDL_NUMBER);
150 } else if (sc.ch == '\"') {
151 sc.SetState(SCE_APDL_STRING);
152 } else if (IsAWordStart(sc.ch) && (!IsADigit(sc.chPrev))) {
153 sc.SetState(SCE_APDL_WORD);
154 }
155
156 }
157 //~ fprintf(fp,"\n");
158
159 if (atEOL) {
160 firstInLine = true;
161 }
162
163 }
164 sc.Complete();
165 }
166
167 static const char * const apdlWordListDesc[] = {
168 "Commands",
169 "Processors",
170 "Functions",
171 0
172 };
173
174 LexerModule lmAPDL(SCLEX_APDL, ColouriseAPDLDoc, "apdl", 0, apdlWordListDesc);