mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-28 08:12:28 +00:00
Auto-generate some documentation
This commit is contained in:
parent
117d0111ab
commit
13437dd19b
97 changed files with 2033 additions and 661 deletions
276
tool/build/lib/javadown.c
Normal file
276
tool/build/lib/javadown.c
Normal file
|
@ -0,0 +1,276 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/limits.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "tool/build/lib/javadown.h"
|
||||
|
||||
#define FILEOVERVIEW "@fileoverview"
|
||||
|
||||
struct Lines {
|
||||
size_t n;
|
||||
struct Line {
|
||||
char *p;
|
||||
size_t n;
|
||||
} * p;
|
||||
};
|
||||
|
||||
static char *SkipEmptyFirstLine(char *p) {
|
||||
int i = 0;
|
||||
while (p[i] == ' ' || p[i] == '\t') ++i;
|
||||
return p[i] == '\n' ? p + i + 1 : p;
|
||||
}
|
||||
|
||||
static void DeleteLastEmptyLine(char *p, size_t n) {
|
||||
while (n && (p[n - 1] == ' ' || p[n - 1] == '\t')) --n;
|
||||
if (n && p[n - 1] == '\n') p[n - 1] = '\0';
|
||||
}
|
||||
|
||||
static void AppendLine(struct Lines *lines) {
|
||||
lines->p = realloc(lines->p, ++lines->n * sizeof(*lines->p));
|
||||
memset(lines->p + lines->n - 1, 0, sizeof(*lines->p));
|
||||
}
|
||||
|
||||
static void AppendTag(struct JavadownTags *tags) {
|
||||
tags->p = realloc(tags->p, ++tags->n * sizeof(*tags->p));
|
||||
memset(tags->p + tags->n - 1, 0, sizeof(*tags->p));
|
||||
}
|
||||
|
||||
static unsigned GetSpacePrefixLen(const char *p, size_t n) {
|
||||
int i;
|
||||
for (i = 0; i < n; ++i) {
|
||||
if (p[i] != ' ' && p[i] != '\t') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
static unsigned GetSpaceStarPrefixLen(const char *p, size_t n) {
|
||||
int i;
|
||||
i = GetSpacePrefixLen(p, n);
|
||||
return i < n && (p[i] == '*') ? i + 1 : 0;
|
||||
}
|
||||
|
||||
static unsigned GetTagLen(const char *p, size_t n) {
|
||||
int i;
|
||||
for (i = 0; i < n; ++i) {
|
||||
if (!islower(p[i])) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
static unsigned GetMinPrefixLen(struct Lines *lines,
|
||||
unsigned GetPrefixLen(const char *, size_t)) {
|
||||
int i;
|
||||
unsigned n, m;
|
||||
for (m = -1, i = 0; i < lines->n; ++i) {
|
||||
if (lines->p[i].n) {
|
||||
n = GetPrefixLen(lines->p[i].p, lines->p[i].n);
|
||||
if (n < m) m = n;
|
||||
}
|
||||
}
|
||||
return m == -1 ? 0 : m;
|
||||
}
|
||||
|
||||
static void RemovePrefixes(struct Lines *lines, unsigned m) {
|
||||
int i;
|
||||
for (i = 0; i < lines->n; ++i) {
|
||||
if (m <= lines->p[i].n) {
|
||||
lines->p[i].p += m;
|
||||
lines->p[i].n -= m;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void SplitLines(struct Lines *lines, char *p) {
|
||||
char *q;
|
||||
DeleteLastEmptyLine(p, strlen(p));
|
||||
p = SkipEmptyFirstLine(p);
|
||||
for (;;) {
|
||||
AppendLine(lines);
|
||||
lines->p[lines->n - 1].p = p;
|
||||
lines->p[lines->n - 1].n = (q = strchr(p, '\n')) ? q - p : strlen(p);
|
||||
if (!q) break;
|
||||
p = q + 1;
|
||||
}
|
||||
RemovePrefixes(lines, GetMinPrefixLen(lines, GetSpaceStarPrefixLen));
|
||||
RemovePrefixes(lines, GetMinPrefixLen(lines, GetSpacePrefixLen));
|
||||
}
|
||||
|
||||
static bool ConsumeFileOverview(struct Lines *lines) {
|
||||
int i;
|
||||
if (lines->n && lines->p[0].n >= strlen(FILEOVERVIEW) &&
|
||||
startswith(lines->p[0].p, FILEOVERVIEW)) {
|
||||
lines->p[0].p += strlen(FILEOVERVIEW);
|
||||
lines->p[0].n -= strlen(FILEOVERVIEW);
|
||||
while (lines->p[0].n &&
|
||||
(lines->p[0].p[0] == ' ' || lines->p[0].p[0] == '\t')) {
|
||||
lines->p[0].p += 1;
|
||||
lines->p[0].n -= 1;
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static void RemoveTrailingWhitespace(struct Lines *lines) {
|
||||
int i;
|
||||
for (i = 0; i < lines->n; ++i) {
|
||||
while (lines->p[i].n && isspace(lines->p[i].p[lines->p[i].n - 1])) {
|
||||
--lines->p[i].n;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int ExtractTitle(struct Javadown *jd, struct Lines *lines) {
|
||||
int i;
|
||||
char *p;
|
||||
size_t n;
|
||||
for (p = NULL, n = i = 0; i < lines->n; ++i) {
|
||||
if (!lines->p[i].n) {
|
||||
++i;
|
||||
break;
|
||||
}
|
||||
if (*lines->p[i].p == '@') {
|
||||
break;
|
||||
}
|
||||
if (i) {
|
||||
p = realloc(p, ++n);
|
||||
p[n - 1] = ' ';
|
||||
}
|
||||
p = realloc(p, n + lines->p[i].n);
|
||||
memcpy(p + n, lines->p[i].p, lines->p[i].n);
|
||||
n += lines->p[i].n;
|
||||
}
|
||||
p = realloc(p, n + 1);
|
||||
p[n] = '\0';
|
||||
jd->title = p;
|
||||
return i;
|
||||
}
|
||||
|
||||
static int ExtractText(struct Javadown *jd, struct Lines *lines, int i) {
|
||||
int j;
|
||||
char *p;
|
||||
size_t n;
|
||||
for (p = NULL, n = j = 0; i + j < lines->n; ++j) {
|
||||
if (lines->p[i + j].n && lines->p[i + j].p[0] == '@') break;
|
||||
if (j) {
|
||||
p = realloc(p, ++n);
|
||||
p[n - 1] = '\n';
|
||||
}
|
||||
p = realloc(p, n + lines->p[i + j].n);
|
||||
memcpy(p + n, lines->p[i + j].p, lines->p[i + j].n);
|
||||
n += lines->p[i + j].n;
|
||||
}
|
||||
p = realloc(p, n + 1);
|
||||
p[n] = '\0';
|
||||
jd->text = p;
|
||||
return i;
|
||||
}
|
||||
|
||||
static void ExtractTags(struct Javadown *jd, struct Lines *lines, int i) {
|
||||
size_t n;
|
||||
char *p, *tag, *text, *p2;
|
||||
unsigned taglen, textlen, n2;
|
||||
for (p = NULL, n = 0; i < lines->n; ++i) {
|
||||
if (!lines->p[i].n) continue;
|
||||
if (lines->p[i].p[0] != '@') continue;
|
||||
tag = lines->p[i].p + 1;
|
||||
taglen = GetTagLen(tag, lines->p[i].n - 1);
|
||||
if (!taglen) continue;
|
||||
text = tag + taglen;
|
||||
tag = strndup(tag, taglen);
|
||||
textlen = lines->p[i].n - 1 - taglen;
|
||||
while (textlen && isspace(*text)) {
|
||||
++text;
|
||||
--textlen;
|
||||
}
|
||||
text = strndup(text, textlen);
|
||||
while (i + 1 < lines->n &&
|
||||
(!lines->p[i + 1].n || lines->p[i + 1].p[0] != '@')) {
|
||||
++i;
|
||||
p2 = lines->p[i].p;
|
||||
n2 = lines->p[i].n;
|
||||
if (n2 && *p2 == '\t') {
|
||||
p2 += 1;
|
||||
n2 -= 1;
|
||||
}
|
||||
if (n2 >= 4 && !memcmp(p2, " ", 4)) {
|
||||
p2 += 4;
|
||||
n2 -= 4;
|
||||
}
|
||||
text = realloc(text, textlen + 1 + n2 + 1);
|
||||
text[textlen] = '\n';
|
||||
memcpy(text + textlen + 1, p2, n2);
|
||||
textlen += 1 + n2;
|
||||
text[textlen] = '\0';
|
||||
}
|
||||
AppendTag(&jd->tags);
|
||||
jd->tags.p[jd->tags.n - 1].tag = tag;
|
||||
jd->tags.p[jd->tags.n - 1].text = text;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses javadown.
|
||||
*
|
||||
* @param data should point to text inside the slash star markers
|
||||
* @param size is length of data in bytes
|
||||
* @return object that should be passed to FreeJavadown()
|
||||
*/
|
||||
struct Javadown *ParseJavadown(const char *data, size_t size) {
|
||||
int i;
|
||||
char *p;
|
||||
struct Lines lines;
|
||||
struct Javadown *jd;
|
||||
memset(&lines, 0, sizeof(lines));
|
||||
jd = calloc(1, sizeof(struct Javadown));
|
||||
p = strndup(data, size);
|
||||
SplitLines(&lines, p);
|
||||
RemoveTrailingWhitespace(&lines);
|
||||
jd->isfileoverview = ConsumeFileOverview(&lines);
|
||||
i = ExtractTitle(jd, &lines);
|
||||
i = ExtractText(jd, &lines, i);
|
||||
ExtractTags(jd, &lines, i);
|
||||
free(lines.p);
|
||||
free(p);
|
||||
return jd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Frees object returned by ParseJavadown().
|
||||
*/
|
||||
void FreeJavadown(struct Javadown *jd) {
|
||||
int i;
|
||||
if (jd) {
|
||||
for (i = 0; i < jd->tags.n; ++i) {
|
||||
free(jd->tags.p[i].tag);
|
||||
free(jd->tags.p[i].text);
|
||||
}
|
||||
free(jd->tags.p);
|
||||
free(jd->title);
|
||||
free(jd->text);
|
||||
free(jd);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue