summaryrefslogtreecommitdiffstats
path: root/main.c
blob: 4e5bb2df6d7ec974885a3eda8f52e4849428d18f (plain) (blame)
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
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>

/**@TEXT # jodgen 1.0.0
 *
 * A documentation generator based on my own commenting specification.
 *
 * Usage:
 * ```
 * jdgen [-o OUTFILE] SRCFILE [SRCFILES...]
 * ```
 *
 * If no output file is specified, text is written to stdout,
 * meaning another valid way to write output would be:
 * ```
 * jdgen SRCFILE [SRCFILES...] > OUTFILE
 * ```
 *
 * The following tags are used to emit special text:  
 * `@TEXT` : Output the following block into the outfile.  
 * `@FUNC` : Output a description and function prototype.  
 * `@STRUCT` : Output a description after the struct definition.  
 * `@ENUM` : Output a description after the enum definition.  
 * `@PARAM` : Describes a function parameter.  
 * `@VPARAM` : Describes a variable function parameter.  
 * `@RETURN` : Describes how a function returns a value.  
 * */

/**@ENUM Token types
 * identifiers for comment format specifiers, starting with \@
 * */
enum
{
    TK_TEXT = 0, /* Text specifier. Comment block only is recorded.*/
    TK_FUNC, /* Function specifier. Comment block and function prototype is recorded.*/
    TK_PARAM, /* Description for parameter in function prototype. */
    TK_VPARAM, /* Description for variadic parameter in function prototype.*/
    TK_RETURN, /* Return description for function prototype.*/
    TK_ENUM, /* Enum specifier. First line is name, following is description, includes enum values.*/
    TK_STRUCT, /* Struct sprcifier. First line is formal name, followed by description. includes full struct definition.*/
};
typedef unsigned char token_type_t;
const char *token_typeNames[] = {
    [TK_TEXT] = "TEXT",
    [TK_FUNC] = "FUNC",
    [TK_PARAM] = "PARAM",
    [TK_VPARAM] = "VPARAM",
    [TK_RETURN] = "RETURN",
    [TK_ENUM] = "ENUM",
    [TK_STRUCT] = "STRUCT"
};
const size_t token_typeCount = sizeof(token_typeNames) / sizeof(char*);

/**@STRUCT Comment block
 * stores data for each block (or block of) comments in a given source file.
 * */
typedef struct commentBlock
{
    FILE *file; /* file that this block comment is home to. */
    size_t start; /* read offset in the file for the start of the block. */
    size_t end; /* read offset in the file for the end of the block. */
} comment_block_t;

/**@STRUCT Parsed token
 * stores data for each comment token.
 * */
typedef struct token
{
    struct token *next; /*Next token in the token stream. */
    FILE *file; /*File this token is located in.*/
    token_type_t type; /*Token ID for this token.*/
    char *proto; /*Function prototype or structure definition or enum definition.*/
    char *name; /* Function name.*/
    char text[]; /*Characters associated with this token.*/
} token_t;

static FILE *docfile;

/**@FUNC Prematurely terminates the program with a specified error message.
 * @PARAM fmt string to format and print.
 * @VPARAM arguments to format text with.
 * */
__attribute__((noreturn)) void
fail(const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    abort();
}

/**@FUNC Extracts the next block of comments from the current position in the 
 * file stream.
 * Supports double slash comments and block comments.
 * Moves the file stream to the end of the comment block it finds.
 * @PARAM srcfile file to read a comment block from.
 * @RETURN found comment block. NULL upon failure.
 * */
static comment_block_t*
next_comment_block(FILE *srcfile)
{
    char c;
    comment_block_t *comment = calloc(1, sizeof(comment_block_t));
    comment->file = srcfile;

    bool is_block = false;

    while((c = fgetc(srcfile)) != EOF) {
        if(c != '/') continue;

        c = fgetc(srcfile);
        if(c == '*') is_block = true;
        else if(c == '/') is_block = false;
        else continue;

        comment->start = ftell(srcfile);
        while((c = fgetc(srcfile)) != EOF) {
            comment->end = ftell(srcfile);
            if(is_block) {
                if(c != '*') continue;
                c = fgetc(srcfile);
                if(c != '/') continue;
                comment->end += 2;
                return comment;
            }else{
                if(c != '\n') continue;
                while(isspace(c = fgetc(srcfile))) continue;
                if(c == '/' && fgetc(srcfile) == '/') continue;
                comment->end--;
                return comment;
            }
        }
    }
    free(comment);
    return NULL;
}

