commit 62956d5
shrub
·
2026-11-18 19:23:04 +0000 UTC
parent 4cdb1b4
doc comments and other module fixes
M
kav.h
+1,
-0
1@@ -43,6 +43,7 @@ struct module {
2 struct module *nextseen;
3 struct scope *scope;
4 const char *path;
5+ const char *canonical;
6 stralloc src;
7 struct decl *decls;
8 int state;
M
lex.re
+9,
-1
1@@ -9,7 +9,7 @@ static void settok(struct lexer *l, struct token *t, enum tokenkind k) {
2 t->loc.line = l->line;
3 t->loc.col = l->tok - l->linestart + 1;
4 t->loc.file = l->file;
5- if(k != TNONE)
6+ if(k != TNONE && k != TDOC)
7 l->afteruse = k == TUSE;
8 }
9
10@@ -29,6 +29,7 @@ void lex(struct lexer *l, struct token *t) {
11 "\x00" { settok(l, t, TEOF); return; }
12 "//" [^\n\x00]* { goto loop; }
13 "/*" { settok(l, t, TNONE); goto comment; }
14+ "/%" { settok(l, t, TDOC); goto doc; }
15
16 //I LOVE MACROHACKS
17 !include "rules.re";
18@@ -47,6 +48,13 @@ void lex(struct lexer *l, struct token *t) {
19 "\x00" { errorat(&t->loc, "unterminated comment"); }
20 * { goto comment; }
21 */
22+ doc:
23+ /*!re2c
24+ "%/" { t->str = l->tok + 2; t->length = l->cur - t->str - 2; return; }
25+ "\n" { l->line++; l->linestart = l->cur; goto doc; }
26+ "\x00" { errorat(&t->loc, "unterminated doc comment"); }
27+ * { goto doc; }
28+ */
29 usepath:
30 /*!re2c
31 "<" [^>\n\x00]* ">" { settok(l, t, TUSEPATH); if(t->length == 2) errorat(&t->loc, "empty module path"); return; }
M
module.c
+23,
-16
1@@ -34,8 +34,8 @@ kavpath(struct token name) {
2 sa0(&sa);
3 canonical = realpath(sa.s, nil);
4 if(canonical) {
5- free(sa.s);
6- return canonical;
7+ free(canonical);
8+ return sa.s;
9 }
10 if(errno != ENOENT && errno != ENOTDIR)
11 errorat(&name.loc, "unable to resolve ", sa.s, ": ", strerror(errno));
12@@ -63,24 +63,25 @@ usepath(const char *from, struct token name) {
13 }
14
15 static struct module *
16-load(const char *path, const struct location *at) {
17+load(const char *path, const char *file, const struct location *at) {
18 struct module *m;
19 struct module *s;
20 struct decl *d;
21 char *canonical;
22 char *importpath;
23+ char *importfile;
24+ char *sourcepath;
25
26 canonical = realpath(path, nil);
27 if(!canonical) {
28 if(!at)
29- diesys("unable to resolve ", path);
30- errorat(at, "unable to resolve ", path, ": ", strerror(errno));
31+ diesys("unable to resolve ", file);
32+ errorat(at, "unable to resolve ", file, ": ", strerror(errno));
33 }
34- path = canonical;
35 for(s = seen; s; s = s->nextseen) {
36- if(strcmp(s->path, path) == 0) {
37+ if(strcmp(s->canonical, canonical) == 0) {
38 if(s->state == 1)
39- die("import cycle through ", path);
40+ die("import cycle through ", file);
41 free(canonical);
42 return s;
43 }
44@@ -89,18 +90,24 @@ load(const char *path, const struct location *at) {
45 m->state = 1;
46 m->nextseen = seen;
47 seen = m;
48- m->path = path;
49- if(!openreadclose(path, &m->src)) {
50+ sourcepath = alloc(strlen(file) + 1);
51+ strcpy(sourcepath, file);
52+ m->path = sourcepath;
53+ m->canonical = canonical;
54+ if(!openreadclose(canonical, &m->src)) {
55 if(!at)
56- diesys("unable to read ", path);
57- errorat(at, "unable to read ", path, ": ", strerror(errno));
58+ diesys("unable to read ", file);
59+ errorat(at, "unable to read ", file, ": ", strerror(errno));
60 }
61 sa0(&m->src);
62- m->decls = parse(path, m->src.s);
63+ m->decls = parse(m->path, m->src.s);
64 for(d = m->decls; d; d = d->next)
65 if(d->kind == DUSE) {
66- importpath = usepath(path, d->name);
67- d->mod = load(importpath, &d->name.loc);
68+ importpath = usepath(canonical, d->name);
69+ importfile = d->name.kind == TUSEPATH ? importpath : usepath(m->path, d->name);
70+ d->mod = load(importpath, importfile, &d->name.loc);
71+ if(importfile != importpath)
72+ free(importfile);
73 free(importpath);
74 }
75 m->state = 2;
76@@ -114,6 +121,6 @@ loadall(const char *path) {
77 seen = nil;
78 head = nil;
79 end = &head;
80- load(path, nil);
81+ load(path, path, nil);
82 return head;
83 }
M
parse.c
+4,
-1
1@@ -12,7 +12,10 @@ static struct param *sortparams(void);
2
3 static void
4 next(void) {
5- lex(&lx, &tok);
6+ /* skip doc comments for now */
7+ do {
8+ lex(&lx, &tok);
9+ } while(tok.kind == TDOC);
10 }
11
12 static int
M
tokens.h
+1,
-0
1@@ -4,6 +4,7 @@ TOKEN(TIDENT, )
2 TOKEN(TNUMBER, )
3 TOKEN(TSTRING, )
4 TOKEN(TUSEPATH, )
5+TOKEN(TDOC, )
6
7 #include "kws.h"
8 #include "punc.h"