Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler)!: Update include syntax #2043

Merged
merged 6 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions compiler/src/formatting/fmt.re
Original file line number Diff line number Diff line change
Expand Up @@ -3212,8 +3212,9 @@ let print_primitive_description = (fmt, {pprim_ident, pprim_name, pprim_loc}) =>
++ double_quotes(string(pprim_name.txt));
};

let print_include_declaration = (fmt, {pinc_path, pinc_alias, pinc_loc}) => {
string("include")
let print_include_declaration =
(fmt, {pinc_path, pinc_module, pinc_alias, pinc_loc}) => {
string("from")
++ fmt.print_comment_range(
fmt,
~allow_breaks=false,
Expand All @@ -3224,6 +3225,17 @@ let print_include_declaration = (fmt, {pinc_path, pinc_alias, pinc_loc}) => {
pinc_path.loc,
)
++ double_quotes(string(pinc_path.txt))
++ fmt.print_comment_range(
fmt,
~allow_breaks=false,
~none=space,
~lead=space,
~trail=space,
pinc_path.loc,
pinc_module.loc,
)
++ string("include ")
++ string(pinc_module.txt)
++ (
switch (pinc_alias) {
| None => empty
Expand All @@ -3235,7 +3247,7 @@ let print_include_declaration = (fmt, {pinc_path, pinc_alias, pinc_loc}) => {
~none=space,
~lead=space,
~trail=space,
pinc_path.loc,
pinc_module.loc,
alias_loc,
)
++ string(alias)
Expand Down
22 changes: 1 addition & 21 deletions compiler/src/language_server/inlayhint.re
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,7 @@ let find_hints = program => {
module Iterator =
TypedtreeIter.MakeIterator({
include TypedtreeIter.DefaultIteratorArgument;
let enter_toplevel_stmt = (stmt: toplevel_stmt) => {
switch (stmt.ttop_desc) {
| TTopInclude(inc) =>
let name = Path.name(inc.tinc_path);

let stmt_loc = stmt.ttop_loc;
let stmt_end = stmt_loc.loc_end;

let p: Protocol.position = {
line: stmt_end.pos_lnum - 1,
character: stmt_end.pos_cnum - stmt_end.pos_bol + 1 + 1,
};

let r: ResponseResult.inlay_hint = {
label: ": " ++ name,
position: p,
};
hints := [r, ...hints^];
| _ => ()
};
};
// Inlay hints for various expressions can be included here.
});
Iterator.iter_typed_program(program);
hints^;
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/parsing/ast_helper.re
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,10 @@ module MatchBranch = {
};

module IncludeDeclaration = {
let mk = (~loc, path, alias) => {
let mk = (~loc, path, module_, alias) => {
{
pinc_alias: alias,
pinc_module: module_,
pinc_path: normalize_string(~loc, path),
pinc_loc: loc,
};
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/parsing/ast_helper.rei
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ module MatchBranch: {
};

module IncludeDeclaration: {
let mk: (~loc: loc, str, option(str)) => include_declaration;
let mk: (~loc: loc, str, str, option(str)) => include_declaration;
};

module TypeArgument: {
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/parsing/ast_mapper.re
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,10 @@ module MB = {
};

module I = {
let map = (sub, {pinc_alias, pinc_path, pinc_loc}) => {
let map = (sub, {pinc_module, pinc_alias, pinc_path, pinc_loc}) => {
{
pinc_path: map_loc(sub, pinc_path),
pinc_module: map_loc(sub, pinc_module),
pinc_alias: Option.map(map_loc(sub), pinc_alias),
pinc_loc: sub.location(sub, pinc_loc),
};
Expand Down
49 changes: 36 additions & 13 deletions compiler/src/parsing/parser.messages
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ program: MODULE UIDENT EOL MODULE YIELD
## The known suffix of the stack is as follows:
## MODULE
##
program: MODULE UIDENT EOL INCLUDE STRING AS YIELD
program: MODULE UIDENT EOL FROM STRING INCLUDE UIDENT AS YIELD
##
## Ends in an error in state: 741.
##
Expand Down Expand Up @@ -328,7 +328,7 @@ program: MODULE UIDENT EOL FOREIGN WASM LIDENT COLON UIDENT AS EOL YIELD
## In state 6, spurious reduction of production eols -> nonempty_list(eol)
## In state 53, spurious reduction of production option(eols) -> eols
##
program: MODULE UIDENT EOL INCLUDE STRING AS EOL YIELD
program: MODULE UIDENT EOL FROM STRING INCLUDE UIDENT AS EOL YIELD
##
## Ends in an error in state: 823.
##
Expand Down Expand Up @@ -3197,16 +3197,6 @@ program: MODULE UIDENT EOL IF WHILE

Expected `(` followed by a condition expression.

program: MODULE UIDENT EOL INCLUDE WHILE
##
## Ends in an error in state: 874.
##
## import_stmt -> INCLUDE . lseparated_nonempty_list_inner(comma,import_shape) option(comma) FROM file_path [ SEMI EOL EOF ]
## toplevel_stmt -> attributes INCLUDE . foreign_stmt [ SEMI EOL EOF ]
##
## The known suffix of the stack is as follows:
## attributes INCLUDE
##
program: MODULE UIDENT EOL FOREIGN WASM LIDENT COLON UIDENT FROM YIELD
##
## Ends in an error in state: 753.
Expand All @@ -3225,10 +3215,43 @@ program: MODULE UIDENT EOL FOREIGN WASM LIDENT COLON EOL UIDENT FROM YIELD
## The known suffix of the stack is as follows:
## FOREIGN WASM id_str COLON eols typ option(as_prefix(id_str)) FROM
##
program: MODULE UIDENT EOL FROM YIELD
##
## Ends in an error in state: 862.
##
## include_stmt -> FROM . file_path INCLUDE qualified_uid option(include_alias) [ SEMI RBRACE EOL EOF ]
##
## The known suffix of the stack is as follows:
## FROM
##

Expected a file path surrounded by `"`.

program: MODULE UIDENT EOL INCLUDE STRING YIELD
program: MODULE UIDENT EOL FROM STRING YIELD
##
## Ends in an error in state: 863.
##
## include_stmt -> FROM file_path . INCLUDE qualified_uid option(include_alias) [ SEMI RBRACE EOL EOF ]
##
## The known suffix of the stack is as follows:
## FROM file_path
##

Expected the keyword `include` followed by a module identifier.

program: MODULE UIDENT EOL FROM STRING INCLUDE YIELD
##
## Ends in an error in state: 864.
##
## include_stmt -> FROM file_path INCLUDE . qualified_uid option(include_alias) [ SEMI RBRACE EOL EOF ]
##
## The known suffix of the stack is as follows:
## FROM file_path INCLUDE
##

Expected a module identifier.

program: MODULE UIDENT EOL FROM STRING INCLUDE UIDENT YIELD
##
## Ends in an error in state: 740.
##
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/parsing/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,10 @@ use_stmt:
| USE qualified_uid_inline dot use_shape { Expression.use ~loc:(to_loc $loc) ~core_loc:(to_loc $loc) $2 $4 }

include_alias:
| AS opt_eols qualified_uid { make_module_alias $3 }
| AS opt_eols qualified_uid { make_include_alias $3 }

include_stmt:
| INCLUDE file_path include_alias? { IncludeDeclaration.mk ~loc:(to_loc $loc) $2 $3 }
| FROM file_path INCLUDE qualified_uid include_alias? { IncludeDeclaration.mk ~loc:(to_loc $loc) $2 (make_include_ident $4) $5 }

data_declaration_stmt:
| ABSTRACT data_declaration { (Abstract, $2, to_loc($loc)) }
Expand Down
15 changes: 14 additions & 1 deletion compiler/src/parsing/parser_header.re
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,20 @@ let mkid_expr = (loc, ns) => {

let mkstr = (loc, s) => mkloc(s, to_loc(loc));

let make_module_alias = ident => {
let make_include_ident = ident => {
switch (ident.txt) {
| IdentName(name) => name
| IdentExternal(_) =>
raise(
SyntaxError(
ident.loc,
"A module include name cannot contain `.` as that would reference a binding within another module.",
),
)
};
};

let make_include_alias = ident => {
switch (ident.txt) {
| IdentName(name) => name
| IdentExternal(_) =>
Expand Down
1 change: 1 addition & 0 deletions compiler/src/parsing/parsetree.re
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ and match_branch = {
[@deriving (sexp, yojson)]
type include_declaration = {
pinc_path: loc(string),
pinc_module: loc(string),
pinc_alias: option(loc(string)),
[@sexp_drop_if sexp_locs_disabled]
pinc_loc: Location.t,
Expand Down
20 changes: 20 additions & 0 deletions compiler/src/typed/typemod.re
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module String = Misc.Stdlib.String;
type error =
| Cannot_apply(module_type)
| Not_included(list(Includemod.error))
| Include_module_name_mismatch(string, string)
| Cannot_eliminate_dependency(module_type)
| Signature_expected
| Structure_expected(module_type)
Expand Down Expand Up @@ -77,6 +78,15 @@ let extract_sig_open = (env, loc, mty) =>
let include_module = (env, sod) => {
let include_path = sod.pinc_path.txt;
let mod_name = Env.load_pers_struct(~loc=sod.pinc_loc, include_path);
if (mod_name != sod.pinc_module.txt) {
raise(
Error(
sod.pinc_module.loc,
env,
Include_module_name_mismatch(sod.pinc_module.txt, mod_name),
),
);
};
let mod_name =
switch (sod.pinc_alias) {
| Some({txt: alias}) => alias
Expand Down Expand Up @@ -1001,6 +1011,7 @@ let use_implicit_module = (m, env) => {
let path = Typetexp.lookup_module(~load=true, env, loc, ident, filepath);
let include_desc = {
pinc_path: Location.mknoloc(filename),
pinc_module: Location.mknoloc(modname),
pinc_alias: None,
pinc_loc: loc,
};
Expand Down Expand Up @@ -1081,6 +1092,15 @@ let report_error = ppf =>
Includemod.report_error,
errs,
)
| Include_module_name_mismatch(provided_name, actual_name) =>
fprintf(
ppf,
"This statement includes module %s, but the file at the path defines module %s. Did you mean `include %s as %s`?",
provided_name,
actual_name,
actual_name,
provided_name,
)
| Cannot_eliminate_dependency(mty) =>
fprintf(
ppf,
Expand Down
1 change: 1 addition & 0 deletions compiler/src/typed/typemod.rei
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ open Typedtree;
type error =
| Cannot_apply(module_type)
| Not_included(list(Includemod.error))
| Include_module_name_mismatch(string, string)
| Cannot_eliminate_dependency(module_type)
| Signature_expected
| Structure_expected(module_type)
Expand Down
6 changes: 3 additions & 3 deletions compiler/test/grainfmt/application.expected.gr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Application

include "array"
include "option"
include "map"
from "array" include Array
from "option" include Option
from "map" include Map

let data = [>]

Expand Down
6 changes: 3 additions & 3 deletions compiler/test/grainfmt/application.input.gr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Application

include "array"
include "option"
include "map"
from "array" include Array
from "option" include Option
from "map" include Map

let data = [>]

Expand Down
2 changes: 1 addition & 1 deletion compiler/test/grainfmt/arrays.expected.gr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Arrays

include "array"
from "array" include Array

let empty = [>]

Expand Down
2 changes: 1 addition & 1 deletion compiler/test/grainfmt/arrays.input.gr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Arrays

include "array"
from "array" include Array

let empty = [>]

Expand Down
4 changes: 2 additions & 2 deletions compiler/test/grainfmt/comments.expected.gr
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,8 @@ type /* type */ Foo</* Foo */

primitive /* prim */ foo = /* foo */ /* hello */ "@hello"

include /* include */ "foo"
include /* include */ "foo" as /* foo */ /* alias */ Foo
from /* from */ "foo" /* foo */ /* include */ include Foo
from /* from */ "foo" /* foo */ /* include */ include Foo as /* as */ /* alias */ Foo

module /* module */ Foo { /* Foo */
foo /* foo */
Expand Down
4 changes: 2 additions & 2 deletions compiler/test/grainfmt/comments.input.gr
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,8 @@ type /* type */ Foo /* Foo */ < /* a */ a, /* post-a */ b, /* post-b */> /* Foo

primitive /* prim */ foo /* foo */ = /* hello */ "@hello"

include /* include */ "foo"
include /* include */ "foo" /* foo */ as /* alias */ Foo
from /* from */ "foo" /* foo */ include /* include */ Foo
from /* from */ "foo" /* foo */ include /* include */ Foo /* as */ as /* alias */ Foo

module /* module */ Foo /* Foo */ {
foo; /* foo */ bar
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/grainfmt/constraints.expected.gr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Constraints

include "list"
from "list" include List
let test = test => {
// Comments
List.forEach(i => {
Expand Down
3 changes: 2 additions & 1 deletion compiler/test/grainfmt/constraints.input.gr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Constraints
include "list"

from "list" include List
let test = test => {
// Comments
List.forEach(i => {
Expand Down
18 changes: 9 additions & 9 deletions compiler/test/grainfmt/includes.expected.gr
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module Includes

include "runtime/unsafe/memory"
include "runtime/unsafe/tags"
include "list"
include "option" as Opt
include "option" as Opt
include /* special include */ "array"
include "array" as /* special include */ Foo
from "runtime/unsafe/memory" include Memory
from "runtime/unsafe/tags" include Tags
from "list" include List
from "option" include Option as Opt
from "option" include Option as Opt
from /* special include */ "array" include Array
from "array" include Array as /* special include */ Foo
use List.{ length, map, forEach as each }
use Opt.{
module MutableOpt,
Expand All @@ -24,13 +24,13 @@ use Opt.{
exception Exc2, /* comment9 */
}

include "runtime/unsafe/wasmi32"
from "runtime/unsafe/wasmi32" include WasmI32
use WasmI32.{ add as (+), mul as (*), xor as (^), shl as (<<) }

provide foreign wasm storage_read:
(WasmI64, WasmI64, WasmI64) => WasmI64 as storageRead from "env"

include "runtime/unsafe/wasmi32"
from "runtime/unsafe/wasmi32" include WasmI32
use WasmI32.{
eq, // comment1
// comment 3
Expand Down
Loading
Loading