static token_t*
s_parse_token_text(comment_block_t *comment, char *buffer, size_t *bufferi)
{
    size_t textw = strlen(&buffer[*bufferi]);
    token_t *token = calloc(1, sizeof(token_t) + textw);
    token->file = comment->file;
    token->proto = NULL;
    token->type = TK_TEXT;
    memcpy(token->text, &buffer[*bufferi], textw);

    while(buffer[*bufferi]) (*bufferi)++;
    return token;
}

static size_t
s_get_token_desc(char *dest, const char *src)
{
    size_t srcw = strlen(src);
    size_t writew = 0;
    size_t extra = 0;
    for(size_t i = 0; i < srcw; i++) {
        if(src[i] == '\\') {
            dest[writew++] = src[++i];
            extra++;
            continue;
        }
        if(src[i] == '@') break;
        dest[writew++] = src[i];
    }
    if(dest[writew - 1] == '\n') dest[--writew] = 0;
    return writew + extra;
}

static token_t*
s_parse_token_func(comment_block_t *comment, char *buffer, size_t *bufferi)
{
    size_t bufferw = strlen(buffer);
    size_t textw = bufferw - *bufferi;
    token_t *token = calloc(1, sizeof(token_t) + textw);
    (*bufferi) += s_get_token_desc(token->text, &buffer[*bufferi]);

    token->file = comment->file;
    token->type = TK_FUNC;
    //Locate function prototype.
    size_t proto_len = 0;
    fseek(comment->file, comment->end, SEEK_SET);
    for(char c; (c = fgetc(comment->file)) != EOF && c != ';' && c != '{'; proto_len++);
    fseek(comment->file, comment->end, SEEK_SET);
    token->proto = calloc(1, proto_len + 1);
    proto_len = fread(token->proto, proto_len, 1, comment->file);

    //Locate function name
    size_t name_len = 0;
    fseek(comment->file, comment->end, SEEK_SET);
    for(char c, lastc = 0; (c = fgetc(comment->file)) != EOF; lastc = c) {
        if(c == '(' && lastc != '_' && lastc != '(') break;
    }
    do { fseek(comment->file, -2, SEEK_CUR); name_len++; } while(!isspace(fgetc(comment->file)));
    token->name = calloc(1, name_len);
    name_len = fread(token->name, name_len - 1, 1, comment->file);

    for(char *s = token->proto; *s; s++) if(*s == '\n') *s = ' ';
    return token;
}

static token_t*
s_parse_token_structlike(comment_block_t *comment, char *buffer, size_t *bufferi, token_type_t type)
{
    size_t bufferw = strlen(buffer);
    size_t textw = bufferw - *bufferi;
    token_t *token = calloc(1, sizeof(token_t) + textw);
    (*bufferi) += s_get_token_desc(token->text, &buffer[*bufferi]);

    token->file = comment->file;
    token->type = type;
    //Locate function prototype.
    size_t proto_len = 0;
    fseek(comment->file, comment->end, SEEK_SET);

    bool is_typedef = false;
    for(char c; (c = fgetc(comment->file)) != EOF; proto_len++) {
        if(c == '}') {
            c = fgetc(comment->file);
            proto_len++;
            if(c == ';') break;
            is_typedef = true;
        }
        if(is_typedef && c == ';') break;
    }
    proto_len++;

    fseek(comment->file, comment->end, SEEK_SET);
    token->proto = calloc(1, proto_len + 1);
    proto_len = fread(token->proto, proto_len, 1, comment->file);

    return token;
}

static token_t*
s_parse_token_struct(comment_block_t *comment, char *buffer, size_t *bufferi)
{ return s_parse_token_structlike(comment, buffer, bufferi, TK_STRUCT); }

static token_t*
s_parse_token_enum(comment_block_t *comment, char *buffer, size_t *bufferi)
{ return s_parse_token_structlike(comment, buffer, bufferi, TK_ENUM); }

