File: perl6.c

package info (click to toggle)
universal-ctags 0%2Bgit20200824-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 22,092 kB
  • sloc: ansic: 103,112; lisp: 7,241; sh: 6,962; vhdl: 5,924; perl: 2,014; cpp: 1,928; python: 1,828; javascript: 1,529; cs: 1,193; sql: 587; php: 544; f90: 534; makefile: 500; ruby: 498; yacc: 459; asm: 358; fortran: 341; xml: 308; objc: 289; ada: 273; tcl: 205; java: 157; cobol: 122; erlang: 61; ml: 49; awk: 43
file content (338 lines) | stat: -rw-r--r-- 10,162 bytes parent folder | download | duplicates (4)
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
/*
 * perl6.c -- Perl6 parser.
 * Author: Dmitri Tikhonov <dmitri@cpan.org>
 *
 * This is a very basic Perl 6 parser.  It does not know how to:
 *   - skip POD;
 *   - skip multiline comments;
 *   - skip quoted strings;
 *   - generate fully-qualified tags.
 *
 * 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.
 */

#include "general.h"    /* must always come first */

#include <stdio.h>
#include <string.h>

#include "debug.h"
#include "entry.h"
#include "parse.h"
#include "read.h"
#include "routines.h"
#include "selectors.h"
#include "vstring.h"

enum perl6Kind {
    K_NONE = -1,
    K_CLASS,
    K_GRAMMAR,
    K_METHOD,
    K_MODULE,
    K_PACKAGE,
    K_ROLE,
    K_RULE,
    K_SUBMETHOD,
    K_SUBROUTINE,
    K_TOKEN,
};

static kindDefinition perl6Kinds[] = {
    [K_CLASS]       = { true,  'c', "class",      "classes" },
    [K_GRAMMAR]     = { true,  'g', "grammar",    "grammars" },
    [K_METHOD]      = { true,  'm', "method",     "methods" },
    [K_MODULE]      = { true,  'o', "module",     "modules" },
    [K_PACKAGE]     = { true,  'p', "package",    "packages" },
    [K_ROLE]        = { true,  'r', "role",       "roles" },
    [K_RULE]        = { true,  'u', "rule",       "rules" },
    [K_SUBMETHOD]   = { true,  'b', "submethod",  "submethods" },
    [K_SUBROUTINE]  = { true,  's', "subroutine", "subroutines" },
    [K_TOKEN]       = { true,  't', "token",      "tokens" },
};

enum token {
    T_CLASS,
    T_GRAMMAR,
    T_METHOD,
    T_MODULE,
    T_MULTI,
    T_MY,
    T_OUR,
    T_PACKAGE,
    T_PROTO,
    T_ROLE,
    T_RULE,
    T_SUB,
    T_SUBMETHOD,
    T_UNIT,
    T_TOKEN,
};

static const enum perl6Kind token2kind[] = {
    [T_CLASS]       = K_CLASS,
    [T_GRAMMAR]     = K_GRAMMAR,
    [T_METHOD]      = K_METHOD,
    [T_MODULE]      = K_MODULE,
    [T_MULTI]       = K_SUBROUTINE,
    [T_MY]          = K_NONE,
    [T_OUR]         = K_NONE,
    [T_PACKAGE]     = K_PACKAGE,
    [T_PROTO]       = K_NONE,
    [T_ROLE]        = K_ROLE,
    [T_RULE]        = K_RULE,
    [T_SUB]         = K_SUBROUTINE,
    [T_SUBMETHOD]   = K_SUBMETHOD,
    [T_UNIT]        = K_NONE,
    [T_TOKEN]       = K_TOKEN,
};

#define STRLEN(s) (sizeof(s) - 1)
#define STREQN(s, token) (0 == strncmp(s, token, STRLEN(token)))

