-
Notifications
You must be signed in to change notification settings - Fork 1
/
CHSnippetParserLexer.rl
229 lines (164 loc) · 5.14 KB
/
CHSnippetParserLexer.rl
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
#import "T2TSnippetConverterLexer.h"
#import "T2TSnippetConverterParser.h"
#import "T2TSnippetConverterShared.h"
%%{
machine TMSnippetToChocolatSnippet;
write data;
}%%
%%{
regex_string = ([^/\\] | "\\\\" | "\\/" | "\\" [^/\\])+;
regex = "/" regex_string "/" regex_string ("/" alnum*)?;
main := |*
"\\$" { emit_char('$', lemon, output); };
"\\\\" { emit_char('\\', lemon, output); };
"\\`" { emit_char('`', lemon, output); };
"${" { emit(DOLLAR_CURLY, ts, te, lemon, output); };
"$" { emit(DOLLAR, ts, te, lemon, output); };
":" { emit(COLON, ts, te, lemon, output); };
":}" { emit(CURLY, ts, te, lemon, output); };
"}" { emit(CURLY, ts, te, lemon, output); };
regex { emit(REGEX, ts, te, lemon, output); };
(alpha | '_') (alnum | '_')* { emit(IDENTIFIER, ts, te, lemon, output); };
digit+ { emit(NUMERIC, ts, te, lemon, output); };
"`" ([^`] | "\\`")* "`" { emit(SHELL, ts, te, lemon, output); };
any { emit_char(fc, lemon, output); };
*|;
}%%
void emit_to_lemon(T2TSnippetToken token, void* lemon, NSMutableString *output)
{
Parse(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 = REGEX;
token.payloadChar = '\0';
token.payloadStart = NULL;
token.payloadEnd = NULL;
emit_to_lemon(token, lemon, output);
}
NSString *T2TConvertTextMateSnippetToChocolat(NSString *tmSnippet)
{
//Get string data
char* data = (char *)[tmSnippet UTF8String];
//Set up lemon
void* lemon = ParseAlloc(malloc);
//Output
NSMutableString *output = [[NSMutableString alloc] initWithCapacity:[tmSnippet length]];
//Start
char *p = data;
//End
char *pe = p + strlen(p);
char *eof = pe;
NSLog(@"p -> pe = %d -> %d = %s", p, pe, p);
//State
int cs = 0, act, have = 0, curline = 1;
char *ts, *te = 0;
int done = 0;
//Run the machine
%% write init;
%% write exec;
//End lemon
T2TSnippetToken token;
Parse(lemon, 0, token, output);
ParseFree(lemon, free);
NSLog(@"output %d string = %@", cs, output);
//Return the output
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;
*|;
*/