-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
tldr.l
267 lines (245 loc) · 6.87 KB
/
tldr.l
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
%options backtrack_lexer
%lex
%{
yy.initLexer(yy.lexer);
%}
eol (?:\r\n)|\n|\r
space [ \t]
%%
\s+<<EOF>>
%{
yy.error(yylloc, 'TLDR008');
%}
.*?\t+.*
%{
yy.error(yylloc, 'TLDR012');
var cleaned = this.match.replace(/\t/g, ' ');
this.unput(cleaned);
%}
[^\n]<<EOF>>
%{
// this.setInput resets the state as well, so push that back in
var currentConditionStack = this.conditionStack;
var currentConditions = this.conditions;
// Basically replace EOF with a final newline so lexing can continue
this.setInput(this.match + '\n')
this.conditionStack = currentConditionStack;
this.currentConditions = currentConditions;
yy.error(yylloc, 'TLDR009');
%}
(\s*)\#(\s*)
%{
if (this.topState() !== 'INITIAL') {
this.reject();
return;
}
this.pushState('title');
if (this.matches[1]) {
yy.error(yylloc, 'TLDR001');
}
if (this.matches[2] !== ' ') {
yy.error(yylloc, 'TLDR002');
}
return 'HASH';
%}
([\>-])(\s*)
%{
if (this.topState() !== 'INITIAL') {
this.reject();
return;
}
if (this.matches[2] !== ' ') {
yy.error(yylloc, 'TLDR002');
}
if (this.matches[1] == '>') {
this.pushState('description');
return 'GREATER_THAN';
} else {
this.pushState('example_description');
return 'DASH';
}
%}
([Mm]ore\s+[Ii]nfo(?:rmation)?\:?\s*)
%{
if (this.topState() !== 'description') {
this.reject();
return;
}
if (this.matches[1] !== 'More information: ') {
yy.error(yylloc, 'TLDR016');
}
this.popState();
this.pushState('information_link');
return 'INFORMATION_LINK';
%}
(\<https?\:\/\/[^\s\>]*\>)
%{
if (this.topState() !== 'information_link') {
this.reject();
return;
}
this.popState();
this.pushState('information_link_url');
return 'ANGLE_BRACKETED_URL';
%}
// All regexes below are actually about the same, but it's better organized
// this way around.
(.+?)([ ]*){eol}
%{
if (this.topState() === "title") {
yytext = this.matches[1];
if (this.matches[1].match(/([^\w+\[. -])|(\.$)/)) yy.error(yylloc, 'TLDR013');
this.checkTrailingWhitespace(this.matches[2], yylloc);
this.checkNewline(this.matches[3], yylloc);
this.popState();
return 'TITLE';
} else if (this.topState() === 'information_link_url') {
if (this.matches[1] != '.') yy.error(yylloc, 'TLDR004');
this.checkTrailingWhitespace(this.matches[2], yylloc);
this.checkNewline(this.matches[3], yylloc);
this.popState();
return 'END_INFORMATION_LINK_URL';
} else if (this.topState() === 'information_link') {
this.checkTrailingWhitespace(this.matches[2], yylloc);
this.checkNewline(this.matches[3], yylloc);
this.popState();
return 'END_INFORMATION_LINK';
} else {
this.reject();
}
%}
(.+?)([\.,;!\?]?)([ ]*){eol}
%{
if (this.topState() === 'description') {
this.popState();
yytext = this.matches[1];
var exceptions = ['npm', 'pnpm'];
if (!exceptions.includes(yytext.replace(/ .*/,'')) && yytext.match(/^[a-z]/)) {
yy.error(yylloc, 'TLDR003');
}
if (yytext.match(/(note|NOTE): /)) {
yy.error(yylloc, 'TLDR020');
}
var punctuation = this.matches[2];
if (punctuation !== '.') {
yy.error(yylloc, 'TLDR004');
}
if (punctuation.match(/[,;]/)) {
console.warn(`Description ends in \'${punctuation}\'. Consider writing your sentence on one line.`);
}
this.checkTrailingWhitespace(this.matches[3], yylloc);
this.checkNewline(this.matches[4], yylloc);
return 'DESCRIPTION_LINE';
} else {
this.reject();
}
%}
(.+?)([\.:,;]?)([ ]*){eol}
%{
if (this.topState() === 'example_description') {
this.popState();
yytext = this.matches[1];
if (!yytext.match(/^[\p{Lu}\[]/u)) yy.error(yylloc, 'TLDR015');
if (this.matches[2] !== ':') yy.error(yylloc, 'TLDR005');
if (yytext.match(/(note|NOTE): /)) {
yy.error(yylloc, 'TLDR020');
}
// Try to ensure that verbs at the beginning of the description are in the infinitive tense
// 1. Any word at the start of a sentence that ends with "ing" and is 6 or more characters long (e.g. executing, writing) is likely a verb in the gerund
// 2. Any word at the start of a sentence that doesn't end with "us", "ss", or "ys" (e.g. superfluous, success, always) is likely a verb in the present tense
if (yytext.match(/(^[A-Za-z]{3,}ing )|(^[A-Za-z]+[^usy]s )/)) {
yy.error(yylloc, 'TLDR104');
}
// Check if any sneaky spaces have been caught
this.checkTrailingWhitespace(this.matches[3], yylloc);
this.checkNewline(this.matches[3], yylloc);
return 'EXAMPLE_DESCRIPTION';
} else {
this.reject();
}
%}
\`{eol}
%{
if (this.topState() === 'example_command') {
this.popState();
this.checkNewline(this.matches[1], yylloc);
return 'BACKTICK';
} else {
this.reject();
}
%}
\`([ ]*)
%{
this.pushState('example_command');
if (this.matches[1].match(/ /)) {
yy.error(yylloc, 'TLDR021')
}
return 'BACKTICK';
%}
\{\{([^\n\`\{\}]*)\}\}({eol}?)
%{
if (this.topState() === 'example_command') {
yytext = this.matches[1];
if (this.matches[3]) {
// Matched a newline where there certainly should not be one
// This code is duplicated from two rules below.. should unify
this.unput('`' + this.matches[2]);
yy.error(yylloc, 'TLDR103')
}
return 'COMMAND_TOKEN';
} else this.reject();
%}
// Example commands text either runs up to two left braces (signaling a token)
// Or up to a backtick, which means that's it for the command.
([^\`\n]+?)\{\{
%{
if (this.topState() === 'example_command') {
this.unput('{{');
yytext = this.matches[1];
return 'COMMAND_TEXT';
} else this.reject();
%}
([^\`\n]+?)(\`[ ]*|[ ]*{eol})
%{
if (this.topState() === 'example_command') {
// Check if there are some trailing spaces
if (this.matches[2].match(/ /)) {
yy.error(yylloc, 'TLDR014');
}
if (this.matches[1].endsWith(' ') && !this.matches[1].endsWith('\\ ')) {
yy.error(yylloc, 'TLDR021');
}
// Don't swallow the trailing backtick just yet
if (this.matches[2].match(/\`/)) this.unput('`');
else {
// If command doesn't end in a backtick, just add a backtick anyway
// Also pop back the newline. Let's pretend we don't care what that is.
this.unput('`\n');
yy.error(yylloc, 'TLDR103')
}
yytext = this.matches[1];
return 'COMMAND_TEXT';
} else this.reject();
%}
[ ]+
%{
yy.error(yylloc, 'TLDR014');
%}
{eol}+
%{
// Either you've got more than a single \n or \r or more than \r\n
var isdos = this.match.match(/\r\n/);
if (isdos && this.match.length > 2 || !isdos && this.match.length > 1) {
yy.error(yylloc, 'TLDR011');
}
this.checkNewline(this.match, yylloc);
return 'NEWLINE';
%}
// This is a catchall that just slurps up a line if doesn't match
// The compiler can then use it to create meaningful errors since it has
// more context than the lexer does
(.+?)[\.:]?{eol}
%{
yytext = this.matches[1];
return 'TEXT';
%}