static enum token
matchToken (const char *s, int len)
{
    switch (len) {
        case 2:
            if (STREQN(s, "my"))                return T_MY;
            break;
        case 3:
            switch (s[0]) {
                case 'o':
                    if (STREQN(s, "our"))       return T_OUR;
                    break;
                case 's':
                    if (STREQN(s, "sub"))       return T_SUB;
                    break;
            }
            break;
        case 4:
            switch (s[1]) {
                case 'o':
                    if (STREQN(s, "role"))      return T_ROLE;
                    break;
                case 'u':
                    if (STREQN(s, "rule"))      return T_RULE;
                    break;
                case 'n':
                    if (STREQN(s, "unit"))      return T_UNIT;
                    break;
            }
            break;
        case 5:
            switch (s[0]) {
                case 'c':
                    if (STREQN(s, "class"))     return T_CLASS;
                    break;
                case 'm':
                    if (STREQN(s, "multi"))     return T_MULTI;
                    break;
                case 'p':
                    if (STREQN(s, "proto"))     return T_PROTO;
                    break;
                case 't':
                    if (STREQN(s, "token"))     return T_TOKEN;
                    break;
            }
            break;
        case 6:
            switch (s[1]) {
                case 'e':
                    if (STREQN(s, "method"))    return T_METHOD;
                    break;
                case 'o':
                    if (STREQN(s, "module"))    return T_MODULE;
                    break;
            }
            break;
        case 7:
            switch (s[0]) {
                case 'g':
                    if (STREQN(s, "grammar"))   return T_GRAMMAR;
                    break;
                case 'p':
                    if (STREQN(s, "package"))   return T_PACKAGE;
                    break;
            }
            break;
        case 9:
            if (STREQN(s, "submethod"))         return T_SUBMETHOD;
            break;
    }
    return -1;
}

static const int validPerl6Identifier[0x100] = {
/* r!perl -e "print qq([(int)'\$_'] = 1,\n)for a..z,A..Z,0..9,':','-','_'"|fmt
 */
    [(int)'a'] = 1, [(int)'b'] = 1, [(int)'c'] = 1, [(int)'d'] = 1,
    [(int)'e'] = 1, [(int)'f'] = 1, [(int)'g'] = 1, [(int)'h'] = 1,
    [(int)'i'] = 1, [(int)'j'] = 1, [(int)'k'] = 1, [(int)'l'] = 1,
    [(int)'m'] = 1, [(int)'n'] = 1, [(int)'o'] = 1, [(int)'p'] = 1,
    [(int)'q'] = 1, [(int)'r'] = 1, [(int)'s'] = 1, [(int)'t'] = 1,
    [(int)'u'] = 1, [(int)'v'] = 1, [(int)'w'] = 1, [(int)'x'] = 1,
    [(int)'y'] = 1, [(int)'z'] = 1, [(int)'A'] = 1, [(int)'B'] = 1,
    [(int)'C'] = 1, [(int)'D'] = 1, [(int)'E'] = 1, [(int)'F'] = 1,
    [(int)'G'] = 1, [(int)'H'] = 1, [(int)'I'] = 1, [(int)'J'] = 1,
    [(int)'K'] = 1, [(int)'L'] = 1, [(int)'M'] = 1, [(int)'N'] = 1,
    [(int)'O'] = 1, [(int)'P'] = 1, [(int)'Q'] = 1, [(int)'R'] = 1,
    [(int)'S'] = 1, [(int)'T'] = 1, [(int)'U'] = 1, [(int)'V'] = 1,
    [(int)'W'] = 1, [(int)'X'] = 1, [(int)'Y'] = 1, [(int)'Z'] = 1,
    [(int)'0'] = 1, [(int)'1'] = 1, [(int)'2'] = 1, [(int)'3'] = 1,
    [(int)'4'] = 1, [(int)'5'] = 1, [(int)'6'] = 1, [(int)'7'] = 1,
    [(int)'8'] = 1, [(int)'9'] = 1, [(int)':'] = 1, [(int)'-'] = 1,
    [(int)'_'] = 1,
};

static const int validMethodPrefix[0x100] = {
    [(int)'!'] = 1, [(int)'^'] = 1,
};

static const int kindMayHaveMethodPrefix = (1 << K_SUBMETHOD) |
                                           (1 << K_METHOD)    ;

/* Trim identifier pointed to by ps, possibly advancing it, and return
 * the length of the valid portion.  If the returned value is zero, the
 * identifier is invalid.
 */
