File: perl-moose.c

package info (click to toggle)
codelite 17.0.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 136,384 kB
  • sloc: cpp: 491,550; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 805; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (573 lines) | stat: -rw-r--r-- 14,716 bytes parent folder | download | duplicates (5)
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
/*
 *   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
 *
 */

/* 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 the 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,
	K_ROLE,
};

static kindDefinition MooseKinds[] = {
	{ true, 'c', "class", "classes"  },
	{ true, 'm', "method", "methods" },
	{ true, 'a', "attribute", "attributes" },
	{ true, 'w', "wrapper", "wrappers" },
	{ true, 'r', "role", "roles" },
};

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;

	int indexForFunctionParameters;

	/* functionParametersModifiersStateCounter is for tracking the conditions
	 * that both "use Moose;" and "use Functions::Parameters qw/:modifiers/"
	 * are specified.
	 * functionParametersModifiersStateCounter is initialized to 2.
	 * Decrement functionParametersModifiersStateCounter when finding one of two.
	 * When functionParametersModifiersStateCounter is 0, the state is
	 * propagated to notInFunctionParametersModifiers. */
	bool notInFunctionParametersModifiers;
	int  functionParametersModifiersStateCounter;
#define RESET_FunctionParameters_STATE(MOOSE)				\
	do {													\
		MOOSE->notInFunctionParametersModifiers = true;		\
		MOOSE->functionParametersModifiersStateCounter = 2;	\
	} while (0)
#define DEC_FunctionParameters_STATE(MOOSE)							\
	do {															\
		MOOSE->functionParametersModifiersStateCounter--;			\
		if (MOOSE->functionParametersModifiersStateCounter == 0)	\
			MOOSE->notInFunctionParametersModifiers = false;		\
		if (MOOSE->functionParametersModifiersStateCounter < 0)		\
			MOOSE->functionParametersModifiersStateCounter = 0; 	\
	} while (0)
#define INC_FunctionParameters_STATE(MOOSE)						\
	do {														\
		MOOSE->functionParametersModifiersStateCounter++;		\
		if (MOOSE->functionParametersModifiersStateCounter > 0)	\
			MOOSE->notInFunctionParametersModifiers = true;		\
		if (MOOSE->functionParametersModifiersStateCounter > 2)	\
			MOOSE->functionParametersModifiersStateCounter = 2;	\
	} while (0)

	vString *supersOrRoles;
};

/*
 *   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, bool role);
static void leaveMoose (struct mooseSubparser *moose);
static void enteringPodNotify (perlSubparser *perl);
static void leavingPodNotify  (perlSubparser *perl);
static void findingQuotedWordNotify (perlSubparser *perl, int moduleIndex, const char *qwd);

/*
 *   DATA DEFINITIONS
 */

static struct mooseSubparser mooseSubparser = {
	.perl = {
		.subparser = {
			.direction  = SUBPARSER_BI_DIRECTION,
			.inputStart = inputStart,
			.inputEnd   = inputEnd,
			.makeTagEntryNotify = makeTagEntryNotify,
		},
		.enteringPodNotify = enteringPodNotify,
		.leavingPodNotify  = leavingPodNotify,
		.findingQuotedWordNotify = findingQuotedWordNotify,
	},
};


