-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathT2TSnippetConverterLexer.m
597 lines (496 loc) · 13.5 KB
/
T2TSnippetConverterLexer.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#line 1 "T2TSnippetConverterLexer.rl"
#import "T2TSnippetConverterLexer.h"
#import "T2TSnippetConverterParser.h"
#import "T2TSnippetConverterShared.h"
#line 9 "T2TSnippetConverterLexer.c"
static const char _TMSnippetToChocolatSnippet_actions[] = {
0, 1, 0, 1, 1, 1, 6, 1,
7, 1, 8, 1, 9, 1, 10, 1,
11, 1, 12, 1, 13, 1, 14, 1,
15, 1, 16, 1, 17, 1, 18, 1,
19, 1, 20, 1, 21, 2, 2, 3,
2, 2, 4, 2, 2, 5
};
static const char _TMSnippetToChocolatSnippet_cond_offsets[] = {
0, 0, 0, 0, 6, 11, 17, 23,
24, 24, 24, 24, 24, 24, 24, 29,
35
};
static const char _TMSnippetToChocolatSnippet_cond_lengths[] = {
0, 0, 0, 6, 5, 6, 6, 1,
0, 0, 0, 0, 0, 0, 5, 6,
3
};
static const short _TMSnippetToChocolatSnippet_cond_keys[] = {
-128, 46, 47, 47, 48, 91, 92, 92,
93, 124, 126, 127, -128, 46, 48, 91,
92, 92, 93, 124, 126, 127, -128, 46,
47, 47, 48, 91, 92, 92, 93, 124,
126, 127, -128, 46, 47, 47, 48, 91,
92, 92, 93, 124, 126, 127, 47, 47,
-128, 46, 48, 91, 92, 92, 93, 124,
126, 127, -128, 46, 47, 47, 48, 91,
92, 92, 93, 124, 126, 127, 48, 57,
65, 90, 97, 122, 0
};
static const char _TMSnippetToChocolatSnippet_cond_spaces[] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0
};
static const char _TMSnippetToChocolatSnippet_key_offsets[] = {
0, 0, 2, 4, 10, 17, 21, 25,
46, 47, 49, 50, 57, 60, 62, 69,
75
};
static const short _TMSnippetToChocolatSnippet_trans_keys[] = {
92, 96, 92, 96, 559, 604, 384, 636,
638, 639, 604, 384, 558, 560, 636, 638,
639, 384, 636, 638, 639, 384, 636, 638,
639, 36, 58, 92, 96, 125, 303, 559,
-128, 46, 48, 57, 59, 64, 65, 90,
91, 94, 95, 122, 123, 127, 123, 48,
57, 125, 95, 48, 57, 65, 90, 97,
122, 36, 92, 96, 92, 96, 604, 384,
558, 560, 636, 638, 639, 559, 604, 384,
636, 638, 639, 560, 569, 577, 602, 609,
634, 0
};
static const char _TMSnippetToChocolatSnippet_single_lengths[] = {
0, 2, 2, 2, 1, 0, 0, 7,
1, 0, 1, 1, 3, 2, 1, 2,
0
};
static const char _TMSnippetToChocolatSnippet_range_lengths[] = {
0, 0, 0, 2, 3, 2, 2, 7,
0, 1, 0, 3, 0, 0, 3, 2,
3
};
static const char _TMSnippetToChocolatSnippet_index_offsets[] = {
0, 0, 3, 6, 11, 16, 19, 22,
37, 39, 41, 43, 48, 52, 55, 60,
65
};
static const char _TMSnippetToChocolatSnippet_indicies[] = {
2, 3, 1, 2, 4, 1, 7, 8,
6, 6, 5, 10, 9, 9, 9, 5,
9, 9, 0, 6, 6, 5, 12, 15,
17, 18, 19, 11, 20, 11, 13, 11,
16, 11, 16, 11, 14, 22, 21, 13,
23, 25, 24, 16, 16, 16, 16, 26,
28, 29, 30, 27, 2, 3, 1, 8,
6, 6, 6, 27, 32, 10, 9, 9,
31, 32, 32, 32, 31, 0
};
static const char _TMSnippetToChocolatSnippet_trans_targs[] = {
7, 1, 2, 7, 13, 7, 3, 4,
6, 15, 5, 7, 8, 9, 0, 10,
11, 12, 13, 7, 14, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7,
16
};
static const char _TMSnippetToChocolatSnippet_trans_actions[] = {
35, 0, 0, 17, 40, 33, 0, 0,
0, 37, 0, 19, 0, 0, 0, 0,
0, 0, 43, 15, 43, 21, 11, 29,
23, 13, 27, 31, 5, 7, 9, 25,
0
};
static const char _TMSnippetToChocolatSnippet_to_state_actions[] = {
0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0,
0
};
static const char _TMSnippetToChocolatSnippet_from_state_actions[] = {
0, 0, 0, 0, 0, 0, 0, 3,
0, 0, 0, 0, 0, 0, 0, 0,
0
};
static const char _TMSnippetToChocolatSnippet_eof_trans[] = {
0, 1, 1, 6, 6, 1, 6, 0,
22, 24, 25, 27, 28, 1, 28, 32,
32
};
static const int TMSnippetToChocolatSnippet_start = 7;
static const int TMSnippetToChocolatSnippet_first_final = 7;
static const int TMSnippetToChocolatSnippet_error = 0;
static const int TMSnippetToChocolatSnippet_en_main = 7;
#line 8 "T2TSnippetConverterLexer.rl"
#line 35 "T2TSnippetConverterLexer.rl"
void emit_to_lemon(T2TSnippetToken token, void* lemon, NSMutableString *output)
{
T2TParse(lemon, token.type, token, output);
}
void emit_char(char c, void* lemon, NSMutableString *output)
{
T2TSnippetToken token;
token.type = CHAR;
token.payloadChar = c;
token.payloadStart = NULL;
token.payloadEnd = NULL;
emit_to_lemon(token, lemon, output);
}
void emit(int code, char* ts, char* te, void* lemon, NSMutableString *output)
{
T2TSnippetToken token;
token.type = code;
token.payloadChar = '\0';
token.payloadStart = ts;
token.payloadEnd = te;
emit_to_lemon(token, lemon, output);
}
void emit_shell(void* lemon, NSMutableString *output)
{
T2TSnippetToken token;
token.type = SHELL;
token.payloadChar = '\0';
token.payloadStart = NULL;
token.payloadEnd = NULL;
emit_to_lemon(token, lemon, output);
}
void emit_regex(void* lemon, NSMutableString *output)
{
T2TSnippetToken token;
token.type = REGEXY;
token.payloadChar = '\0';
token.payloadStart = NULL;
token.payloadEnd = NULL;
emit_to_lemon(token, lemon, output);
}
NSString *T2TConvertTextMateSnippetToChocolat(NSString *tmSnippet)
{
NSLog(@"Snippet: %@", tmSnippet);
//Get string data
char* data = (char *)[tmSnippet UTF8String];
//Set up lemon
void* lemon = T2TParseAlloc(malloc);
//Output
NSMutableString *output = [[NSMutableString alloc] initWithCapacity:[tmSnippet length]];
//Start
char *p = data;
//End
char *pe = p + strlen(p);
char *eof = pe;
//State
int cs = 0, act, have = 0, curline = 1;
char *ts, *te = 0;
int done = 0;
int nesting = 0;
//Run the machine
#line 231 "T2TSnippetConverterLexer.c"
{
cs = TMSnippetToChocolatSnippet_start;
ts = 0;
te = 0;
act = 0;
}
#line 118 "T2TSnippetConverterLexer.rl"
#line 241 "T2TSnippetConverterLexer.c"
{
int _klen;
unsigned int _trans;
short _widec;
const char *_acts;
unsigned int _nacts;
const short *_keys;
if ( p == pe )
goto _test_eof;
if ( cs == 0 )
goto _out;
_resume:
_acts = _TMSnippetToChocolatSnippet_actions + _TMSnippetToChocolatSnippet_from_state_actions[cs];
_nacts = (unsigned int) *_acts++;
while ( _nacts-- > 0 ) {
switch ( *_acts++ ) {
case 1:
#line 1 "NONE"
{ts = p;}
break;
#line 263 "T2TSnippetConverterLexer.c"
}
}
_widec = (*p);
_klen = _TMSnippetToChocolatSnippet_cond_lengths[cs];
_keys = _TMSnippetToChocolatSnippet_cond_keys + (_TMSnippetToChocolatSnippet_cond_offsets[cs]*2);
if ( _klen > 0 ) {
const short *_lower = _keys;
const short *_mid;
const short *_upper = _keys + (_klen<<1) - 2;
while (1) {
if ( _upper < _lower )
break;
_mid = _lower + (((_upper-_lower) >> 1) & ~1);
if ( _widec < _mid[0] )
_upper = _mid - 2;
else if ( _widec > _mid[1] )
_lower = _mid + 2;
else {
switch ( _TMSnippetToChocolatSnippet_cond_spaces[_TMSnippetToChocolatSnippet_cond_offsets[cs] + ((_mid - _keys)>>1)] ) {
case 0: {
_widec = (short)(128 + ((*p) - -128));
if (
#line 14 "T2TSnippetConverterLexer.rl"
nesting > 0 ) _widec += 256;
break;
}
}
break;
}
}
}
_keys = _TMSnippetToChocolatSnippet_trans_keys + _TMSnippetToChocolatSnippet_key_offsets[cs];
_trans = _TMSnippetToChocolatSnippet_index_offsets[cs];
_klen = _TMSnippetToChocolatSnippet_single_lengths[cs];
if ( _klen > 0 ) {
const short *_lower = _keys;
const short *_mid;
const short *_upper = _keys + _klen - 1;
while (1) {
if ( _upper < _lower )
break;
_mid = _lower + ((_upper-_lower) >> 1);
if ( _widec < *_mid )
_upper = _mid - 1;
else if ( _widec > *_mid )
_lower = _mid + 1;
else {
_trans += (_mid - _keys);
goto _match;
}
}
_keys += _klen;
_trans += _klen;
}
_klen = _TMSnippetToChocolatSnippet_range_lengths[cs];
if ( _klen > 0 ) {
const short *_lower = _keys;
const short *_mid;
const short *_upper = _keys + (_klen<<1) - 2;
while (1) {
if ( _upper < _lower )
break;
_mid = _lower + (((_upper-_lower) >> 1) & ~1);
if ( _widec < _mid[0] )
_upper = _mid - 2;
else if ( _widec > _mid[1] )
_lower = _mid + 2;
else {
_trans += ((_mid - _keys)>>1);
goto _match;
}
}
_trans += _klen;
}
_match:
_trans = _TMSnippetToChocolatSnippet_indicies[_trans];
_eof_trans:
cs = _TMSnippetToChocolatSnippet_trans_targs[_trans];
if ( _TMSnippetToChocolatSnippet_trans_actions[_trans] == 0 )
goto _again;
_acts = _TMSnippetToChocolatSnippet_actions + _TMSnippetToChocolatSnippet_trans_actions[_trans];
_nacts = (unsigned int) *_acts++;
while ( _nacts-- > 0 )
{
switch ( *_acts++ )
{
case 2:
#line 1 "NONE"
{te = p+1;}
break;
case 3:
#line 26 "T2TSnippetConverterLexer.rl"
{act = 9;}
break;
case 4:
#line 31 "T2TSnippetConverterLexer.rl"
{act = 12;}
break;
case 5:
#line 33 "T2TSnippetConverterLexer.rl"
{act = 13;}
break;
case 6:
#line 17 "T2TSnippetConverterLexer.rl"
{te = p+1;{ emit_char('$', lemon, output); }}
break;
case 7:
#line 18 "T2TSnippetConverterLexer.rl"
{te = p+1;{ emit_char('\\', lemon, output); }}
break;
case 8:
#line 19 "T2TSnippetConverterLexer.rl"
{te = p+1;{ emit_char('`', lemon, output); }}
break;
case 9:
#line 21 "T2TSnippetConverterLexer.rl"
{te = p+1;{ emit(DOLLAR_CURLY, ts, te, lemon, output); nesting++; }}
break;
case 10:
#line 24 "T2TSnippetConverterLexer.rl"
{te = p+1;{ emit(CURLY, ts, te, lemon, output); if (nesting > 0) nesting--; }}
break;
case 11:
#line 25 "T2TSnippetConverterLexer.rl"
{te = p+1;{ emit(CURLY, ts, te, lemon, output); if (nesting > 0) nesting--; }}
break;
case 12:
#line 31 "T2TSnippetConverterLexer.rl"
{te = p+1;{ emit(SHELL, ts, te, lemon, output); }}
break;
case 13:
#line 33 "T2TSnippetConverterLexer.rl"
{te = p+1;{ emit_char((*p), lemon, output); }}
break;
case 14:
#line 22 "T2TSnippetConverterLexer.rl"
{te = p;p--;{ emit(DOLLAR, ts, te, lemon, output); }}
break;
case 15:
#line 23 "T2TSnippetConverterLexer.rl"
{te = p;p--;{ emit(COLON, ts, te, lemon, output); }}
break;
case 16:
#line 26 "T2TSnippetConverterLexer.rl"
{te = p;p--;{ emit(REGEXY, ts, te, lemon, output); }}
break;
case 17:
#line 28 "T2TSnippetConverterLexer.rl"
{te = p;p--;{ emit(IDENTIFIER, ts, te, lemon, output); }}
break;
case 18:
#line 29 "T2TSnippetConverterLexer.rl"
{te = p;p--;{ emit(NUMERIC, ts, te, lemon, output); }}
break;
case 19:
#line 33 "T2TSnippetConverterLexer.rl"
{te = p;p--;{ emit_char((*p), lemon, output); }}
break;
case 20:
#line 33 "T2TSnippetConverterLexer.rl"
{{p = ((te))-1;}{ emit_char((*p), lemon, output); }}
break;
case 21:
#line 1 "NONE"
{ switch( act ) {
case 9:
{{p = ((te))-1;} emit(REGEXY, ts, te, lemon, output); }
break;
case 12:
{{p = ((te))-1;} emit(SHELL, ts, te, lemon, output); }
break;
case 13:
{{p = ((te))-1;} emit_char((*p), lemon, output); }
break;
}
}
break;
#line 451 "T2TSnippetConverterLexer.c"
}
}
_again:
_acts = _TMSnippetToChocolatSnippet_actions + _TMSnippetToChocolatSnippet_to_state_actions[cs];
_nacts = (unsigned int) *_acts++;
while ( _nacts-- > 0 ) {
switch ( *_acts++ ) {
case 0:
#line 1 "NONE"
{ts = 0;}
break;
#line 464 "T2TSnippetConverterLexer.c"
}
}
if ( cs == 0 )
goto _out;
if ( ++p != pe )
goto _resume;
_test_eof: {}
if ( p == eof )
{
if ( _TMSnippetToChocolatSnippet_eof_trans[cs] > 0 ) {
_trans = _TMSnippetToChocolatSnippet_eof_trans[cs] - 1;
goto _eof_trans;
}
}
_out: {}
}
#line 119 "T2TSnippetConverterLexer.rl"
//End lemon
T2TSnippetToken token;
T2TParse(lemon, 0, token, output);
T2TParseFree(lemon, free);
//Return the output
NSLog(@"Output: %@", output);
NSLog(@"-----");
return output;
}
NSString* T2TEscapeSnippetString(NSString *literal)
{
if (![literal length])
return literal;
NSUInteger len = [literal length];
unichar* cs = (unichar*)malloc(sizeof(unichar) * (len + 1));
[literal getCharacters:cs range:NSMakeRange(0, len)];
NSMutableString *outputLiteral = [literal mutableCopy];
NSUInteger i = 0;
BOOL isInEmbeddedMode = NO;
NSInteger bracketNestingLevel = 0;
for (; i < len; i++)
{
if (cs[i] == '{')
bracketNestingLevel++;
else if (cs[i] == '}' && bracketNestingLevel > 0)
bracketNestingLevel--;
if (!isInEmbeddedMode && cs[i] == '$' && (i + 1) < len && cs[i + 1] == '{')
{
isInEmbeddedMode = YES;
i++;
}
else if (isInEmbeddedMode && bracketNestingLevel == 0 && cs[i] == '}')
{
isInEmbeddedMode = NO;
}
else
{
NSString *substring = [literal substringWithRange:NSMakeRange(i, 1)];
if (!isInEmbeddedMode && (cs[i] == '\\' || cs[i] == '\"'))
substring = [@"\\" stringByAppendingString:substring];
[outputLiteral appendString:substring];
}
}
free(cs);
return outputLiteral;
}
/*
# <description>
## <textmate-syntax> -> <chocolat-syntax>
# An escape sequence
escape_sequence = backslash_escape | dollar_escape | backtick_escape;
backslash_escape = "\\\\" @{ output("\\"); };
dollar_escape = "\\$" @{ output("$"); };
backtick_escape = "\\`" @{ output("%"); };
# An identifier for a variable
## TM_SELECTED_TEXT -> TM_SELECTED_TEXT
variable_identifier = (alpha | '_') (alnum | '_') +;
# A tabstop is a dollar sign followed by a digit
any_tabstop = inline_tabstop | block_tabstop;
## $42 -> {42}
inline_tabstop = "$" digit+;
## ${42} -> {42}
block_tabstop = "${" digit+ misc_stuff"}";
any_variable = inline_Variable | block_variable;
## $TM_SELECTED_TEXT -> {TM_SELECTED_TEXT}
inline_variable = "$" variable_identifier;
## ${TM_SELECTED_TEXT} -> {TM_SELECTED_TEXT}
block_variable = "${" variable_identifier misc_stuff "}";
misc_stuff = filter | default_condition;
filter = "/" regex "/" regex_replacement ("/" regex_options)?;
default_condition = ":" (tabstop | variable | escape_sequence | other);
other := any @{ output_char(fc) };
main := |*
any_tabstop => { };
any_variable => { };
tabstop | variable | escape_sequence | other;
*|;
*/