summaryrefslogtreecommitdiffstats
path: root/src/LexCsound.cpp
blob: 27f7b990a2595aade43c0c3475d3eed70fb2862b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Scintilla source code edit control
/** @file LexCsound.cxx
 ** Lexer for Csound (Orchestra & Score)
 ** Written by Georg Ritter - <ritterfuture A T gmail D O T com>
 **/
// Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>
#include "Platform.h"

#include "PropSet.h"
#include "Accessor.h"
#include "StyleContext.h"
#include "KeyWords.h"
#include "Scintilla.h"
#include "SciLexer.h"


static inline bool IsAWordChar(const int ch) {
	return (ch < 0x80) && (isalnum(ch) || ch == '.' ||
		ch == '_' || ch == '?');
}

static inline bool IsAWordStart(const int ch) {
	return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '.' ||
		ch == '%' || ch == '@' || ch == '$' || ch == '?');
}

static inline bool IsCsoundOperator(char ch) {
	if (isalnum(ch))
		return false;
	// '.' left out as it is used to make up numbers
	if (ch == '*' || ch == '/' || ch == '-' || ch == '+' ||
		ch == '(' || ch == ')' || ch == '=' || ch == '^' ||
		ch == '[' || ch == ']' || ch == '<' || ch == '&' ||
		ch == '>' || ch == ',' || ch == '|' || ch == '~' ||
		ch == '%' || ch == ':')
		return true;
	return false;
}

static void ColouriseCsoundDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
				Accessor &styler) {

	WordList &opcode = *keywordlists[0];
	WordList &headerStmt = *keywordlists[1];
	WordList &otherKeyword = *keywordlists[2];

	// Do not leak onto next line
	if (initStyle == SCE_CSOUND_STRINGEOL)
		initStyle = SCE_CSOUND_DEFAULT;

	StyleContext sc(startPos, length, initStyle, styler);

	for (; sc.More(); sc.Forward())
	{
		// Handle line continuation generically.
		if (sc.ch == '\\') {
			if (sc.chNext == '\n' || sc.chNext == '\r') {
				sc.Forward();
				if (sc.ch == '\r' && sc.chNext == '\n') {
					sc.Forward();
				}
				continue;
			}
		}
              
		// Determine if the current state should terminate.
		if (sc.state == SCE_CSOUND_OPERATOR) {
			if (!IsCsoundOperator(static_cast<char>(sc.ch))) {
			    sc.SetState(SCE_CSOUND_DEFAULT);
			}
		}else if (sc.state == SCE_CSOUND_NUMBER) {
			if (!IsAWordChar(sc.ch)) {
				sc.SetState(SCE_CSOUND_DEFAULT);
			}
		} else if (sc.state == SCE_CSOUND_IDENTIFIER) {
			if (!IsAWordChar(sc.ch) ) {
				char s[100];
				sc.GetCurrent(s, sizeof(s));

				if (opcode.InList(s)) {
					sc.ChangeState(SCE_CSOUND_OPCODE);
				} else if (headerStmt.InList(s)) {
					sc.ChangeState(SCE_CSOUND_HEADERSTMT);
				} else if (otherKeyword.InList(s)) {
					sc.ChangeState(SCE_CSOUND_USERKEYWORD);
				} else if (s[0] == 'p') {
					sc.ChangeState(SCE_CSOUND_PARAM);
				} else if (s[0] == 'a') {
					sc.ChangeState(SCE_CSOUND_ARATE_VAR);
				} else if (s[0] == 'k') {
					sc.ChangeState(SCE_CSOUND_KRATE_VAR);
				} else if (s[0] == 'i') { // covers both i-rate variables and i-statements
					sc.ChangeState(SCE_CSOUND_IRATE_VAR);
				} else if (s[0] == 'g') {
					sc.ChangeState(SCE_CSOUND_GLOBAL_VAR);
				}
				sc.SetState(SCE_CSOUND_DEFAULT);
			}
		}
		else if (sc.state == SCE_CSOUND_COMMENT ) {
			if (sc.atLineEnd) {
				sc.SetState(SCE_CSOUND_DEFAULT);
			}
		}
		else if ((sc.state == SCE_CSOUND_ARATE_VAR) ||
			(sc.state == SCE_CSOUND_KRATE_VAR) ||
		(sc.state == SCE_CSOUND_IRATE_VAR)) {
			if (!IsAWordChar(sc.ch)) {
				sc.SetState(SCE_CSOUND_DEFAULT);
			}
		}
		
		// Determine if a new state should be entered.
		if (sc.state == SCE_CSOUND_DEFAULT) {
			if (sc.ch == ';'){
				sc.SetState(SCE_CSOUND_COMMENT);
			} else if (isdigit(sc.ch) || (sc.ch == '.' && isdigit(sc.chNext))) {
				sc.SetState(SCE_CSOUND_NUMBER);
			} else if (IsAWordStart(sc.ch)) {
				sc.SetState(SCE_CSOUND_IDENTIFIER);
			} else if (IsCsoundOperator(static_cast<char>(sc.ch))) {
				sc.SetState(SCE_CSOUND_OPERATOR);
			} else if (sc.ch == 'p') {
				sc.SetState(SCE_CSOUND_PARAM);
			} else if (sc.ch == 'a') {
				sc.SetState(SCE_CSOUND_ARATE_VAR);
			} else if (sc.ch == 'k') {
				sc.SetState(SCE_CSOUND_KRATE_VAR);
			} else if (sc.ch == 'i') { // covers both i-rate variables and i-statements
				sc.SetState(SCE_CSOUND_IRATE_VAR);
			} else if (sc.ch == 'g') {
				sc.SetState(SCE_CSOUND_GLOBAL_VAR);
			}
		}
	}
	sc.Complete();
}