static int
trimIdentifier (enum perl6Kind kind, const char **ps, int len)
{
    Assert(len > 0);
    const char *const end = *ps + len;
    const char *s = *ps;
    /* Trim the front if possible: */
    s += (kindMayHaveMethodPrefix & (1 << kind)) &&
         validMethodPrefix[(int)*s];
    /* Record the start of identifier: */
    *ps = s;
    /* Continuous string of valid characters: */
    while (s < end && validPerl6Identifier[(int)*s])
        ++s;
    /* sub multi infix:<...>        -- we want the "infix" only */
    while (s - *ps > 0 && ':' == s[-1])
        --s;
    /* It's ok if this is zero: */
    return s - *ps;
}

struct p6Ctx {
    enum token  tokens[128 /* unlikely to need more than this */];
    unsigned int n_tokens;
    vString    *name;
    const char *line;      /* Saved from readLineFromInputFile() */
};

static void
makeTag (struct p6Ctx *ctx, int kind, const char *name, int len)
{
    tagEntryInfo entry;
    vStringNCopyS(ctx->name, name, len);
    initTagEntry(&entry, vStringValue(ctx->name), kind);
    makeTagEntry(&entry);
}

static void
possiblyMakeTag (struct p6Ctx *ctx, const char *s, int len)
{
    Assert(ctx->n_tokens > 0);
    enum perl6Kind kind = token2kind[ ctx->tokens[ctx->n_tokens - 1] ];
    if (K_NONE != kind && perl6Kinds[kind].enabled
                       && (len = trimIdentifier(kind, &s, len)) > 0)
        makeTag(ctx, kind, s, len);
}

static void
initP6Ctx (struct p6Ctx *ctx)
{
    ctx->n_tokens = 0;
    ctx->name = vStringNew();
    ctx->line = NULL;
}

static void
deinitP6Ctx (struct p6Ctx *ctx)
{
    vStringDelete(ctx->name);
}

/* Read next contiguous sequence of non-whitespace characters, store
 * the address in `ptok', and return its length.  Return value of zero
 * means EOF.
 *
 * TODO: Currently, POD and multi-line comments are not handled.
 */
static int
getNonSpaceStr (struct p6Ctx *ctx, const char **ptok)
{
    const char *s = ctx->line;
    if (!s) {
next_line:
        s = (const char *) readLineFromInputFile();
        if (!s)
            return 0;                           /* EOF */
    }
    while (*s && isspace(*s))                   /* Skip whitespace */
        ++s;
    if ('#' == *s)
        goto next_line;
    int non_white_len = strcspn(s, ",; \t");
    if (non_white_len) {
        ctx->line = s + non_white_len;          /* Save state */
        *ptok = s;
        return non_white_len;
    } else
        goto next_line;
}

static void
findPerl6Tags (void)
{
    struct p6Ctx ctx;

#define RESET_TOKENS() do { ctx.n_tokens = 0; } while (0)

#define PUSH_TOKEN(_t_) do {                                            \
    if (ctx.n_tokens < ARRAY_SIZE(ctx.tokens)) {			\
        ctx.tokens[ ctx.n_tokens ] = _t_;                               \
        ++ctx.n_tokens;                                                 \
    } else {                                                            \
        Assert(!"Token stack overflown: this is quite odd");            \
        RESET_TOKENS();                                                 \
    }                                                                   \
} while (0)

    initP6Ctx(&ctx);

    const char *s;
    int len;

    while ((len = getNonSpaceStr(&ctx, &s)) > 0) {
        enum token token = matchToken(s, len);
        if ((int) token >= 0) {
            PUSH_TOKEN(token);
        } else if (ctx.n_tokens > 0) {
            possiblyMakeTag(&ctx, s, len);
            RESET_TOKENS();
        }
    }

    deinitP6Ctx(&ctx);
}

parserDefinition *
Perl6Parser (void)
{
    static const char *const extensions[] = { "p6", "pm6", "pm", "pl6", NULL };
    static selectLanguage selectors [] = { selectByPickingPerlVersion,
					   NULL };
    parserDefinition* def = parserNew("Perl6");
    def->kindTable      = perl6Kinds;
    def->kindCount  = ARRAY_SIZE(perl6Kinds);
    def->extensions = extensions;
    def->parser     = findPerl6Tags;
    def->selectLanguage = selectors;
    return def;
}