-
Notifications
You must be signed in to change notification settings - Fork 620
/
moose.c
516 lines (431 loc) · 12 KB
/
moose.c
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
/*
* Copyright (c) 2019, Masatake YAMATO
*
* This source code is released for free distribution under the terms of the
* GNU General Public License version 2 or (at your option) any later version.
*
* This module contains functions for generating tags for Moose perl extension.
* https://metacpan.org/pod/Moose
*
* This module can gather tags for Moo perl extension, too.
* https://metacpan.org/pod/Moo
*
* From the output of this parser, a client cannot know which extension is used
* in the input source file. However, perl parser can give you hints about the
* extension; with "--extras=+r --kinds-Perl=+M" options, the perl parser tags
* module names specified as arguments for `use' perl built-in function.
* You don't need the options if you just want to run This parser.
* This parser just needs --extras=+s option that run subparsers including this
* parser.
*
*/
/* NOTE about kind/role design:
*
* sub foo { ... }
* after 'foo' => sub { ...}
*
* There were two ideas to capture 'foo':
*
* A: capturing 'foo' as a reference tag with 'method' kind and 'wrapped' role, and
* B: capturing 'foo' as a definition tag with 'wrapper' kind.
*
* This implementation takes idea B. */
/*
* INCLUDE FILES
*/
#include "general.h" /* must always come first */
#include "debug.h"
#include "entry.h"
#include "kind.h"
#include "parse.h"
#include "perl.h"
#include "read.h"
#include "routines.h"
#include "trace.h"
#include <string.h>
/*
* DATA DECLARATIONS
*/
enum MooseKind {
K_CLASS,
K_METHOD,
K_ATTR,
K_WRAPPER,
};
static kindDefinition MooseKinds[] = {
{ true, 'c', "class", "classes" },
{ true, 'm', "method", "methods" },
{ true, 'a', "attribute", "attributes" },
{ true, 'w', "wrapper", "wrappers" },
};
typedef enum {
F_WRAPPING,
} MooseField;
static fieldDefinition MooseFields [] = {
{
.name = "wrapping",
.description = "how a wrapper wrapping the method (around, after, or before)",
.enabled = true,
},
};
enum Wrapping {
W_UNKNOWN,
W_AROUND,
W_BEFORE,
W_AFTER,
};
static char * WrappingStrings[] = {
"unknown",
"around",
"before",
"after",
};
struct mooseSubparser {
perlSubparser perl;
bool notInMoose;
bool inPod;
int packageCork;
int classCork;
bool notContinuousExtendsLines;
const char *superClass;
};
/*
* FUNCTION PROTOTYPES
*/
static void inputStart (subparser *s);
static void inputEnd (subparser *s);
static void makeTagEntryNotify (subparser *s, const tagEntryInfo *tag, int corkIndex);
static void enterMoose (struct mooseSubparser *moose);
static void leaveMoose (struct mooseSubparser *moose);
static void enteringPodNotify (perlSubparser *perl);
static void leavingPodNotify (perlSubparser *perl);
/*
* DATA DEFINITIONS
*/
static struct mooseSubparser mooseSubparser = {
.perl = {
.subparser = {
.direction = SUBPARSER_BI_DIRECTION,
.inputStart = inputStart,
.inputEnd = inputEnd,
.makeTagEntryNotify = makeTagEntryNotify,
},
.enteringPodNotify = enteringPodNotify,
.leavingPodNotify = leavingPodNotify,
},
};
/*
* FUNCTION DEFINITIONS
*/
static void inputStart (subparser *s)
{
struct mooseSubparser *moose = (struct mooseSubparser *)s;
moose->notInMoose = true;
moose->packageCork = CORK_NIL;
moose->classCork = CORK_NIL;
moose->inPod = false;
moose->superClass = NULL;
moose->notContinuousExtendsLines = true;
}
static void inputEnd (subparser *s)
{
struct mooseSubparser *moose = (struct mooseSubparser *)s;
if (moose->classCork != CORK_NIL)
{
tagEntryInfo *e = getEntryInCorkQueue (moose->classCork);
Assert (e);
e->extensionFields.endLine = getInputLineNumber ();
}
if (moose->superClass)
eFree ((char *)moose->superClass);
}
static void makeTagEntryNotify (subparser *s, const tagEntryInfo *tag, int corkIndex)
{
perlSubparser *perl = (perlSubparser *)s;
struct mooseSubparser *moose = (struct mooseSubparser *)perl;
if (isTagExtraBitMarked(tag, XTAG_QUALIFIED_TAGS))
return;
if (tag->kindIndex == KIND_PERL_PACKAGE)
moose->packageCork = corkIndex;
else if ((!moose->notInMoose) && tag->kindIndex == KIND_PERL_SUBROUTINE)
{
tagEntryInfo moose_e;
initTagEntry (&moose_e, tag->name, K_METHOD);
moose_e.extensionFields.scopeIndex = moose->classCork;
makeTagEntry (&moose_e);
}
else if (tag->kindIndex == KIND_PERL_MODULE)
{
if (isRoleAssigned(tag, ROLE_PERL_MODULE_USED))
{
if (strcmp (tag->name, "Moose") == 0
|| strcmp (tag->name, "Moo") == 0)
enterMoose (moose);
}
else if (isRoleAssigned(tag, ROLE_PERL_MODULE_UNUSED))
{
if (strcmp (tag->name, "Moose") == 0
|| strcmp (tag->name, "Moo") == 0)
leaveMoose (moose);
}
}
}
static void enteringPodNotify (perlSubparser *perl)
{
struct mooseSubparser *moose = (struct mooseSubparser *)perl;
moose->inPod = true;
}
static void leavingPodNotify (perlSubparser *perl)
{
struct mooseSubparser *moose = (struct mooseSubparser *)perl;
moose->inPod = false;
}
static void leaveMoose (struct mooseSubparser *moose)
{
moose->notContinuousExtendsLines = true;
tagEntryInfo *e = getEntryInCorkQueue (moose->classCork);
Assert (e);
e->extensionFields.endLine = getInputLineNumber ();
moose->classCork = CORK_NIL;
moose->notInMoose = true;
moose->packageCork = CORK_NIL;
}
static void enterMoose (struct mooseSubparser *moose)
{
moose->notContinuousExtendsLines = true;
if (moose->packageCork == CORK_NIL)
return;
moose->notInMoose = false;
tagEntryInfo *perl_e = getEntryInCorkQueue (moose->packageCork);
Assert (perl_e);
tagEntryInfo moose_e;
initTagEntry (&moose_e, perl_e->name, K_CLASS);
moose_e.lineNumber = perl_e->lineNumber;
moose_e.filePosition = perl_e->filePosition;
moose->classCork = makeTagEntry (&moose_e);
return;
}
static void parseExtendsClass (const char *input,
size_t input_len,
vString *inherits,
bool *notContinuousLine)
{
int i = 0;
do
{
if (input [i] == ',')
i++;
for (; (i < input_len) && input[i] != '\n' && input[i] != '\0'; i++)
{
if (input[i] == '\'' || input[i] == ' ' || input[i] == '\t')
continue;
else if (input[i] == ',')
{
vStringPut (inherits, ',');
*notContinuousLine = false;
break;
}
else if (input[i] == ';')
{
*notContinuousLine = true;
break;
}
else
vStringPut (inherits, input[i]);
}
}
while (input[i] == ',');
}
static bool findExtendsClass (const char *line,
const regexMatch *matches,
unsigned int count,
void *data)
{
struct mooseSubparser *moose = data;
if (moose->inPod)
return true;
moose->notContinuousExtendsLines = true;
if (moose->classCork == CORK_NIL)
return true;
const char *input = line + matches[1].start;
vString *str = vStringNew ();
parseExtendsClass (input, matches[1].length, str,
&moose->notContinuousExtendsLines);
if (vStringLength(str) == 0)
{
vStringDelete (str);
return true;
}
tagEntryInfo *e = getEntryInCorkQueue (moose->classCork);
Assert (e);
if (e->extensionFields.inheritance == NULL)
e->extensionFields.inheritance = vStringDeleteUnwrap (str);
return true;
}
static bool findExtendsClassContinuation (const char *line,
const regexMatch *matches,
unsigned int count,
void *data)
{
struct mooseSubparser *moose = data;
moose->notContinuousExtendsLines = true;
tagEntryInfo *e = getEntryInCorkQueue (moose->classCork);
Assert (e);
const char *input = line + matches[1].start;
vString *str;
if (e->extensionFields.inheritance)
str = vStringNewInit (e->extensionFields.inheritance);
else
str = vStringNew ();
parseExtendsClass (input, matches[1].length, str,
&moose->notContinuousExtendsLines);
if (e->extensionFields.inheritance != NULL)
eFree ((char *)e->extensionFields.inheritance);
e->extensionFields.inheritance = vStringDeleteUnwrap (str);
return true;
}
static const char *parseAttributeOrWrapper (const char *str, int parentCorkIndex, int extraTerminator,
int kind, enum Wrapping wrapping)
{
int i;
for (i = 0;
str[i]
&& str[i] != extraTerminator
&& (isalnum ((unsigned char)str[i]) || str[i] == '_');
i++)
; /* Just advancing `i' */
if (i == 0)
return NULL;
tagEntryInfo e;
char *buf = eStrndup (str, i);
initTagEntry (&e, buf, kind);
if (parentCorkIndex != CORK_NIL)
e.extensionFields.scopeIndex = parentCorkIndex;
int corkIndex = makeTagEntry (&e);
if (kind == K_WRAPPER)
attachParserFieldToCorkEntry (corkIndex, MooseFields[F_WRAPPING].ftype,
WrappingStrings [wrapping]);
eFree (buf);
return str[i] == '\0'? NULL: str + i;
}
static void solveKindAndWrappng (const char *str, int *kind, enum Wrapping *wrapping)
{
*kind = K_WRAPPER;
*wrapping = W_UNKNOWN;
switch (str[0])
{
case 'h': /* has */
*kind = K_ATTR;
break;
case 'a': /* around or after */
if (str[1] == 'r')
*wrapping = W_AROUND;
else if (str[1] == 'f')
*wrapping = W_AFTER;
break;
case 'b': /* before */
*wrapping = W_BEFORE;
break;
case 'o': /* override */
*kind = K_METHOD;
}
}
static bool findAttributeOrWrapperOne (const char *line,
const regexMatch *matches,
unsigned int count,
void *data)
{
struct mooseSubparser *moose = data;
int kind;
enum Wrapping wrapping;
if (moose->inPod)
return true;
moose->notContinuousExtendsLines = true;
if (count < 3)
return true;
solveKindAndWrappng (line + matches[1].start, &kind, &wrapping);
parseAttributeOrWrapper (line + matches[2].start, moose->classCork, -1,
kind, wrapping);
return true;
}
static bool findAttributeOrWrapperMulit (const char *line,
const regexMatch *matches,
unsigned int count,
void *data)
{
struct mooseSubparser *moose = data;
int kind;
enum Wrapping wrapping;
int terminator;
if (moose->inPod)
return true;
moose->notContinuousExtendsLines = true;
if (count < 4)
return true;
solveKindAndWrappng (line + matches[1].start, &kind, &wrapping);
terminator = line[matches[2].start];
const char *str = line + matches[3].start;
while ((str = parseAttributeOrWrapper (str, moose->classCork, terminator,
kind, wrapping)))
{
int i;
for (i = 0; (str[i] == ' ') || (str[i] == '\t'); i++)
;
if (str[i] == '\0' || str[i] == terminator)
break;
str = str + i;
}
return true;
}
static void findMooseTags (void)
{
scheduleRunningBaseparser (RUN_DEFAULT_SUBPARSERS);
}
static void initializeMooseParser (langType language)
{
addLanguageCallbackRegex (language, "^[ \t]*extends *(.+)",
"{exclusive}",
findExtendsClass, &mooseSubparser.notInMoose,
&mooseSubparser);
addLanguageCallbackRegex (language, "^[ \t]*(has|after|before|around|override) +"
/* foo => ()
* 'foo' => ()
* "foo" => ()
* ( foo => ())
* ( "foo" => ())
* ( 'foo' => ()) */
"\\(?[ \t]*[\"']?"
""
"([a-zA-Z_][a-zA-Z0-9_]*([ \t][a-zA-Z_][a-zA-Z0-9_]*)*).*=>",
"{exclusive}",
findAttributeOrWrapperOne, &mooseSubparser.notInMoose,
&mooseSubparser);
addLanguageCallbackRegex (language, "^[ \t]*(has|after|before|around) +\\(?\\[qw([^ \t])[ \t]*"
/* [qw/foo bar/] => ()
* ([qw/foo bar/] => ()) */
"([a-zA-Z_][a-zA-Z0-9_]*([ \t][a-zA-Z_][a-zA-Z0-9_]*)*).*=>",
"{exclusive}",
findAttributeOrWrapperMulit, &mooseSubparser.notInMoose,
&mooseSubparser);
addLanguageCallbackRegex (language, "^[ \t]*(.+)",
"{exclusive}",
findExtendsClassContinuation, &mooseSubparser.notContinuousExtendsLines,
&mooseSubparser);
}
extern parserDefinition* MooseParser (void)
{
parserDefinition* const def = parserNew("Moose");
static parserDependency dependencies [] = {
[0] = { DEPTYPE_SUBPARSER, "Perl", &mooseSubparser },
};
def->dependencies = dependencies;
def->dependencyCount = ARRAY_SIZE (dependencies);
def->kindTable = MooseKinds;
def->kindCount = ARRAY_SIZE(MooseKinds);
def->fieldTable = MooseFields;
def->fieldCount = ARRAY_SIZE (MooseFields);
def->initialize = initializeMooseParser;
def->parser = findMooseTags;
def->useCork = true;
return def;
}