From d74292cc457b353b517806fbd8216f60c00f336d Mon Sep 17 00:00:00 2001 From: Cheng Lou Date: Fri, 3 Jul 2020 07:04:59 -0700 Subject: [PATCH] Change poly variant `##` spread to `#...` Fixes #7 --- src/napkin_core.ml | 27 +++++++------ src/napkin_grammar.ml | 2 +- src/napkin_parser.ml | 2 +- src/napkin_printer.ml | 2 +- src/napkin_scanner.ml | 5 +-- src/napkin_token.ml | 4 +- tests/parsing/grammar/pattern/polyvariants.js | 38 +++++++++---------- tests/parsing/grammar/pattern/variants.js | 15 ++++---- .../expr/__snapshots__/render.spec.js.snap | 2 +- tests/printer/expr/polyvariant.js | 2 +- .../pattern/__snapshots__/render.spec.js.snap | 38 +++++++++---------- tests/printer/pattern/type.ns | 16 ++++---- tests/printer/pattern/variant.ns | 22 +++++------ 13 files changed, 87 insertions(+), 88 deletions(-) diff --git a/src/napkin_core.ml b/src/napkin_core.ml index efc1fecb..b00530e8 100644 --- a/src/napkin_core.ml +++ b/src/napkin_core.ml @@ -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 } [ ; ] ] *) @@ -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 diff --git a/src/napkin_grammar.ml b/src/napkin_grammar.ml index 81c4e0b3..e07a6750 100644 --- a/src/napkin_grammar.ml +++ b/src/napkin_grammar.ml @@ -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 diff --git a/src/napkin_parser.ml b/src/napkin_parser.ml index 6cdcfd46..af385b6d 100644 --- a/src/napkin_parser.ml +++ b/src/napkin_parser.ml @@ -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 diff --git a/src/napkin_printer.ml b/src/napkin_printer.ml index 35d9b875..39d10714 100644 --- a/src/napkin_printer.ml +++ b/src/napkin_printer.ml @@ -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([ diff --git a/src/napkin_scanner.ml b/src/napkin_scanner.ml index 8bb508c6..2299b5aa 100644 --- a/src/napkin_scanner.ml +++ b/src/napkin_scanner.ml @@ -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 ( diff --git a/src/napkin_token.ml b/src/napkin_token.ml index 0f810236..139ff1bd 100644 --- a/src/napkin_token.ml +++ b/src/napkin_token.ml @@ -41,7 +41,7 @@ type t = | GreaterThan | LessThan | LessThanSlash - | Hash | HashEqual | HashHash + | Hash | HashEqual | Assert | Lazy | Tilde @@ -120,7 +120,7 @@ let toString = function | Backslash -> "\\" | Forwardslash -> "/" | ForwardslashDot -> "/." | Exception -> "exception" - | Hash -> "#" | HashHash -> "##" | HashEqual -> "#=" + | Hash -> "#" | HashEqual -> "#=" | GreaterThan -> ">" | LessThan -> "<" | LessThanSlash -> " () | #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) => () @@ -40,8 +40,8 @@ switch x { } -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 @@ -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 } diff --git a/tests/parsing/grammar/pattern/variants.js b/tests/parsing/grammar/pattern/variants.js index c0b73dc5..9fa828f5 100644 --- a/tests/parsing/grammar/pattern/variants.js +++ b/tests/parsing/grammar/pattern/variants.js @@ -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 @@ -31,8 +31,8 @@ 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) => () @@ -40,8 +40,8 @@ switch x { } -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 @@ -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 => () } - \ No newline at end of file diff --git a/tests/printer/expr/__snapshots__/render.spec.js.snap b/tests/printer/expr/__snapshots__/render.spec.js.snap index 22f42e95..309a91c5 100644 --- a/tests/printer/expr/__snapshots__/render.spec.js.snap +++ b/tests/printer/expr/__snapshots__/render.spec.js.snap @@ -2826,7 +2826,7 @@ let math = if discriminant < 0. { } switch x { -| ##typevar => 42 +| #...typevar => 42 } let r = #\\\\\\"Reducer⛪️\\" diff --git a/tests/printer/expr/polyvariant.js b/tests/printer/expr/polyvariant.js index 1225eeef..2925e342 100644 --- a/tests/printer/expr/polyvariant.js +++ b/tests/printer/expr/polyvariant.js @@ -92,7 +92,7 @@ let math = if discriminant < 0. { } switch x { -| ##typevar => 42 +| #...typevar => 42 } diff --git a/tests/printer/pattern/__snapshots__/render.spec.js.snap b/tests/printer/pattern/__snapshots__/render.spec.js.snap index 5b098370..c0d97248 100644 --- a/tests/printer/pattern/__snapshots__/render.spec.js.snap +++ b/tests/printer/pattern/__snapshots__/render.spec.js.snap @@ -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 } " @@ -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 } " diff --git a/tests/printer/pattern/type.ns b/tests/printer/pattern/type.ns index 3837a879..560ce89e 100644 --- a/tests/printer/pattern/type.ns +++ b/tests/printer/pattern/type.ns @@ -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 } diff --git a/tests/printer/pattern/variant.ns b/tests/printer/pattern/variant.ns index 3682bd06..9f608654 100644 --- a/tests/printer/pattern/variant.ns +++ b/tests/printer/pattern/variant.ns @@ -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 }