mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-28 07:18:30 +00:00
Improve documentation
The Cosmo API documentation page is pretty good now https://justine.lol/cosmopolitan/documentation.html
This commit is contained in:
parent
13437dd19b
commit
1bc3a25505
367 changed files with 2542 additions and 26178 deletions
169
tool/build/lib/asmdown.c
Normal file
169
tool/build/lib/asmdown.c
Normal file
|
@ -0,0 +1,169 @@
|
|||
/*-*- 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/alg/alg.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "tool/build/lib/asmdown.h"
|
||||
#include "tool/build/lib/javadown.h"
|
||||
|
||||
static bool IsSymbolChar1(char c) {
|
||||
return (c & 0x80) || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') ||
|
||||
c == '$' || c == '_';
|
||||
}
|
||||
|
||||
static bool IsSymbolChar2(char c) {
|
||||
return (c & 0x80) || ('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') ||
|
||||
('a' <= c && c <= 'z') || c == '$' || c == '_';
|
||||
}
|
||||
|
||||
static bool IsSymbolString(const char *s) {
|
||||
int i;
|
||||
if (!IsSymbolChar1(*s++)) return false;
|
||||
while (*s) {
|
||||
if (!IsSymbolChar2(*s++)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts symbols and docstrings from .S file.
|
||||
*
|
||||
* Docstrings are multiline Fortran-style AT&T assembly comments
|
||||
* preceding a symbol that have at least one @tag line.
|
||||
*
|
||||
* @param code is contents of .S file
|
||||
* @param size is byte length of code
|
||||
* @return object that needs to be FreeAsmdown()'d
|
||||
* @note this function assumes canonical unix newlines
|
||||
*/
|
||||
struct Asmdown *ParseAsmdown(const char *code, size_t size) {
|
||||
struct Asmdown *ad;
|
||||
char *p1, *p2, *p3, *symbol, *alias;
|
||||
enum { BOL, COM, SYM, OTHER } state;
|
||||
int i, j, line, start_line, start_docstring, end_docstring, start_symbol;
|
||||
ad = calloc(1, sizeof(struct Asmdown));
|
||||
line = 1;
|
||||
start_line = 1;
|
||||
state = BOL;
|
||||
start_docstring = 0;
|
||||
end_docstring = 0;
|
||||
start_symbol = 0;
|
||||
for (i = 0; i < size; ++i) {
|
||||
switch (state) {
|
||||
case BOL:
|
||||
if (code[i] == '/') {
|
||||
start_line = line;
|
||||
start_docstring = i;
|
||||
state = COM;
|
||||
} else if (IsSymbolChar1(code[i])) {
|
||||
start_symbol = i;
|
||||
state = SYM;
|
||||
} else if (code[i] == '\n') {
|
||||
++line;
|
||||
} else if (i + 8 < size && !memcmp(code + i, "\t.alias\t", 8)) {
|
||||
p1 = code + i + 8;
|
||||
if ((p2 = strchr(p1, ',')) && (p3 = strchr(p2, '\n'))) {
|
||||
symbol = strndup(p1, p2 - p1);
|
||||
alias = strndup(p2 + 1, p3 - (p2 + 1));
|
||||
if (IsSymbolString(symbol) && IsSymbolString(alias)) {
|
||||
for (j = 0; j < ad->symbols.n; ++j) {
|
||||
if (!strcmp(ad->symbols.p[j].name, symbol)) {
|
||||
ad->symbols.p = realloc(
|
||||
ad->symbols.p, ++ad->symbols.n * sizeof(*ad->symbols.p));
|
||||
ad->symbols.p[ad->symbols.n - 1].line = ad->symbols.p[j].line;
|
||||
ad->symbols.p[ad->symbols.n - 1].name = strdup(alias);
|
||||
ad->symbols.p[ad->symbols.n - 1].is_alias = true;
|
||||
ad->symbols.p[ad->symbols.n - 1].javadown =
|
||||
ad->symbols.p[j].javadown;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
free(symbol);
|
||||
free(alias);
|
||||
}
|
||||
state = OTHER;
|
||||
} else {
|
||||
state = OTHER;
|
||||
}
|
||||
break;
|
||||
case COM:
|
||||
if (code[i] == '\n') {
|
||||
++line;
|
||||
if (i + 1 < size && code[i + 1] != '/') {
|
||||
state = BOL;
|
||||
end_docstring = i + 1;
|
||||
if (!memmem(code + start_docstring, end_docstring - start_docstring,
|
||||
"/\t@", 3)) {
|
||||
start_docstring = 0;
|
||||
end_docstring = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SYM:
|
||||
if (code[i] == ':' && end_docstring > start_docstring) {
|
||||
ad->symbols.p =
|
||||
realloc(ad->symbols.p, ++ad->symbols.n * sizeof(*ad->symbols.p));
|
||||
ad->symbols.p[ad->symbols.n - 1].line = start_line;
|
||||
ad->symbols.p[ad->symbols.n - 1].name =
|
||||
strndup(code + start_symbol, i - start_symbol);
|
||||
ad->symbols.p[ad->symbols.n - 1].is_alias = false;
|
||||
ad->symbols.p[ad->symbols.n - 1].javadown = ParseJavadown(
|
||||
code + start_docstring, end_docstring - start_docstring);
|
||||
end_docstring = 0;
|
||||
start_docstring = 0;
|
||||
state = OTHER;
|
||||
} else if (code[i] == '\n') {
|
||||
++line;
|
||||
state = BOL;
|
||||
} else if (!IsSymbolChar2(code[i])) {
|
||||
state = OTHER;
|
||||
}
|
||||
break;
|
||||
case OTHER:
|
||||
if (code[i] == '\n') {
|
||||
++line;
|
||||
state = BOL;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
unreachable;
|
||||
}
|
||||
}
|
||||
return ad;
|
||||
}
|
||||
|
||||
/**
|
||||
* Frees object returned by ParseAsmdown().
|
||||
*/
|
||||
void FreeAsmdown(struct Asmdown *ad) {
|
||||
int i;
|
||||
if (ad) {
|
||||
for (i = 0; i < ad->symbols.n; ++i) {
|
||||
free(ad->symbols.p[i].name);
|
||||
if (!ad->symbols.p[i].is_alias) {
|
||||
FreeJavadown(ad->symbols.p[i].javadown);
|
||||
}
|
||||
}
|
||||
free(ad->symbols.p);
|
||||
free(ad);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue