forked from erkyrath/Inform7-IDE-Mac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIFNaturalHighlighter.m
289 lines (236 loc) · 7.65 KB
/
IFNaturalHighlighter.m
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//
// IFNaturalHighlighter.m
// Inform
//
// Created by Andrew Hunter on 18/11/2004.
// Copyright 2004 Andrew Hunter. All rights reserved.
//
#import "IFNaturalHighlighter.h"
#import "IFProjectPane.h"
#import "IFPreferences.h"
@implementation IFNaturalHighlighter
// = Initialisation =
- (id) init {
self = [super init];
if (self) {
inform6Highlighter = [[IFInform6Highlighter alloc] init];
}
return self;
}
- (void) dealloc {
[activeStorage release];
[inform6Highlighter release];
[super dealloc];
}
// = Notifying of the highlighter currently in use =
- (void) setSyntaxStorage: (IFSyntaxStorage*) storage {
[activeStorage release];
activeStorage = [storage retain];
[inform6Highlighter setSyntaxStorage: storage];
}
// = The highlighter itself =
- (IFSyntaxState) stateForCharacter: (unichar) chr
afterState: (IFSyntaxState) lastState {
switch ([activeStorage highlighterMode]) {
case IFNaturalModeStandard:
// Natural Inform mode
switch (lastState) {
case IFNaturalStateMaybeInform6:
if (chr == '-') {
// Switch to Inform 6 mode
[activeStorage pushState];
[activeStorage setHighlighterMode: IFNaturalModeInform6];
if ([activeStorage preceededByKeyword: @"Include"
offset: 1]) {
// Is top-level Inform 6
return IFSyntaxStateDefault;
} else {
// Is as if preceeded by '[T;'
IFSyntaxState newState = [inform6Highlighter stateForCharacter: '['
afterState: IFSyntaxStateDefault];
newState = [inform6Highlighter stateForCharacter: 'T'
afterState: newState];
newState = [inform6Highlighter stateForCharacter: ';'
afterState: newState];
return newState;
}
}
case IFNaturalStateBlankLine:
if (chr == ' ' || chr == '\n' || chr == '\t' || chr == '\r')
return IFNaturalStateBlankLine;
// ObRAIF: here is why fall-through cases are a *good* thing
// (Of course, first time I tried this, I got these conditions the wrong way round... Mmm, bug)
case IFNaturalStateSpace:
if (chr == '"')
return IFNaturalStateQuote;
case IFNaturalStateText:
// Just plain ole text
if (chr == '[') {
[activeStorage pushState];
return IFNaturalStateComment;
}
if (chr == '\n' || chr == '\r')
return IFNaturalStateBlankLine;
if (chr == '(')
return IFNaturalStateMaybeInform6;
if (chr == ' ' || chr == '\t' || chr == '.' || chr == ')' || chr == '}' || chr == ';'
|| chr == ':' || chr == ',' || chr == '?' || chr == '!' || chr == '{')
return IFNaturalStateSpace;
return IFNaturalStateText;
case IFNaturalStateTitle:
// The title
if (chr == '\n' || chr == '\r')
return IFNaturalStateBlankLine;
if (chr == '"')
return IFNaturalStateTitleQuote;
return IFNaturalStateTitle;
case IFNaturalStateComment:
if (chr == '[') {
[activeStorage pushState];
return IFNaturalStateComment;
}
if (chr == ']') {
return [activeStorage popState];
}
return IFNaturalStateComment;
case IFNaturalStateSubstitution:
if (chr == ']')
return IFNaturalStateQuote;
case IFNaturalStateQuote:
if (chr == '"')
return IFNaturalStateText;
if (chr == '[')
return IFNaturalStateSubstitution;
return lastState;
case IFNaturalStateTitleQuote:
if (chr == '"')
return IFNaturalStateTitle;
return lastState;
case IFNaturalStateHeading:
if (chr == '\n' || chr == '\r')
return IFNaturalStateBlankLine;
return IFNaturalStateHeading;
default:
// Unknown state
return IFNaturalStateText;
}
break;
// Various Inform 6 modes
case IFNaturalModeInform6MightEnd:
if (chr == ')') {
// Switch back to standard mode
[activeStorage popState];
return IFNaturalStateText;
}
// Switch back to Inform 6 mode
[activeStorage setHighlighterMode: IFNaturalModeInform6];
case IFNaturalModeInform6:
if (chr == '-') {
// Next character might break us out of Inform 6 mode
[activeStorage setHighlighterMode: IFNaturalModeInform6MightEnd];
}
// Run the Inform 6 highlighter
return [inform6Highlighter stateForCharacter: chr
afterState: lastState];
}
return IFNaturalStateText;
}
- (IFSyntaxStyle) styleForCharacter: (unichar) chr
nextState: (IFSyntaxState) nextState
lastState: (IFSyntaxState) lastState {
switch ([activeStorage highlighterMode]) {
// Natural Inform styles
case IFNaturalModeStandard:
// Some states override the next state
if (lastState == IFNaturalStateComment) return IFSyntaxComment;
if (lastState == IFNaturalStateQuote && nextState != IFNaturalStateTitleQuote)
return IFSyntaxGameText;
if (lastState == IFNaturalStateTitleQuote) return IFSyntaxTitle;
if (lastState == IFNaturalStateHeading) return IFSyntaxHeading;
if (lastState == IFNaturalStateSubstitution) return IFSyntaxSubstitution;
switch (nextState) {
case IFNaturalStateText:
case IFNaturalStateBlankLine:
if (nextState == IFNaturalStateComment) return IFSyntaxComment;
if (nextState == IFNaturalStateQuote) return IFSyntaxGameText;
return IFSyntaxNaturalInform;
case IFNaturalStateTitle:
return IFSyntaxNaturalInform;
case IFNaturalStateTitleQuote:
return IFSyntaxTitle;
case IFNaturalStateSubstitution:
return IFSyntaxSubstitution;
case IFNaturalStateQuote:
return IFSyntaxGameText;
case IFNaturalStateComment:
return IFSyntaxComment;
case IFNaturalStateHeading:
return IFSyntaxHeading;
default:
return IFSyntaxNone;
}
break;
// Inform 6 styles
case IFNaturalModeInform6MightEnd:
case IFNaturalModeInform6:
return [inform6Highlighter styleForCharacter: chr
nextState: nextState
lastState: lastState] + 0x20;
break;
}
return IFSyntaxNone;
}
static BOOL IsInform6Style(IFSyntaxStyle style) {
if (style >= 0x20 && style <= 0x40)
return YES;
else
return NO;
}
- (void) rehintLine: (NSString*) line
styles: (IFSyntaxStyle*) styles
initialState: (IFSyntaxState) initialState {
NSString* thisLine = [line lowercaseString];
// This line might be a header line
BOOL isHeader = NO;
if (initialState == IFNaturalStateBlankLine && !IsInform6Style(styles[0])) {
if ([thisLine hasPrefix: @"volume "]) isHeader = YES;
if ([thisLine hasPrefix: @"book "]) isHeader = YES;
if ([thisLine hasPrefix: @"part "]) isHeader = YES;
if ([thisLine hasPrefix: @"chapter "]) isHeader = YES;
if ([thisLine hasPrefix: @"section "]) isHeader = YES;
}
if (isHeader) {
int x;
for (x=0; x<[line length]; x++) {
styles[x] = IFSyntaxHeading;
}
}
// This line might have some Inform 6 highlighting to do
int x;
for (x=0; x<[line length]; x++) {
if (IsInform6Style(styles[x])) {
// Found an Inform 6 region
NSRange thisRegion;
thisRegion.location = x;
thisRegion.length = 0;
// Convert to standard style, work out how long the region is
for (;x<[line length] && IsInform6Style(styles[x]);x++) {
thisRegion.length++;
styles[x] -= 0x20;
}
// Get the Inform 6 highlighter to rehint this section
[inform6Highlighter rehintLine: [line substringWithRange: thisRegion]
styles: styles+thisRegion.location
initialState: IFSyntaxStateDefault];
}
}
}
// = Styles =
- (NSDictionary*) attributesForStyle: (IFSyntaxStyle) style {
return [IFProjectPane attributeForStyle: style];
}
- (float) tabStopWidth {
return [[IFPreferences sharedPreferences] tabWidth];
//return 56.0;
}
@end