Skip to content

Commit 2ff9d9e

Browse files
authored
feat(graindoc)!: Replace module attribute with docblock on module header (#1647)
chore(compiler): Add module_name to typedtree chore(graindoc): Avoid catch-all case for ttop variants chore(stdlib): Remove module attribute from module docblocks fix(stdlib)!: Use correct casing for BigInt module name chore(stdlib): Regenerate docs chore(graindoc): Cleanup module name defaults & utilities
1 parent 9408568 commit 2ff9d9e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+521
-444
lines changed

compiler/graindoc/docblock.re

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ let enumerate_exports = stmts => {
8181
},
8282
vbinds,
8383
)
84-
| _ => ()
84+
| TTopModule(_)
85+
| TTopInclude(_)
86+
| TTopException(_)
87+
| TTopExpr(_) => ()
8588
};
8689
};
8790
});
@@ -95,12 +98,6 @@ let location_for_ident = (~exports, ident) => {
9598
snd(Ident.find_name(Ident.name(ident), exports));
9699
};
97100

98-
let module_name_of_location = (loc: Grain_parsing.Location.t) => {
99-
Grain_utils.Filepath.String.filename_to_module_name(
100-
loc.loc_start.pos_fname,
101-
);
102-
};
103-
104101
let title_for_api = (~module_name, ident: Ident.t) => {
105102
Format.asprintf("%s.**%a**", module_name, Printtyp.ident, ident);
106103
};
@@ -148,8 +145,8 @@ let lookup_type_expr = (~idx, type_exprs) => {
148145
let for_value_description =
149146
(
150147
~comments,
151-
~loc,
152-
~module_name=module_name_of_location(loc),
148+
~loc: Grain_parsing.Location.t,
149+
~module_name,
153150
~ident: Ident.t,
154151
vd: Types.value_description,
155152
) => {
@@ -188,8 +185,8 @@ let for_value_description =
188185
let for_type_declaration =
189186
(
190187
~comments,
191-
~loc,
192-
~module_name=module_name_of_location(loc),
188+
~loc: Grain_parsing.Location.t,
189+
~module_name,
193190
~ident: Ident.t,
194191
td: Types.type_declaration,
195192
) => {
@@ -211,19 +208,19 @@ let for_signature_item =
211208
(
212209
~comments,
213210
~exports: Ident.tbl(Grain_parsing.Location.t),
214-
~module_name=?,
211+
~module_name,
215212
sig_item: Types.signature_item,
216213
) => {
217214
switch (sig_item) {
218215
| TSigValue(ident, vd) =>
219216
let loc = location_for_ident(~exports, ident);
220217
let docblock =
221-
for_value_description(~comments, ~module_name?, ~ident, ~loc, vd);
218+
for_value_description(~comments, ~module_name, ~ident, ~loc, vd);
222219
Some(docblock);
223220
| TSigType(ident, td, _rec) =>
224221
let loc = location_for_ident(~exports, ident);
225222
let docblock =
226-
for_type_declaration(~comments, ~module_name?, ~ident, ~loc, td);
223+
for_type_declaration(~comments, ~module_name, ~ident, ~loc, td);
227224
Some(docblock);
228225
| _ => None
229226
};

compiler/graindoc/graindoc.re

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,14 @@ let generate_docs =
8787
let signature_items = program.signature.cmi_sign;
8888

8989
let buf = Buffer.create(0);
90-
let module_comment = Comments.Doc.find_module(comments);
91-
let module_name = ref(None);
90+
let module_name = program.module_name.txt;
91+
let module_comment =
92+
Comments.Doc.ending_on(
93+
~lnum=program.module_name.loc.loc_start.pos_lnum - 1,
94+
comments,
95+
);
96+
97+
Buffer.add_string(buf, Markdown.frontmatter([("title", module_name)]));
9298
switch (module_comment) {
9399
| Some((_, desc, attrs)) =>
94100
let deprecations =
@@ -104,31 +110,20 @@ let generate_docs =
104110
}
105111
});
106112

107-
// TODO(#787): Should we fail if more than one `@module` attribute?
108-
let module_attr = attrs |> List.find(Comments.Attribute.is_module);
109-
switch (module_attr) {
110-
| Module({attr_name, attr_desc}) =>
111-
module_name := Some(attr_name);
112-
Buffer.add_string(buf, Markdown.frontmatter([("title", attr_name)]));
113-
if (List.length(deprecations) > 0) {
114-
List.iter(
115-
msg =>
116-
Buffer.add_string(
117-
buf,
118-
Markdown.blockquote(
119-
Markdown.bold("Deprecated:") ++ " " ++ msg,
120-
),
121-
),
122-
deprecations,
123-
);
124-
};
125-
Buffer.add_string(buf, Markdown.paragraph(attr_desc));
126-
switch (desc) {
127-
// Guard isn't be needed because we turn an empty string into None during extraction
128-
| Some(desc) => Buffer.add_string(buf, Markdown.paragraph(desc))
129-
| None => ()
130-
};
131-
| _ => failwith("Unreachable: Non-`module` attribute can't exist here.")
113+
if (List.length(deprecations) > 0) {
114+
List.iter(
115+
msg =>
116+
Buffer.add_string(
117+
buf,
118+
Markdown.blockquote(Markdown.bold("Deprecated:") ++ " " ++ msg),
119+
),
120+
deprecations,
121+
);
122+
};
123+
124+
switch (desc) {
125+
| Some(desc) => Buffer.add_string(buf, Markdown.paragraph(desc))
126+
| None => ()
132127
};
133128

134129
// TODO(#787): Should we fail if more than one `@since` attribute?
@@ -187,13 +182,12 @@ let generate_docs =
187182
| None => ()
188183
};
189184

190-
let module_name = module_name^;
191185
let add_docblock = sig_item => {
192186
let docblock =
193187
Docblock.for_signature_item(
194188
~comments,
195189
~exports,
196-
~module_name?,
190+
~module_name,
197191
sig_item,
198192
);
199193
switch (docblock) {

compiler/src/diagnostics/comments.re

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ module Attribute = {
2323
attr_desc,
2424
attr_type,
2525
})
26-
| Module({
27-
attr_name,
28-
attr_desc,
29-
})
3026
// Currently only accepts single-line examples
3127
| Example({attr_desc})
3228
| Section({
@@ -72,19 +68,6 @@ module Attribute = {
7268
};
7369
};
7470

75-
let parse_module = (~attr, content) => {
76-
let re = Str.regexp({|^\([^:]+\):[ ]+\(.+\)$|});
77-
if (Str.string_match(re, content, 0)) {
78-
let attr_name = Str.matched_group(1, content);
79-
let attr_desc = Str.matched_group(2, content);
80-
Module({attr_name, attr_desc});
81-
} else {
82-
raise(
83-
MalformedAttribute(attr, "@module ModuleName: Description of module"),
84-
);
85-
};
86-
};
87-
8871
let parse_example = (~attr, content) => {
8972
let re = Str.regexp({|^\(.+\)$|});
9073
if (Str.string_match(re, content, 0)) {
@@ -181,9 +164,6 @@ module Attribute = {
181164
| "returns" =>
182165
let returns_attr = parse_returns(~attr, content);
183166
attrs := [returns_attr, ...attrs^];
184-
| "module" =>
185-
let module_attr = parse_module(~attr, content);
186-
attrs := [module_attr, ...attrs^];
187167
| "example" =>
188168
let example_attr = parse_example(~attr, content);
189169
attrs := [example_attr, ...attrs^];
@@ -235,13 +215,6 @@ module Attribute = {
235215
};
236216
};
237217

238-
let is_module = (attr: t) => {
239-
switch (attr) {
240-
| Module(_) => true
241-
| _ => false
242-
};
243-
};
244-
245218
let is_example = (attr: t) => {
246219
switch (attr) {
247220
| Example(_) => true
@@ -414,20 +387,6 @@ module Doc = {
414387
ending_on_lnum_help(lnum, true);
415388
};
416389

417-
let find_module = (module C: OrderedComments) => {
418-
let module_comments = ref([]);
419-
C.iter((_, (_comment, _desc, attrs) as comment) =>
420-
if (List.exists(Attribute.is_module, attrs)) {
421-
module_comments := [comment, ...module_comments^];
422-
}
423-
);
424-
if (List.length(module_comments^) > 1) {
425-
failwith("More than one @module block is not supported");
426-
} else {
427-
List.nth_opt(module_comments^, 0);
428-
};
429-
};
430-
431390
let find_sections = (module C: OrderedComments) => {
432391
let section_comments = ref([]);
433392
C.iter((_, (_comment, _desc, attrs) as comment) =>

compiler/src/typed/typedtree.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ type comment =
587587

588588
[@deriving sexp]
589589
type typed_program = {
590+
module_name: loc(string),
590591
statements: list(toplevel_stmt),
591592
env: [@sexp.opaque] Env.t,
592593
signature: Cmi_format.cmi_infos,

compiler/src/typed/typedtree.rei

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ type comment =
548548

549549
[@deriving sexp]
550550
type typed_program = {
551+
module_name: loc(string),
551552
statements: list(toplevel_stmt),
552553
env: Env.t,
553554
signature: Cmi_format.cmi_infos,

compiler/src/typed/typemod.re

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,13 @@ let type_implementation = prog => {
947947
check_nongen_schemes(finalenv, simple_sg);
948948
let normalized_sig = normalize_signature(finalenv, simple_sg);
949949
let signature = Env.build_signature(normalized_sig, module_name, filename);
950-
{statements, env: finalenv, signature, comments: prog.comments};
950+
{
951+
module_name: prog.module_name,
952+
statements,
953+
env: finalenv,
954+
signature,
955+
comments: prog.comments,
956+
};
951957
};
952958

953959
/* Error report */

compiler/src/utils/filepath.re

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,6 @@ module String = {
6363
Printf.sprintf("%s.%s", remove_extension(baseName), newExt);
6464
};
6565

66-
// TODO(#216): Turn this into a function that only operates on Fp
67-
let filename_to_module_name = fname => {
68-
let baseName =
69-
Option.bind(from_string(fname), p =>
70-
switch (p) {
71-
| Absolute(path) => Fp.baseName(path)
72-
| Relative(path) => Fp.baseName(path)
73-
}
74-
);
75-
let name =
76-
switch (baseName) {
77-
| Some(baseName) => remove_extension(baseName)
78-
| None =>
79-
raise(
80-
Invalid_argument(
81-
Printf.sprintf("Invalid filepath (fname: '%s')", fname),
82-
),
83-
)
84-
};
85-
String.capitalize_ascii(name);
86-
};
87-
8866
let normalize_separators = path =>
8967
if (Sys.unix) {
9068
path;

compiler/test/stdlib/bigint.test.gr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module BigintTest
33
include "list"
44
include "runtime/wasi"
55
include "bigint"
6-
from Bigint use *
6+
from BigInt use *
77

88
assert toString(15t) == "15"
99
assert toString(-15t) == "-15"

stdlib/array.gr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @module Array: Utilities for working with arrays.
2+
* Utilities for working with arrays.
33
*
44
* @example include "array"
55
*

stdlib/bigint.gr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/**
2-
* @module BigInt: Utilities for working with the BigInt type.
2+
* Utilities for working with the BigInt type.
3+
*
34
* @example include "bigint"
45
*
56
* @since v0.5.0
67
*/
78

8-
module Bigint
9+
module BigInt
910

1011
include "runtime/unsafe/wasmi32"
1112
include "runtime/unsafe/memory"

0 commit comments

Comments
 (0)