static token_t*
s_parse_token_param(comment_block_t *comment, char *buffer, size_t *bufferi)
{
    size_t bufferw = strlen(buffer);
    size_t textw = bufferw - *bufferi;
    token_t *token = calloc(1, sizeof(token_t) + textw);
    
    //Locate variable name.
    size_t protow = 0;
    for(; !isspace(buffer[*bufferi + protow]); protow++);
    token->proto = calloc(protow + 1, 1);
    memcpy(token->proto, &buffer[*bufferi], protow);
    *bufferi += protow + 1;

    //Copy param description.
    (*bufferi) += s_get_token_desc(token->text, &buffer[*bufferi]);

    token->file = comment->file;
    token->type = TK_PARAM;

    return token;
}

static token_t*
s_parse_token_vparam(comment_block_t *comment, char *buffer, size_t *bufferi)
{
    size_t bufferw = strlen(buffer);
    size_t textw = bufferw - *bufferi;
    token_t *token = calloc(1, sizeof(token_t) + textw);

    //Copy param description.
    (*bufferi) += s_get_token_desc(token->text, &buffer[*bufferi]);

    token->file = comment->file;
    token->type = TK_VPARAM;

    return token;
}

static token_t*
s_parse_token_return(comment_block_t *comment, char *buffer, size_t *bufferi)
{
    size_t bufferw = strlen(buffer);
    size_t textw = bufferw - *bufferi;
    token_t *token = calloc(1, sizeof(token_t) + textw);

    //Copy param description.
    (*bufferi) += s_get_token_desc(token->text, &buffer[*bufferi]);

    token->file = comment->file;
    token->type = TK_RETURN;

    return token;
}

static token_t*
s_eval_token_text(comment_block_t *comment, token_t *token)
{
    (void)comment;
    token_t *next = token->next;
 
    fprintf(docfile, "%s\n\n***\n", token->text);

    free(token);
    return next;
}

static void
s_md_codeblock(const char *text)
{
    fprintf(docfile, "```\n%s\n```\n", text);

}

static token_t*
s_eval_token_func(comment_block_t *comment, token_t *token)
{
    (void)comment;
    token_t *next = token->next;
    
    fprintf(docfile, "#### %s\n\n", token->name);
    fprintf(docfile, "%s\n\n", token->text);
    s_md_codeblock(token->proto);

    free(token->proto);
    free(token->name);
    free(token);
    return next;
}

static token_t*
s_eval_token_enum(comment_block_t *comment, token_t *token)
{
    (void)comment;
    token_t *next = token->next;
    
    s_md_codeblock(token->proto);
    fprintf(docfile, "%s\n\n***\n", token->text);

    free(token->proto);
    free(token);
    return next;
}

static token_t*
s_eval_token_param(comment_block_t *comment, token_t *token)
{
    (void)comment;
    token_t *next = token->next;
    
    fprintf(docfile, "`%s` : ", token->proto);
    fprintf(docfile, "%s\n\n", token->text);

    free(token);
    return next;
}

static token_t*
s_eval_token_vparam(comment_block_t *comment, token_t *token)
{
    (void)comment;
    token_t *next = token->next;
    
    fprintf(docfile, "`...` : %s\n\n", token->text);

    free(token);
    return next;
}

static token_t*
s_eval_token_return(comment_block_t *comment, token_t *token)
{
    (void)comment;
    token_t *next = token->next;
    
    fprintf(docfile, "`return` : %s\n\n", token->text);

    free(token);
    return next;
}

static token_t *(*tokenParsers[])(comment_block_t*, char*, size_t*) = {
    [TK_TEXT] = s_parse_token_text,
    [TK_FUNC] = s_parse_token_func,
    [TK_PARAM] = s_parse_token_param,
    [TK_VPARAM] = s_parse_token_vparam,
    [TK_RETURN] = s_parse_token_return,
    [TK_ENUM] = s_parse_token_enum,
    [TK_STRUCT] = s_parse_token_struct
};

typedef token_t *(*token_evaluator_t)(comment_block_t*, token_t*);
static token_evaluator_t tokenEvaluators[] = {
    [TK_TEXT] = s_eval_token_text,
    [TK_FUNC] = s_eval_token_func,
    [TK_PARAM] = s_eval_token_param,
    [TK_VPARAM] = s_eval_token_vparam,
    [TK_RETURN] = s_eval_token_return,
    [TK_ENUM] = s_eval_token_enum,
    [TK_STRUCT] = s_eval_token_enum
};

