Skip to content

Commit 687e747

Browse files
authored
feat(compiler): Providing, including, reproviding exceptions (#1849)
1 parent 00abee8 commit 687e747

26 files changed

+266
-22
lines changed

compiler/src/formatting/format.re

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3917,7 +3917,8 @@ and print_expression_inner =
39173917
switch (item) {
39183918
| PUseValue({loc})
39193919
| PUseModule({loc})
3920-
| PUseType({loc}) => loc
3920+
| PUseType({loc})
3921+
| PUseException({loc}) => loc
39213922
};
39223923
};
39233924

@@ -3961,6 +3962,8 @@ and print_expression_inner =
39613962
Doc.concat([Doc.text("module "), item_name(name, alias)])
39623963
| PUseType({name, alias}) =>
39633964
Doc.concat([Doc.text("type "), item_name(name, alias)])
3965+
| PUseException({name, alias}) =>
3966+
Doc.concat([Doc.text("exception "), item_name(name, alias)])
39643967
};
39653968
};
39663969

@@ -5068,6 +5071,7 @@ let rec toplevel_print =
50685071
switch (item) {
50695072
| PProvideValue({loc})
50705073
| PProvideModule({loc})
5074+
| PProvideException({loc})
50715075
| PProvideType({loc}) => loc
50725076
};
50735077
};
@@ -5112,6 +5116,8 @@ let rec toplevel_print =
51125116
Doc.concat([Doc.text("module "), item_name(name, alias)])
51135117
| PProvideType({name, alias}) =>
51145118
Doc.concat([Doc.text("type "), item_name(name, alias)])
5119+
| PProvideException({name, alias}) =>
5120+
Doc.concat([Doc.text("exception "), item_name(name, alias)])
51155121
};
51165122
};
51175123

compiler/src/language_server/definition.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ let process =
7171
| [Pattern({definition}), ..._]
7272
| [Type({definition}), ..._]
7373
| [Declaration({definition}), ..._]
74+
| [Exception({definition}), ..._]
7475
| [Module({definition}), ..._] =>
7576
switch (definition) {
7677
| None => send_no_result(~id)

compiler/src/language_server/hover.re

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ let declaration_lens = (ident: Ident.t, decl: Types.type_declaration) => {
125125
grain_type_code_block(Printtyp.string_of_type_declaration(~ident, decl));
126126
};
127127

128+
let exception_declaration_lens =
129+
(ident: Ident.t, ext: Types.extension_constructor) => {
130+
grain_type_code_block(
131+
Printtyp.string_of_extension_constructor(~ident, ext),
132+
);
133+
};
134+
128135
let process =
129136
(
130137
~id: Protocol.message_id,
@@ -161,6 +168,12 @@ let process =
161168
~range=Utils.loc_to_range(loc),
162169
declaration_lens(ident, decl),
163170
)
171+
| [Exception({ident, ext, loc}), ..._] =>
172+
send_hover(
173+
~id,
174+
~range=Utils.loc_to_range(loc),
175+
exception_declaration_lens(ident, ext),
176+
)
164177
| [Module({path, decl, loc}), ..._] =>
165178
send_hover(~id, ~range=Utils.loc_to_range(loc), module_lens(decl))
166179
| _ => send_no_result(~id)

compiler/src/language_server/sourcetree.re

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ module type Sourcetree = {
157157
loc: Location.t,
158158
definition: option(Location.t),
159159
})
160+
| Exception({
161+
ident: Ident.t,
162+
ext: Types.extension_constructor,
163+
loc: Location.t,
164+
definition: option(Location.t),
165+
})
160166
| Module({
161167
path: Path.t,
162168
decl: Types.module_declaration,
@@ -236,6 +242,12 @@ module Sourcetree: Sourcetree = {
236242
loc: Location.t,
237243
definition: option(Location.t),
238244
})
245+
| Exception({
246+
ident: Ident.t,
247+
ext: Types.extension_constructor,
248+
loc: Location.t,
249+
definition: option(Location.t),
250+
})
239251
| Module({
240252
path: Path.t,
241253
decl: Types.module_declaration,
@@ -387,6 +399,15 @@ module Sourcetree: Sourcetree = {
387399
definition: Some(value.val_loc),
388400
}),
389401
)
402+
| TUseException({name, ext, loc}) => (
403+
loc_to_interval(loc),
404+
Exception({
405+
ident: Ident.create(name),
406+
ext,
407+
loc,
408+
definition: Some(ext.ext_loc),
409+
}),
410+
)
390411
}
391412
},
392413
items,

compiler/src/middle_end/linearize.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let compile_constructor_tag =
1313
fun
1414
| CstrConstant(i) => i
1515
| CstrBlock(i) => i
16-
| CstrExtension(i, _, _) => i
16+
| CstrExtension(i, _, _, _) => i
1717
| CstrUnboxed =>
1818
failwith("compile_constructor_tag: cannot compile CstrUnboxed")
1919
);

