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

Change poly variant ## spread to #... #11

Merged
merged 1 commit into from
Jul 3, 2020
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
27 changes: 15 additions & 12 deletions src/napkin_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ let parseRegion p ~grammar ~f =
(* ∣ pattern | pattern *)
(* ∣ constr pattern *)
(* ∣ #variant variant-pattern *)
(* ∣ ##type *)
(* ∣ #...type *)
(* ∣ / pattern { , pattern }+ / *)
(* ∣ { field [: typexpr] [= pattern] { ; field [: typexpr] [= pattern] } [; _ ] [ ; ] } *)
(* ∣ [ pattern { ; pattern } [ ; ] ] *)
Expand Down Expand Up @@ -1067,18 +1067,21 @@ let rec parsePattern ?(alias=true) ?(or_=true) p =
Ast_helper.Pat.construct ~loc:constr.loc ~attrs constr None
end
| Hash ->
let (ident, loc) = parseHashIdent ~startPos p in
begin match p.Parser.token with
| Lparen ->
parseVariantPatternArgs p ident startPos attrs
| _ ->
Ast_helper.Pat.variant ~loc ~attrs ident None
end
| HashHash ->
Parser.next p;
let ident = parseValuePath p in
let loc = mkLoc startPos ident.loc.loc_end in
Ast_helper.Pat.type_ ~loc ~attrs ident
if p.Parser.token == DotDotDot then (
Parser.next p;
let ident = parseValuePath p in
let loc = mkLoc startPos ident.loc.loc_end in
Ast_helper.Pat.type_ ~loc ~attrs ident
) else (
let (ident, loc) = parseIdent ~msg:ErrorMessages.variantIdent ~startPos p in
begin match p.Parser.token with
| Lparen ->
parseVariantPatternArgs p ident startPos attrs
| _ ->
Ast_helper.Pat.variant ~loc ~attrs ident None
end
)
| Exception ->
Parser.next p;
let pat = parsePattern ~alias:false ~or_:false p in
Expand Down
2 changes: 1 addition & 1 deletion src/napkin_grammar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ let isPatternStart = function
| Token.Int _ | Float _ | String _ | Character _ | True | False | Minus | Plus
| Lparen | Lbracket | Lbrace | List
| Underscore
| Lident _ | Uident _ | Hash | HashHash
| Lident _ | Uident _ | Hash
| Exception | Lazy | Percent | Module
| At -> true
| _ -> false
Expand Down
2 changes: 1 addition & 1 deletion src/napkin_parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type mode = ParseForTypeChecker | Default
try p.regions <- List.tl p.regions with Failure _ -> ()

(* Advance to the next non-comment token and store any encountered comment
* in the parser's state. Every comment contains the end position of it's
* in the parser's state. Every comment contains the end position of its
* previous token to facilite comment interleaving *)
let rec next ?prevEndPos p =
let prevEndPos = match prevEndPos with Some pos -> pos | None -> p.endPos in
Expand Down
2 changes: 1 addition & 1 deletion src/napkin_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2204,7 +2204,7 @@ and printPattern (p : Parsetree.pattern) cmtTbl =
in
Doc.group(Doc.concat [variantName; argsDoc])
| Ppat_type ident ->
Doc.concat [Doc.text "##"; printIdentPath ident cmtTbl]
Doc.concat [Doc.text "#..."; printIdentPath ident cmtTbl]
| Ppat_record(rows, openFlag) ->
Doc.group(
Doc.concat([
Expand Down
5 changes: 1 addition & 4 deletions src/napkin_scanner.ml
Original file line number Diff line number Diff line change
Expand Up @@ -633,10 +633,7 @@ let rec scan scanner =
Token.LessThan
)
else if ch == CharacterCodes.hash then
if scanner.ch == CharacterCodes.hash then(
next scanner;
Token.HashHash
) else if scanner.ch == CharacterCodes.equal then(
if scanner.ch == CharacterCodes.equal then(
next scanner;
Token.HashEqual
) else (
Expand Down
4 changes: 2 additions & 2 deletions src/napkin_token.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type t =
| GreaterThan
| LessThan
| LessThanSlash
| Hash | HashEqual | HashHash
| Hash | HashEqual
| Assert
| Lazy
| Tilde
Expand Down Expand Up @@ -120,7 +120,7 @@ let toString = function
| Backslash -> "\\"
| Forwardslash -> "/" | ForwardslashDot -> "/."
| Exception -> "exception"
| Hash -> "#" | HashHash -> "##" | HashEqual -> "#="
| Hash -> "#" | HashEqual -> "#="
| GreaterThan -> ">"
| LessThan -> "<"
| LessThanSlash -> "</"
Expand Down
38 changes: 19 additions & 19 deletions tests/parsing/grammar/pattern/polyvariants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let #Instance = i;
let #Instance as inst = i;
let #Instance = i;
let #Instance as inst = i;

let #Instance(component) = i
let #Instance(component) as inst = i
Expand Down Expand Up @@ -31,17 +31,17 @@ switch x {
| #Instance as inst => ()
| #Instance(comp) => ()
| #Instance(comp) as inst => ()
| #Instance({render, subtree}) => ()
| #Instance({render, subtree}, inst) => ()
| #Instance({render, subtree}) => ()
| #Instance({render, subtree}, inst) => ()
| #Instance({render, subtree} : Instance.t) => ()
| #Instance(({render, subtree} : Instance.t)) => ()
| #Instance(comp, tree) => ()
| (#Instance(comp: Component.t) : React.t) => ()
}


let f = (#Instance) => i;
let f = (#Instance as i) => i;
let f = (#Instance) => i;
let f = (#Instance as i) => i;

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

switch x {
| ##typeVar => ()
| #...typeVar => ()
| #lowercase => ()
}

let cmp = (selectedChoice, value) =>
switch (selectedChoice, value) {
| (##a, ##a) => true
| [##b, ##b] => true
| list[##b, ##b] => true
| {x: ##c, y: ##c} => true
| Constructor(##a, ##a) => true
| #Constuctor(##a, ##a) => true
| ##a as x => true
| ##a | ##b => true
| (##a : typ) => true
| lazy ##a => true
| exception ##a => true
| (#...a, #...a) => true
| [#...b, #...b] => true
| list[#...b, #...b] => true
| {x: #...c, y: #...c} => true
| Constructor(#...a, #...a) => true
| #Constuctor(#...a, #...a) => true
| #...a as x => true
| # ...a | # ... b => true
| (#...a : typ) => true
| lazy #...a => true
| exception #...a => true
| _ => false
}
15 changes: 7 additions & 8 deletions tests/parsing/grammar/pattern/variants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let #Instance = i;
let #Instance as inst = i;
let #Instance = i;
let #Instance as inst = i;

let #Instance(component) = i
let #Instance(component) as inst = i
Expand Down Expand Up @@ -31,17 +31,17 @@ switch x {
| #Instance as inst => ()
| #Instance(comp) => ()
| #Instance(comp) as inst => ()
| #Instance({render, subtree}) => ()
| #Instance({render, subtree}, inst) => ()
| #Instance({render, subtree}) => ()
| #Instance({render, subtree}, inst) => ()
| #Instance({render, subtree} : Instance.t) => ()
| #Instance(({render, subtree} : Instance.t)) => ()
| #Instance(comp, tree) => ()
| (#Instance(comp: Component.t) : React.t) => ()
}


let f = (#Instance) => i;
let f = (#Instance as i) => i;
let f = (#Instance) => i;
let f = (#Instance as i) => i;

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

switch x {
| ##typeVar => ()
| #...typeVar => ()
| #lowercase => ()
}

2 changes: 1 addition & 1 deletion tests/printer/expr/__snapshots__/render.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2826,7 +2826,7 @@ let math = if discriminant < 0. {
}

switch x {
| ##typevar => 42
| #...typevar => 42
}

let r = #\\\\\\"Reducer⛪️\\"
Expand Down
2 changes: 1 addition & 1 deletion tests/printer/expr/polyvariant.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ let math = if discriminant < 0. {
}

switch x {
| ##typevar => 42
| #...typevar => 42
}


Expand Down
38 changes: 19 additions & 19 deletions tests/printer/pattern/__snapshots__/render.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,16 @@ let (
`;

exports[`type.ns 1`] = `
"let ##shape = x
let ##\\\\\\"Shape\\" = x
let ##\\\\\\"type\\" = x
let ##\\\\\\"test 🏚\\" = x
let ##\\\\\\"Shape✅\\" = x
"let #...shape = x
let #...\\\\\\"Shape\\" = x
let #...\\\\\\"type\\" = x
let #...\\\\\\"test 🏚\\" = x
let #...\\\\\\"Shape✅\\" = x

switch (selectedChoice, value) {
| (##A.a, ##A.a) => true
| (##A.b, ##A.b) => true
| (##A.c, ##A.c) => true
| (#...A.a, #...A.a) => true
| (#...A.b, #...A.b) => true
| (#...A.c, #...A.c) => true
| _ => false
}
"
Expand All @@ -292,17 +292,17 @@ let #\\\\\\"Shape🎡\\"(\\\\\\"module\\", \\\\\\"ExoticIdent\\") = x

let cmp = (selectedChoice, value) =>
switch (selectedChoice, value) {
| (##a, ##a) => true
| [##b, ##b] => true
| list[##b, ##b] => true
| {x: ##c, y: ##c} => true
| Constructor(##a, ##a) => true
| #Constuctor(##a, ##a) => true
| ##a as x => true
| ##a | ##b => true
| (##a: typ) => true
| lazy ##a => true
| exception ##a => true
| (#...a, #...a) => true
| [#...b, #...b] => true
| list[#...b, #...b] => true
| {x: #...c, y: #...c} => true
| Constructor(#...a, #...a) => true
| #Constuctor(#...a, #...a) => true
| #...a as x => true
| #...a | #...b => true
| (#...a: typ) => true
| lazy #...a => true
| exception #...a => true
| _ => false
}
"
Expand Down
16 changes: 8 additions & 8 deletions tests/printer/pattern/type.ns
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
let ##shape = x
let ##\"Shape" = x
let ##\"type" = x
let ##\"test 🏚" = x
let ##\"Shape✅" = x
let #...shape = x
let #...\"Shape" = x
let #...\"type" = x
let #...\"test 🏚" = x
let #...\"Shape✅" = x

switch (selectedChoice, value) {
| (##A.a, ##A.a) => true
| (##A.b, ##A.b) => true
| (##A.c, ##A.c) => true
| (#...A.a, #...A.a) => true
| (#...A.b, #...A.b) => true
| (#...A.c, #...A.c) => true
| _ => false
}
22 changes: 11 additions & 11 deletions tests/printer/pattern/variant.ns
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ let #\"Shape🎡"(\"module", \"ExoticIdent") = x

let cmp = (selectedChoice, value) =>
switch (selectedChoice, value) {
| (##a, ##a) => true
| [##b, ##b] => true
| list[##b, ##b] => true
| {x: ##c, y: ##c} => true
| Constructor(##a, ##a) => true
| #Constuctor(##a, ##a) => true
| ##a as x => true
| ##a | ##b => true
| (##a : typ) => true
| lazy ##a => true
| exception ##a => true
| (#...a, #...a) => true
| [#...b, #...b] => true
| list[#...b, #...b] => true
| {x: #...c, y: #...c} => true
| Constructor(#...a, #...a) => true
| #Constuctor(#...a, #...a) => true
| #...a as x => true
| #...a | #...b => true
| (#...a : typ) => true
| lazy #...a => true
| exception #...a => true
| _ => false
}