-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbcodelexer.ts
687 lines (681 loc) · 27.3 KB
/
bbcodelexer.ts
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
import Debugger from "./debugger";
//Types
import { BBToken, LexState } from '../@types/enums';
import { Param, State, TagType } from "../@types/dataTypes";
//Modules
import preg_split, {PREG_SPLIT_DELIM_CAPTURE, PREG_SPLIT_NO_EMPTY} from "../modules/preg_split";
/**
* This is the BBCodeLexer class, it is responsible for taking an input and breaking it
* into an array of "tokens", which we will walk through to generate the output in the
* {@link BBCode} Class
*/
export default class BBCodeLexer {
/**
* Return token type: One of the {@link BBToken} enum values
*/
public token: BBToken;
/**
* Actual exact, original text of token.
*/
public text: string;
/**
* If token is a tag, this is the decoded array version.
*/
public tag: TagType | boolean;
/**
* Next state of the lexer's state machine: text, or tag/ws/nl
*/
public state: LexState;
/**
* The input string, split into an array of tokens.
*/
public input: string[];
/**
* Read pointer into the input array.
*/
public ptr: number;
/**
* Whether to "unget" the last token.
*/
public unget: boolean;
/**
* In verbatim mode, we return all input, unparsed, including comments.
*/
public verbatim: boolean;
/**
* In debug mode, we dump decoded tags when we find them.
*/
public debug: boolean;
/**
* Which kind of tag marker we're using: "[", "<", "(", or "{"
*/
public tagMarker: string;
/**
* The ending tag marker: "]", ">", "(", or "{"
*/
public endTagMarker: string;
/**
* Main tag-matching pattern.
*/
public patMain: string|RegExp;
/**
* Pattern for matching comments.
*/
public patComment: RegExp;
/**
* Pattern for matching comments.
*/
public patComment2: RegExp;
/**
* Pattern for matching wiki-links.
*/
public patWiki: RegExp;
/**
* Regex generated from {@link patMain} to remove escape characters from escaped tags
*/
public genEscapeRegex: RegExp;
/**
* The actual Regex used to determine if something should be escaped
* Examples:
* - `(?<!\\\\)` Negative lookbehind to make sure there isn't a \ behind the tag (Current)
* - `(?:(?<!\\\\)|(?<=\\\\\\\\))` Noncapturing group containing both a negative lookbehind
* and a positive lookbehind, to make sure that there isn't a \ behind the tag, or if there
* is, make sure that it is 2 \, if there is 2 \, it will still parse the tag.
* - Nothing, this will disable escape characters
*/
public escapeRegex: string;
/**
* Whether to allow Escape Characters or not
*/
public allowEscape: boolean;
/**
* Instantiate a new instance of the {@link BBCodeLexer} class.
*
* @param string The string to be broken up into tokens.
* @param tagMarker The BBCode tag marker.
*/
public constructor(string: string, tagMarker = '[', debug: boolean = false, allowEscape: boolean = true) {
// First thing we do is to split the input string into tuples of
// text and tags. This will make it easy to tokenize. We define a tag as
// anything starting with a [, ending with a ], and containing no [ or ] in
// between unless surrounded by "" or '', and containing no newlines.
// We also separate out whitespace and newlines.
// Choose a tag marker based on the possible tag markers.
const regexBeginMarkers: Record<string, string> = {
"[": "\\[",
"<": "<",
"{": "\\{",
"(": "\\("
};
const regexEndMarkers: Record<string, string> = {
"[": "\\]",
"<": ">",
"{": "\\}",
"(": "\\)"
};
const endMarkers: Record<string, string> = {
"[": "]",
"<": ">",
"{": "}",
"(": ")"
};
if (!regexEndMarkers[tagMarker]) {
tagMarker = '[';
}
const end = regexEndMarkers[tagMarker];
const start = regexBeginMarkers[tagMarker];
this.tagMarker = tagMarker;
this.endTagMarker = endMarkers[tagMarker];
this.allowEscape = allowEscape;
if (allowEscape)
this.escapeRegex = "(?<!\\\\)";
else
this.escapeRegex = "";
// this.input will be an array of tokens, with the special property that
// the elements strictly alternate between plain text and tags/whitespace/newlines,
// and that tags always have *two* entries per tag. The first element will
// always be plain text. Note that the regexes below make VERY heavy use of
// PCRE regex-syntax extensions, so don't even try to modify them unless you
// know how things like (?!) and (?:) and (?=) work. Unfortanetly the /x modifier
// doesn't exist here to make this a *lot* more legible and debuggable, so it's
// been modified to stretch across multiple lines with the comments edited.
this.patMain = "("
// Match tags, as long as they do not start with [-- or [' or [!-- or [rem or [[.
// Tags may contain "quoted" or 'quoted' sections that may contain [ or ] characters.
// Tags may not contain newlines.
+`${this.escapeRegex}${start}`
+`(?!--|'|!--|${start}${start})`
+`(?:[^\\n\\r${start}${end}]|\\"[^\\"\\n\\r]*\\"|\\'[^\\'\\n\\r]*\\')*`
+`${end}`
// Match wiki-links, which are of the form [[...]] or [[...|...]]. Unlike
// tags, wiki-links treat " and ' marks as normal input characters; but they
// still may not contain newlines.
+`|${this.escapeRegex}${start}${start}(?:[^${end}\\r\\n]|${end}[^${end}\\r\\n]){1,256}${end}${end}`
// Match single-line comments, which start with [-- or [' or [rem .
+`|${this.escapeRegex}${start}(?:--|')(?:[^${end}\\n\\r]*)${end}`
// Match multi-line comments, which start with [!-- and end with --] and contain
// no --] in between.
+`|${this.escapeRegex}${start}!--(?:[^-]|-[^-]|--[^${end}])*--${end}`
// Match five or more hyphens as a special token, which gets returned as a [rule] tag.
+`|${this.escapeRegex}-----+`
// Match newlines, in all four possible forms.
+"|\\x0D\\x0A|\\x0A\\x0D|\\x0D|\\x0A"
// Match whitespace, but only if it butts up against a newline, rule, or
// bracket on at least one end.
+`|[\\x00-\\x09\\x0B-\\x0C\\x0E-\\x20]+(?=[\\x0D\\x0A${start}]|-----|$)`
+`|(?<=[\\x0D\\x0A${end}]|-----|^)[\\x00-\\x09\\x0B-\\x0C\\x0E-\\x20]+`
+")";
this.input = preg_split(this.patMain, string, -1, PREG_SPLIT_DELIM_CAPTURE);
if (this.allowEscape) {
this.genEscapeRegex = new RegExp(this.patMain.replaceAll(this.escapeRegex, "(\\\\)"), "g");
for (const input in this.input) {
const value = this.input[input];
this.input[input] = value.replace(this.genEscapeRegex, function(match) {
// If there's a backslash before the match, remove it
if (match[0] === '\\') {
return match.slice(1); // Remove the backslash
}
return match;
});
}
}
// Patterns for matching specific types of tokens during lexing. (originally contained Dx flags)
this.patComment = new RegExp(`^${start}(?:--|')`);
this.patComment2 = new RegExp(`^${start}!--(?:[^-]|-[^-]|--[^${end}])*--${end}$`);
this.patWiki = new RegExp(`^${start}${start}([^\\|]*)(?:\\|(.*))?${end}${end}$`);
// Current lexing state.
this.ptr = 0;
this.unget = false;
this.state = LexState.TEXT;
this.verbatim = false;
// Return values.
this.token = BBToken.EOI;
this.tag = false;
this.text = "";
this.debug = debug;
}
/**
* Compute how many non-tag characters there are in the input, give or take a few.
*
* This is optimized for speed, not accuracy, so it'll get some stuff like
* horizontal rules and weird whitespace characters wrong, but it's only supposed
* to provide a rough quick guess, not a hard fact.
*
* @return Returns the approximate text length.
*/
public guessTextLength(): number {
let length = 0;
let ptr = 0;
let state = LexState.TEXT;
// Loop until we find a valid (nonempty) token.
while (ptr < this.input.length) {
const text = this.input[ptr++];
if (state === LexState.TEXT) {
state = LexState.TAG;
length += text.length;
} else {
switch (this.text.slice(0, 1).charCodeAt(0)) {
case 10:
case 13:
state = LexState.TEXT;
length++;
break;
default:
state = LexState.TEXT;
length += text.length;
break;
case 40:
case 60:
case 91:
case 123:
state = LexState.TEXT;
break;
}
}
}
return length;
}
/**
* Return the type of the next token, either BBCODE_TAG or BBCODE_TEXT or BBCODE_EOI.
*
* This stores the content of this token into this.text, the type of this token in this.token, and possibly an
* array into this.tag.
*
* If this is a BBCODE_TAG token, this.tag will be an array computed from the tag's contents, like this:
*
* ```
* [
* '_name': tag_name,
* '_end': true if this is an end tag (i.e., the name starts with a /)
* '_default': default value (for example, in [url=foo], this is "foo").
* ...
* ...all other key: value parameters given in the tag...
* ...
* ]
* ```
*/
public nextToken(): BBToken {
// Handle ungets; if the last token has been "ungotten", just return it again.
if (this.unget) {
this.unget = false;
return this.token;
}
// Loop until we find a valid (nonempty) token.
while (true) {
// Did we run out of tokens in the input?
if (this.ptr >= this.input.length) {
this.text = "";
this.tag = false;
return this.token = BBToken.EOI;
}
// Inhale one token, sanitizing away any weird control characters. We
// allow \t, \r, and \n to pass through, but that's it.
this.text = this.input[this.ptr++].replace(/[\x00-\x08\x0B-\x0C\x0E-\x1F]/g, "");
if (this.verbatim) {
// In verbatim mode, we return *everything* as plain text or whitespace.
this.tag = false;
let tokenType: BBToken;
if (this.state === LexState.TEXT) {
this.state = LexState.TAG;
tokenType = BBToken.TEXT;
} else {
// This must be either whitespace, a newline, or a tag.
this.state = LexState.TEXT;
switch (this.text.slice(0, 1).charCodeAt(0)) {
case 10:
case 13:
// Newline.
tokenType = BBToken.NL;
break;
default:
// Whitespace.
tokenType = BBToken.WS;
break;
case 45:
case 40:
case 60:
case 91:
case 123:
// Tag or comment.
tokenType = BBToken.TEXT;
break;
}
}
if (this.text.length > 0) {
return this.token = tokenType;
}
} else if (this.state === LexState.TEXT) {
// Next up is plain text, but only return it if it's nonempty.
this.state = LexState.TAG;
this.tag = false;
if (this.text.length > 0) {
return this.token = BBToken.TEXT;
}
} else {
let matches;
// This must be either whitespace, a newline, or a tag.
switch (this.text.slice(0, 1).charCodeAt(0)) {
case 10:
case 13:
// Newline.
this.tag = false;
this.state = LexState.TEXT;
return this.token = BBToken.NL;
case 45:
// A rule made of hyphens; return it as a [rule] tag.
if (/^-----/.test(this.text)) {
this.tag = {
"_name": "rule",
"_endtag": false,
"_default": ""
};
this.state = LexState.TEXT;
return this.token = BBToken.TAG;
} else {
this.tag = false;
this.state = LexState.TEXT;
if (this.text.length > 0) {
return this.token = BBToken.TEXT;
}
break;
}
break;
default:
// Whitespace.
this.tag = false;
this.state = LexState.TEXT;
return this.token = BBToken.WS;
case 40:
case 60:
case 91:
case 123:
// Tag or comment. This is the most complicated one, because it
// needs to be parsed into its component pieces.
// See if this is a comment; if so, skip it.
if (this.patComment.test(this.text)) {
// This is a comment, not a tag, so treat it like it doesn't exist.
this.state = LexState.TEXT;
break;
}
if (this.patComment2.test(this.text)) {
// This is a comment, not a tag, so treat it like it doesn't exist.
this.state = LexState.TEXT;
break;
}
// See if this is a [[wiki link]]; if so, convert it into a [wiki="" title=""] tag.
if ((matches = this.text.match(this.patWiki))) {
matches = {...{1: undefined, 2: undefined}, ...matches};
this.tag = {
'_name': 'wiki',
'_endtag': false,
'_default': matches[1],
'title': matches[2]
};
this.state = LexState.TEXT;
return this.token = BBToken.TAG;
}
// Not a comment, so parse it like a tag.
this.tag = this.decodeTag(this.text);
this.state = LexState.TEXT;
return this.token = (this.tag['_end'] ? BBToken.ENDTAG : BBToken.TAG);
}
}
}
}
/**
* Un-gets the last token read so that a subsequent call to NextToken() will return it.
*
* Note that ungetToken() does not switch states when you switch between verbatim mode and standard mode: For
* example, if you read a tag, unget the tag, switch to verbatim mode, and then get the next token, you'll get back
* a BBCODE_TAG --- exactly what you ungot, not a BBCODE_TEXT token.
*/
public ungetToken() {
if (this.token !== BBToken.EOI) {
this.unget = true;
}
}
/**
* Peek at the next token, but don't remove it.
*/
public peekToken() {
const result = this.nextToken();
if (this.token !== BBToken.EOI) {
this.unget = true;
}
return result;
}
/**
* Save the state of this lexer so it can be restored later.
*
* The return value from this should be considered opaque. Because PHP uses copy-on-write references, the total
* cost of the returned state is relatively small, and the running time of this function (and RestoreState) is very
* fast.
*/
public saveState(): State {
return {
'token': this.token,
'text': this.text,
'tag': this.tag,
'state': this.state,
'input': this.input,
'ptr': this.ptr,
'unget': this.unget,
'verbatim': this.verbatim
};
}
/**
* Restore the state of this lexer from a saved previous state.
*
* @param lexState The previous lexer state.
*/
public restoreState(lexState: State) {
if (!(typeof lexState === "object")) {
return;
}
lexState = {...{
'token': undefined,
'text': undefined,
'tag': undefined,
'state': undefined,
'input': undefined,
'ptr': undefined,
'unget': undefined,
'verbatim': undefined
}, ...lexState};
this.token = lexState['token'];
this.text = lexState['text'];
this.tag = lexState['tag'];
this.state = lexState['state'];
this.input = lexState['input'];
this.ptr = lexState['ptr'];
this.unget = lexState['unget'];
this.verbatim = lexState['verbatim'];
}
/**
* Given a string, if it's surrounded by "quotes" or 'quotes', remove them.
*
* @param string The string to strip.
* @return Returns the string stripped of quotes.
*/
protected stripQuotes(string: string): string {
if (string.length > 1) {
const first = string.slice(0, 1);
const last = string.slice(-1);
if (first === last && (first === '"' || first === "'")) {
return string.slice(1, -1);
}
}
return string;
}
/**
* Given a tokenized piece of a tag, decide what type of token it is.
*
* Our return values are:
*
* - -1 End-of-input (EOI).
* - '=' Token is an = sign.
* - ' ' Token is whitespace.
* - '"' Token is quoted text.
* - 'A' Token is unquoted text.
*
* @param ptr The index of {@link pieces} to examine.
* @param pieces The pieces array to classify.
* @return Returns the tokenized piece of the tag.
*/
protected classifyPiece(ptr: number, pieces: string[]) {
if (ptr >= pieces.length) {
return -1; // EOI.
}
const piece = pieces[ptr];
if (piece === '=') {
return '=';
} else if (/^['"]/.test(piece)) {
return '"';
} else if (/^[\x00-\x20]+$/.test(piece)) {
return ' ';
} else {
return 'A';
}
}
/**
* Given a string containing a complete [tag] (including its brackets), break it down into its components and return them as an array.
*
* @param tag The tag to decode.
* @return Returns the object representation of the tag.
*/
protected decodeTag(tag: string): TagType {
Debugger.debug("Lexer.InternalDecodeTag: input:", tag);
// Create the initial result object.
const result: TagType = {'_tag': tag, '_endtag': '', '_name': '', '_hasend': false, '_end': false, '_default': false};
// Strip off the [brackets] around the tag, leaving just its content.
tag = tag.slice(1, tag.length - 1);
// The starting bracket *must* be followed by a non-whitespace character.
const ch = tag.slice(0, 1).charCodeAt(0);
if (ch >= 0 && ch <= 32) {
return result;
}
// Break it apart into words, quoted text, whitespace, and equal signs.
const pieces = preg_split(
"/(\\\"[^\\\"]+\\\"|\\'[^\\']+\\'|=|[\\x00-\\x20]+)/",
tag,
-1,
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);
let ptr = 0;
let type;
// Handle malformed (empty) tags correctly.
if (pieces.length < 1) {
return result;
}
// The first piece should be the tag name, whatever it is. If it starts with a /
// we remove the / and mark it as an end tag.
if (pieces[ptr] && pieces[ptr].slice(0, 1) === '/') {
result['_name'] = pieces[ptr++].slice(1).toLowerCase();
result['_end'] = true;
} else {
result['_name'] = pieces[ptr++].toLowerCase();
result['_end'] = false;
}
// Skip whitespace after the tag name.
while ((type = this.classifyPiece(ptr, pieces)) === ' ') {
ptr++;
}
const params: Param[] = [];
let value;
// If the next piece is an equal sign, then the tag's default value follows.
if (type !== '=') {
result['_default'] = false;
params.push({'key': '', 'value': ''});
} else {
ptr++;
// Skip whitespace after the initial equal-sign.
while ((type = this.classifyPiece(ptr, pieces)) === ' ') {
ptr++;
}
// Examine the next (real) piece, and see if it's quoted; if not, we need to
// use heuristics to guess where the default value begins and ends.
if (type === "\"") {
value = this.stripQuotes(pieces[ptr++]);
} else {
// Collect pieces going forward until we reach an = sign or the end of the
// tag; then rewind before whatever comes before the = sign, and everything
// between here and there becomes the default value. This allows tags like
// [font=Times New Roman size=4] to make sense even though the font name is
// not quoted. Note, however, that there's a special initial case, where
// any equal-signs before whitespace are considered to be part of the parameter
// as well; this allows an ugly tag like [url=http://foo?bar=baz target=my_window]
// to behave in a way that makes (tolerable) sense.
let afterSpace = false;
let start = ptr;
while ((type = this.classifyPiece(ptr, pieces)) !== -1) {
if (type === ' ') {
afterSpace = true;
}
if (type === '=' && afterSpace) {
break;
}
ptr++;
}
if (type === -1) {
ptr--;
}
// We've now found the first (appropriate) equal-sign after the start of the
// default value. (In the example above, that's the "=" after "target"+) We
// now have to rewind back to the last whitespace to find where the default
// value ended.
if (type === '=') {
// Rewind before = sign.
ptr--;
// Rewind before any whitespace before = sign.
while (ptr > start && this.classifyPiece(ptr, pieces) === ' ') {
ptr--;
}
// Rewind before any text elements before that.
while (ptr > start && this.classifyPiece(ptr, pieces) !== ' ') {
ptr--;
}
}
// The default value is everything from `start` to `ptr`, inclusive.
value = "";
for (; start <= ptr; start++) {
if (this.classifyPiece(start, pieces) === ' ') {
value += " ";
} else {
value += this.stripQuotes(pieces[start]);
}
}
value = value.trim();
ptr++;
}
result['_default'] = value;
params.push({'key': '', 'value': value});
}
// The rest of the tag is composed of either floating keys or key=value pairs, so walk through
// the tag and collect them all. Again, we have the nasty special case where an equal sign
// in a parameter but before whitespace counts as part of that parameter.
while ((type = this.classifyPiece(ptr, pieces)) !== -1) {
let key = '';
// Skip whitespace before the next key name.
while (type === ' ') {
ptr++;
type = this.classifyPiece(ptr, pieces);
}
// Decode the key name.
if (type === 'A' || type === '"') {
if (pieces[ptr]) {
key = this.stripQuotes(pieces[ptr].toLowerCase());
} else {
key = '';
}
ptr++;
} else if (type === '=') {
ptr++;
continue;
} else if (type === -1) {
break;
}
// Skip whitespace after the key name.
while ((type = this.classifyPiece(ptr, pieces)) === ' ') {
ptr++;
}
// If an equal-sign follows, we need to collect a value. Otherwise, we
// take the key itself as the value.
if (type !== '=') {
value = this.stripQuotes(key);
} else {
ptr++;
// Skip whitespace after the equal sign.
while ((type = this.classifyPiece(ptr, pieces)) === ' ') {
ptr++;
}
if (type === '"') {
// If we get a quoted value, take that as the only value.
value = this.stripQuotes(pieces[ptr++]);
} else if (type !== -1) {
// If we get a non-quoted value, consume non-quoted values
// until we reach whitespace.
value = pieces[ptr++];
while ((type = this.classifyPiece(ptr, pieces)) !== -1
&& type !== ' ') {
value += pieces[ptr++];
}
} else {
value = "";
}
}
// Record this in the associative array if it's a legal public identifier name.
// Legal *public* identifier names must *not* begin with an underscore.
if (key.slice(0, 1) !== '_') {
result[key] = value;
}
// Record this in the parameter list always.
params.push({'key': key, 'value': value});
}
// Add the parameter list as a member of the associative array.
result['_params'] = params;
// In debugging modes, output the tag as we collected it.
Debugger.debug("Lexer.InternalDecodeTag: output:", result);
// Save the resulting parameters, and return the whole shebang.
return result;
}
}