]>
Commit | Line | Data |
---|---|---|
8e54aaed | 1 | // Scintilla source code edit control |
9e96e16f RD |
2 | /** @file LexForth.cxx |
3 | ** Lexer for FORTH | |
8e54aaed | 4 | **/ |
9e96e16f | 5 | // Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org> |
8e54aaed RD |
6 | // The License.txt file describes the conditions under which this software may be distributed. |
7 | ||
8 | #include <stdlib.h> | |
9 | #include <string.h> | |
8e54aaed RD |
10 | #include <stdio.h> |
11 | #include <stdarg.h> | |
1dcf666d RD |
12 | #include <assert.h> |
13 | #include <ctype.h> | |
8e54aaed | 14 | |
1dcf666d RD |
15 | #include "ILexer.h" |
16 | #include "Scintilla.h" | |
17 | #include "SciLexer.h" | |
8e54aaed | 18 | |
1dcf666d RD |
19 | #include "WordList.h" |
20 | #include "LexAccessor.h" | |
8e54aaed | 21 | #include "Accessor.h" |
9e96e16f | 22 | #include "StyleContext.h" |
1dcf666d RD |
23 | #include "CharacterSet.h" |
24 | #include "LexerModule.h" | |
8e54aaed | 25 | |
7e0c58e9 RD |
26 | #ifdef SCI_NAMESPACE |
27 | using namespace Scintilla; | |
28 | #endif | |
29 | ||
9e96e16f RD |
30 | static inline bool IsAWordChar(int ch) { |
31 | return (ch < 0x80) && (isalnum(ch) || ch == '.' || | |
32 | ch == '_' || ch == '?' || ch == '"' || ch == '@' || | |
33 | ch == '!' || ch == '[' || ch == ']' || ch == '/' || | |
34 | ch == '+' || ch == '-' || ch == '*' || ch == '<' || | |
35 | ch == '>' || ch == '=' || ch == ';' || ch == '(' || | |
36 | ch == ')' ); | |
8e54aaed RD |
37 | } |
38 | ||
9e96e16f RD |
39 | static inline bool IsAWordStart(int ch) { |
40 | return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '.'); | |
8e54aaed | 41 | } |
8e54aaed | 42 | |
9e96e16f RD |
43 | static inline bool IsANumChar(int ch) { |
44 | return (ch < 0x80) && (isxdigit(ch) || ch == '.' || ch == 'e' || ch == 'E' ); | |
8e54aaed RD |
45 | } |
46 | ||
9e96e16f RD |
47 | static inline bool IsASpaceChar(int ch) { |
48 | return (ch < 0x80) && isspace(ch); | |
8e54aaed RD |
49 | } |
50 | ||
9e96e16f RD |
51 | static void ColouriseForthDoc(unsigned int startPos, int length, int initStyle, WordList *keywordLists[], |
52 | Accessor &styler) { | |
8e54aaed RD |
53 | |
54 | WordList &control = *keywordLists[0]; | |
55 | WordList &keyword = *keywordLists[1]; | |
56 | WordList &defword = *keywordLists[2]; | |
57 | WordList &preword1 = *keywordLists[3]; | |
58 | WordList &preword2 = *keywordLists[4]; | |
59 | WordList &strings = *keywordLists[5]; | |
60 | ||
9e96e16f RD |
61 | StyleContext sc(startPos, length, initStyle, styler); |
62 | ||
63 | for (; sc.More(); sc.Forward()) | |
64 | { | |
65 | // Determine if the current state should terminate. | |
66 | if (sc.state == SCE_FORTH_COMMENT) { | |
67 | if (sc.atLineEnd) { | |
68 | sc.SetState(SCE_FORTH_DEFAULT); | |
69 | } | |
70 | }else if (sc.state == SCE_FORTH_COMMENT_ML) { | |
71 | if (sc.ch == ')') { | |
72 | sc.ForwardSetState(SCE_FORTH_DEFAULT); | |
73 | } | |
74 | }else if (sc.state == SCE_FORTH_IDENTIFIER || sc.state == SCE_FORTH_NUMBER) { | |
75 | // handle numbers here too, because what we thought was a number might | |
76 | // turn out to be a keyword e.g. 2DUP | |
77 | if (IsASpaceChar(sc.ch) ) { | |
78 | char s[100]; | |
79 | sc.GetCurrentLowered(s, sizeof(s)); | |
80 | int newState = sc.state == SCE_FORTH_NUMBER ? SCE_FORTH_NUMBER : SCE_FORTH_DEFAULT; | |
81 | if (control.InList(s)) { | |
82 | sc.ChangeState(SCE_FORTH_CONTROL); | |
83 | } else if (keyword.InList(s)) { | |
84 | sc.ChangeState(SCE_FORTH_KEYWORD); | |
85 | } else if (defword.InList(s)) { | |
86 | sc.ChangeState(SCE_FORTH_DEFWORD); | |
87 | } else if (preword1.InList(s)) { | |
88 | sc.ChangeState(SCE_FORTH_PREWORD1); | |
89 | } else if (preword2.InList(s)) { | |
90 | sc.ChangeState(SCE_FORTH_PREWORD2); | |
91 | } else if (strings.InList(s)) { | |
92 | sc.ChangeState(SCE_FORTH_STRING); | |
93 | newState = SCE_FORTH_STRING; | |
94 | } | |
95 | sc.SetState(newState); | |
96 | } | |
97 | if (sc.state == SCE_FORTH_NUMBER) { | |
98 | if (IsASpaceChar(sc.ch)) { | |
99 | sc.SetState(SCE_FORTH_DEFAULT); | |
100 | } else if (!IsANumChar(sc.ch)) { | |
101 | sc.ChangeState(SCE_FORTH_IDENTIFIER); | |
102 | } | |
103 | } | |
104 | }else if (sc.state == SCE_FORTH_STRING) { | |
105 | if (sc.ch == '\"') { | |
106 | sc.ForwardSetState(SCE_FORTH_DEFAULT); | |
107 | } | |
108 | }else if (sc.state == SCE_FORTH_LOCALE) { | |
109 | if (sc.ch == '}') { | |
110 | sc.ForwardSetState(SCE_FORTH_DEFAULT); | |
111 | } | |
112 | }else if (sc.state == SCE_FORTH_DEFWORD) { | |
113 | if (IsASpaceChar(sc.ch)) { | |
114 | sc.SetState(SCE_FORTH_DEFAULT); | |
115 | } | |
116 | } | |
117 | ||
118 | // Determine if a new state should be entered. | |
119 | if (sc.state == SCE_FORTH_DEFAULT) { | |
120 | if (sc.ch == '\\'){ | |
121 | sc.SetState(SCE_FORTH_COMMENT); | |
122 | } else if (sc.ch == '(' && | |
123 | (sc.atLineStart || IsASpaceChar(sc.chPrev)) && | |
124 | (sc.atLineEnd || IsASpaceChar(sc.chNext))) { | |
125 | sc.SetState(SCE_FORTH_COMMENT_ML); | |
126 | } else if ( (sc.ch == '$' && (isascii(sc.chNext) && isxdigit(sc.chNext))) ) { | |
127 | // number starting with $ is a hex number | |
128 | sc.SetState(SCE_FORTH_NUMBER); | |
129 | while(sc.More() && isascii(sc.chNext) && isxdigit(sc.chNext)) | |
130 | sc.Forward(); | |
131 | } else if ( (sc.ch == '%' && (isascii(sc.chNext) && (sc.chNext == '0' || sc.chNext == '1'))) ) { | |
132 | // number starting with % is binary | |
133 | sc.SetState(SCE_FORTH_NUMBER); | |
134 | while(sc.More() && isascii(sc.chNext) && (sc.chNext == '0' || sc.chNext == '1')) | |
135 | sc.Forward(); | |
1dcf666d | 136 | } else if ( isascii(sc.ch) && |
9e96e16f RD |
137 | (isxdigit(sc.ch) || ((sc.ch == '.' || sc.ch == '-') && isascii(sc.chNext) && isxdigit(sc.chNext)) ) |
138 | ){ | |
139 | sc.SetState(SCE_FORTH_NUMBER); | |
140 | } else if (IsAWordStart(sc.ch)) { | |
141 | sc.SetState(SCE_FORTH_IDENTIFIER); | |
142 | } else if (sc.ch == '{') { | |
143 | sc.SetState(SCE_FORTH_LOCALE); | |
144 | } else if (sc.ch == ':' && isascii(sc.chNext) && isspace(sc.chNext)) { | |
145 | // highlight word definitions e.g. : GCD ( n n -- n ) ..... ; | |
146 | // ^ ^^^ | |
147 | sc.SetState(SCE_FORTH_DEFWORD); | |
148 | while(sc.More() && isascii(sc.chNext) && isspace(sc.chNext)) | |
149 | sc.Forward(); | |
150 | } else if (sc.ch == ';' && | |
151 | (sc.atLineStart || IsASpaceChar(sc.chPrev)) && | |
152 | (sc.atLineEnd || IsASpaceChar(sc.chNext)) ) { | |
153 | // mark the ';' that ends a word | |
154 | sc.SetState(SCE_FORTH_DEFWORD); | |
155 | sc.ForwardSetState(SCE_FORTH_DEFAULT); | |
156 | } | |
157 | } | |
158 | ||
159 | } | |
160 | sc.Complete(); | |
8e54aaed RD |
161 | } |
162 | ||
163 | static void FoldForthDoc(unsigned int, int, int, WordList *[], | |
9e96e16f | 164 | Accessor &) { |
8e54aaed RD |
165 | } |
166 | ||
167 | static const char * const forthWordLists[] = { | |
9e96e16f RD |
168 | "control keywords", |
169 | "keywords", | |
170 | "definition words", | |
171 | "prewords with one argument", | |
172 | "prewords with two arguments", | |
173 | "string definition keywords", | |
174 | 0, | |
175 | }; | |
176 | ||
177 | LexerModule lmForth(SCLEX_FORTH, ColouriseForthDoc, "forth", FoldForthDoc, forthWordLists); | |
178 | ||
1dcf666d | 179 |