/*
 *   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->supersOrRoles = vStringNew ();
	moose->notContinuousExtendsLines = true;
	moose->indexForFunctionParameters = CORK_NIL;
	RESET_FunctionParameters_STATE (moose);
}

static void inputEnd (subparser *s)
{
	struct mooseSubparser *moose = (struct mooseSubparser *)s;
	tagEntryInfo *e = getEntryInCorkQueue (moose->classCork);
	if (e)
		e->extensionFields.endLine = getInputLineNumber ();

	vStringDelete (moose->supersOrRoles);
	moose->supersOrRoles = NULL;
}

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);
		setTagPositionFromTag (&moose_e, tag);
		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, false);
			else if (strcmp (tag->name, "Moose::Role") == 0)
				enterMoose (moose, true);
			else if (strcmp (tag->name, "Function::Parameters") == 0)
				moose->indexForFunctionParameters = corkIndex;
		}
		else if (isRoleAssigned(tag, ROLE_PERL_MODULE_UNUSED))
		{
			if (strcmp (tag->name, "Moose") == 0
				|| strcmp (tag->name, "Moo") == 0)
				leaveMoose (moose);
			else if (strcmp (tag->name, "Function::Parameters") == 0)
			{
				moose->indexForFunctionParameters = CORK_NIL;
				INC_FunctionParameters_STATE(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 findingQuotedWordNotify (perlSubparser *perl,
									 int moduleIndex, const char *qwd)
{
	struct mooseSubparser *moose = (struct mooseSubparser *)perl;
	if (moose->indexForFunctionParameters != moduleIndex)
		return;

	if (strcmp (qwd, ":modifiers") == 0)
		DEC_FunctionParameters_STATE(moose);
}

static void leaveMoose (struct mooseSubparser *moose)
{
	moose->notContinuousExtendsLines = true;

	tagEntryInfo *e = getEntryInCorkQueue (moose->classCork);
	if (!e)
		return;

	e->extensionFields.endLine = getInputLineNumber ();

	moose->classCork = CORK_NIL;
	moose->notInMoose = true;
	moose->packageCork = CORK_NIL;
	INC_FunctionParameters_STATE(moose);
}

static void enterMoose (struct mooseSubparser *moose, bool role)
{
	moose->notContinuousExtendsLines = true;

	tagEntryInfo *perl_e = getEntryInCorkQueue (moose->packageCork);
	if (!perl_e)
		return;

	moose->notInMoose = false;

	tagEntryInfo moose_e;
	initTagEntry (&moose_e, perl_e->name, role? K_ROLE: K_CLASS);
	moose_e.lineNumber = perl_e->lineNumber;
	moose_e.filePosition = perl_e->filePosition;
	moose->classCork = makeTagEntry (&moose_e);
	vStringClear (moose->supersOrRoles);

	DEC_FunctionParameters_STATE(moose);

	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;
	moose->notContinuousExtendsLines = true;

	if (moose->inPod)
		return true;

	tagEntryInfo *e = getEntryInCorkQueue (moose->classCork);
	if (!e)
		return true;

	const char *input = line + matches[2].start;
	vString *str = moose->supersOrRoles;

	if (vStringLength (str) > 0)
		vStringPut (str, ',');
	parseExtendsClass (input, matches[2].length, str,
					   &moose->notContinuousExtendsLines);

	if (moose->notContinuousExtendsLines == true
		&& vStringLength (str) > 0)
	{
		if (e->extensionFields.inheritance)
			eFree ((void *)e->extensionFields.inheritance);
		e->extensionFields.inheritance = vStringStrdup (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);
	if (!e)
		return true;

	const char *input = line + matches[1].start;
	vString *str = moose->supersOrRoles;

	parseExtendsClass (input, matches[1].length, str,
					   &moose->notContinuousExtendsLines);

	if (moose->notContinuousExtendsLines == true
		&& vStringLength (str) > 0)
	{
		if (e->extensionFields.inheritance)
			eFree ((void *)e->extensionFields.inheritance);
		e->extensionFields.inheritance = vStringStrdup (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 solveKindAndWrapping (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;

	solveKindAndWrapping (line + matches[1].start, &kind, &wrapping);
	parseAttributeOrWrapper (line + matches[2].start, moose->classCork, -1,
							 kind, wrapping);
	return true;
}

static bool findAttributeOrWrapperMulti (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;

	solveKindAndWrapping (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|with) *(.+)",
							  "{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}",
							  findAttributeOrWrapperMulti, &mooseSubparser.notInMoose,
							  &mooseSubparser);
	addLanguageCallbackRegex (language, "^[ \t]*(has|after|before|around|override) +"
							  "([a-zA-Z_][a-zA-Z0-9_]*)[ \t]*\\(",
							  "{exclusive}",
							  findAttributeOrWrapperOne, &mooseSubparser.notInFunctionParametersModifiers,
							  &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 = CORK_QUEUE;

	return def;
}