compiler/src/parsing/ast_mapper.re

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ module E = {
190190
alias: Option.map(map_identifier(sub), alias),
191191
loc: sub.location(sub, loc),
192192
})
193+
| PUseException({name, alias, loc}) =>
194+
PUseException({
195+
name: map_identifier(sub, name),
196+
alias: Option.map(map_identifier(sub), alias),
197+
loc: sub.location(sub, loc),
198+
})
193199
| PUseModule({name, alias, loc}) =>
194200
PUseModule({
195201
name: map_identifier(sub, name),
@@ -432,6 +438,12 @@ module Pr = {
432438
alias: Option.map(map_identifier(sub), alias),
433439
loc: sub.location(sub, loc),
434440
})
441+
| PProvideException({name, alias, loc}) =>
442+
PProvideException({
443+
name: map_identifier(sub, name),
444+
alias: Option.map(map_identifier(sub), alias),
445+
loc: sub.location(sub, loc),
446+
})
435447
| PProvideModule({name, alias, loc}) =>
436448
PProvideModule({
437449
name: map_identifier(sub, name),

compiler/src/parsing/parser.messages

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,18 @@ program: MODULE UIDENT EOL FROM UIDENT USE LBRACE MODULE YIELD
455455

456456
Expected an uppercase type identifier.
457457

458+
program: MODULE UIDENT EOL FROM UIDENT USE LBRACE EXCEPTION YIELD
459+
##
460+
## Ends in an error in state: 57.
461+
##
462+
## use_item -> EXCEPTION . aliasable(uid) [ RBRACE EOL COMMA ]
463+
##
464+
## The known suffix of the stack is as follows:
465+
## EXCEPTION
466+
##
467+
468+
Expected an uppercase exception identifier.
469+
458470
program: MODULE UIDENT EOL PROVIDE LBRACE MODULE UIDENT YIELD
459471
##
460472
## Ends in an error in state: 49.
@@ -558,6 +570,18 @@ program: MODULE UIDENT EOL PROVIDE LBRACE TYPE YIELD
558570

559571
Expected a type identifier to provide.
560572

573+
program: MODULE UIDENT EOL PROVIDE LBRACE EXCEPTION YIELD
574+
##
575+
## Ends in an error in state: 825.
576+
##
577+
## provide_item -> EXCEPTION . aliasable(uid) [ RBRACE EOL COMMA ]
578+
##
579+
## The known suffix of the stack is as follows:
580+
## EXCEPTION
581+
##
582+
583+
Expected an exception identifier to provide.
584+
561585
program: MODULE UIDENT EOL FROM UIDENT USE LBRACE LIDENT AS LIDENT YIELD
562586
##
563587
## Ends in an error in state: 62.

compiler/src/parsing/parser.mly

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ aliasable(X):
343343
use_item:
344344
| TYPE aliasable(uid) { PUseType { name=fst $2; alias = snd $2; loc=to_loc $loc} }
345345
| MODULE aliasable(uid) { PUseModule { name=fst $2; alias = snd $2; loc=to_loc $loc} }
346+
| EXCEPTION aliasable(uid) { PUseException { name=fst $2; alias = snd $2; loc=to_loc $loc} }
346347
| aliasable(lid) { PUseValue { name=fst $1; alias = snd $1; loc=to_loc $loc} }
347348

348349
use_items:
@@ -372,6 +373,7 @@ data_declaration_stmts:
372373
provide_item:
373374
| TYPE aliasable(uid) { PProvideType { name=fst $2; alias = snd $2; loc=to_loc $loc} }
374375
| MODULE aliasable(uid) { PProvideModule { name=fst $2; alias = snd $2; loc=to_loc $loc} }
376+
| EXCEPTION aliasable(uid) { PProvideException { name=fst $2; alias = snd $2; loc=to_loc $loc} }
375377
| aliasable(lid) { PProvideValue { name=fst $1; alias = snd $1; loc=to_loc $loc} }
376378

377379
provide_items:

compiler/src/parsing/parsetree.re

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,11 @@ and use_item =
464464
alias: option(loc(Identifier.t)),
465465
loc: Location.t,
466466
})
467+
| PUseException({
468+
name: loc(Identifier.t),
469+
alias: option(loc(Identifier.t)),
470+
loc: Location.t,
471+
})
467472
| PUseModule({
468473
name: loc(Identifier.t),
469474
alias: option(loc(Identifier.t)),
@@ -595,6 +600,11 @@ type provide_item =
595600
alias: option(loc(Identifier.t)),
596601
loc: Location.t,
597602
})
603+
| PProvideException({
604+
name: loc(Identifier.t),
605+
alias: option(loc(Identifier.t)),
606+
loc: Location.t,
607+
})
598608
| PProvideModule({
599609
name: loc(Identifier.t),
600610
alias: option(loc(Identifier.t)),

compiler/src/parsing/parsetree_iter.re

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ and iter_provide = (hooks, items) => {
124124
item => {
125125
switch (item) {
126126
| PProvideType({name, alias, loc})
127+
| PProvideException({name, alias, loc})
127128
| PProvideModule({name, alias, loc})
128129
| PProvideValue({name, alias, loc}) =>
129130
iter_ident(hooks, name);
@@ -290,6 +291,7 @@ and iter_expression =
290291
item => {
291292
switch (item) {
292293
| PUseType({name, alias, loc})
294+
| PUseException({name, alias, loc})
293295
| PUseModule({name, alias, loc})
294296
| PUseValue({name, alias, loc}) =>
295297
iter_ident(hooks, name);

0 commit comments

Comments
 (0)