Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 5e87f9d

Browse files
Merge pull request #11 from chenglou/hashdotdotdort
Change poly variant `##` spread to `#...`
2 parents ad7b6de + d74292c commit 5e87f9d

File tree

13 files changed

+87
-88
lines changed

13 files changed

+87
-88
lines changed

src/napkin_core.ml

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ let parseRegion p ~grammar ~f =
996996
(* ∣ pattern | pattern *)
997997
(* ∣ constr pattern *)
998998
(* ∣ #variant variant-pattern *)
999-
(* ∣ ##type *)
999+
(* ∣ #...type *)
10001000
(* ∣ / pattern { , pattern }+ / *)
10011001
(* ∣ { field [: typexpr] [= pattern] { ; field [: typexpr] [= pattern] } [; _ ] [ ; ] } *)
10021002
(* ∣ [ pattern { ; pattern } [ ; ] ] *)
@@ -1067,18 +1067,21 @@ let rec parsePattern ?(alias=true) ?(or_=true) p =
10671067
Ast_helper.Pat.construct ~loc:constr.loc ~attrs constr None
10681068
end
10691069
| Hash ->
1070-
let (ident, loc) = parseHashIdent ~startPos p in
1071-
begin match p.Parser.token with
1072-
| Lparen ->
1073-
parseVariantPatternArgs p ident startPos attrs
1074-
| _ ->
1075-
Ast_helper.Pat.variant ~loc ~attrs ident None
1076-
end
1077-
| HashHash ->
10781070
Parser.next p;
1079-
let ident = parseValuePath p in
1080-
let loc = mkLoc startPos ident.loc.loc_end in
1081-
Ast_helper.Pat.type_ ~loc ~attrs ident
1071+
if p.Parser.token == DotDotDot then (
1072+
Parser.next p;
1073+
let ident = parseValuePath p in
1074+
let loc = mkLoc startPos ident.loc.loc_end in
1075+
Ast_helper.Pat.type_ ~loc ~attrs ident
1076+
) else (
1077+
let (ident, loc) = parseIdent ~msg:ErrorMessages.variantIdent ~startPos p in
1078+
begin match p.Parser.token with
1079+
| Lparen ->
1080+
parseVariantPatternArgs p ident startPos attrs
1081+
| _ ->
1082+
Ast_helper.Pat.variant ~loc ~attrs ident None
1083+
end
1084+
)
10821085
| Exception ->
10831086
Parser.next p;
10841087
let pat = parsePattern ~alias:false ~or_:false p in

src/napkin_grammar.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ let isPatternStart = function
194194
| Token.Int _ | Float _ | String _ | Character _ | True | False | Minus | Plus
195195
| Lparen | Lbracket | Lbrace | List
196196
| Underscore
197-
| Lident _ | Uident _ | Hash | HashHash
197+
| Lident _ | Uident _ | Hash
198198
| Exception | Lazy | Percent | Module
199199
| At -> true
200200
| _ -> false

src/napkin_parser.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type mode = ParseForTypeChecker | Default
4444
try p.regions <- List.tl p.regions with Failure _ -> ()
4545

4646
(* Advance to the next non-comment token and store any encountered comment
47-
* in the parser's state. Every comment contains the end position of it's
47+
* in the parser's state. Every comment contains the end position of its
4848
* previous token to facilite comment interleaving *)
4949
let rec next ?prevEndPos p =
5050
let prevEndPos = match prevEndPos with Some pos -> pos | None -> p.endPos in

src/napkin_printer.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2204,7 +2204,7 @@ and printPattern (p : Parsetree.pattern) cmtTbl =
22042204
in
22052205
Doc.group(Doc.concat [variantName; argsDoc])
22062206
| Ppat_type ident ->
2207-
Doc.concat [Doc.text "##"; printIdentPath ident cmtTbl]
2207+
Doc.concat [Doc.text "#..."; printIdentPath ident cmtTbl]
22082208
| Ppat_record(rows, openFlag) ->
22092209
Doc.group(
22102210
Doc.concat([

src/napkin_scanner.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -633,10 +633,7 @@ let rec scan scanner =
633633
Token.LessThan
634634
)
635635
else if ch == CharacterCodes.hash then
636-
if scanner.ch == CharacterCodes.hash then(
637-
next scanner;
638-
Token.HashHash
639-
) else if scanner.ch == CharacterCodes.equal then(
636+
if scanner.ch == CharacterCodes.equal then(
640637
next scanner;
641638
Token.HashEqual
642639
) else (

src/napkin_token.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type t =
4141
| GreaterThan
4242
| LessThan
4343
| LessThanSlash
44-
| Hash | HashEqual | HashHash
44+
| Hash | HashEqual
4545
| Assert
4646
| Lazy
4747
| Tilde
@@ -120,7 +120,7 @@ let toString = function
120120
| Backslash -> "\\"
121121
| Forwardslash -> "/" | ForwardslashDot -> "/."
122122
| Exception -> "exception"
123-
| Hash -> "#" | HashHash -> "##" | HashEqual -> "#="
123+
| Hash -> "#" | HashEqual -> "#="
124124
| GreaterThan -> ">"
125125
| LessThan -> "<"
126126
| LessThanSlash -> "</"

tests/parsing/grammar/pattern/polyvariants.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
let #Instance = i;
2-
let #Instance as inst = i;
1+
let #Instance = i;
2+
let #Instance as inst = i;
33

44
let #Instance(component) = i
55
let #Instance(component) as inst = i
@@ -31,17 +31,17 @@ switch x {
3131
| #Instance as inst => ()
3232
| #Instance(comp) => ()
3333
| #Instance(comp) as inst => ()
34-
| #Instance({render, subtree}) => ()
35-
| #Instance({render, subtree}, inst) => ()
34+
| #Instance({render, subtree}) => ()
35+
| #Instance({render, subtree}, inst) => ()
3636
| #Instance({render, subtree} : Instance.t) => ()
3737
| #Instance(({render, subtree} : Instance.t)) => ()
3838
| #Instance(comp, tree) => ()
3939
| (#Instance(comp: Component.t) : React.t) => ()
4040
}
4141

4242

43-
let f = (#Instance) => i;
44-
let f = (#Instance as i) => i;
43+
let f = (#Instance) => i;
44+
let f = (#Instance as i) => i;
4545

4646
let f = (#Instance(component)) => i
4747
let f = (#Instance(component,)) => i // trailing comma
@@ -79,22 +79,22 @@ for (#Point({x, y, z}) in x to y) { () }
7979
for (#Point({x, y, z}) as p in x to y) { () }
8080

8181
switch x {
82-
| ##typeVar => ()
82+
| #...typeVar => ()
8383
| #lowercase => ()
8484
}
85-
85+
8686
let cmp = (selectedChoice, value) =>
8787
switch (selectedChoice, value) {
88-
| (##a, ##a) => true
89-
| [##b, ##b] => true
90-
| list[##b, ##b] => true
91-
| {x: ##c, y: ##c} => true
92-
| Constructor(##a, ##a) => true
93-
| #Constuctor(##a, ##a) => true
94-
| ##a as x => true
95-
| ##a | ##b => true
96-
| (##a : typ) => true
97-
| lazy ##a => true
98-
| exception ##a => true
88+
| (#...a, #...a) => true
89+
| [#...b, #...b] => true
90+
| list[#...b, #...b] => true
91+
| {x: #...c, y: #...c} => true
92+
| Constructor(#...a, #...a) => true
93+
| #Constuctor(#...a, #...a) => true
94+
| #...a as x => true
95+
| # ...a | # ... b => true
96+
| (#...a : typ) => true
97+
| lazy #...a => true
98+
| exception #...a => true
9999
| _ => false
100100
}

tests/parsing/grammar/pattern/variants.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
let #Instance = i;
2-
let #Instance as inst = i;
1+
let #Instance = i;
2+
let #Instance as inst = i;
33

44
let #Instance(component) = i
55
let #Instance(component) as inst = i
@@ -31,17 +31,17 @@ switch x {
3131
| #Instance as inst => ()
3232
| #Instance(comp) => ()
3333
| #Instance(comp) as inst => ()
34-
| #Instance({render, subtree}) => ()
35-
| #Instance({render, subtree}, inst) => ()
34+
| #Instance({render, subtree}) => ()
35+
| #Instance({render, subtree}, inst) => ()
3636
| #Instance({render, subtree} : Instance.t) => ()
3737
| #Instance(({render, subtree} : Instance.t)) => ()
3838
| #Instance(comp, tree) => ()
3939
| (#Instance(comp: Component.t) : React.t) => ()
4040
}
4141

4242

43-
let f = (#Instance) => i;
44-
let f = (#Instance as i) => i;
43+
let f = (#Instance) => i;
44+
let f = (#Instance as i) => i;
4545

4646
let f = (#Instance(component)) => i
4747
let f = (#Instance(component,)) => i // trailing comma
@@ -79,7 +79,6 @@ for (#Point({x, y, z}) in x to y) { () }
7979
for (#Point({x, y, z}) as p in x to y) { () }
8080

8181
switch x {
82-
| ##typeVar => ()
82+
| #...typeVar => ()
8383
| #lowercase => ()
8484
}
85-

tests/printer/expr/__snapshots__/render.spec.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2826,7 +2826,7 @@ let math = if discriminant < 0. {
28262826
}
28272827
28282828
switch x {
2829-
| ##typevar => 42
2829+
| #...typevar => 42
28302830
}
28312831
28322832
let r = #\\\\\\"Reducer⛪️\\"

tests/printer/expr/polyvariant.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ let math = if discriminant < 0. {
9292
}
9393

9494
switch x {
95-
| ##typevar => 42
95+
| #...typevar => 42
9696
}
9797

9898

tests/printer/pattern/__snapshots__/render.spec.js.snap

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,16 @@ let (
256256
`;
257257

258258
exports[`type.ns 1`] = `
259-
"let ##shape = x
260-
let ##\\\\\\"Shape\\" = x
261-
let ##\\\\\\"type\\" = x
262-
let ##\\\\\\"test 🏚\\" = x
263-
let ##\\\\\\"Shape✅\\" = x
259+
"let #...shape = x
260+
let #...\\\\\\"Shape\\" = x
261+
let #...\\\\\\"type\\" = x
262+
let #...\\\\\\"test 🏚\\" = x
263+
let #...\\\\\\"Shape✅\\" = x
264264
265265
switch (selectedChoice, value) {
266-
| (##A.a, ##A.a) => true
267-
| (##A.b, ##A.b) => true
268-
| (##A.c, ##A.c) => true
266+
| (#...A.a, #...A.a) => true
267+
| (#...A.b, #...A.b) => true
268+
| (#...A.c, #...A.c) => true
269269
| _ => false
270270
}
271271
"
@@ -292,17 +292,17 @@ let #\\\\\\"Shape🎡\\"(\\\\\\"module\\", \\\\\\"ExoticIdent\\") = x
292292
293293
let cmp = (selectedChoice, value) =>
294294
switch (selectedChoice, value) {
295-
| (##a, ##a) => true
296-
| [##b, ##b] => true
297-
| list[##b, ##b] => true
298-
| {x: ##c, y: ##c} => true
299-
| Constructor(##a, ##a) => true
300-
| #Constuctor(##a, ##a) => true
301-
| ##a as x => true
302-
| ##a | ##b => true
303-
| (##a: typ) => true
304-
| lazy ##a => true
305-
| exception ##a => true
295+
| (#...a, #...a) => true
296+
| [#...b, #...b] => true
297+
| list[#...b, #...b] => true
298+
| {x: #...c, y: #...c} => true
299+
| Constructor(#...a, #...a) => true
300+
| #Constuctor(#...a, #...a) => true
301+
| #...a as x => true
302+
| #...a | #...b => true
303+
| (#...a: typ) => true
304+
| lazy #...a => true
305+
| exception #...a => true
306306
| _ => false
307307
}
308308
"

tests/printer/pattern/type.ns

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
let ##shape = x
2-
let ##\"Shape" = x
3-
let ##\"type" = x
4-
let ##\"test 🏚" = x
5-
let ##\"Shape✅" = x
1+
let #...shape = x
2+
let #...\"Shape" = x
3+
let #...\"type" = x
4+
let #...\"test 🏚" = x
5+
let #...\"Shape✅" = x
66

77
switch (selectedChoice, value) {
8-
| (##A.a, ##A.a) => true
9-
| (##A.b, ##A.b) => true
10-
| (##A.c, ##A.c) => true
8+
| (#...A.a, #...A.a) => true
9+
| (#...A.b, #...A.b) => true
10+
| (#...A.c, #...A.c) => true
1111
| _ => false
1212
}

tests/printer/pattern/variant.ns

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ let #\"Shape🎡"(\"module", \"ExoticIdent") = x
1111

1212
let cmp = (selectedChoice, value) =>
1313
switch (selectedChoice, value) {
14-
| (##a, ##a) => true
15-
| [##b, ##b] => true
16-
| list[##b, ##b] => true
17-
| {x: ##c, y: ##c} => true
18-
| Constructor(##a, ##a) => true
19-
| #Constuctor(##a, ##a) => true
20-
| ##a as x => true
21-
| ##a | ##b => true
22-
| (##a : typ) => true
23-
| lazy ##a => true
24-
| exception ##a => true
14+
| (#...a, #...a) => true
15+
| [#...b, #...b] => true
16+
| list[#...b, #...b] => true
17+
| {x: #...c, y: #...c} => true
18+
| Constructor(#...a, #...a) => true
19+
| #Constuctor(#...a, #...a) => true
20+
| #...a as x => true
21+
| #...a | #...b => true
22+
| (#...a : typ) => true
23+
| lazy #...a => true
24+
| exception #...a => true
2525
| _ => false
2626
}

0 commit comments

Comments
 (0)