token_t*
tokenize_comment_stripped(comment_block_t *comment, char *buffer)
{
    token_t *tokenTail = NULL, *tokenHead = NULL;
    for(size_t i = 0; buffer[i]; i++) {
        if(buffer[i] != '@') continue;

        size_t bufferWidth = strlen(&buffer[++i]);
        /*Look for token keywords.*/

        token_t *token = NULL;
        for(size_t tki = 0; tki < token_typeCount; tki++) {
            const char *tokenName = token_typeNames[tki];
            size_t tokenNameW = strlen(tokenName);

            if(bufferWidth < tokenNameW 
                    || strncmp(&buffer[i], tokenName, tokenNameW) 
                    || buffer[i + tokenNameW] != ' ') continue;
            if(tokenParsers[tki] == NULL) continue;

            i += tokenNameW + 1;
            token = tokenParsers[tki](comment, buffer, &i);
            break;
        }
        if(token == NULL) continue;
        if(tokenTail == NULL) tokenTail = tokenHead = token;
        else {
            tokenHead->next = token;
            tokenHead = token;
        }
    }
    return tokenTail;
}

/**@FUNC Converts a comment block into a stream of tokens.
 * @PARAM srcfile file to source comment data from.
 * @PARAM comment data to use to read comment block.
 * */
token_t*
tokenize_comment(FILE *srcfile, comment_block_t *comment)
{
    size_t commentSize = comment->end - comment->start;
    char *commentBuffer = calloc(commentSize, 1);
    size_t strippedSize = 0;

    long opos = ftell(srcfile);
    fseek(srcfile, comment->start, SEEK_SET);
    size_t readsize = fread(commentBuffer, 1, commentSize, srcfile);
    fseek(srcfile, opos, SEEK_SET);

    if(commentSize > readsize) commentSize = readsize;

    //First, strip the comment of comment characters.
    for(size_t i = 0; i < commentSize; i++) {
        char c = commentBuffer[i];
        if(isspace(c) || c == '*' || c == '/') continue;
        while(c != '\n' && i < commentSize) {
            commentBuffer[strippedSize++] = c;
            c = commentBuffer[++i];

            if(c == '\n') 
                commentBuffer[strippedSize++] = c;
            if(i >= commentSize) break;
        }
    }

    if(commentBuffer[strippedSize - 2] == '/' &&
       commentBuffer[strippedSize - 3] == '*') strippedSize -= 3;

    commentBuffer[strippedSize] = 0;
    return tokenize_comment_stripped(comment, commentBuffer);
}

void
process_comment(comment_block_t *comment)
{
    token_t *token = tokenize_comment(comment->file, comment);
    while(token) {
        token_evaluator_t eval = tokenEvaluators[token->type];
        if(!eval) {
            token_t *next = token->next;
            free(token);
            token = next;
            continue;
        }
        token = eval(comment, token);
    }
}

int
main(int argc, char *argv[])
{
    /* TODO: Proper file IO.*/
    char *ovalue = NULL;
    int opt;
    while((opt = getopt(argc, argv, "o:")) != -1) {
        switch(opt) {
            case 'o':
                ovalue = optarg;
                break;
            case '?':
                if(optopt == 'o')
                    fail("Option %c requires an argument.\n", optopt);
                else if(isprint(optopt))
                    fail("Unknown option '%c'\n", optopt);
                else 
                    fail("Unknown option character '\\x%x'.\n", optopt);
            default:
                abort();
        }
    }

    if(ovalue) {
        docfile = fopen(ovalue, "w");
        if(!docfile)
            fail("Failed to open output file '%s' for writing\n", ovalue);
    }else docfile = stdout;

    int nsrc = 0;
    for(int i = optind; i < argc; i++) {
        char *srcstr = argv[i];
        FILE *srcfile = fopen(srcstr, "r");
        if(!srcfile) fail("Failed to open source file '%s' for reading\n", srcstr);

        comment_block_t *block = NULL;
        while((block = next_comment_block(srcfile))) {
            process_comment(block);
            free(block);
        }

        fclose(srcfile);
        nsrc++;
    }

    if(nsrc == 0)
        fail("Missing input files.\n");

    return 0;
}