1 // Scintilla source code edit control
2 /** @file LexCsound.cxx
3  ** Lexer for Csound (Orchestra & Score)
4  ** Written by Georg Ritter - <ritterfuture A T gmail D O T com>
5  **/
6 // Copyright 1998-2003 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 <ctype.h>
12 #include <stdio.h>
13 #include <stdarg.h>
14 #include "Platform.h"
15 
16 #include "PropSet.h"
17 #include "Accessor.h"
18 #include "StyleContext.h"
19 #include "KeyWords.h"
20 #include "Scintilla.h"
21 #include "SciLexer.h"
22 
23 
IsAWordChar(const int ch)24 static inline bool IsAWordChar(const int ch) {
25 	return (ch < 0x80) && (isalnum(ch) || ch == '.' ||
26 		ch == '_' || ch == '?');
27 }
28 
IsAWordStart(const int ch)29 static inline bool IsAWordStart(const int ch) {
30 	return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '.' ||
31 		ch == '%' || ch == '@' || ch == '$' || ch == '?');
32 }
33 
IsCsoundOperator(char ch)34 static inline bool IsCsoundOperator(char ch) {
35 	if (isalnum(ch))
36 		return false;
37 	// '.' left out as it is used to make up numbers
38 	if (ch == '*' || ch == '/' || ch == '-' || ch == '+' ||
39 		ch == '(' || ch == ')' || ch == '=' || ch == '^' ||
40 		ch == '[' || ch == ']' || ch == '<' || ch == '&' ||
41 		ch == '>' || ch == ',' || ch == '|' || ch == '~' ||
42 		ch == '%' || ch == ':')
43 		return true;
44 	return false;
45 }
46 
ColouriseCsoundDoc(unsigned int startPos,int length,int initStyle,WordList * keywordlists[],Accessor & styler)47 static void ColouriseCsoundDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
48 				Accessor &styler) {
49 
50 	WordList &opcode = *keywordlists[0];
51 	WordList &headerStmt = *keywordlists[1];
52 	WordList &otherKeyword = *keywordlists[2];
53 
54 	// Do not leak onto next line
55 	if (initStyle == SCE_CSOUND_STRINGEOL)
56 		initStyle = SCE_CSOUND_DEFAULT;
57 
58 	StyleContext sc(startPos, length, initStyle, styler);
59 
60 	for (; sc.More(); sc.Forward())
61 	{
62 		// Handle line continuation generically.
63 		if (sc.ch == '\\') {
64 			if (sc.chNext == '\n' || sc.chNext == '\r') {
65 				sc.Forward();
66 				if (sc.ch == '\r' && sc.chNext == '\n') {
67 					sc.Forward();
68 				}
69 				continue;
70 			}
71 		}
72 
73 		// Determine if the current state should terminate.
74 		if (sc.state == SCE_CSOUND_OPERATOR) {
75 			if (!IsCsoundOperator(static_cast<char>(sc.ch))) {
76 			    sc.SetState(SCE_CSOUND_DEFAULT);
77 			}
78 		}else if (sc.state == SCE_CSOUND_NUMBER) {
79 			if (!IsAWordChar(sc.ch)) {
80 				sc.SetState(SCE_CSOUND_DEFAULT);
81 			}
82 		} else if (sc.state == SCE_CSOUND_IDENTIFIER) {
83 			if (!IsAWordChar(sc.ch) ) {
84 				char s[100];
85 				sc.GetCurrent(s, sizeof(s));
86 
87 				if (opcode.InList(s)) {
88 					sc.ChangeState(SCE_CSOUND_OPCODE);
89 				} else if (headerStmt.InList(s)) {
90 					sc.ChangeState(SCE_CSOUND_HEADERSTMT);
91 				} else if (otherKeyword.InList(s)) {
92 					sc.ChangeState(SCE_CSOUND_USERKEYWORD);
93 				} else if (s[0] == 'p') {
94 					sc.ChangeState(SCE_CSOUND_PARAM);
95 				} else if (s[0] == 'a') {
96 					sc.ChangeState(SCE_CSOUND_ARATE_VAR);
97 				} else if (s[0] == 'k') {
98 					sc.ChangeState(SCE_CSOUND_KRATE_VAR);
99 				} else if (s[0] == 'i') { // covers both i-rate variables and i-statements
100 					sc.ChangeState(SCE_CSOUND_IRATE_VAR);
101 				} else if (s[0] == 'g') {
102 					sc.ChangeState(SCE_CSOUND_GLOBAL_VAR);
103 				}
104 				sc.SetState(SCE_CSOUND_DEFAULT);
105 			}
106 		}
107 		else if (sc.state == SCE_CSOUND_COMMENT ) {
108 			if (sc.atLineEnd) {
109 				sc.SetState(SCE_CSOUND_DEFAULT);
110 			}
111 		}
112 		else if ((sc.state == SCE_CSOUND_ARATE_VAR) ||
113 			(sc.state == SCE_CSOUND_KRATE_VAR) ||
114 		(sc.state == SCE_CSOUND_IRATE_VAR)) {
115 			if (!IsAWordChar(sc.ch)) {
116 				sc.SetState(SCE_CSOUND_DEFAULT);
117 			}
118 		}
119 
120 		// Determine if a new state should be entered.
121 		if (sc.state == SCE_CSOUND_DEFAULT) {
122 			if (sc.ch == ';'){
123 				sc.SetState(SCE_CSOUND_COMMENT);
124 			} else if (isdigit(sc.ch) || (sc.ch == '.' && isdigit(sc.chNext))) {
125 				sc.SetState(SCE_CSOUND_NUMBER);
126 			} else if (IsAWordStart(sc.ch)) {
127 				sc.SetState(SCE_CSOUND_IDENTIFIER);
128 			} else if (IsCsoundOperator(static_cast<char>(sc.ch))) {
129 				sc.SetState(SCE_CSOUND_OPERATOR);
130 			} else if (sc.ch == 'p') {
131 				sc.SetState(SCE_CSOUND_PARAM);
132 			} else if (sc.ch == 'a') {
133 				sc.SetState(SCE_CSOUND_ARATE_VAR);
134 			} else if (sc.ch == 'k') {
135 				sc.SetState(SCE_CSOUND_KRATE_VAR);
136 			} else if (sc.ch == 'i') { // covers both i-rate variables and i-statements
137 				sc.SetState(SCE_CSOUND_IRATE_VAR);
138 			} else if (sc.ch == 'g') {
139 				sc.SetState(SCE_CSOUND_GLOBAL_VAR);
140 			}
141 		}
142 	}
143 	sc.Complete();
144 }
145 
FoldCsoundInstruments(unsigned int startPos,int length,int,WordList * [],Accessor & styler)146 static void FoldCsoundInstruments(unsigned int startPos, int length, int /* initStyle */, WordList *[],
147 		Accessor &styler) {
148 	unsigned int lengthDoc = startPos + length;
149 	int visibleChars = 0;
150 	int lineCurrent = styler.GetLine(startPos);
151 	int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
152 	int levelCurrent = levelPrev;
153 	char chNext = styler[startPos];
154 	int stylePrev = 0;
155 	int styleNext = styler.StyleAt(startPos);
156 	for (unsigned int i = startPos; i < lengthDoc; i++) {
157 		char ch = chNext;
158 		chNext = styler.SafeGetCharAt(i + 1);
159 		int style = styleNext;
160 		styleNext = styler.StyleAt(i + 1);
161 		bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
162 		if ((stylePrev != SCE_CSOUND_OPCODE) && (style == SCE_CSOUND_OPCODE)) {
163 			char s[20];
164 			unsigned int j = 0;
165 			while ((j < (sizeof(s) - 1)) && (iswordchar(styler[i + j]))) {
166 				s[j] = styler[i + j];
167 				j++;
168 			}
169 			s[j] = '\0';
170 
171 			if (strcmp(s, "instr") == 0)
172 				levelCurrent++;
173 			if (strcmp(s, "endin") == 0)
174 				levelCurrent--;
175 		}
176 
177 		if (atEOL) {
178 			int lev = levelPrev;
179 			if (visibleChars == 0)
180 				lev |= SC_FOLDLEVELWHITEFLAG;
181 			if ((levelCurrent > levelPrev) && (visibleChars > 0))
182 				lev |= SC_FOLDLEVELHEADERFLAG;
183 			if (lev != styler.LevelAt(lineCurrent)) {
184 				styler.SetLevel(lineCurrent, lev);
185 			}
186 			lineCurrent++;
187 			levelPrev = levelCurrent;
188 			visibleChars = 0;
189 		}
190 		if (!isspacechar(ch))
191 			visibleChars++;
192 		stylePrev = style;
193 	}
194 	// Fill in the real level of the next line, keeping the current flags as they will be filled in later
195 	int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
196 	styler.SetLevel(lineCurrent, levelPrev | flagsNext);
197 }
198 
199 
200 static const char * const csoundWordListDesc[] = {
201 	"Opcodes",
202 	"Header Statements",
203 	"User keywords",
204 	0
205 };
206 
207 LexerModule lmCsound(SCLEX_CSOUND, ColouriseCsoundDoc, "csound", FoldCsoundInstruments, csoundWordListDesc);
208