1 // Scintilla source code edit control
2 /** @file LexKix.cxx
3  ** Lexer for KIX-Scripts.
4  **/
5 // Copyright 2004 by Manfred Becker <manfred@becker-trdf.de>
6 // The License.txt file describes the conditions under which this software may be distributed.
7 // Edited by Lee Wilmott (24-Jun-2014) added support for block comments
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 #ifdef SCI_NAMESPACE
28 using namespace Scintilla;
29 #endif
30 
31 // Extended to accept accented characters
IsAWordChar(int ch)32 static inline bool IsAWordChar(int ch) {
33 	return ch >= 0x80 || isalnum(ch) || ch == '_';
34 }
35 
IsOperator(const int ch)36 static inline bool IsOperator(const int ch) {
37 	return (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '&' || ch == '|' || ch == '<' || ch == '>' || ch == '=');
38 }
39 
ColouriseKixDoc(Sci_PositionU startPos,Sci_Position length,int initStyle,WordList * keywordlists[],Accessor & styler)40 static void ColouriseKixDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
41                            WordList *keywordlists[], Accessor &styler) {
42 
43 	WordList &keywords = *keywordlists[0];
44 	WordList &keywords2 = *keywordlists[1];
45 	WordList &keywords3 = *keywordlists[2];
46 //	WordList &keywords4 = *keywordlists[3];
47 
48 	styler.StartAt(startPos);
49 
50 	StyleContext sc(startPos, length, initStyle, styler);
51 
52 	for (; sc.More(); sc.Forward()) {
53 
54 		if (sc.state == SCE_KIX_COMMENT) {
55 			if (sc.atLineEnd) {
56 				sc.SetState(SCE_KIX_DEFAULT);
57 			}
58 		} else if (sc.state == SCE_KIX_COMMENTSTREAM) {
59 			if (sc.ch == '/' && sc.chPrev == '*') {
60 				sc.ForwardSetState(SCE_KIX_DEFAULT);
61 			}
62 		} else if (sc.state == SCE_KIX_STRING1) {
63 			// This is a doubles quotes string
64 			if (sc.ch == '\"') {
65 				sc.ForwardSetState(SCE_KIX_DEFAULT);
66 			}
67 		} else if (sc.state == SCE_KIX_STRING2) {
68 			// This is a single quote string
69 			if (sc.ch == '\'') {
70 				sc.ForwardSetState(SCE_KIX_DEFAULT);
71 			}
72 		} else if (sc.state == SCE_KIX_NUMBER) {
73 			if (!IsADigit(sc.ch)) {
74 				sc.SetState(SCE_KIX_DEFAULT);
75 			}
76 		} else if (sc.state == SCE_KIX_VAR) {
77 			if (!IsAWordChar(sc.ch)) {
78 				sc.SetState(SCE_KIX_DEFAULT);
79 			}
80 		} else if (sc.state == SCE_KIX_MACRO) {
81 			if (!IsAWordChar(sc.ch) && !IsADigit(sc.ch)) {
82 				char s[100];
83 				sc.GetCurrentLowered(s, sizeof(s));
84 
85 				if (!keywords3.InList(&s[1])) {
86 					sc.ChangeState(SCE_KIX_DEFAULT);
87 				}
88 				sc.SetState(SCE_KIX_DEFAULT);
89 			}
90 		} else if (sc.state == SCE_KIX_OPERATOR) {
91 			if (!IsOperator(sc.ch)) {
92 				sc.SetState(SCE_KIX_DEFAULT);
93 			}
94 		} else if (sc.state == SCE_KIX_IDENTIFIER) {
95 			if (!IsAWordChar(sc.ch)) {
96 				char s[100];
97 				sc.GetCurrentLowered(s, sizeof(s));
98 
99 				if (keywords.InList(s)) {
100 					sc.ChangeState(SCE_KIX_KEYWORD);
101 				} else if (keywords2.InList(s)) {
102 					sc.ChangeState(SCE_KIX_FUNCTIONS);
103 				}
104 				sc.SetState(SCE_KIX_DEFAULT);
105 			}
106 		}
107 
108 		// Determine if a new state should be entered.
109 		if (sc.state == SCE_KIX_DEFAULT) {
110 			if (sc.ch == ';') {
111 				sc.SetState(SCE_KIX_COMMENT);
112 			} else if (sc.ch == '/' && sc.chNext == '*') {
113 				sc.SetState(SCE_KIX_COMMENTSTREAM);
114 			} else if (sc.ch == '\"') {
115 				sc.SetState(SCE_KIX_STRING1);
116 			} else if (sc.ch == '\'') {
117 				sc.SetState(SCE_KIX_STRING2);
118 			} else if (sc.ch == '$') {
119 				sc.SetState(SCE_KIX_VAR);
120 			} else if (sc.ch == '@') {
121 				sc.SetState(SCE_KIX_MACRO);
122 			} else if (IsADigit(sc.ch) || ((sc.ch == '.' || sc.ch == '&') && IsADigit(sc.chNext))) {
123 				sc.SetState(SCE_KIX_NUMBER);
124 			} else if (IsOperator(sc.ch)) {
125 				sc.SetState(SCE_KIX_OPERATOR);
126 			} else if (IsAWordChar(sc.ch)) {
127 				sc.SetState(SCE_KIX_IDENTIFIER);
128 			}
129 		}
130 	}
131 	sc.Complete();
132 }
133 
134 
135 LexerModule lmKix(SCLEX_KIX, ColouriseKixDoc, "kix");
136 
137