static void FoldCsoundInstruments(unsigned int startPos, int length, int /* initStyle */, WordList *[], 
		Accessor &styler) {
	unsigned int lengthDoc = startPos + length;
	int visibleChars = 0;
	int lineCurrent = styler.GetLine(startPos);
	int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
	int levelCurrent = levelPrev;
	char chNext = styler[startPos];
	int stylePrev = 0;
	int styleNext = styler.StyleAt(startPos);
	for (unsigned int i = startPos; i < lengthDoc; i++) {
		char ch = chNext;
		chNext = styler.SafeGetCharAt(i + 1);
		int style = styleNext;
		styleNext = styler.StyleAt(i + 1);
		bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
		if ((stylePrev != SCE_CSOUND_OPCODE) && (style == SCE_CSOUND_OPCODE)) {
			char s[20];
			unsigned int j = 0;
			while ((j < (sizeof(s) - 1)) && (iswordchar(styler[i + j]))) {
				s[j] = styler[i + j];
				j++;
			}
			s[j] = '\0';

			if (strcmp(s, "instr") == 0)
				levelCurrent++;
			if (strcmp(s, "endin") == 0)
				levelCurrent--;
		}

		if (atEOL) {
			int lev = levelPrev;
			if (visibleChars == 0)
				lev |= SC_FOLDLEVELWHITEFLAG;
			if ((levelCurrent > levelPrev) && (visibleChars > 0))
				lev |= SC_FOLDLEVELHEADERFLAG;
			if (lev != styler.LevelAt(lineCurrent)) {
				styler.SetLevel(lineCurrent, lev);
			}
			lineCurrent++;
			levelPrev = levelCurrent;
			visibleChars = 0;
		}
		if (!isspacechar(ch))
			visibleChars++;
		stylePrev = style;
	}
	// Fill in the real level of the next line, keeping the current flags as they will be filled in later
	int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
	styler.SetLevel(lineCurrent, levelPrev | flagsNext);
}


static const char * const csoundWordListDesc[] = {
	"Opcodes",
	"Header Statements",
	"User keywords",
	0
};

LexerModule lmCsound(SCLEX_CSOUND, ColouriseCsoundDoc, "csound", FoldCsoundInstruments, csoundWordListDesc);