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