Skip to content

Commit debe092

Browse files
author
Viktor Chernev
committed
finished preprocessor for v1.0
1 parent 95c2c7a commit debe092

File tree

1 file changed

+173
-101
lines changed

1 file changed

+173
-101
lines changed

@DescribeCompilerAPI/Compiler/Preprocessors/PreprocessorForDescribe10.cs

Lines changed: 173 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public string ProcessSource(string value)
1919
//in order to skip if tests to see if we are not on the first or last char
2020
//for null reference. The new line we keep, as it is a workaround for the
2121
//runaway group last comment bug
22-
value = "." + value + Environment.NewLine;
22+
value = "." + value + Environment.NewLine + Environment.NewLine;
2323

2424
StringBuilder sb = new StringBuilder();
25-
for (int i = 1; i < value.Length - 1; i++)
25+
for (int i = 1; i < value.Length - 2; i++)
2626
{
2727
if (value[i] > 127) processNonLatin(value, sb, ref i);
2828
else if (value[i] == '-') processHyphen(value, sb, ref i);
@@ -55,11 +55,11 @@ void ReadDirectives(string value)
5555
text = RemoveWhitespace(text);
5656
if(text.StartsWith("directives->") == false) return;
5757

58-
string[] directives = text.Substring(12).Split(',');
58+
string[] directives = text.Substring(12).TrimStart('>').Split(',');
5959
foreach (string directive in directives)
6060
{
6161
string[] sep = directive.Split('<');
62-
if (sep[0] == "delimiter-mode") ReadDeliiterMode(sep[1]);
62+
if (sep[0] == "delimiter-mode") ReadDelimiterMode(sep[sep.Length - 1]);
6363
}
6464
}
6565
catch { }
@@ -68,12 +68,12 @@ void ReadDirectives(string value)
6868
// delimiter-mode <bi>,
6969
// namespace <treeofall.public>;
7070
}
71-
void ReadDeliiterMode(string value)
71+
void ReadDelimiterMode(string value)
7272
{
73-
if(value == "bi>") BiLimited = true;
74-
else if (value == "mono>") BiLimited = false;
75-
else if (value == "2>") BiLimited = true;
76-
else if (value == "1>") BiLimited = false;
73+
if(value.StartsWith("bi>")) BiLimited = true;
74+
else if (value.StartsWith("mono>")) BiLimited = false;
75+
else if (value.StartsWith("2>")) BiLimited = true;
76+
else if (value.StartsWith("1>")) BiLimited = false;
7777
}
7878
string RemoveWhitespace(string input)
7979
{
@@ -163,129 +163,201 @@ void processBSlash(string source, StringBuilder sb, ref int i)
163163
// start BiLimited
164164
void processSquareBracketLeft(string source, StringBuilder sb, ref int i)
165165
{
166-
// source[i] == '[' here.
167-
// When we find '[' we will either escape it or double it or do nothing.
168-
// If the symbol before and the symbol after is not also '[' or an
169-
// escape sequence, we escape it. Otherwise, we do nothing.
170-
171-
if ((source[i - 1] != '[' || isEscaped(source, i - 1))
172-
&& source[i + 1] != '['
173-
&& isEscaped(source, i) == false)
166+
if (BiLimited)
174167
{
175-
// if we have an alone symbol, and we are using double symbols (BiLimited)
176-
// then we escape it, otherwise we double it
177-
if (BiLimited) sb.Append('\\');
178-
else sb.Append('[');
168+
// source[i] == '[' here.
169+
// When we find '[' we will either escape it or double it or do nothing.
170+
// If the symbol before and the symbol after is not also '[' or an
171+
// escape sequence, we escape it. Otherwise, we do nothing.
172+
173+
if ((source[i - 1] != '[' || isEscaped(source, i - 1))
174+
&& source[i + 1] != '['
175+
&& isEscaped(source, i) == false)
176+
{
177+
// if we have an alone symbol, and we are using double symbols (BiLimited)
178+
// then we escape it, otherwise we double it
179+
sb.Append('\\');
180+
}
181+
sb.Append(source[i]);
182+
}
183+
else
184+
{
185+
if (isEscaped(source, i) == false)
186+
{
187+
sb.Append('[');
188+
}
189+
sb.Append(source[i]);
179190
}
180-
sb.Append(source[i]);
181191
}
182192
void processSquareBracketRight(string source, StringBuilder sb, ref int i)
183193
{
184-
// source[i] == ']' here.
185-
// When we find ']' we will either escape it or double it or do nothing.
186-
// If the symbol before and the symbol after is not also ']' or an
187-
// escape sequence, we escape it. Otherwise, we do nothing.
188-
189-
if ((source[i - 1] != ']' || isEscaped(source, i - 1))
190-
&& source[i + 1] != ']'
191-
&& isEscaped(source, i) == false)
194+
if (BiLimited)
192195
{
193-
// if we have an alone symbol, and we are using double symbols (BiLimited)
194-
// then we escape it, otherwise we double it
195-
if (BiLimited) sb.Append('\\');
196-
else sb.Append(']');
196+
// source[i] == ']' here.
197+
// When we find ']' we will either escape it or double it or do nothing.
198+
// If the symbol before and the symbol after is not also ']' or an
199+
// escape sequence, we escape it. Otherwise, we do nothing.
200+
201+
if ((source[i - 1] != ']' || isEscaped(source, i - 1))
202+
&& source[i + 1] != ']'
203+
&& isEscaped(source, i) == false)
204+
{
205+
// if we have an alone symbol, and we are using double symbols (BiLimited)
206+
// then we escape it, otherwise we double it
207+
sb.Append('\\');
208+
}
209+
sb.Append(source[i]);
210+
}
211+
else
212+
{
213+
if (isEscaped(source, i) == false)
214+
{
215+
sb.Append(']');
216+
}
217+
sb.Append(source[i]);
197218
}
198-
sb.Append(source[i]);
199219
}
200220
void processCurlyBracketLeft(string source, StringBuilder sb, ref int i)
201221
{
202-
// source[i] == '{' here.
203-
// When we find '{' we will either escape it or double it or do nothing.
204-
// If the symbol before and the symbol after is not also '{' or an
205-
// escape sequence, we escape it. Otherwise, we do nothing.
206-
207-
if ((source[i - 1] != '{' || isEscaped(source, i - 1))
208-
&& source[i + 1] != '{'
209-
&& isEscaped(source, i) == false)
222+
if (BiLimited)
210223
{
211-
// if we have an alone symbol, and we are using double symbols (BiLimited)
212-
// then we escape it, otherwise we double it
213-
if (BiLimited) sb.Append('\\');
214-
else sb.Append('{');
224+
// source[i] == '{' here.
225+
// When we find '{' we will either escape it or double it or do nothing.
226+
// If the symbol before and the symbol after is not also '{' or an
227+
// escape sequence, we escape it. Otherwise, we do nothing.
228+
229+
if ((source[i - 1] != '{' || isEscaped(source, i - 1))
230+
&& source[i + 1] != '{'
231+
&& isEscaped(source, i) == false)
232+
{
233+
// if we have an alone symbol, and we are using double symbols (BiLimited)
234+
// then we escape it, otherwise we double it
235+
sb.Append('\\');
236+
}
237+
sb.Append(source[i]);
238+
}
239+
else
240+
{
241+
if (isEscaped(source, i) == false)
242+
{
243+
sb.Append('{');
244+
}
245+
sb.Append(source[i]);
215246
}
216-
sb.Append(source[i]);
217247
}
218248
void processCurlyBracketRight(string source, StringBuilder sb, ref int i)
219249
{
220-
// source[i] == '}' here.
221-
// When we find '}' we will either escape it or double it or do nothing.
222-
// If the symbol before and the symbol after is not also '}' or an
223-
// escape sequence, we escape it. Otherwise, we do nothing.
250+
if (BiLimited)
251+
{
252+
// source[i] == '}' here.
253+
// When we find '}' we will either escape it or double it or do nothing.
254+
// If the symbol before and the symbol after is not also '}' or an
255+
// escape sequence, we escape it. Otherwise, we do nothing.
224256

225-
if ((source[i - 1] != '}' || isEscaped(source, i - 1))
257+
if ((source[i - 1] != '}' || isEscaped(source, i - 1))
226258
&& source[i + 1] != '}'
227259
&& isEscaped(source, i) == false)
260+
{
261+
// if we have an alone symbol, and we are using double symbols (BiLimited)
262+
// then we escape it, otherwise we double it
263+
sb.Append('\\');
264+
}
265+
sb.Append(source[i]);
266+
}
267+
else
228268
{
229-
// if we have an alone symbol, and we are using double symbols (BiLimited)
230-
// then we escape it, otherwise we double it
231-
if (BiLimited) sb.Append('\\');
232-
else sb.Append('}');
269+
if(isEscaped(source, i) == false)
270+
{
271+
sb.Append('}');
272+
}
273+
sb.Append(source[i]);
233274
}
234-
sb.Append(source[i]);
235275
}
236276
void processArrowLeft(string source, StringBuilder sb, ref int i)
237277
{
238-
// source[i] == '<' here.
239-
// When we find '<' we will either escape it or double it or do nothing.
240-
// If the symbol before and the symbol after is not also '<' or an
241-
// escape sequence, we escape it. Otherwise, we do nothing.
242-
243-
if ((source[i - 1] != '<' || isEscaped(source, i - 1))
244-
&& source[i + 1] != '<'
245-
&& isEscaped(source, i) == false)
278+
if (BiLimited)
246279
{
247-
// if we have an alone symbol, and we are using double symbols (BiLimited)
248-
// then we escape it, otherwise we double it
249-
if (BiLimited) sb.Append('\\');
250-
else sb.Append('<');
280+
// source[i] == '<' here.
281+
// When we find '<' we will either escape it or double it or do nothing.
282+
// If the symbol before and the symbol after is not also '<' or an
283+
// escape sequence, we escape it. Otherwise, we do nothing.
284+
285+
if (((source[i - 1] != '<' || isEscaped(source, i - 1))
286+
&& source[i + 1] != '<'
287+
&& isEscaped(source, i) == false)
288+
|| (source[i + 1] == '<' && source[i + 2] == '<'))
289+
{
290+
// if we have an alone symbol, and we are using double symbols (BiLimited)
291+
// then we escape it, otherwise we double it
292+
sb.Append('\\');
293+
}
294+
else if (BiLimited == false)
295+
{
296+
sb.Append('<');
297+
}
298+
sb.Append(source[i]);
299+
}
300+
else
301+
{
302+
if (isEscaped(source, i) == false)
303+
{
304+
sb.Append('<');
305+
}
306+
sb.Append(source[i]);
251307
}
252-
sb.Append(source[i]);
253308
}
254309
void processArrowRight(string source, StringBuilder sb, ref int i)
255310
{
256-
// source[i] == '>' here.
257-
// When we find '>' we will either escape it or double it or do nothing.
258-
259-
// If the symbol before or after is also '>', or current symbol is already
260-
// escaped, we do nothing.
261-
if ((source[i - 1] == '>' && isEscaped(source, i - 1) == false)
262-
|| source[i + 1] == '>'
263-
|| isEscaped(source, i))
311+
if (BiLimited)
264312
{
265-
//do nothing
266-
}
313+
// source[i] == '>' here.
314+
// When we find '>' we will either escape it or double it or do nothing.
315+
316+
// If the symbol before or after is also '>', or current symbol is already
317+
// escaped, we do nothing.
318+
if ((source[i - 1] == '>' && isEscaped(source, i - 1) == false)
319+
|| source[i + 1] == '>'
320+
|| isEscaped(source, i))
321+
{
322+
//do nothing
323+
//unles triple. however, we are not allowed to have text after a tag
324+
if (source[i - 2] == '>' && source[i - 1] == '>' && isEscaped(source, i - 2) == false) sb.Append('\\');
325+
}
267326

268-
// If the symbol before is '-' and the symbol after is either a new line
269-
// ('\n' or '\r') or whitespace(s) followed by a new line or a line comment,
270-
// or a delimited comment(s) followed by new line, we double the sign by
271-
// adding an extra '>' symbol
272-
else if (source[i - 1] == '-' && isEscaped(source, i - 1) == false)
273-
{
274-
if (isLineEndingWithStar(source, i)) sb.Append('>');
275-
else sb.Append('\\');
276-
}
327+
// If the symbol before is '-' and the symbol after is either a new line
328+
// ('\n' or '\r') or whitespace(s) followed by a new line or a line comment,
329+
// or a delimited comment(s) followed by new line, we double the sign by
330+
// adding an extra '>' symbol
331+
else if (source[i - 1] == '-' && isEscaped(source, i - 1) == false)
332+
{
333+
if (isLineEndingWithStar(source, i)) sb.Append('>');
334+
else
335+
{
336+
sb.Insert(sb.Length - 1, '\\');
337+
sb.Append('\\');
338+
}
339+
}
340+
341+
// otherwise we escape
342+
else
343+
{
344+
// if we have an alone symbol, and we are using double symbols (BiLimited)
345+
// then we escape it, otherwise we double it
346+
if (BiLimited) sb.Append('\\');
347+
else sb.Append('>');
348+
}
277349

278-
// otherwise we escape
350+
// append actual symbol
351+
sb.Append(source[i]);
352+
}
279353
else
280354
{
281-
// if we have an alone symbol, and we are using double symbols (BiLimited)
282-
// then we escape it, otherwise we double it
283-
if (BiLimited) sb.Append('\\');
284-
else sb.Append('>');
355+
if (isEscaped(source, i) == false) sb.Append('>');
356+
357+
// append actual symbol
358+
sb.Append(source[i]);
285359
}
286360

287-
// append actual symbol
288-
sb.Append(source[i]);
289361
}
290362
// end BiLimited
291363

@@ -342,7 +414,7 @@ void processSemicolon(string source, StringBuilder sb, ref int i)
342414
bool isEscaped(string source, int i)
343415
{
344416
int count = 0;
345-
for (int j = i-1; j > -1; j--)
417+
for (int j = i - 1; j > -1; j--)
346418
{
347419
if (source[j] == '\\') count++;
348420
else break;
@@ -352,7 +424,7 @@ bool isEscaped(string source, int i)
352424
bool isEscapingEscaper(string source, int i)
353425
{
354426
int count = 0;
355-
for (int j = i + 1; j > source.Length; j++)
427+
for (int j = i + 1; j < source.Length; j++)
356428
{
357429
if (source[j] == '\\') count++;
358430
else break;
@@ -361,7 +433,7 @@ bool isEscapingEscaper(string source, int i)
361433
}
362434
bool isLineEnding(string source, int i)
363435
{
364-
for (int j = i + 1; j > source.Length; j++)
436+
for (int j = i + 1; j < source.Length; j++)
365437
{
366438
// We have Windows-style new line, so yes,
367439
// the character we are testing is line ending.
@@ -390,7 +462,7 @@ bool isLineEndingWithStar(string source, int i)
390462
// accounted for
391463

392464
bool inDelimitedComment = false;
393-
for (int j = i + 1; j > source.Length; j++)
465+
for (int j = i + 1; j < source.Length; j++)
394466
{
395467
if(inDelimitedComment == false)
396468
{

0 commit comments

Comments
 (0)