-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathETTeXScanner.m
374 lines (361 loc) · 8.92 KB
/
ETTeXScanner.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#import "EtoileText.h"
static NSCharacterSet *CommandEndCharacterSet;
//NSLog(@"Passing %@", [aString substringWithRange: textRange]);
#define BUFFER_SIZE 64
#define PASS_TEXT() \
if (textRange.length > 0)\
{\
[delegate handleText: [aString substringWithRange: textRange]];\
textRange.length = 0;\
}\
textRange.location = idx + 1;
#define SET_INDEX(n) \
idx = n;\
i = idx - range.location - (NSUInteger)BUFFER_SIZE;\
if (i >= range.length)\
{\
range.location = idx + 1;\
}
@implementation ETTeXScanner
@synthesize delegate;
+ (void)initialize
{
if ([ETTeXScanner class] != self) { return; }
CommandEndCharacterSet =
[[NSCharacterSet characterSetWithCharactersInString: @"\n\t\\ {[]}"] retain];
}
- (void)parseString: (NSString*)aString
{
NSUInteger end = [aString length];
NSRange range = { 0, BUFFER_SIZE };
NSRange textRange = { 0, 0 };
NSUInteger idx = 0;
while (range.location < end)
{
unichar buffer[BUFFER_SIZE];
if (range.location + range.length > end)
{
range.length = end - range.location;
}
[aString getCharacters: buffer range: range];
//NSLog(@"Buffer: %@", [aString substringWithRange: range]);
range.location += BUFFER_SIZE;
for (NSUInteger i=0 ; i<range.length ; i++, idx++)
{
unichar c = buffer[i];
switch (c)
{
case '{':
PASS_TEXT();
[delegate beginArgument];
//NSLog(@"Text range: %@", NSStringFromRange(textRange));
break;
case '}':
PASS_TEXT();
[delegate endArgument];
break;
case '[':
PASS_TEXT();
[delegate beginOptArg];
break;
case ']':
PASS_TEXT();
[delegate endOptArg];
break;
case '\\':
if (idx != end)
{
// Special case for escaped control characters
switch ([aString characterAtIndex: idx+1])
{
case '\\':
case '_':
case '%':
{
PASS_TEXT();
// Skip the slash:
SET_INDEX(idx+1);
// We've already parsed the next character, so
// add it to the text range.
textRange.length = 1;
break;
}
default:
{
PASS_TEXT();
NSUInteger start = idx + 1;
// Find the end of the command
NSRange r =
[aString rangeOfCharacterFromSet: CommandEndCharacterSet
options: 0
range: NSMakeRange(start, end-start)];
// Command goes to the end of the input.
if (NSNotFound == r.location)
{
r.location = end - 1;
}
r.length = r.location - start;
r.location = start;
[delegate beginCommand: [aString substringWithRange: r]];
SET_INDEX(idx + r.length);
textRange.location = idx + 1;
//NSLog(@"Continuing from %d (%d) %c %@", idx, i, [aString characterAtIndex: idx], NSStringFromRange(textRange));
}
}
}
break;
case '-':
{
if (idx + 1 < end)
{
if ([aString characterAtIndex: idx+1] == '-')
{
PASS_TEXT();
unichar dash = 8211; // en
int skip = 1;
if (idx + 2 < end)
{
if ([aString characterAtIndex: idx+2] == '-')
{
skip = 2;
dash = 8212; // em
}
}
NSString *dashString = [NSString stringWithCharacters: &dash length: 1];
[delegate handleText: dashString];
SET_INDEX(idx + skip);
textRange.location = idx + 1;
break;
}
else
{
textRange.length++;
}
}
break;
}
case '\'':
{
PASS_TEXT();
unichar quote = 8217; // '
if (idx + 1 < end)
{
if ([aString characterAtIndex: idx+1] == '\'')
{
quote = 8221; // ''
SET_INDEX(idx + 1);
}
}
NSString *quoteString = [NSString stringWithCharacters: "e length: 1];
[delegate handleText: quoteString];
textRange.location = idx + 1;
break;
}
case '`':
{
PASS_TEXT();
unichar quote = 8216; // `
if (idx + 1 < end)
{
if ([aString characterAtIndex: idx+1] == '`')
{
quote = 8220; // `
SET_INDEX(idx + 1);
}
}
NSString *quoteString = [NSString stringWithCharacters: "e length: 1];
[delegate handleText: quoteString];
textRange.location = idx + 1;
break;
}
case '%':
{
PASS_TEXT();
NSRange lineEnd = [aString rangeOfCharacterFromSet: [NSCharacterSet newlineCharacterSet]
options: 0
range : NSMakeRange(idx, end-idx)];
SET_INDEX(lineEnd.location + lineEnd.length - 1);
textRange.location = idx;
break;
}
default:
textRange.length++;
}
}
}
PASS_TEXT();
}
@end
@implementation ETTeXParser
@synthesize parent, document, builder, scanner, root;
static NSDictionary *DefaultCommandHandlers;
+ (void)initialize
{
if ([ETTeXParser class] != self) { return; }
// Some standard command handlers.
DefaultCommandHandlers = [D(
[ETTeXSectionHandler class], @"part*",
[ETTeXSectionHandler class], @"chapter*",
[ETTeXSectionHandler class], @"section*",
[ETTeXSectionHandler class], @"subsection*",
[ETTeXSectionHandler class], @"subsubsection*",
[ETTeXSectionHandler class], @"paragraph*",
[ETTeXSectionHandler class], @"part",
[ETTeXSectionHandler class], @"chapter",
[ETTeXSectionHandler class], @"section",
[ETTeXSectionHandler class], @"subsection",
[ETTeXSectionHandler class], @"subsubsection",
[ETTeXSectionHandler class], @"paragraph",
[ETTeXNestableHandler class], @"ks",
[ETTeXNestableHandler class], @"file",
[ETTeXNestableHandler class], @"java",
[ETTeXNonNestedHandler class], @"cxx",
[ETTeXNonNestedHandler class], @"code",
[ETTeXNestableHandler class], @"note",
[ETTeXNestableHandler class], @"textit",
[ETTeXNestableHandler class], @"footnote",
[ETTeXLabelHandler class], @"label",
[ETTeXRefHandler class], @"ref",
[ETTeXRefHandler class], @"pageref",
[ETTeXEnvironmentHandler class], @"begin",
[ETTeXItemHandler class], @"item",
[ETTeXIndexHandler class], @"index") retain];
}
- (id)init
{
SUPERINIT;
commandHandlers = [DefaultCommandHandlers mutableCopy];
unknownTags = [NSMutableSet new];
builder = [ETTextTreeBuilder new];
document = [ETTextDocument new];
document.text = builder.textTree;
paragraphType =
[document typeFromDictionary: D(
ETTextParagraphType, kETTextStyleName)];
return self;
}
- (void)dealloc
{
[commandHandlers release];
[unknownTags release];
[builder release];
[scanner release];
[super dealloc];
}
- (void)registerDelegate: (Class)aClass forCommand: (NSString*)command
{
NSAssert([aClass conformsToProtocol: @protocol(ETTeXParsing)],
@"Handlers must conform to the ETTeXParsing protocol");
[commandHandlers setObject: aClass forKey: command];
}
- (void)beginCommand: (NSString*)aCommand
{
Class handler = [commandHandlers objectForKey: aCommand];
if (nil == handler)
{
if (![unknownTags containsObject: aCommand])
{
[unknownTags addObject: aCommand];
NSLog(@"No handler registered for: %@", aCommand);
NSLog(@"Currently using %@", scanner.delegate);
}
return;
}
id<ETTeXParsing> d = [[handler new] autorelease];
d.root = self;
d.scanner = scanner;
// Note: Not self, so that children can call this
d.parent = (id<ETTeXParsing>)scanner.delegate;
d.builder = builder;
d.document = document;
scanner.delegate = d;
[d beginCommand: aCommand];
}
- (void)beginOptArg {}
- (void)endOptArg {}
- (void)beginArgument {}
- (void)endArgument {}
- (void)handleText: (NSString*)aString
{
NSArray *paragraphs = [aString componentsSeparatedByString: @"\n\n"];
NSString *p0 = [paragraphs objectAtIndex: 0];
// If this segment starts with a blank line, end the existing paragraph and
// start a new one.
if ([@"" isEqualToString: p0])
{
[self beginParagraph];
}
else
{
[self addTextToParagraph: p0];
}
NSInteger c = [paragraphs count];
NSInteger i = 1;
while (i<c - 1)
{
NSString *p = [paragraphs objectAtIndex: i];
[self beginParagraph];
[self addTextToParagraph: p];
i++;
}
if (c-1 > 0)
{
p0 = [paragraphs objectAtIndex: c-1];
if ([@"" isEqualToString: p0])
{
[self beginParagraph];
}
else
{
[self beginParagraph];
[self addTextToParagraph: p0];
}
}
}
- (void)beginParagraph
{
if (isInParagraph)
{
[builder endNode];
}
[builder startNodeWithStyle: paragraphType];
isInParagraph = YES;
}
- (void)endParagraph
{
if (isInParagraph)
{
[builder endNode];
isInParagraph = NO;
}
}
- (void)addTextToParagraph: (NSString*)aString
{
if (!isInParagraph)
{
[self beginParagraph];
}
[builder appendString: aString];
}
@end
@implementation ETTeXHandler
@synthesize parent, document, builder, scanner, root;
- (void)beginCommand: (NSString*)aCommand
{
[root beginCommand: aCommand];
}
- (void)beginOptArg
{
[self.builder appendString: @"["];
}
- (void)endOptArg
{
[self.builder appendString: @"]"];
}
- (void)beginArgument {}
- (void)endArgument {}
- (void)handleText: (NSString*)aString
{
[root handleText: aString];
}
@end