From dd6d919208b60ed4b65a1a0c5db47f1a19160dd8 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Fri, 6 Jun 2025 08:56:08 +0700 Subject: [PATCH 1/4] Fail CI on benchmark assert --- .github/workflows/ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8571789012..b4ff7e1d89 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -364,7 +364,9 @@ jobs: - name: Run syntax benchmarks if: matrix.benchmarks - run: ./_build/install/default/bin/syntax_benchmarks | tee tests/benchmark-output.json + run: | + set -o pipefail + ./_build/install/default/bin/syntax_benchmarks | tee tests/benchmark-output.json # Benchmarking is disabled for now because of inconsistent run times on different runners # From dbc4b90a12203fe569042f682f8b31f493c957b1 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Wed, 4 Jun 2025 09:40:09 +0200 Subject: [PATCH 2/4] Benchmark: print diagnostics in case of error --- tests/syntax_benchmarks/Benchmark.ml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/syntax_benchmarks/Benchmark.ml b/tests/syntax_benchmarks/Benchmark.ml index dc7093dbe9..1a7d2ffffa 100644 --- a/tests/syntax_benchmarks/Benchmark.ml +++ b/tests/syntax_benchmarks/Benchmark.ml @@ -160,7 +160,9 @@ end = struct let parse_rescript src filename = let p = Parser.make src filename in let structure = ResParser.parse_implementation p in - assert (p.diagnostics == []); + if p.diagnostics != [] then ( + Res_diagnostics.print_report p.diagnostics src; + assert false); structure let data_dir = "tests/syntax_benchmarks/data" From 978e6db9ba594b4ff5dcfb63b59b63cc91623d3b Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Fri, 6 Jun 2025 09:09:50 +0700 Subject: [PATCH 3/4] Remove pipe last in syntax benchmarks --- tests/syntax_benchmarks/data/Napkinscript.res | 184 +++++++++--------- 1 file changed, 92 insertions(+), 92 deletions(-) diff --git a/tests/syntax_benchmarks/data/Napkinscript.res b/tests/syntax_benchmarks/data/Napkinscript.res index 80429ca4de..7716c4b984 100644 --- a/tests/syntax_benchmarks/data/Napkinscript.res +++ b/tests/syntax_benchmarks/data/Napkinscript.res @@ -406,7 +406,7 @@ module Doc = { } let doc = toDoc(t) - toString(~width=10, doc) |> print_endline + toString(~width=10, doc)->print_endline } } @@ -1558,7 +1558,7 @@ module Comment: { decr(j) } if j.contents >= i.contents { - (@doesNotRaise Bytes.sub)(b, i.contents, j.contents - i.contents + 1) |> Bytes.to_string + (@doesNotRaise Bytes.sub)(b, i.contents, j.contents - i.contents + 1)->Bytes.to_string } else { "" } @@ -2540,7 +2540,7 @@ module Reporting = { min(len, startPos.pos_lnum + 3) } /* 2 lines after */ - let lines = lines |> drop(startLine) |> take(endLine - startLine) |> Array.of_list + let lines = lines->drop(startLine)->take(endLine - startLine)->Array.of_list let renderLine = (x, ix) => { let x = if ix == startPos.pos_lnum { @@ -2581,7 +2581,7 @@ module Reporting = { switch missing { | Some(len) => underline( - ~from=startCol + String.length(String.length(string_of_int(ix)) |> string_of_int) + 5, + ~from=startCol + String.length(String.length(string_of_int(ix))->string_of_int) + 5, ~len, x, ) @@ -4242,8 +4242,8 @@ module CommentTable = { Doc.line, }), ) - |> Doc.toString(~width=80) - |> print_endline + ->Doc.toString(~width=80) + ->print_endline } let attach = (tbl, loc, comments) => switch comments { @@ -5981,7 +5981,7 @@ module Printer = { } else { Doc.nil } - List.rev(list{trailingSpace, doc, ...acc}) |> Doc.concat + List.rev(list{trailingSpace, doc, ...acc})->Doc.concat | list{line, ...lines} => let line = String.trim(line) let len = String.length(line) @@ -9851,7 +9851,7 @@ module Printer = { switch props { | list{} => Doc.nil | props => - Doc.concat(list{Doc.line, Doc.group(Doc.join(~sep=Doc.line, props |> List.rev))}) + Doc.concat(list{Doc.line, Doc.group(Doc.join(~sep=Doc.line, props->List.rev))}) }, ) let (children, _) = ParsetreeViewer.collectListExpressions(children) @@ -9965,7 +9965,7 @@ module Printer = { let callback = Doc.concat(list{lblDoc, printPexpFun(~inCallback=true, expr, cmtTbl)}) let printedArgs = - List.map(arg => printArgument(arg, cmtTbl), args) |> Doc.join( + List.map(arg => printArgument(arg, cmtTbl), args)->Doc.join( ~sep=Doc.concat(list{Doc.comma, Doc.line}), ) @@ -11818,7 +11818,7 @@ module Scanner = { false } else { let leftOk = { - let c = startCnum - 1 |> (@doesNotRaise Bytes.get)(src) |> Char.code + let c = startCnum - 1->(@doesNotRaise Bytes.get)(src)->Char.code c === CharacterCodes.space || (c === CharacterCodes.tab || CharacterCodes.isLineBreak(c)) } @@ -11827,7 +11827,7 @@ module Scanner = { let c = if endCnum === Bytes.length(src) { -1 } else { - endCnum |> (@doesNotRaise Bytes.get)(src) |> Char.code + endCnum->(@doesNotRaise Bytes.get)(src)->Char.code } c === CharacterCodes.space || @@ -11888,17 +11888,17 @@ module JsFfi = { * @genType.import(/"./MyMath", "default"/) */ | Module(s) => let structure = list{ - Parsetree.Pconst_string(s, None) |> Ast_helper.Exp.constant |> Ast_helper.Str.eval, + Parsetree.Pconst_string(s, None)->Ast_helper.Exp.constant->Ast_helper.Str.eval, } let genType = (Location.mknoloc("genType.import"), Parsetree.PStr(structure)) list{genType} | Scope(longident) => let structureItem = { - let expr = switch Longident.flatten(longident) |> List.map(s => + let expr = switch Longident.flatten(longident)->List.map(s => Ast_helper.Exp.constant(Parsetree.Pconst_string(s, None)) ) { | list{expr} => expr - | list{} as exprs | _ as exprs => exprs |> Ast_helper.Exp.tuple + | list{} as exprs | _ as exprs => exprs->Ast_helper.Exp.tuple } Ast_helper.Str.eval(expr) @@ -11911,16 +11911,16 @@ module JsFfi = { let valueDescrs = switch importDescr.jid_spec { | Default(decl) => let prim = list{decl.jld_name} - let allAttrs = List.concat(list{attrs, importDescr.jid_attributes}) |> List.map(attr => + let allAttrs = List.concat(list{attrs, importDescr.jid_attributes})->List.map(attr => switch attr { | ( {Location.txt: "genType.import"} as id, Parsetree.PStr(list{{pstr_desc: Parsetree.Pstr_eval(moduleName, _)}}), ) => - let default = Parsetree.Pconst_string("default", None) |> Ast_helper.Exp.constant + let default = Parsetree.Pconst_string("default", None)->Ast_helper.Exp.constant let structureItem = - list{moduleName, default} |> Ast_helper.Exp.tuple |> Ast_helper.Str.eval + list{moduleName, default}->Ast_helper.Exp.tuple->Ast_helper.Str.eval (id, Parsetree.PStr(list{structureItem})) | attr => attr @@ -11934,7 +11934,7 @@ module JsFfi = { ~attrs=allAttrs, Location.mknoloc(decl.jld_alias), decl.jld_type, - ) |> Ast_helper.Str.primitive, + )->Ast_helper.Str.primitive, } | Spec(decls) => List.map(decl => { let prim = list{decl.jld_name} @@ -11945,14 +11945,14 @@ module JsFfi = { ~attrs=allAttrs, Location.mknoloc(decl.jld_alias), decl.jld_type, - ) |> Ast_helper.Str.primitive(~loc=decl.jld_loc) + )->Ast_helper.Str.primitive(~loc=decl.jld_loc) }, decls) } let jsFfiAttr = (Location.mknoloc("ns.jsFfi"), Parsetree.PStr(list{})) Ast_helper.Mod.structure(~loc=importDescr.jid_loc, valueDescrs) - |> Ast_helper.Incl.mk(~attrs=list{jsFfiAttr}, ~loc=importDescr.jid_loc) - |> Ast_helper.Str.include_(~loc=importDescr.jid_loc) + ->Ast_helper.Incl.mk(~attrs=list{jsFfiAttr}, ~loc=importDescr.jid_loc) + ->Ast_helper.Str.include_(~loc=importDescr.jid_loc) } } @@ -11960,7 +11960,7 @@ module ParsetreeCompatibility = { let concatLongidents = (l1, l2) => { let parts1 = Longident.flatten(l1) let parts2 = Longident.flatten(l2) - switch List.concat(list{parts1, parts2}) |> Longident.unflatten { + switch List.concat(list{parts1, parts2})->Longident.unflatten { | Some(longident) => longident | None => l2 } @@ -12312,7 +12312,7 @@ module ParsetreeCompatibility = { open Ast_mapper { ...default_mapper, - attributes: (mapper, attrs) => attrs |> List.filter(attr => + attributes: (mapper, attrs) => attrs->List.filter(attr => switch attr { | ( {Location.txt: "reason.preserve_braces" | "explicit_arity" | "implicity_arity"}, @@ -12320,7 +12320,7 @@ module ParsetreeCompatibility = { ) => false | _ => true } - ) |> default_mapper.attributes(mapper), + )->default_mapper.attributes(mapper), pat: (mapper, p) => switch p.ppat_desc { | Ppat_open({txt: longidentOpen}, pattern) => @@ -13108,7 +13108,7 @@ Solution: directly use `concat`." /* TODO: diagnostic reporting */ let lidentOfPath = longident => - switch Longident.flatten(longident) |> List.rev { + switch Longident.flatten(longident)->List.rev { | list{} => "" | list{ident, ..._} => ident } @@ -13398,7 +13398,7 @@ Solution: directly use `concat`." | Pexp_ident(openingIdent) => let opening = { let withoutCreateElement = - Longident.flatten(openingIdent.txt) |> List.filter(s => s != "createElement") + Longident.flatten(openingIdent.txt)->List.filter(s => s != "createElement") switch Longident.unflatten(withoutCreateElement) { | Some(li) => li @@ -13415,8 +13415,8 @@ Solution: directly use `concat`." switch nameExpr.Parsetree.pexp_desc { | Pexp_ident(openingIdent) => Longident.flatten(openingIdent.txt) - |> List.filter(s => s != "createElement") - |> String.concat(".") + ->List.filter(s => s != "createElement") + ->String.concat(".") | _ => "" } @@ -14162,11 +14162,11 @@ Solution: directly use `concat`." switch listPatterns { | list{(true, pattern), ...patterns} => - let patterns = patterns |> List.map(filterSpread) |> List.rev + let patterns = patterns->List.map(filterSpread)->List.rev let pat = makeListPattern(loc, patterns, Some(pattern)) {...pat, ppat_loc: loc, ppat_attributes: attrs} | patterns => - let patterns = patterns |> List.map(filterSpread) |> List.rev + let patterns = patterns->List.map(filterSpread)->List.rev let pat = makeListPattern(loc, patterns, None) {...pat, ppat_loc: loc, ppat_attributes: attrs} } @@ -15150,7 +15150,7 @@ Solution: directly use `concat`." | Pexp_ident(longident) => Ast_helper.Pat.var( ~loc=expr.pexp_loc, - Location.mkloc(Longident.flatten(longident.txt) |> String.concat("."), longident.loc), + Location.mkloc(Longident.flatten(longident.txt)->String.concat("."), longident.loc), ) /* TODO: can we convert more expressions to patterns? */ | _ => Ast_helper.Pat.var(~loc=expr.pexp_loc, Location.mkloc("pattern", expr.pexp_loc)) @@ -15188,7 +15188,7 @@ Solution: directly use `concat`." }), ), }), - ) |> Doc.toString(~width=80) + )->Doc.toString(~width=80) Parser.err( ~startPos=expr.pexp_loc.loc_start, @@ -15217,7 +15217,7 @@ Solution: directly use `concat`." }), ), }), - ) |> Doc.toString(~width=80), + )->Doc.toString(~width=80), ), ) @@ -15332,7 +15332,7 @@ Solution: directly use `concat`." /* definition ::= let [rec] let-binding { and let-binding } */ and parseLetBindings = (~attrs, p) => { let startPos = p.Parser.startPos - Parser.optional(p, Let) |> ignore + Parser.optional(p, Let)->ignore let recFlag = if Parser.optional(p, Token.Rec) { Asttypes.Recursive } else { @@ -15649,7 +15649,7 @@ Solution: directly use `concat`." | Colon => Parser.next(p) let fieldExpr = parseExpr(p) - Parser.optional(p, Comma) |> ignore + Parser.optional(p, Comma)->ignore let expr = parseRecordExprWithStringKeys(~startPos, (field, fieldExpr), p) Parser.expect(Rbrace, p) expr @@ -15928,20 +15928,20 @@ Solution: directly use `concat`." } let body = parseModuleBindingBody(p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let expr = parseExprBlock(p) let loc = mkLoc(startPos, p.prevEndPos) Ast_helper.Exp.letmodule(~loc, name, body, expr) } | Exception => let extensionConstructor = parseExceptionDef(~attrs, p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let blockExpr = parseExprBlock(p) let loc = mkLoc(startPos, p.prevEndPos) Ast_helper.Exp.letexception(~loc, extensionConstructor, blockExpr) | Open => let od = parseOpenDescription(~attrs, p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let blockExpr = parseExprBlock(p) let loc = mkLoc(startPos, p.prevEndPos) Ast_helper.Exp.open_(~loc, od.popen_override, od.popen_lid, blockExpr) @@ -16524,15 +16524,15 @@ Solution: directly use `concat`." let loc = mkLoc(startPos, p.prevEndPos) switch listExprs { | list{(true, expr), ...exprs} => - let exprs = exprs |> List.map(snd) |> List.rev + let exprs = exprs->List.map(snd)->List.rev makeListExpression(loc, exprs, Some(expr)) | exprs => - let exprs = exprs |> List.map(((spread, expr)) => { + let exprs = exprs->List.map(((spread, expr)) => { if spread { Parser.err(p, Diagnostics.message(ErrorMessages.listExprSpread)) } expr - }) |> List.rev + })->List.rev makeListExpression(loc, exprs, None) } @@ -17034,7 +17034,7 @@ Solution: directly use `concat`." Doc.text("Type parameters require angle brackets:"), Doc.indent(Doc.concat(list{Doc.line, Printer.printTypExpr(typ, CommentTable.empty)})), }), - ) |> Doc.toString(~width=80) + )->Doc.toString(~width=80) Parser.err(~startPos=openingStartPos, p, Diagnostics.message(msg)) Parser.next(p) @@ -17191,7 +17191,7 @@ Solution: directly use `concat`." Parser.expect(Rbrace, p) let loc = mkLoc(startPos, p.prevEndPos) let typ = makeBsObjType(~attrs=list{}, ~loc, ~closed=closedFlag, fields) - Parser.optional(p, Comma) |> ignore + Parser.optional(p, Comma)->ignore let moreArgs = parseCommaDelimitedRegion( ~grammar=Grammar.TypExprList, ~closing=Rparen, @@ -17249,7 +17249,7 @@ Solution: directly use `concat`." Parser.expect(Rbrace, p) let loc = mkLoc(startPos, p.prevEndPos) let typ = makeBsObjType(~attrs=list{}, ~loc, ~closed=closedFlag, fields) - Parser.optional(p, Comma) |> ignore + Parser.optional(p, Comma)->ignore let moreArgs = parseCommaDelimitedRegion( ~grammar=Grammar.TypExprList, ~closing=Rparen, @@ -17297,7 +17297,7 @@ Solution: directly use `concat`." } Parser.expect(Rbrace, p) - Parser.optional(p, Comma) |> ignore + Parser.optional(p, Comma)->ignore Parser.expect(Rparen, p) Parsetree.Pcstr_record(fields) } @@ -17486,7 +17486,7 @@ Solution: directly use `concat`." }), ), }), - ) |> Doc.toString(~width=80) + )->Doc.toString(~width=80) Parser.err(~startPos=openingStartPos, p, Diagnostics.message(msg)) Parser.next(p) @@ -17615,7 +17615,7 @@ Solution: directly use `concat`." Parser.expect(Rbrace, p) let loc = mkLoc(startPos, p.prevEndPos) - let typ = makeBsObjType(~attrs=list{}, ~loc, ~closed=closedFlag, fields) |> parseTypeAlias(p) + let typ = makeBsObjType(~attrs=list{}, ~loc, ~closed=closedFlag, fields)->parseTypeAlias(p) let typ = parseArrowTypeRest(~es6Arrow=true, ~startPos, typ, p) (Some(typ), Asttypes.Public, Parsetree.Ptype_abstract) @@ -17668,7 +17668,7 @@ Solution: directly use `concat`." Parser.expect(Rbrace, p) let loc = mkLoc(startPos, p.prevEndPos) let typ = - makeBsObjType(~attrs=list{}, ~loc, ~closed=closedFlag, fields) |> parseTypeAlias(p) + makeBsObjType(~attrs=list{}, ~loc, ~closed=closedFlag, fields)->parseTypeAlias(p) let typ = parseArrowTypeRest(~es6Arrow=true, ~startPos, typ, p) (Some(typ), Asttypes.Public, Parsetree.Ptype_abstract) @@ -17685,13 +17685,13 @@ Solution: directly use `concat`." | list{attr, ..._} as attrs => let first = { let field = parseFieldDeclaration(p) - Parser.optional(p, Comma) |> ignore + Parser.optional(p, Comma)->ignore { ...field, Parsetree.pld_attributes: attrs, pld_loc: { ...field.Parsetree.pld_loc, - loc_start: (attr |> fst).loc.loc_start, + loc_start: (attr->fst).loc.loc_start, }, } } @@ -17780,7 +17780,7 @@ Solution: directly use `concat`." variant | LessThan => Parser.next(p) - Parser.optional(p, Bar) |> ignore + Parser.optional(p, Bar)->ignore let rowField = parseTagSpecFull(p) let rowFields = parseTagSpecFulls(p) let tagNames = if p.token === GreaterThan { @@ -17980,7 +17980,7 @@ Solution: directly use `concat`." } let constrStart = p.Parser.startPos - Parser.optional(p, Bar) |> ignore + Parser.optional(p, Bar)->ignore let first = { let (attrs, name, kind) = switch p.Parser.token { | Bar => @@ -18179,73 +18179,73 @@ Solution: directly use `concat`." switch p.Parser.token { | Open => let openDescription = parseOpenDescription(~attrs, p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Some(Ast_helper.Str.open_(~loc, openDescription)) | Let => let (recFlag, letBindings) = parseLetBindings(~attrs, p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Some(Ast_helper.Str.value(~loc, recFlag, letBindings)) | Typ => Parser.beginRegion(p) switch parseTypeDefinitionOrExtension(~attrs, p) { | TypeDef({recFlag, types}) => - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Parser.endRegion(p) Some(Ast_helper.Str.type_(~loc, recFlag, types)) | TypeExt(ext) => - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Parser.endRegion(p) Some(Ast_helper.Str.type_extension(~loc, ext)) } | External => let externalDef = parseExternalDef(~attrs, p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Some(Ast_helper.Str.primitive(~loc, externalDef)) | Import => let importDescr = parseJsImport(~startPos, ~attrs, p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) let structureItem = JsFfi.toParsetree(importDescr) Some({...structureItem, pstr_loc: loc}) | Exception => let exceptionDef = parseExceptionDef(~attrs, p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Some(Ast_helper.Str.exception_(~loc, exceptionDef)) | Include => let includeStatement = parseIncludeStatement(~attrs, p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Some(Ast_helper.Str.include_(~loc, includeStatement)) | Export => let structureItem = parseJsExport(~attrs, p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Some({...structureItem, pstr_loc: loc}) | Module => let structureItem = parseModuleOrModuleTypeImplOrPackExpr(~attrs, p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Some({...structureItem, pstr_loc: loc}) | AtAt => let attr = parseStandaloneAttribute(p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Some(Ast_helper.Str.attribute(~loc, attr)) | PercentPercent => let extension = parseExtension(~moduleLanguage=true, p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Some(Ast_helper.Str.extension(~attrs, ~loc, extension)) | token when Grammar.isExprStart(token) => let prevEndPos = p.Parser.endPos let exp = parseExpr(p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Parser.checkProgress(~prevEndPos, ~result=Ast_helper.Str.eval(~loc, ~attrs, exp), p) | _ => None @@ -18928,7 +18928,7 @@ Solution: directly use `concat`." | Let => Parser.beginRegion(p) let valueDesc = parseSignLetDesc(~attrs, p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Parser.endRegion(p) Some(Ast_helper.Sig.value(~loc, valueDesc)) @@ -18936,29 +18936,29 @@ Solution: directly use `concat`." Parser.beginRegion(p) switch parseTypeDefinitionOrExtension(~attrs, p) { | TypeDef({recFlag, types}) => - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Parser.endRegion(p) Some(Ast_helper.Sig.type_(~loc, recFlag, types)) | TypeExt(ext) => - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Parser.endRegion(p) Some(Ast_helper.Sig.type_extension(~loc, ext)) } | External => let externalDef = parseExternalDef(~attrs, p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Some(Ast_helper.Sig.value(~loc, externalDef)) | Exception => let exceptionDef = parseExceptionDef(~attrs, p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Some(Ast_helper.Sig.exception_(~loc, exceptionDef)) | Open => let openDescription = parseOpenDescription(~attrs, p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Some(Ast_helper.Sig.open_(~loc, openDescription)) | Include => @@ -18970,7 +18970,7 @@ Solution: directly use `concat`." moduleType, ) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Some(Ast_helper.Sig.include_(~loc, includeDescription)) | Module => @@ -18978,29 +18978,29 @@ Solution: directly use `concat`." switch p.Parser.token { | Uident(_) => let modDecl = parseModuleDeclarationOrAlias(~attrs, p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Some(Ast_helper.Sig.module_(~loc, modDecl)) | Rec => let recModule = parseRecModuleSpec(~attrs, ~startPos, p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Some(Ast_helper.Sig.rec_module(~loc, recModule)) | Typ => Some(parseModuleTypeDeclaration(~attrs, ~startPos, p)) | _t => let modDecl = parseModuleDeclarationOrAlias(~attrs, p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Some(Ast_helper.Sig.module_(~loc, modDecl)) } | AtAt => let attr = parseStandaloneAttribute(p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Some(Ast_helper.Sig.attribute(~loc, attr)) | PercentPercent => let extension = parseExtension(~moduleLanguage=true, p) - Parser.optional(p, Semicolon) |> ignore + Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Some(Ast_helper.Sig.extension(~attrs, ~loc, extension)) | Import => @@ -20337,7 +20337,7 @@ module Driver: { let extractOcamlStringData = filename => { let lexbuf = if String.length(filename) > 0 { - IO.readFile(filename) |> Lexing.from_string + IO.readFile(filename)->Lexing.from_string } else { Lexing.from_channel(stdin) } @@ -20370,7 +20370,7 @@ module Driver: { let parseOcaml = (type a, kind: file_kind, filename): a => { let lexbuf = if String.length(filename) > 0 { - IO.readFile(filename) |> Lexing.from_string + IO.readFile(filename)->Lexing.from_string } else { Lexing.from_channel(stdin) } @@ -20379,12 +20379,12 @@ module Driver: { switch kind { | Structure => Parse.implementation(lexbuf) - |> ParsetreeCompatibility.replaceStringLiteralStructure(stringData) - |> ParsetreeCompatibility.structure + ->ParsetreeCompatibility.replaceStringLiteralStructure(stringData) + ->ParsetreeCompatibility.structure | Signature => Parse.interface(lexbuf) - |> ParsetreeCompatibility.replaceStringLiteralSignature(stringData) - |> ParsetreeCompatibility.signature + ->ParsetreeCompatibility.replaceStringLiteralSignature(stringData) + ->ParsetreeCompatibility.signature } } @@ -20415,7 +20415,7 @@ module Driver: { let parseOcamlFile = (kind, filename) => { let ast = parseOcaml(kind, filename) let lexbuf2 = if String.length(filename) > 0 { - IO.readFile(filename) |> Lexing.from_string + IO.readFile(filename)->Lexing.from_string } else { Lexing.from_channel(stdin) } @@ -20500,14 +20500,14 @@ module Driver: { switch kind { | Structure => ast - |> ParsetreeCompatibility.replaceStringLiteralStructure(stringData.contents) - |> ParsetreeCompatibility.normalizeReasonArityStructure(~forPrinter=true) - |> ParsetreeCompatibility.structure + ->ParsetreeCompatibility.replaceStringLiteralStructure(stringData.contents) + ->ParsetreeCompatibility.normalizeReasonArityStructure(~forPrinter=true) + ->ParsetreeCompatibility.structure | Signature => ast - |> ParsetreeCompatibility.replaceStringLiteralSignature(stringData.contents) - |> ParsetreeCompatibility.normalizeReasonAritySignature(~forPrinter=true) - |> ParsetreeCompatibility.signature + ->ParsetreeCompatibility.replaceStringLiteralSignature(stringData.contents) + ->ParsetreeCompatibility.normalizeReasonAritySignature(~forPrinter=true) + ->ParsetreeCompatibility.signature } } @@ -20583,7 +20583,7 @@ module Driver: { | "ns" | "napkinscript" => Printer.printImplementation(~width, ast, List.rev(_parserState.Parser.comments)) | "ast" => Printast.implementation(Format.std_formatter, ast) - | "sexp" => ast |> SexpAst.implementation |> Sexp.toString |> print_string + | "sexp" => ast->SexpAst.implementation->Sexp.toString->print_string | _ => /* default binary */ output_string(stdout, Config.ast_impl_magic_number) @@ -20597,7 +20597,7 @@ module Driver: { | "ns" | "napkinscript" => Printer.printInterface(~width, ast, List.rev(_parserState.Parser.comments)) | "ast" => Printast.interface(Format.std_formatter, ast) - | "sexp" => ast |> SexpAst.interface |> Sexp.toString |> print_string + | "sexp" => ast->SexpAst.interface->Sexp.toString->print_string | _ => /* default binary */ output_string(stdout, Config.ast_intf_magic_number) From 08fe9456d75b24f60fc79db033c7e3decc6448c3 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Fri, 6 Jun 2025 09:18:08 +0700 Subject: [PATCH 4/4] Format syntax benchmarks --- scripts/format.sh | 2 +- scripts/format_check.sh | 2 +- tests/syntax_benchmarks/data/Napkinscript.res | 719 +++++++++--------- tests/syntax_benchmarks/data/RedBlackTree.res | 411 +++++----- .../data/RedBlackTreeNoComments.res | 110 +-- 5 files changed, 600 insertions(+), 644 deletions(-) diff --git a/scripts/format.sh b/scripts/format.sh index b9b8f0fed7..a1bbd91607 100755 --- a/scripts/format.sh +++ b/scripts/format.sh @@ -4,7 +4,7 @@ shopt -s extglob dune build @fmt --auto-promote -files=$(find runtime tests -type f \( -name "*.res" -o -name "*.resi" \) ! -name "syntaxErrors*" ! -name "generated_mocha_test.res" ! -path "tests/syntax_*" ! -path "tests/analysis_tests/tests*" ! -path "*/node_modules/*") +files=$(find runtime tests -type f \( -name "*.res" -o -name "*.resi" \) ! -name "syntaxErrors*" ! -name "generated_mocha_test.res" ! -path "tests/syntax_tests*" ! -path "tests/analysis_tests/tests*" ! -path "*/node_modules/*") ./cli/rescript.js format $files yarn format diff --git a/scripts/format_check.sh b/scripts/format_check.sh index 4415b470c1..b7daf67d4f 100755 --- a/scripts/format_check.sh +++ b/scripts/format_check.sh @@ -17,7 +17,7 @@ case "$(uname -s)" in fi echo "Checking ReScript code formatting..." - files=$(find runtime tests -type f \( -name "*.res" -o -name "*.resi" \) ! -name "syntaxErrors*" ! -name "generated_mocha_test.res" ! -path "tests/syntax_*" ! -path "tests/analysis_tests/tests*" ! -path "*/node_modules/*") + files=$(find runtime tests -type f \( -name "*.res" -o -name "*.resi" \) ! -name "syntaxErrors*" ! -name "generated_mocha_test.res" ! -path "tests/syntax_tests*" ! -path "tests/analysis_tests/tests*" ! -path "*/node_modules/*") if ./cli/rescript.js format -check $files; then printf "${successGreen}✅ ReScript code formatting ok.${reset}\n" else diff --git a/tests/syntax_benchmarks/data/Napkinscript.res b/tests/syntax_benchmarks/data/Napkinscript.res index 7716c4b984..03093ace2f 100644 --- a/tests/syntax_benchmarks/data/Napkinscript.res +++ b/tests/syntax_benchmarks/data/Napkinscript.res @@ -45,6 +45,7 @@ module MiniBuffer: { let new_buffer = (@doesNotRaise Bytes.create)(new_len.contents) /* PR#6148: let's keep using [blit] rather than [unsafe_blit] in this tricky function that is slow anyway. */ + @doesNotRaise Bytes.blit(b.buffer, 0, new_buffer, 0, b.position) b.buffer = new_buffer @@ -66,6 +67,7 @@ module MiniBuffer: { if new_position > b.length { resize_internal(b, len) } + @doesNotRaise Bytes.blit_string(s, 0, b.buffer, b.position, len) b.position = new_position @@ -159,7 +161,7 @@ module Doc = { | Group({shouldBreak: forceBreak, doc: children}) => let (childForcesBreak, newChildren) = walk(children) let shouldBreak = forceBreak || childForcesBreak - (shouldBreak, Group({shouldBreak: shouldBreak, doc: newChildren})) + (shouldBreak, Group({shouldBreak, doc: newChildren})) | Concat(children) => let (forceBreak, newChildren) = List.fold_left(((forceBreak, newChildren), child) => { let (childForcesBreak, newChild) = walk(child) @@ -175,7 +177,7 @@ module Doc = { * might need to be broken. We just don't propagate them any higher here */ let children = switch walk(Concat(children)) { | (_, Concat(children)) => children - | _ => assert false + | _ => assert(false) } (false, CustomLayout(children)) @@ -198,7 +200,7 @@ module Doc = { let rec fits = (w, doc) => switch doc { - | _ when w < 0 => false + | _ if w < 0 => false | list{} => true | list{(_ind, _mode, Text(txt)), ...rest} => fits(w - String.length(txt), rest) | list{(ind, mode, Indent(doc)), ...rest} => fits(w, list{(ind + 2, mode, doc), ...rest}) @@ -1523,24 +1525,24 @@ module Comment: { ) let makeSingleLineComment = (~loc, txt) => { - txt: txt, - loc: loc, + txt, + loc, style: SingleLine, prevTokEndPos: Lexing.dummy_pos, } let makeMultiLineComment = (~loc, txt) => { - txt: txt, - loc: loc, + txt, + loc, style: MultiLine, prevTokEndPos: Lexing.dummy_pos, } let fromOcamlComment = (~loc, ~txt, ~prevTokEndPos) => { - txt: txt, - loc: loc, + txt, + loc, style: MultiLine, - prevTokEndPos: prevTokEndPos, + prevTokEndPos, } let trimSpaces = s => { @@ -2140,7 +2142,7 @@ module Grammar = { | AtAt | PercentPercent | At => true - | t when isExprStart(t) => true + | t if isExprStart(t) => true | _ => false } @@ -2174,7 +2176,7 @@ module Grammar = { let isParameterStart = x => switch x { | Token.Typ | Tilde | Dot => true - | token when isPatternStart(token) => true + | token if isPatternStart(token) => true | _ => false } @@ -2191,7 +2193,7 @@ module Grammar = { | Token.At | Mutable | Lident(_) | List => true /* recovery, TODO: this is not ideal… */ | Uident(_) => true - | t when Token.isKeyword(t) => true + | t if Token.isKeyword(t) => true | _ => false } @@ -2220,7 +2222,7 @@ module Grammar = { let isTypeParameterStart = x => switch x { | Token.Tilde | Dot => true - | token when isTypExprStart(token) => true + | token if isTypExprStart(token) => true | _ => false } @@ -2247,7 +2249,7 @@ module Grammar = { | Token.DotDotDot => true | Token.Uident(_) | Lident(_) | List => true /* TODO */ - | t when Token.isKeyword(t) => true + | t if Token.isKeyword(t) => true | _ => false } @@ -2260,21 +2262,21 @@ module Grammar = { let isArgumentStart = x => switch x { | Token.Tilde | Dot | Underscore => true - | t when isExprStart(t) => true + | t if isExprStart(t) => true | _ => false } let isPatternMatchStart = x => switch x { | Token.Bar => true - | t when isPatternStart(t) => true + | t if isPatternStart(t) => true | _ => false } let isPatternOcamlListStart = x => switch x { | Token.DotDotDot => true - | t when isPatternStart(t) => true + | t if isPatternStart(t) => true | _ => false } @@ -2393,12 +2395,12 @@ module Grammar = { | (JsFfiImport, Rbrace) | (StringFieldDeclarations, Rbrace) => true - | (Attribute, token) when token != At => true - | (TypeConstraint, token) when token != Constraint => true - | (PackageConstraint, token) when token != And => true - | (ConstructorDeclaration, token) when token != Bar => true + | (Attribute, token) if token != At => true + | (TypeConstraint, token) if token != Constraint => true + | (PackageConstraint, token) if token != And => true + | (ConstructorDeclaration, token) if token != Bar => true | (Primitive, Semicolon) => true - | (Primitive, token) when isStructureItemStart(token) => true + | (Primitive, token) if isStructureItemStart(token) => true | _ => false } @@ -2420,7 +2422,7 @@ module Reporting = { | Indent({amount: int, doc: document}) | Append({doc1: document, doc2: document}) - let group = (~break, doc) => Group({break: break, doc: doc}) + let group = (~break, doc) => Group({break, doc}) let text = txt => Text(txt) let indent = (i, d) => Indent({amount: i, doc: d}) let append = (d1, d2) => Append({doc1: d1, doc2: d2}) @@ -2430,7 +2432,7 @@ module Reporting = { | Empty | Cons({doc: document, stack: stack}) - let push = (stack, doc) => Cons({doc: doc, stack: stack}) + let push = (stack, doc) => Cons({doc, stack}) type mode = | Flat @@ -2488,11 +2490,11 @@ module Reporting = { | Invalid_argument(_) => "" } let content = - "\027[31m" ++ + "\x1b[31m" ++ (try String.sub(txt, from, len) catch { | Invalid_argument(_) => "" } ++ - "\027[0m") + "\x1b[0m") let after = try String.sub(txt, from + len, String.length(txt) - (from + len)) catch { | Invalid_argument(_) => "" @@ -2523,7 +2525,7 @@ module Reporting = { let rec take = (n, l) => switch l { - | _ when n === 0 => list{} + | _ if n === 0 => list{} | list{} => list{} | list{x, ...xs} => list{x, ...take(n - 1, xs)} } @@ -2545,7 +2547,7 @@ module Reporting = { let renderLine = (x, ix) => { let x = if ix == startPos.pos_lnum { switch missing { - | Some(_len) => x ++ @doesNotRaise String.make(10, ' ') + | Some(_len) => x ++ (@doesNotRaise String.make(10, ' ')) | None => x } } else { @@ -2696,7 +2698,7 @@ module Diagnostics: { | Lident(lident) => let guess = String.capitalize_ascii(lident) "Did you mean `" ++ (guess ++ ("` instead of `" ++ (lident ++ "`?"))) - | t when Token.isKeyword(t) => + | t if Token.isKeyword(t) => let token = Token.toString(t) "`" ++ (token ++ "` is a reserved keyword.") | _ => "At this point, I'm looking for an uppercased identifier like `Belt` or `Array`" @@ -2706,7 +2708,7 @@ module Diagnostics: { | Uident(uident) => let guess = String.uncapitalize_ascii(uident) "Did you mean `" ++ (guess ++ ("` instead of `" ++ (uident ++ "`?"))) - | t when Token.isKeyword(t) => + | t if Token.isKeyword(t) => let token = Token.toString(t) "`" ++ (token ++ @@ -2739,7 +2741,7 @@ module Diagnostics: { list{(StringFieldDeclarations | FieldDeclarations, _), ..._}, String(_) | At | Rbrace | Comma | Eof, ) => "I'm missing a type here" - | (_, t) when Grammar.isStructureItemStart(t) || t == Eof => "Missing a type here" + | (_, t) if Grammar.isStructureItemStart(t) || t == Eof => "Missing a type here" | _ => defaultUnexpected(t) } | list{(ExprOperand, _), ...breadcrumbs} => @@ -2819,10 +2821,10 @@ module Diagnostics: { } let make = (~filename, ~startPos, ~endPos, category) => { - filename: filename, - startPos: startPos, - endPos: endPos, - category: category, + filename, + startPos, + endPos, + category, } let stringOfReport = (~style, diagnostics, src) => @@ -2842,9 +2844,9 @@ module Diagnostics: { Buffer.contents(buffer) } - let unexpected = (token, context) => Unexpected({token: token, context: context}) + let unexpected = (token, context) => Unexpected({token, context}) - let expected = (~grammar=?, pos, token) => Expected({context: grammar, pos: pos, token: token}) + let expected = (~grammar=?, pos, token) => Expected({context: grammar, pos, token}) let uident = currentToken => Uident(currentToken) let lident = currentToken => Lident(currentToken) @@ -3130,34 +3132,33 @@ module ParsetreeViewer: { let rec collect = (attrsBefore, acc, expr) => switch expr { | { - pexp_desc: - Pexp_fun( - Nolabel, - None, - {ppat_desc: Ppat_var({txt: "__x"})}, - {pexp_desc: Pexp_apply(_)}, - ), + pexp_desc: Pexp_fun( + Nolabel, + None, + {ppat_desc: Ppat_var({txt: "__x"})}, + {pexp_desc: Pexp_apply(_)}, + ), } => (attrsBefore, List.rev(acc), rewriteUnderscoreApply(expr)) | {pexp_desc: Pexp_fun(lbl, defaultExpr, pattern, returnExpr), pexp_attributes: list{}} => let parameter = Parameter({ attrs: list{}, - lbl: lbl, - defaultExpr: defaultExpr, + lbl, + defaultExpr, pat: pattern, }) collect(attrsBefore, list{parameter, ...acc}, returnExpr) | {pexp_desc: Pexp_newtype(stringLoc, rest), pexp_attributes: attrs} => let (stringLocs, returnExpr) = collectNewTypes(list{stringLoc}, rest) - let param = NewTypes({attrs: attrs, locs: stringLocs}) + let param = NewTypes({attrs, locs: stringLocs}) collect(attrsBefore, list{param, ...acc}, returnExpr) | { pexp_desc: Pexp_fun(lbl, defaultExpr, pattern, returnExpr), pexp_attributes: list{({txt: "bs"}, _)} as attrs, } => let parameter = Parameter({ - attrs: attrs, - lbl: lbl, - defaultExpr: defaultExpr, + attrs, + lbl, + defaultExpr, pat: pattern, }) collect(attrsBefore, list{parameter, ...acc}, returnExpr) @@ -3166,9 +3167,9 @@ module ParsetreeViewer: { pexp_attributes: attrs, } => let parameter = Parameter({ - attrs: attrs, - lbl: lbl, - defaultExpr: defaultExpr, + attrs, + lbl, + defaultExpr, pat: pattern, }) collect(attrsBefore, list{parameter, ...acc}, returnExpr) @@ -3224,8 +3225,8 @@ module ParsetreeViewer: { | Pexp_construct({txt: Longident.Lident("::" | "[]")}, _) | Pexp_extension({txt: "obj"}, _) | Pexp_record(_) => true - | _ when isBlockExpr(expr) => true - | _ when isBracedExpr(expr) => true + | _ if isBlockExpr(expr) => true + | _ if isBracedExpr(expr) => true | _ => false } @@ -3236,7 +3237,7 @@ module ParsetreeViewer: { | Pexp_construct({txt: Longident.Lident("::" | "[]")}, _) | Pexp_extension({txt: "obj"}, _) | Pexp_record(_) => true - | _ when isBracedExpr(expr) => true + | _ if isBracedExpr(expr) => true | _ => false } @@ -3268,7 +3269,7 @@ module ParsetreeViewer: { let isUnaryExpression = expr => switch expr.pexp_desc { | Pexp_apply({pexp_desc: Pexp_ident({txt: Longident.Lident(operator)})}, list{(Nolabel, _arg)}) - when isUnaryOperator(operator) => true + if isUnaryOperator(operator) => true | _ => false } @@ -3306,7 +3307,7 @@ module ParsetreeViewer: { | Pexp_apply( {pexp_desc: Pexp_ident({txt: Longident.Lident(operator)})}, list{(Nolabel, _operand1), (Nolabel, _operand2)}, - ) when isBinaryOperator(operator) => true + ) if isBinaryOperator(operator) => true | _ => false } @@ -3351,8 +3352,7 @@ module ParsetreeViewer: { let isTernaryExpr = expr => switch expr { - | {pexp_attributes: attrs, pexp_desc: Pexp_ifthenelse(_)} - when hasTernaryAttribute(attrs) => true + | {pexp_attributes: attrs, pexp_desc: Pexp_ifthenelse(_)} if hasTernaryAttribute(attrs) => true | _ => false } @@ -3360,7 +3360,7 @@ module ParsetreeViewer: { let rec collect = (acc, expr) => switch expr { | {pexp_attributes: attrs, pexp_desc: Pexp_ifthenelse(condition, consequent, Some(alternate))} - when hasTernaryAttribute(attrs) => + if hasTernaryAttribute(attrs) => collect(list{(condition, consequent), ...acc}, alternate) | alternate => (List.rev(acc), alternate) } @@ -3371,7 +3371,7 @@ module ParsetreeViewer: { let parametersShouldHug = parameters => switch parameters { | list{Parameter({attrs: list{}, lbl: Asttypes.Nolabel, defaultExpr: None, pat})} - when isHuggablePattern(pat) => true + if isHuggablePattern(pat) => true | _ => false } @@ -3406,24 +3406,22 @@ module ParsetreeViewer: { let samePrecedenceSubExpression = (operator, subExpression) => switch subExpression { | { - pexp_desc: - Pexp_apply( - {pexp_desc: Pexp_ident({txt: Longident.Lident(subOperator)})}, - list{(Nolabel, _lhs), (Nolabel, _rhs)}, - ), - } when isBinaryOperator(subOperator) => + pexp_desc: Pexp_apply( + {pexp_desc: Pexp_ident({txt: Longident.Lident(subOperator)})}, + list{(Nolabel, _lhs), (Nolabel, _rhs)}, + ), + } if isBinaryOperator(subOperator) => flattenableOperators(operator, subOperator) | _ => true } switch expr { | { - pexp_desc: - Pexp_apply( - {pexp_desc: Pexp_ident({txt: Longident.Lident(operator)})}, - list{(Nolabel, lhs), (Nolabel, _rhs)}, - ), - } when isBinaryOperator(operator) => + pexp_desc: Pexp_apply( + {pexp_desc: Pexp_ident({txt: Longident.Lident(operator)})}, + list{(Nolabel, lhs), (Nolabel, _rhs)}, + ), + } if isBinaryOperator(operator) => isEqualityOperator(operator) || (!samePrecedenceSubExpression(operator, lhs) || operator == ":=") @@ -3540,7 +3538,7 @@ module ParsetreeViewer: { | Pexp_apply( {pexp_desc: Pexp_ident({txt: Longident.Lident("^")})}, list{(Nolabel, arg1), (Nolabel, arg2)}, - ) when !(isPexpConstantString(arg1) && isPexpConstantString(arg2)) => + ) if !(isPexpConstantString(arg1) && isPexpConstantString(arg2)) => isTemplateLiteral(arg1) || isTemplateLiteral(arg2) | Pexp_constant(Pconst_string(_, Some(_))) => true | _ => false @@ -3687,8 +3685,10 @@ module Parens: { | _ => switch expr { | { - Parsetree.pexp_desc: - Pexp_constraint({pexp_desc: Pexp_pack(_)}, {ptyp_desc: Ptyp_package(_)}), + Parsetree.pexp_desc: Pexp_constraint( + {pexp_desc: Pexp_pack(_)}, + {ptyp_desc: Ptyp_package(_)}, + ), } => Nothing | {pexp_desc: Pexp_constraint(_)} => Parenthesized @@ -3704,20 +3704,21 @@ module Parens: { | _ => switch expr { | {Parsetree.pexp_attributes: attrs} - when switch ParsetreeViewer.filterParsingAttrs(attrs) { + if switch ParsetreeViewer.filterParsingAttrs(attrs) { | list{_, ..._} => true | list{} => false } => Parenthesized - | _ - when ParsetreeViewer.isUnaryExpression(expr) || ParsetreeViewer.isBinaryExpression(expr) => + | _ if ParsetreeViewer.isUnaryExpression(expr) || ParsetreeViewer.isBinaryExpression(expr) => Parenthesized | { - Parsetree.pexp_desc: - Pexp_constraint({pexp_desc: Pexp_pack(_)}, {ptyp_desc: Ptyp_package(_)}), + Parsetree.pexp_desc: Pexp_constraint( + {pexp_desc: Pexp_pack(_)}, + {ptyp_desc: Ptyp_package(_)}, + ), } => Nothing - | {pexp_desc: Pexp_fun(_)} when ParsetreeViewer.isUnderscoreApplySugar(expr) => Nothing + | {pexp_desc: Pexp_fun(_)} if ParsetreeViewer.isUnderscoreApplySugar(expr) => Nothing | { pexp_desc: Pexp_lazy(_) @@ -3746,12 +3747,14 @@ module Parens: { | None => switch expr { | _ - when ParsetreeViewer.hasAttributes(expr.pexp_attributes) && + if ParsetreeViewer.hasAttributes(expr.pexp_attributes) && !ParsetreeViewer.isJsxExpression(expr) => Parenthesized | { - Parsetree.pexp_desc: - Pexp_constraint({pexp_desc: Pexp_pack(_)}, {ptyp_desc: Ptyp_package(_)}), + Parsetree.pexp_desc: Pexp_constraint( + {pexp_desc: Pexp_pack(_)}, + {ptyp_desc: Ptyp_package(_)}, + ), } => Nothing | {pexp_desc: Pexp_constraint(_)} => Parenthesized @@ -3767,17 +3770,17 @@ module Parens: { | None => switch expr { | {Parsetree.pexp_attributes: attrs} - when switch ParsetreeViewer.filterParsingAttrs(attrs) { + if switch ParsetreeViewer.filterParsingAttrs(attrs) { | list{_, ..._} => true | list{} => false } => Parenthesized | expr - when ParsetreeViewer.isUnaryExpression(expr) || ParsetreeViewer.isBinaryExpression(expr) => + if ParsetreeViewer.isUnaryExpression(expr) || ParsetreeViewer.isBinaryExpression(expr) => Parenthesized | {pexp_desc: Pexp_constraint({pexp_desc: Pexp_pack(_)}, {ptyp_desc: Ptyp_package(_)})} => Nothing - | {pexp_desc: Pexp_fun(_)} when ParsetreeViewer.isUnderscoreApplySugar(expr) => Nothing + | {pexp_desc: Pexp_fun(_)} if ParsetreeViewer.isUnderscoreApplySugar(expr) => Nothing | { pexp_desc: Pexp_lazy(_) @@ -3807,16 +3810,18 @@ module Parens: { | None => switch expr { | { - Parsetree.pexp_desc: - Pexp_constraint({pexp_desc: Pexp_pack(_)}, {ptyp_desc: Ptyp_package(_)}), + Parsetree.pexp_desc: Pexp_constraint( + {pexp_desc: Pexp_pack(_)}, + {ptyp_desc: Ptyp_package(_)}, + ), } => Nothing - | {pexp_desc: Pexp_fun(_)} when ParsetreeViewer.isUnderscoreApplySugar(expr) => Nothing + | {pexp_desc: Pexp_fun(_)} if ParsetreeViewer.isUnderscoreApplySugar(expr) => Nothing | {pexp_desc: Pexp_constraint(_) | Pexp_fun(_) | Pexp_function(_) | Pexp_newtype(_)} => Parenthesized - | expr when ParsetreeViewer.isBinaryExpression(expr) => Parenthesized - | expr when ParsetreeViewer.isTernaryExpr(expr) => Parenthesized - | {pexp_desc: Pexp_lazy(_) | Pexp_assert(_)} when isLhs => Parenthesized + | expr if ParsetreeViewer.isBinaryExpression(expr) => Parenthesized + | expr if ParsetreeViewer.isTernaryExpr(expr) => Parenthesized + | {pexp_desc: Pexp_lazy(_) | Pexp_assert(_)} if isLhs => Parenthesized | _ => Nothing } } @@ -3837,7 +3842,7 @@ module Parens: { | Parsetree.Pexp_apply( {pexp_attributes: list{}, pexp_desc: Pexp_ident({txt: Longident.Lident(operator)})}, list{(_, _left), (_, _right)}, - ) when ParsetreeViewer.isBinaryOperator(operator) => + ) if ParsetreeViewer.isBinaryOperator(operator) => let precParent = ParsetreeViewer.operatorPrecedence(parentOperator) let precChild = ParsetreeViewer.operatorPrecedence(operator) precParent === precChild @@ -3849,14 +3854,14 @@ module Parens: { | Parsetree.Pexp_apply( {pexp_desc: Pexp_ident({txt: Longident.Lident(operator)})}, list{(_, _left), (_, _right)}, - ) when ParsetreeViewer.isBinaryOperator(operator) => + ) if ParsetreeViewer.isBinaryOperator(operator) => let precParent = ParsetreeViewer.operatorPrecedence(parentOperator) let precChild = ParsetreeViewer.operatorPrecedence(operator) precParent >= precChild || rhs.pexp_attributes != list{} | Pexp_constraint({pexp_desc: Pexp_pack(_)}, {ptyp_desc: Ptyp_package(_)}) => false - | Pexp_fun(_) when ParsetreeViewer.isUnderscoreApplySugar(rhs) => false + | Pexp_fun(_) if ParsetreeViewer.isUnderscoreApplySugar(rhs) => false | Pexp_fun(_) | Pexp_newtype(_) | Pexp_setfield(_) | Pexp_constraint(_) => true - | _ when ParsetreeViewer.isTernaryExpr(rhs) => true + | _ if ParsetreeViewer.isTernaryExpr(rhs) => true | _ => false } @@ -3867,15 +3872,15 @@ module Parens: { | None => switch expr { | {Parsetree.pexp_attributes: attrs} - when switch ParsetreeViewer.filterParsingAttrs(attrs) { + if switch ParsetreeViewer.filterParsingAttrs(attrs) { | list{_, ..._} => true | list{} => false } => Parenthesized - | expr when ParsetreeViewer.isBinaryExpression(expr) => Parenthesized + | expr if ParsetreeViewer.isBinaryExpression(expr) => Parenthesized | {pexp_desc: Pexp_constraint({pexp_desc: Pexp_pack(_)}, {ptyp_desc: Ptyp_package(_)})} => Nothing - | {pexp_desc: Pexp_fun(_)} when ParsetreeViewer.isUnderscoreApplySugar(expr) => Nothing + | {pexp_desc: Pexp_fun(_)} if ParsetreeViewer.isUnderscoreApplySugar(expr) => Nothing | { pexp_desc: Pexp_lazy(_) @@ -3904,7 +3909,7 @@ module Parens: { } switch constant { - | Parsetree.Pconst_integer(i, _) | Pconst_float(i, _) when isNeg(i) => true + | Parsetree.Pconst_integer(i, _) | Pconst_float(i, _) if isNeg(i) => true | _ => false } } @@ -3916,18 +3921,18 @@ module Parens: { | None => switch expr { | {Parsetree.pexp_attributes: attrs} - when switch ParsetreeViewer.filterParsingAttrs(attrs) { + if switch ParsetreeViewer.filterParsingAttrs(attrs) { | list{_, ..._} => true | list{} => false } => Parenthesized | expr - when ParsetreeViewer.isBinaryExpression(expr) || ParsetreeViewer.isUnaryExpression(expr) => + if ParsetreeViewer.isBinaryExpression(expr) || ParsetreeViewer.isUnaryExpression(expr) => Parenthesized | {pexp_desc: Pexp_constraint({pexp_desc: Pexp_pack(_)}, {ptyp_desc: Ptyp_package(_)})} => Nothing - | {pexp_desc: Pexp_constant(c)} when isNegativeConstant(c) => Parenthesized - | {pexp_desc: Pexp_fun(_)} when ParsetreeViewer.isUnderscoreApplySugar(expr) => Nothing + | {pexp_desc: Pexp_constant(c)} if isNegativeConstant(c) => Parenthesized + | {pexp_desc: Pexp_fun(_)} if ParsetreeViewer.isUnderscoreApplySugar(expr) => Nothing | { pexp_desc: Pexp_lazy(_) @@ -3957,8 +3962,10 @@ module Parens: { | None => switch expr { | { - Parsetree.pexp_desc: - Pexp_constraint({pexp_desc: Pexp_pack(_)}, {ptyp_desc: Ptyp_package(_)}), + Parsetree.pexp_desc: Pexp_constraint( + {pexp_desc: Pexp_pack(_)}, + {ptyp_desc: Ptyp_package(_)}, + ), } => Nothing | {pexp_desc: Pexp_constraint(_)} => Parenthesized @@ -3974,8 +3981,10 @@ module Parens: { | None => switch expr { | { - Parsetree.pexp_desc: - Pexp_constraint({pexp_desc: Pexp_pack(_)}, {ptyp_desc: Ptyp_package(_)}), + Parsetree.pexp_desc: Pexp_constraint( + {pexp_desc: Pexp_pack(_)}, + {ptyp_desc: Ptyp_package(_)}, + ), } => Nothing | {pexp_desc: Pexp_constraint(_)} => Parenthesized @@ -4017,7 +4026,7 @@ module Parens: { | { Parsetree.pexp_desc: Pexp_constant(Pconst_integer(x, _) | Pconst_float(x, _)), pexp_attributes: list{}, - } when startsWithMinus(x) => + } if startsWithMinus(x) => Parenthesized | { Parsetree.pexp_desc: @@ -4040,8 +4049,10 @@ module Parens: { } => Nothing | { - Parsetree.pexp_desc: - Pexp_constraint({pexp_desc: Pexp_pack(_)}, {ptyp_desc: Ptyp_package(_)}), + Parsetree.pexp_desc: Pexp_constraint( + {pexp_desc: Pexp_pack(_)}, + {ptyp_desc: Ptyp_package(_)}, + ), pexp_attributes: list{}, } => Nothing @@ -4067,7 +4078,7 @@ module Parens: { | { Parsetree.pexp_desc: Pexp_constant(Pconst_integer(x, _) | Pconst_float(x, _)), pexp_attributes: list{}, - } when startsWithMinus(x) => + } if startsWithMinus(x) => Parenthesized | { Parsetree.pexp_desc: @@ -4089,12 +4100,14 @@ module Parens: { } => Nothing | { - Parsetree.pexp_desc: - Pexp_constraint({pexp_desc: Pexp_pack(_)}, {ptyp_desc: Ptyp_package(_)}), + Parsetree.pexp_desc: Pexp_constraint( + {pexp_desc: Pexp_pack(_)}, + {ptyp_desc: Ptyp_package(_)}, + ), pexp_attributes: list{}, } => Nothing - | expr when ParsetreeViewer.isJsxExpression(expr) => Nothing + | expr if ParsetreeViewer.isJsxExpression(expr) => Nothing | _ => Parenthesized } } @@ -4107,7 +4120,7 @@ module Parens: { | None => switch expr { | {Parsetree.pexp_attributes: list{_, ..._}} as expr - when ParsetreeViewer.isBinaryExpression(expr) => + if ParsetreeViewer.isBinaryExpression(expr) => Parenthesized | _ => Nothing } @@ -4242,8 +4255,8 @@ module CommentTable = { Doc.line, }), ) - ->Doc.toString(~width=80) - ->print_endline + ->Doc.toString(~width=80) + ->print_endline } let attach = (tbl, loc, comments) => switch comments { @@ -4489,24 +4502,24 @@ module CommentTable = { | Pexp_let(_) | Pexp_open(_) | Pexp_sequence(_) => true - | Pexp_apply(callExpr, _) when isBlockExpr(callExpr) => true - | Pexp_constraint(expr, _) when isBlockExpr(expr) => true - | Pexp_field(expr, _) when isBlockExpr(expr) => true - | Pexp_setfield(expr, _, _) when isBlockExpr(expr) => true + | Pexp_apply(callExpr, _) if isBlockExpr(callExpr) => true + | Pexp_constraint(expr, _) if isBlockExpr(expr) => true + | Pexp_field(expr, _) if isBlockExpr(expr) => true + | Pexp_setfield(expr, _, _) if isBlockExpr(expr) => true | _ => false } } let rec walkStructure = (s, t, comments) => switch s { - | _ when comments == list{} => () + | _ if comments == list{} => () | list{} => attach(t.inside, Location.none, comments) | s => walkList(~getLoc=n => n.Parsetree.pstr_loc, ~walkNode=walkStructureItem, s, t, comments) } and walkStructureItem = (si, t, comments) => switch si.Parsetree.pstr_desc { - | _ when comments == list{} => () + | _ if comments == list{} => () | Pstr_primitive(valueDescription) => walkValueDescription(valueDescription, t, comments) | Pstr_open(openDescription) => walkOpenDescription(openDescription, t, comments) | Pstr_value(_, valueBindings) => walkValueBindings(valueBindings, t, comments) @@ -4610,7 +4623,7 @@ module CommentTable = { and walkSignature = (signature, t, comments) => switch signature { - | _ when comments == list{} => () + | _ if comments == list{} => () | list{} => attach(t.inside, Location.none, comments) | _s => walkList( @@ -4624,7 +4637,7 @@ module CommentTable = { and walkSignatureItem = (si, t, comments) => switch si.psig_desc { - | _ when comments == list{} => () + | _ if comments == list{} => () | Psig_value(valueDescription) => walkValueDescription(valueDescription, t, comments) | Psig_type(_, typeDeclarations) => walkTypeDeclarations(typeDeclarations, t, comments) | Psig_typext(typeExtension) => walkTypeExtension(typeExtension, t, comments) @@ -4675,7 +4688,7 @@ module CommentTable = { ) => unit = (~prevLoc=?, ~getLoc, ~walkNode, l, t, comments) => { open Location switch l { - | _ when comments == list{} => () + | _ if comments == list{} => () | list{} => switch prevLoc { | Some(loc) => attach(t.trailing, loc, comments) @@ -4720,7 +4733,7 @@ module CommentTable = { ) => list = (~prevLoc=?, ~newlineDelimited, ~getLoc, ~walkNode, l, t, comments) => { open Location switch l { - | _ when comments == list{} => list{} + | _ if comments == list{} => list{} | list{} => switch prevLoc { | Some(loc) => @@ -4967,7 +4980,7 @@ module CommentTable = { and walkExpr = (expr, t, comments) => { open Location switch expr.Parsetree.pexp_desc { - | _ when comments == list{} => () + | _ if comments == list{} => () | Pexp_constant(_) => let (leading, trailing) = partitionLeadingTrailing(comments, expr.pexp_loc) attach(t.leading, expr.pexp_loc, leading) @@ -5311,37 +5324,35 @@ module CommentTable = { /* binary expression */ | Pexp_apply( { - pexp_desc: - Pexp_ident({ - txt: - Longident.Lident( - ":=" - | "||" - | "&&" - | "=" - | "==" - | "<" - | ">" - | "!=" - | "!==" - | "<=" - | ">=" - | "|>" - | "+" - | "+." - | "-" - | "-." - | "++" - | "^" - | "*" - | "*." - | "/" - | "/." - | "**" - | "|." - | "<>", - ), - }), + pexp_desc: Pexp_ident({ + txt: Longident.Lident( + ":=" + | "||" + | "&&" + | "=" + | "==" + | "<" + | ">" + | "!=" + | "!==" + | "<=" + | ">=" + | "|>" + | "+" + | "+." + | "-" + | "-." + | "++" + | "^" + | "*" + | "*." + | "/" + | "/." + | "**" + | "|." + | "<>", + ), + }), }, list{(Nolabel, operand1), (Nolabel, operand2)}, ) => @@ -5406,7 +5417,7 @@ module CommentTable = { switch returnExpr.pexp_desc { | Pexp_constraint(expr, typ) - when expr.pexp_loc.loc_start.pos_cnum >= typ.ptyp_loc.loc_end.pos_cnum => + if expr.pexp_loc.loc_start.pos_cnum >= typ.ptyp_loc.loc_end.pos_cnum => let (leading, inside, trailing) = partitionByLoc(comments, typ.ptyp_loc) attach(t.leading, typ.ptyp_loc, leading) walkTypExpr(typ, t, inside) @@ -5591,7 +5602,7 @@ module CommentTable = { switch returnModExpr.pmod_desc { | Pmod_constraint(modExpr, modType) - when modType.pmty_loc.loc_end.pos_cnum <= modExpr.pmod_loc.loc_start.pos_cnum => + if modType.pmty_loc.loc_end.pos_cnum <= modExpr.pmod_loc.loc_start.pos_cnum => let (before, inside, after) = partitionByLoc(comments, modType.pmty_loc) attach(t.leading, modType.pmty_loc, before) walkModType(modType, t, inside) @@ -5682,7 +5693,7 @@ module CommentTable = { and walkPattern = (pat, t, comments) => { open Location switch pat.Parsetree.ppat_desc { - | _ when comments == list{} => () + | _ if comments == list{} => () | Ppat_alias(pat, alias) => let (leading, inside, trailing) = partitionByLoc(comments, pat.ppat_loc) attach(t.leading, pat.ppat_loc, leading) @@ -5725,13 +5736,17 @@ module CommentTable = { | Ppat_variant(_label, None) => () | Ppat_variant(_label, Some(pat)) => walkPattern(pat, t, comments) | Ppat_type(_) => () - | Ppat_record(recordRows, _) => walkList(~getLoc=((longidentLoc, pattern): ( - Asttypes.loc, - Parsetree.pattern, - )) => { - ...longidentLoc.loc, - loc_end: pattern.Parsetree.ppat_loc.loc_end, - }, ~walkNode=walkPatternRecordRow, recordRows, t, comments) + | Ppat_record(recordRows, _) => + walkList( + ~getLoc=((longidentLoc, pattern): (Asttypes.loc, Parsetree.pattern)) => { + ...longidentLoc.loc, + loc_end: pattern.Parsetree.ppat_loc.loc_end, + }, + ~walkNode=walkPatternRecordRow, + recordRows, + t, + comments, + ) | Ppat_or(pattern1, pattern2) => let (beforePattern1, insidePattern1, afterPattern1) = partitionByLoc( comments, @@ -5782,7 +5797,7 @@ module CommentTable = { | ( {Location.txt: Longident.Lident(ident), loc: longidentLoc}, {Parsetree.ppat_desc: Ppat_var({txt, _})}, - ) when ident == txt => + ) if ident == txt => let (beforeLbl, afterLbl) = partitionLeadingTrailing(comments, longidentLoc) attach(t.leading, longidentLoc, beforeLbl) @@ -5801,7 +5816,7 @@ module CommentTable = { and walkTypExpr = (typ, t, comments) => switch typ.Parsetree.ptyp_desc { - | _ when comments == list{} => () + | _ if comments == list{} => () | Ptyp_tuple(typexprs) => walkList(~getLoc=n => n.Parsetree.ptyp_loc, ~walkNode=walkTypExpr, typexprs, t, comments) | Ptyp_extension(extension) => walkExtension(extension, t, comments) @@ -5893,10 +5908,8 @@ module CommentTable = { walkPackageConstraints(packageConstraints, t, rest) } - and walkPackageConstraints = (packageConstraints, t, comments) => walkList(~getLoc=(( - longident, - typexpr, - )) => { + and walkPackageConstraints = (packageConstraints, t, comments) => + walkList(~getLoc=((longident, typexpr)) => { ...longident.Asttypes.loc, loc_end: typexpr.Parsetree.ptyp_loc.loc_end, }, ~walkNode=walkPackageConstraint, packageConstraints, t, comments) @@ -6592,11 +6605,9 @@ module Printer = { Doc.indent( Doc.concat(list{ Doc.softLine, - Doc.join(~sep=Doc.concat(list{Doc.comma, Doc.line}), List.map((( - attrs, - lbl, - modType, - )) => { + Doc.join( + ~sep=Doc.concat(list{Doc.comma, Doc.line}), + List.map(((attrs, lbl, modType)) => { let cmtLoc = switch modType { | None => lbl.Asttypes.loc | Some(modType) => { @@ -6637,7 +6648,8 @@ module Printer = { }, }) printComments(doc, cmtTbl, cmtLoc) - }, params)), + }, params), + ), }), ), Doc.trailingComma, @@ -7533,11 +7545,10 @@ module Printer = { switch constrArgs { | list{} => constrName | list{{ - Parsetree.ptyp_desc: - Ptyp_constr( - {txt: Longident.Ldot(Longident.Lident("Js"), "t")}, - list{{ptyp_desc: Ptyp_object(fields, openFlag)}}, - ), + Parsetree.ptyp_desc: Ptyp_constr( + {txt: Longident.Ldot(Longident.Lident("Js"), "t")}, + list{{ptyp_desc: Ptyp_object(fields, openFlag)}}, + ), }} => Doc.concat(list{ constrName, @@ -7585,7 +7596,7 @@ module Printer = { switch args { | list{} => Doc.nil - | list{(list{}, Nolabel, n)} when !isUncurried => + | list{(list{}, Nolabel, n)} if !isUncurried => let hasAttrsBefore = !(attrs == list{}) let attrs = if hasAttrsBefore { Doc.concat(list{ @@ -7654,10 +7665,8 @@ module Printer = { | Ptyp_tuple(types) => printTupleType(~inline=false, types, cmtTbl) | Ptyp_object(fields, openFlag) => printBsObjectSugar(~inline=false, fields, openFlag, cmtTbl) | Ptyp_poly(list{}, typ) => printTypExpr(typ, cmtTbl) - | Ptyp_poly(stringLocs, typ) => Doc.concat(list{Doc.join(~sep=Doc.space, List.map(({ - Location.txt: txt, - loc, - }) => { + | Ptyp_poly(stringLocs, typ) => + Doc.concat(list{Doc.join(~sep=Doc.space, List.map(({Location.txt: txt, loc}) => { let doc = Doc.concat(list{Doc.text("'"), Doc.text(txt)}) printComments(doc, cmtTbl, loc) }, stringLocs)), Doc.dot, Doc.space, printTypExpr(typ, cmtTbl)}) @@ -7749,7 +7758,7 @@ module Printer = { } let doc = switch typExpr.ptyp_attributes { - | list{_, ..._} as attrs when !shouldPrintItsOwnAttributes => + | list{_, ..._} as attrs if !shouldPrintItsOwnAttributes => Doc.group(Doc.concat(list{printAttributes(attrs), renderedType})) | _ => renderedType } @@ -8144,7 +8153,7 @@ module Printer = { let (patterns, tail) = ParsetreeViewer.collectPatternsFromListConstruct(list{}, p) let shouldHug = switch (patterns, tail) { | (list{pat}, {ppat_desc: Ppat_construct({txt: Longident.Lident("[]")}, _)}) - when ParsetreeViewer.isHuggablePattern(pat) => true + if ParsetreeViewer.isHuggablePattern(pat) => true | _ => false } @@ -8413,7 +8422,7 @@ module Printer = { | ( {Location.txt: Longident.Lident(ident)} as longident, {Parsetree.ppat_desc: Ppat_var({txt, _})}, - ) when ident == txt => + ) if ident == txt => printLidentPath(longident, cmtTbl) | (longident, pattern) => let locForComments = { @@ -8438,7 +8447,7 @@ module Printer = { and printExpression = (e: Parsetree.expression, cmtTbl) => { let printedExpression = switch e.pexp_desc { | Parsetree.Pexp_constant(c) => printConstant(c) - | Pexp_construct(_) when ParsetreeViewer.hasJsxAttribute(e.pexp_attributes) => + | Pexp_construct(_) if ParsetreeViewer.hasJsxAttribute(e.pexp_attributes) => printJsxFragment(e, cmtTbl) | Pexp_construct({txt: Longident.Lident("()")}, _) => Doc.text("()") | Pexp_construct({txt: Longident.Lident("[]")}, _) => @@ -9111,13 +9120,13 @@ module Printer = { let shouldPrintItsOwnAttributes = switch e.pexp_desc { | Pexp_apply(_) | Pexp_fun(_) | Pexp_newtype(_) | Pexp_setfield(_) | Pexp_ifthenelse(_) => true - | Pexp_construct(_) when ParsetreeViewer.hasJsxAttribute(e.pexp_attributes) => true + | Pexp_construct(_) if ParsetreeViewer.hasJsxAttribute(e.pexp_attributes) => true | _ => false } switch e.pexp_attributes { | list{} => printedExpression - | attrs when !shouldPrintItsOwnAttributes => + | attrs if !shouldPrintItsOwnAttributes => Doc.group(Doc.concat(list{printAttributes(attrs), printedExpression})) | _ => printedExpression } @@ -9303,7 +9312,7 @@ module Printer = { | "~-" => "-" | "~-." => "-." | "not" => "!" - | _ => assert false + | _ => assert(false) }, ) switch expr.pexp_desc { @@ -9322,7 +9331,7 @@ module Printer = { let doc = Doc.concat(list{printUnaryOperator(operator), printedOperand}) printComments(doc, cmtTbl, expr.pexp_loc) - | _ => assert false + | _ => assert(false) } } @@ -9364,11 +9373,10 @@ module Printer = { if ParsetreeViewer.isBinaryExpression(expr) { switch expr { | { - pexp_desc: - Pexp_apply( - {pexp_desc: Pexp_ident({txt: Longident.Lident(operator)})}, - list{(_, left), (_, right)}, - ), + pexp_desc: Pexp_apply( + {pexp_desc: Pexp_ident({txt: Longident.Lident(operator)})}, + list{(_, left), (_, right)}, + ), } => if ( ParsetreeViewer.flattenableOperators(parentOperator, operator) && @@ -9423,7 +9431,7 @@ module Printer = { } Doc.concat(list{printAttributes(expr.pexp_attributes), doc}) } - | _ => assert false + | _ => assert(false) } } else { switch expr.pexp_desc { @@ -9487,8 +9495,7 @@ module Printer = { | Pexp_apply( {pexp_desc: Pexp_ident({txt: Longident.Lident(("|." | "|>") as op)})}, list{(Nolabel, lhs), (Nolabel, rhs)}, - ) - when !(ParsetreeViewer.isBinaryExpression(lhs) || ParsetreeViewer.isBinaryExpression(rhs)) => + ) if !(ParsetreeViewer.isBinaryExpression(lhs) || ParsetreeViewer.isBinaryExpression(rhs)) => let lhsDoc = printOperand(~isLhs=true, lhs, op) let rhsDoc = printOperand(~isLhs=false, rhs, op) Doc.group( @@ -9497,7 +9504,7 @@ module Printer = { switch op { | "|." => Doc.text("->") | "|>" => Doc.text(" |> ") - | _ => assert false + | _ => assert(false) }, rhsDoc, }), @@ -9733,8 +9740,7 @@ module Printer = { }), ) /* TODO: cleanup, are those branches even remotely performant? */ - | Pexp_apply({pexp_desc: Pexp_ident(lident)}, args) - when ParsetreeViewer.isJsxExpression(expr) => + | Pexp_apply({pexp_desc: Pexp_ident(lident)}, args) if ParsetreeViewer.isJsxExpression(expr) => printJsxExpression(lident, args, cmtTbl) | Pexp_apply(callExpr, args) => let args = List.map(((lbl, arg)) => (lbl, ParsetreeViewer.rewriteUnderscoreApply(arg)), args) @@ -9762,7 +9768,7 @@ module Printer = { let argsDoc = printArguments(~uncurried, args, cmtTbl) Doc.concat(list{printAttributes(attrs), callExprDoc, argsDoc}) } - | _ => assert false + | _ => assert(false) } and printJsxExpression = (lident, args, cmtTbl) => { @@ -9850,8 +9856,7 @@ module Printer = { let formattedProps = Doc.indent( switch props { | list{} => Doc.nil - | props => - Doc.concat(list{Doc.line, Doc.group(Doc.join(~sep=Doc.line, props->List.rev))}) + | props => Doc.concat(list{Doc.line, Doc.group(Doc.join(~sep=Doc.line, props->List.rev))}) }, ) let (children, _) = ParsetreeViewer.collectListExpressions(children) @@ -9872,7 +9877,7 @@ module Printer = { Parsetree.pexp_attributes: list{({Location.txt: "res.namedArgLoc", loc: argLoc}, _)}, pexp_desc: Pexp_ident({txt: Longident.Lident(ident)}), }, - ) when lblTxt == ident /* jsx punning */ => + ) if lblTxt == ident /* jsx punning */ => switch lbl { | Nolabel => Doc.nil | Labelled(_lbl) => printComments(printIdentLike(ident), cmtTbl, argLoc) @@ -9883,7 +9888,7 @@ module Printer = { | ( (Asttypes.Labelled(lblTxt) | Optional(lblTxt)) as lbl, {Parsetree.pexp_attributes: list{}, pexp_desc: Pexp_ident({txt: Longident.Lident(ident)})}, - ) when lblTxt == ident /* jsx punning when printing from Reason */ => + ) if lblTxt == ident /* jsx punning when printing from Reason */ => switch lbl { | Nolabel => Doc.nil | Labelled(_lbl) => printIdentLike(ident) @@ -9970,7 +9975,7 @@ module Printer = { ) (callback, printedArgs) - | _ => assert false + | _ => assert(false) } /* Thing.map((arg1, arg2) => MyModuleBlah.toList(argument), foo) */ @@ -10075,7 +10080,7 @@ module Printer = { } else { Doc.text("()") } - | list{(Nolabel, arg)} when ParsetreeViewer.isHuggableExpression(arg) => + | list{(Nolabel, arg)} if ParsetreeViewer.isHuggableExpression(arg) => let argDoc = { let doc = printExpressionWithComments(arg, cmtTbl) switch Parens.expr(arg) { @@ -10145,7 +10150,7 @@ module Printer = { pexp_desc: Pexp_ident({txt: Longident.Lident(name)}), pexp_attributes: list{} | list{({Location.txt: "res.namedArgLoc"}, _)}, } as argExpr, - ) when lbl == name && !ParsetreeViewer.isBracedExpr(argExpr) => + ) if lbl == name && !ParsetreeViewer.isBracedExpr(argExpr) => let loc = switch arg.pexp_attributes { | list{({Location.txt: "res.namedArgLoc", loc}, _), ..._} => loc | _ => arg.pexp_loc @@ -10158,12 +10163,14 @@ module Printer = { | ( Asttypes.Labelled(lbl), { - pexp_desc: - Pexp_constraint({pexp_desc: Pexp_ident({txt: Longident.Lident(name)})} as argExpr, typ), + pexp_desc: Pexp_constraint( + {pexp_desc: Pexp_ident({txt: Longident.Lident(name)})} as argExpr, + typ, + ), pexp_loc, pexp_attributes: (list{} | list{({Location.txt: "res.namedArgLoc"}, _)}) as attrs, }, - ) when lbl == name && !ParsetreeViewer.isBracedExpr(argExpr) => + ) if lbl == name && !ParsetreeViewer.isBracedExpr(argExpr) => let loc = switch attrs { | list{({Location.txt: "res.namedArgLoc", loc}, _), ..._} => { ...loc, @@ -10186,7 +10193,7 @@ module Printer = { pexp_desc: Pexp_ident({txt: Longident.Lident(name)}), pexp_attributes: list{} | list{({Location.txt: "res.namedArgLoc"}, _)}, }, - ) when lbl == name => + ) if lbl == name => let loc = switch arg.pexp_attributes { | list{({Location.txt: "res.namedArgLoc", loc}, _), ..._} => loc | _ => arg.pexp_loc @@ -10263,7 +10270,7 @@ module Printer = { | Pexp_construct({txt: Longident.Lident("()" | "true" | "false")}, _) | Pexp_constant(_) | Pexp_ident(_) => true - | _ when ParsetreeViewer.isHuggableRhs(case.pc_rhs) => true + | _ if ParsetreeViewer.isHuggableRhs(case.pc_rhs) => true | _ => false } @@ -10310,7 +10317,7 @@ module Printer = { lbl: Asttypes.Nolabel, defaultExpr: None, pat: {Parsetree.ppat_desc: Ppat_any}, - })} when !uncurried => + })} if !uncurried => if hasConstraint { Doc.text("(_)") } else { @@ -10322,7 +10329,7 @@ module Printer = { lbl: Asttypes.Nolabel, defaultExpr: None, pat: {Parsetree.ppat_desc: Ppat_var(stringLoc)}, - })} when !uncurried => + })} if !uncurried => let txtDoc = { let var = printIdentLike(stringLoc.txt) if hasConstraint { @@ -10339,7 +10346,7 @@ module Printer = { lbl: Asttypes.Nolabel, defaultExpr: None, pat: {ppat_desc: Ppat_construct({txt: Longident.Lident("()")}, None)}, - })} when !uncurried => + })} if !uncurried => Doc.text("()") /* let f = (~greeting, ~from as hometown, ~x=?) => () */ | parameters => @@ -10425,7 +10432,7 @@ module Printer = { ppat_desc: Ppat_var(stringLoc), ppat_attributes: list{} | list{({Location.txt: "res.namedArgLoc"}, _)}, }, - ) when lbl == stringLoc.txt => + ) if lbl == stringLoc.txt => /* ~d */ Doc.concat(list{Doc.text("~"), printIdentLike(lbl)}) | ( @@ -10434,7 +10441,7 @@ module Printer = { ppat_desc: Ppat_constraint({ppat_desc: Ppat_var({txt})}, typ), ppat_attributes: list{} | list{({Location.txt: "res.namedArgLoc"}, _)}, }, - ) when lbl == txt => + ) if lbl == txt => /* ~d: e */ Doc.concat(list{ Doc.text("~"), @@ -10712,7 +10719,7 @@ module Printer = { | Some(loc) => switch List.rev(attrs) { | list{({loc: firstLoc}, _), ..._} - when loc.loc_start.pos_lnum > firstLoc.loc_end.pos_lnum => Doc.hardLine + if loc.loc_start.pos_lnum > firstLoc.loc_end.pos_lnum => Doc.hardLine | _ => Doc.line } } @@ -11080,7 +11087,7 @@ module Scanner = { let popMode = (scanner, mode) => switch scanner.mode { - | list{m, ...ms} when m == mode => scanner.mode = ms + | list{m, ...ms} if m == mode => scanner.mode = ms | _ => () } @@ -11138,7 +11145,7 @@ module Scanner = { let make = (b, filename) => { let scanner = { - filename: filename, + filename, src: b, err: (~startPos as _, ~endPos as _, _) => (), ch: CharacterCodes.space, @@ -11262,9 +11269,9 @@ module Scanner = { } if isFloat { - Token.Float({f: literal, suffix: suffix}) + Token.Float({f: literal, suffix}) } else { - Token.Int({i: literal, suffix: suffix}) + Token.Int({i: literal, suffix}) } } @@ -11423,14 +11430,14 @@ module Scanner = { | 116 /* t */ => next(scanner) '\t' - | ch when CharacterCodes.isDigit(ch) => + | ch if CharacterCodes.isDigit(ch) => let x = convertNumber(scanner, ~n=3, ~base=10) (@doesNotRaise Char.chr)(x) - | ch when ch === CharacterCodes.Lower.x => + | ch if ch === CharacterCodes.Lower.x => next(scanner) let x = convertNumber(scanner, ~n=2, ~base=16) (@doesNotRaise Char.chr)(x) - | ch when ch === CharacterCodes.Lower.o => + | ch if ch === CharacterCodes.Lower.o => next(scanner) let x = convertNumber(scanner, ~n=3, ~base=8) (@doesNotRaise Char.chr)(x) @@ -11919,8 +11926,7 @@ module JsFfi = { ) => let default = Parsetree.Pconst_string("default", None)->Ast_helper.Exp.constant - let structureItem = - list{moduleName, default}->Ast_helper.Exp.tuple->Ast_helper.Str.eval + let structureItem = list{moduleName, default}->Ast_helper.Exp.tuple->Ast_helper.Str.eval (id, Parsetree.PStr(list{structureItem})) | attr => attr @@ -11951,8 +11957,8 @@ module JsFfi = { let jsFfiAttr = (Location.mknoloc("ns.jsFfi"), Parsetree.PStr(list{})) Ast_helper.Mod.structure(~loc=importDescr.jid_loc, valueDescrs) - ->Ast_helper.Incl.mk(~attrs=list{jsFfiAttr}, ~loc=importDescr.jid_loc) - ->Ast_helper.Str.include_(~loc=importDescr.jid_loc) + ->Ast_helper.Incl.mk(~attrs=list{jsFfiAttr}, ~loc=importDescr.jid_loc) + ->Ast_helper.Str.include_(~loc=importDescr.jid_loc) } } @@ -12032,11 +12038,10 @@ module ParsetreeCompatibility = { switch expr.pexp_desc { | Pexp_apply( { - pexp_desc: - Pexp_apply( - {pexp_desc: Pexp_ident({txt: Longident.Lident("|.")})} as op, - list{(Asttypes.Nolabel, lhs), (Nolabel, rhs)}, - ), + pexp_desc: Pexp_apply( + {pexp_desc: Pexp_ident({txt: Longident.Lident("|.")})} as op, + list{(Asttypes.Nolabel, lhs), (Nolabel, rhs)}, + ), pexp_attributes: subAttrs, }, args, @@ -12088,8 +12093,8 @@ module ParsetreeCompatibility = { mapper, { pexp_desc: Pexp_construct(lid, newArgs), - pexp_loc: pexp_loc, - pexp_attributes: pexp_attributes, + pexp_loc, + pexp_attributes, }, ) | expr => default_mapper.expr(mapper, rewriteReasonFastPipe(expr)) @@ -12120,8 +12125,8 @@ module ParsetreeCompatibility = { mapper, { ppat_desc: Ppat_construct(lid, new_args), - ppat_loc: ppat_loc, - ppat_attributes: ppat_attributes, + ppat_loc, + ppat_attributes, }, ) | x => default_mapper.pat(mapper, x) @@ -12312,7 +12317,9 @@ module ParsetreeCompatibility = { open Ast_mapper { ...default_mapper, - attributes: (mapper, attrs) => attrs->List.filter(attr => + attributes: (mapper, attrs) => + attrs + ->List.filter(attr => switch attr { | ( {Location.txt: "reason.preserve_braces" | "explicit_arity" | "implicity_arity"}, @@ -12320,7 +12327,8 @@ module ParsetreeCompatibility = { ) => false | _ => true } - )->default_mapper.attributes(mapper), + ) + ->default_mapper.attributes(mapper), pat: (mapper, p) => switch p.ppat_desc { | Ppat_open({txt: longidentOpen}, pattern) => @@ -12489,7 +12497,7 @@ module ParsetreeCompatibility = { | { pvb_pat: {ppat_desc: Ppat_var(_)} as pat, pvb_expr: {pexp_loc: expr_loc, pexp_desc: Pexp_constraint(expr, typ)}, - } when expr_loc.loc_ghost => + } if expr_loc.loc_ghost => /* let t: t = (expr : t) -> let t: t = expr */ let typ = default_mapper.typ(mapper, typ) let pat = default_mapper.pat(mapper, pat) @@ -12508,7 +12516,7 @@ module ParsetreeCompatibility = { | { pvb_pat: {ppat_desc: Ppat_constraint(pat, {ptyp_desc: Ptyp_poly(list{}, _)})}, pvb_expr: {pexp_loc: expr_loc, pexp_desc: Pexp_constraint(expr, typ)}, - } when expr_loc.loc_ghost => + } if expr_loc.loc_ghost => /* let t: . t = (expr : t) -> let t: t = expr */ let typ = default_mapper.typ(mapper, typ) let pat = default_mapper.pat(mapper, pat) @@ -12638,8 +12646,8 @@ module Parser = { let make = (~mode=ParseForTypeChecker, src, filename) => { let scanner = Scanner.make(Bytes.of_string(src), filename) let parserState = { - mode: mode, - scanner: scanner, + mode, + scanner, token: Token.Eof, startPos: Lexing.dummy_pos, prevEndPos: Lexing.dummy_pos, @@ -12848,7 +12856,7 @@ Solution: directly use `concat`." | Token.Lparen => Token.Rparen | Lbrace => Rbrace | Lbracket => Rbracket - | _ => assert false + | _ => assert(false) } let rec goToClosing = (closingToken, state) => @@ -12867,7 +12875,8 @@ Solution: directly use `concat`." } /* Madness */ - let isEs6ArrowExpression = (~inTernary, p) => Parser.lookahead(p, state => + let isEs6ArrowExpression = (~inTernary, p) => + Parser.lookahead(p, state => switch state.Parser.token { | Lident(_) | List | Underscore => Parser.next(state) @@ -12887,7 +12896,7 @@ Solution: directly use `concat`." | Rparen => Parser.next(state) switch state.Parser.token { - | Colon when !inTernary => true + | Colon if !inTernary => true | EqualGreater => true | _ => false } @@ -12899,7 +12908,7 @@ Solution: directly use `concat`." switch state.Parser.token { | EqualGreater => true /* | Lbrace TODO: detect missing =>, is this possible? */ - | Colon when !inTernary => true + | Colon if !inTernary => true | Rparen => /* imagine having something as : * switch colour { * | Red @@ -12916,7 +12925,7 @@ Solution: directly use `concat`." * in the example above, we have an unbalanced ] here */ switch state.Parser.token { - | EqualGreater when state.startPos.pos_lnum === prevEndPos.pos_lnum => true + | EqualGreater if state.startPos.pos_lnum === prevEndPos.pos_lnum => true | _ => false } } @@ -12925,7 +12934,8 @@ Solution: directly use `concat`." } ) - let isEs6ArrowFunctor = p => Parser.lookahead(p, state => + let isEs6ArrowFunctor = p => + Parser.lookahead(p, state => switch state.Parser.token { /* | Uident _ | Underscore -> */ /* Parser.next state; */ @@ -12954,7 +12964,8 @@ Solution: directly use `concat`." } ) - let isEs6ArrowType = p => Parser.lookahead(p, state => + let isEs6ArrowType = p => + Parser.lookahead(p, state => switch state.Parser.token { | Lparen => Parser.next(state) @@ -12980,7 +12991,7 @@ Solution: directly use `concat`." let buildLongident = words => switch List.rev(words) { - | list{} => assert false + | list{} => assert(false) | list{hd, ...tl} => List.fold_left((p, s) => Longident.Ldot(p, s), Lident(hd), tl) } @@ -13012,7 +13023,7 @@ Solution: directly use `concat`." } let negateString = s => - if String.length(s) > 0 && @doesNotRaise String.get(s, 0) == '-' { + if String.length(s) > 0 && (@doesNotRaise String.get(s, 0)) == '-' { (@doesNotRaise String.sub)(s, 1, String.length(s) - 1) } else { "-" ++ s @@ -13078,7 +13089,7 @@ Solution: directly use `concat`." | Some(ext) => ext | None => let loc = {...loc, Location.loc_ghost: true} - let nil = {Location.txt: Longident.Lident("[]"), loc: loc} + let nil = {Location.txt: Longident.Lident("[]"), loc} Ast_helper.Pat.construct(~loc, nil, None) } @@ -13145,14 +13156,12 @@ Solution: directly use `concat`." (exp, typ) } - @ocaml.doc( - " + @ocaml.doc(" * process the occurrence of _ in the arguments of a function application * replace _ with a new variable, currently __x, in the arguments * return a wrapping function that wraps ((__x) => ...) around an expression * e.g. foo(_, 3) becomes (__x) => foo(__x, 3) - " - ) + ") let processUnderscoreApplication = args => { open Parsetree let exp_question = ref(None) @@ -13295,11 +13304,11 @@ Solution: directly use `concat`." let parseModuleLongIdentTail = (~lowercase, p, startPos, ident) => { let rec loop = (p, acc) => switch p.Parser.token { - | List when lowercase => + | List if lowercase => Parser.next(p) let lident = Longident.Ldot(acc, "list") Location.mkloc(lident, mkLoc(startPos, p.prevEndPos)) - | Lident(ident) when lowercase => + | Lident(ident) if lowercase => Parser.next(p) let lident = Longident.Ldot(acc, ident) Location.mkloc(lident, mkLoc(startPos, p.prevEndPos)) @@ -13328,11 +13337,11 @@ Solution: directly use `concat`." /* Parser.leaveBreadcrumb p Reporting.ModuleLongIdent; */ let startPos = p.Parser.startPos let moduleIdent = switch p.Parser.token { - | List when lowercase => + | List if lowercase => let loc = mkLoc(startPos, p.endPos) Parser.next(p) Location.mkloc(Longident.Lident("list"), loc) - | Lident(ident) when lowercase => + | Lident(ident) if lowercase => let loc = mkLoc(startPos, p.endPos) let lident = Longident.Lident(ident) Parser.next(p) @@ -13407,7 +13416,7 @@ Solution: directly use `concat`." } opening == closing - | _ => assert false + | _ => assert(false) } } @@ -13415,8 +13424,8 @@ Solution: directly use `concat`." switch nameExpr.Parsetree.pexp_desc { | Pexp_ident(openingIdent) => Longident.flatten(openingIdent.txt) - ->List.filter(s => s != "createElement") - ->String.concat(".") + ->List.filter(s => s != "createElement") + ->String.concat(".") | _ => "" } @@ -13669,7 +13678,7 @@ Solution: directly use `concat`." | Comma => Parser.next(p) loop(list{node, ...nodes}) - | token when token == closing || token == Eof => List.rev(list{node, ...nodes}) + | token if token == closing || token == Eof => List.rev(list{node, ...nodes}) | _ => if !(p.token == Eof || (p.token == closing || Recover.shouldAbortListParse(p))) { Parser.expect(Comma, p) @@ -13703,7 +13712,7 @@ Solution: directly use `concat`." | Comma => Parser.next(p) loop(list{node, ...nodes}) - | token when token == closing || token == Eof => list{node, ...nodes} + | token if token == closing || token == Eof => list{node, ...nodes} | _ => if !(p.token == Eof || (p.token == closing || Recover.shouldAbortListParse(p))) { Parser.expect(Comma, p) @@ -13968,7 +13977,7 @@ Solution: directly use `concat`." } switch p.Parser.token { - | token when Grammar.isPatternStart(token) => + | token if Grammar.isPatternStart(token) => let pat = parsePattern(p) switch p.Parser.token { | Colon => @@ -13996,7 +14005,7 @@ Solution: directly use `concat`." and parseConstrainedPatternRegion = p => switch p.Parser.token { - | token when Grammar.isPatternStart(token) => Some(parseConstrainedPattern(p)) + | token if Grammar.isPatternStart(token) => Some(parseConstrainedPattern(p)) | _ => None } @@ -14102,7 +14111,7 @@ Solution: directly use `concat`." | DotDotDot => Parser.next(p) Some(true, parseConstrainedPattern(p)) - | token when Grammar.isPatternStart(token) => Some(false, parseConstrainedPattern(p)) + | token if Grammar.isPatternStart(token) => Some(false, parseConstrainedPattern(p)) | _ => None } @@ -14364,7 +14373,7 @@ Solution: directly use `concat`." if p.Parser.token == Typ { Parser.next(p) let lidents = parseLidentList(p) - Some(TypeParameter({uncurried: uncurried, attrs: attrs, locs: lidents, pos: startPos})) + Some(TypeParameter({uncurried, attrs, locs: lidents, pos: startPos})) } else { let (attrs, lbl, pat) = switch p.Parser.token { | Tilde => @@ -14427,11 +14436,11 @@ Solution: directly use `concat`." Parser.next(p) Some( TermParameter({ - uncurried: uncurried, - attrs: attrs, + uncurried, + attrs, label: lbl, expr: None, - pat: pat, + pat, pos: startPos, }), ) @@ -14439,11 +14448,11 @@ Solution: directly use `concat`." let expr = parseConstrainedOrCoercedExpr(p) Some( TermParameter({ - uncurried: uncurried, - attrs: attrs, + uncurried, + attrs, label: lbl, expr: Some(expr), - pat: pat, + pat, pos: startPos, }), ) @@ -14451,11 +14460,11 @@ Solution: directly use `concat`." | _ => Some( TermParameter({ - uncurried: uncurried, - attrs: attrs, + uncurried, + attrs, label: lbl, expr: None, - pat: pat, + pat, pos: startPos, }), ) @@ -14575,10 +14584,11 @@ Solution: directly use `concat`." | list{ TermParameter({attrs, label: lbl, expr: defaultExpr, pat: pattern, pos: startPos}), ...rest, - } => list{ + } => + list{ TermParameter({ uncurried: true, - attrs: attrs, + attrs, label: lbl, expr: defaultExpr, pat: pattern, @@ -14626,7 +14636,7 @@ Solution: directly use `concat`." and parseConstrainedExprRegion = p => switch p.Parser.token { - | token when Grammar.isExprStart(token) => + | token if Grammar.isExprStart(token) => let expr = parseExpr(p) switch p.Parser.token { | Colon => @@ -14849,7 +14859,7 @@ Solution: directly use `concat`." Parser.next(p) let lident = parseValuePath(p) switch p.Parser.token { - | Equal when noCall == false => + | Equal if noCall == false => Parser.leaveBreadcrumb(p, Grammar.ExprSetField) Parser.next(p) let targetExpr = parseExpr(p) @@ -14862,11 +14872,11 @@ Solution: directly use `concat`." let loc = mkLoc(startPos, endPos) loop(p, Ast_helper.Exp.field(~loc, expr, lident)) } - | Lbracket when noCall == false && p.prevEndPos.pos_lnum === p.startPos.pos_lnum => + | Lbracket if noCall == false && p.prevEndPos.pos_lnum === p.startPos.pos_lnum => parseBracketAccess(p, expr, startPos) - | Lparen when noCall == false && p.prevEndPos.pos_lnum === p.startPos.pos_lnum => + | Lparen if noCall == false && p.prevEndPos.pos_lnum === p.startPos.pos_lnum => loop(p, parseCallExpr(p, expr)) - | Backtick when noCall == false && p.prevEndPos.pos_lnum === p.startPos.pos_lnum => + | Backtick if noCall == false && p.prevEndPos.pos_lnum === p.startPos.pos_lnum => switch expr.pexp_desc { | Pexp_ident({txt: Longident.Lident(ident)}) => parseTemplateExpr(~prefix=ident, p) | _ => @@ -14975,7 +14985,7 @@ Solution: directly use `concat`." * First case is unary, second is a binary operator. * See Scanner.isBinaryOp */ | Minus | MinusDot | LessThan - when !Scanner.isBinaryOp(p.scanner.src, p.startPos.pos_cnum, p.endPos.pos_cnum) && + if !Scanner.isBinaryOp(p.scanner.src, p.startPos.pos_cnum, p.endPos.pos_cnum) && p.startPos.pos_lnum > p.prevEndPos.pos_lnum => -1 | token => Token.precedence(token) } @@ -15417,12 +15427,12 @@ Solution: directly use `concat`." | LessThan => Parser.next(p) Parser.expect(Forwardslash, p) - | token when Grammar.isStructureItemStart(token) => () + | token if Grammar.isStructureItemStart(token) => () | _ => Parser.expect(LessThanSlash, p) } switch p.Parser.token { - | Lident(_) | Uident(_) when verifyJsxOpeningClosingName(p, name) => + | Lident(_) | Uident(_) if verifyJsxOpeningClosingName(p, name) => Parser.expect(GreaterThan, p) let loc = mkLoc(childrenStartPos, childrenEndPos) switch (spread, children) { @@ -15527,6 +15537,7 @@ Solution: directly use `concat`." let optional = Parser.optional(p, Question) let (name, loc) = parseLident(p) let propLocAttr = (Location.mkloc("res.namedArgLoc", loc), Parsetree.PStr(list{})) + /* optional punning: */ if optional { Some( @@ -15598,7 +15609,7 @@ Solution: directly use `concat`." let () = Scanner.popMode(p.scanner, Jsx) List.rev(children) } - | token when Grammar.isJsxChildStart(token) => + | token if Grammar.isJsxChildStart(token) => let () = Scanner.popMode(p.scanner, Jsx) let child = parsePrimaryExpr(~operand=parseAtomicExpr(p), ~noCall=true, p) loop(p, list{child, ...children}) @@ -15956,7 +15967,7 @@ Solution: directly use `concat`." let loc = mkLoc(p.startPos, p.endPos) Ast_helper.Exp.construct(~loc, Location.mkloc(Longident.Lident("()"), loc), None) } - | token when Grammar.isBlockExprStart(token) => parseExprBlock(p) + | token if Grammar.isBlockExprStart(token) => parseExprBlock(p) | _ => let loc = mkLoc(p.startPos, p.endPos) Ast_helper.Exp.construct(~loc, Location.mkloc(Longident.Lident("()"), loc), None) @@ -16012,7 +16023,7 @@ Solution: directly use `concat`." } else { item } - | token when Grammar.isBlockExprStart(token) => + | token if Grammar.isBlockExprStart(token) => let next = parseExprBlockItem(p) ignore(Parser.optional(p, Semicolon)) let loc = {...item.pexp_loc, loc_end: next.pexp_loc.loc_end} @@ -16258,7 +16269,7 @@ Solution: directly use `concat`." and parseArgument2 = (p, ~uncurried) => switch p.Parser.token { /* foo(_), do not confuse with foo(_ => x), TODO: performance */ - | Underscore when !isEs6ArrowExpression(~inTernary=false, p) => + | Underscore if !isEs6ArrowExpression(~inTernary=false, p) => let loc = mkLoc(p.startPos, p.endPos) Parser.next(p) let exp = Ast_helper.Exp.ident(~loc, Location.mkloc(Longident.Lident("_"), loc)) @@ -16292,7 +16303,7 @@ Solution: directly use `concat`." } let expr = switch p.Parser.token { - | Underscore when !isEs6ArrowExpression(~inTernary=false, p) => + | Underscore if !isEs6ArrowExpression(~inTernary=false, p) => let loc = mkLoc(p.startPos, p.endPos) Parser.next(p) Ast_helper.Exp.ident(~loc, Location.mkloc(Longident.Lident("_"), loc)) @@ -16388,7 +16399,7 @@ Solution: directly use `concat`." | Dot => Parser.next(p) aux(p, list{ident, ...acc}) - | Lparen when p.prevEndPos.pos_lnum === p.startPos.pos_lnum => + | Lparen if p.prevEndPos.pos_lnum === p.startPos.pos_lnum => let lparen = p.startPos let args = parseConstructorArgs(p) let rparen = p.prevEndPos @@ -16441,7 +16452,7 @@ Solution: directly use `concat`." let startPos = p.startPos let (ident, _loc) = parseHashIdent(~startPos, p) switch p.Parser.token { - | Lparen when p.prevEndPos.pos_lnum === p.startPos.pos_lnum => + | Lparen if p.prevEndPos.pos_lnum === p.startPos.pos_lnum => let lparen = p.startPos let args = parseConstructorArgs(p) let rparen = p.prevEndPos @@ -16507,7 +16518,7 @@ Solution: directly use `concat`." Parser.next(p) let expr = parseConstrainedOrCoercedExpr(p) Some(true, expr) - | token when Grammar.isExprStart(token) => Some(false, parseConstrainedOrCoercedExpr(p)) + | token if Grammar.isExprStart(token) => Some(false, parseConstrainedOrCoercedExpr(p)) | _ => None } @@ -16527,12 +16538,15 @@ Solution: directly use `concat`." let exprs = exprs->List.map(snd)->List.rev makeListExpression(loc, exprs, Some(expr)) | exprs => - let exprs = exprs->List.map(((spread, expr)) => { - if spread { - Parser.err(p, Diagnostics.message(ErrorMessages.listExprSpread)) - } - expr - })->List.rev + let exprs = + exprs + ->List.map(((spread, expr)) => { + if spread { + Parser.err(p, Diagnostics.message(ErrorMessages.listExprSpread)) + } + expr + }) + ->List.rev makeListExpression(loc, exprs, None) } @@ -16548,7 +16562,7 @@ Solution: directly use `concat`." } switch p.Parser.token { - | token when Grammar.isExprStart(token) => + | token if Grammar.isExprStart(token) => let expr = parseExpr(p) switch p.Parser.token { | Colon => @@ -16604,7 +16618,7 @@ Solution: directly use `concat`." Ast_helper.Typ.arrow(~loc, Asttypes.Nolabel, typ, returnType) | _ => Ast_helper.Typ.var(~loc=var.loc, var.txt) } - | _ => assert false + | _ => assert(false) } | _ => parseTypExpr(p) } @@ -16966,7 +16980,7 @@ Solution: directly use `concat`." and parseArrowTypeRest = (~es6Arrow, ~startPos, typ, p) => switch p.Parser.token { - | (EqualGreater | MinusGreater) as token when es6Arrow === true => + | (EqualGreater | MinusGreater) as token if es6Arrow === true => /* error recovery */ if token == MinusGreater { Parser.expect(EqualGreater, p) @@ -17025,7 +17039,7 @@ Solution: directly use `concat`." ) let () = switch p.token { - | Rparen when opening == Token.Lparen => + | Rparen if opening == Token.Lparen => let typ = Ast_helper.Typ.constr(constrName, typeArgs) let msg = Doc.breakableGroup( @@ -17219,7 +17233,7 @@ Solution: directly use `concat`." Parser.leaveBreadcrumb(p, Grammar.StringFieldDeclarations) let field = switch parseStringFieldDeclaration(p) { | Some(field) => field - | None => assert false + | None => assert(false) } /* parse comma after first */ @@ -17457,7 +17471,7 @@ Solution: directly use `concat`." and parseTypeParams = (~parent, p) => { let opening = p.Parser.token switch opening { - | LessThan | Lparen when p.startPos.pos_lnum === p.prevEndPos.pos_lnum => + | LessThan | Lparen if p.startPos.pos_lnum === p.prevEndPos.pos_lnum => Scanner.setDiamondMode(p.scanner) let openingStartPos = p.startPos Parser.leaveBreadcrumb(p, Grammar.TypeParams) @@ -17470,7 +17484,7 @@ Solution: directly use `concat`." ) let () = switch p.token { - | Rparen when opening == Token.Lparen => + | Rparen if opening == Token.Lparen => let msg = Doc.breakableGroup( ~forceBreak=true, @@ -17637,7 +17651,7 @@ Solution: directly use `concat`." Parser.leaveBreadcrumb(p, Grammar.StringFieldDeclarations) let field = switch parseStringFieldDeclaration(p) { | Some(field) => field - | None => assert false + | None => assert(false) } /* parse comma after first */ @@ -17667,8 +17681,7 @@ Solution: directly use `concat`." Parser.expect(Rbrace, p) let loc = mkLoc(startPos, p.prevEndPos) - let typ = - makeBsObjType(~attrs=list{}, ~loc, ~closed=closedFlag, fields)->parseTypeAlias(p) + let typ = makeBsObjType(~attrs=list{}, ~loc, ~closed=closedFlag, fields)->parseTypeAlias(p) let typ = parseArrowTypeRest(~es6Arrow=true, ~startPos, typ, p) (Some(typ), Asttypes.Public, Parsetree.Ptype_abstract) @@ -17732,7 +17745,7 @@ Solution: directly use `concat`." | Bar | DotDot => let (_, kind) = parseTypeRepresentation(p) (None, Asttypes.Private, kind) - | t when Grammar.isTypExprStart(t) => ( + | t if Grammar.isTypExprStart(t) => ( Some(parseTypExpr(p)), Asttypes.Private, Parsetree.Ptype_abstract, @@ -17874,7 +17887,7 @@ Solution: directly use `concat`." let (ident, loc) = parseHashIdent(~startPos, p) let rec loop = p => switch p.Parser.token { - | Band when full => + | Band if full => Parser.next(p) let rowField = parsePolymorphicVariantTypeArgs(p) list{rowField, ...loop(p)} @@ -17882,7 +17895,7 @@ Solution: directly use `concat`." } let (firstTuple, tagContainsAConstantEmptyConstructor) = switch p.Parser.token { - | Band when full => + | Band if full => Parser.next(p) (list{parsePolymorphicVariantTypeArgs(p)}, true) | Lparen => (list{parsePolymorphicVariantTypeArgs(p)}, false) @@ -18078,7 +18091,7 @@ Solution: directly use `concat`." | PlusEqual => TypeExt(parseTypeExtension(~params, ~attrs, ~name, p)) | _ => let typeDefs = parseTypeDefinitions(~attrs, ~name, ~params, ~startPos, p) - TypeDef({recFlag: recFlag, types: typeDefs}) + TypeDef({recFlag, types: typeDefs}) } } @@ -18242,7 +18255,7 @@ Solution: directly use `concat`." Parser.optional(p, Semicolon)->ignore let loc = mkLoc(startPos, p.prevEndPos) Some(Ast_helper.Str.extension(~attrs, ~loc, extension)) - | token when Grammar.isExprStart(token) => + | token if Grammar.isExprStart(token) => let prevEndPos = p.Parser.endPos let exp = parseExpr(p) Parser.optional(p, Semicolon)->ignore @@ -18258,7 +18271,7 @@ Solution: directly use `concat`." | Token.Lident(_) | Token.At => let decl = switch parseJsFfiDeclaration(p) { | Some(decl) => decl - | None => assert false + | None => assert(false) } JsFfi.Default(decl) @@ -18412,7 +18425,7 @@ Solution: directly use `concat`." let modExpr = parseAtomicModuleExpr(p) let rec loop = (p, modExpr) => switch p.Parser.token { - | Lparen when p.prevEndPos.pos_lnum === p.startPos.pos_lnum => + | Lparen if p.prevEndPos.pos_lnum === p.startPos.pos_lnum => loop(p, parseModuleApplication(p, modExpr)) | _ => modExpr } @@ -18788,7 +18801,7 @@ Solution: directly use `concat`." } else { let modty = parseAtomicModuleType(p) switch p.Parser.token { - | EqualGreater when es6Arrow === true => + | EqualGreater if es6Arrow === true => Parser.next(p) let rhs = parseModuleType(~with_=false, p) let str = Location.mknoloc("_") @@ -19135,7 +19148,7 @@ Solution: directly use `concat`." loop(p, id ++ ".") | _ => id } - | token when Token.isKeyword(token) => + | token if Token.isKeyword(token) => Parser.next(p) let id = acc ++ Token.toString(token) switch p.Parser.token { @@ -19166,7 +19179,7 @@ Solution: directly use `concat`." */ and parsePayload = p => switch p.Parser.token { - | Lparen when p.startPos.pos_cnum == p.prevEndPos.pos_cnum => + | Lparen if p.startPos.pos_cnum == p.prevEndPos.pos_cnum => Parser.next(p) switch p.token { | Colon => @@ -20250,7 +20263,7 @@ module Repl = { try { let sstr = switch parseToplevelPhrase(filename) { | Parsetree.Ptop_def(sstr) => sstr - | _ => assert false + | _ => assert(false) } let (_str, signature, _newenv) = Typemod.type_toplevel_phrase(env, sstr) @@ -20379,12 +20392,12 @@ module Driver: { switch kind { | Structure => Parse.implementation(lexbuf) - ->ParsetreeCompatibility.replaceStringLiteralStructure(stringData) - ->ParsetreeCompatibility.structure + ->ParsetreeCompatibility.replaceStringLiteralStructure(stringData) + ->ParsetreeCompatibility.structure | Signature => Parse.interface(lexbuf) - ->ParsetreeCompatibility.replaceStringLiteralSignature(stringData) - ->ParsetreeCompatibility.signature + ->ParsetreeCompatibility.replaceStringLiteralSignature(stringData) + ->ParsetreeCompatibility.signature } } @@ -20463,7 +20476,7 @@ module Driver: { } let buffer = (@doesNotRaise really_input_string)(ic, String.length(magic)) - assert (buffer == magic) + assert(buffer == magic) let filename = input_value(ic) reasonFilename := filename let ast = input_value(ic) @@ -20500,14 +20513,14 @@ module Driver: { switch kind { | Structure => ast - ->ParsetreeCompatibility.replaceStringLiteralStructure(stringData.contents) - ->ParsetreeCompatibility.normalizeReasonArityStructure(~forPrinter=true) - ->ParsetreeCompatibility.structure + ->ParsetreeCompatibility.replaceStringLiteralStructure(stringData.contents) + ->ParsetreeCompatibility.normalizeReasonArityStructure(~forPrinter=true) + ->ParsetreeCompatibility.structure | Signature => ast - ->ParsetreeCompatibility.replaceStringLiteralSignature(stringData.contents) - ->ParsetreeCompatibility.normalizeReasonAritySignature(~forPrinter=true) - ->ParsetreeCompatibility.signature + ->ParsetreeCompatibility.replaceStringLiteralSignature(stringData.contents) + ->ParsetreeCompatibility.normalizeReasonAritySignature(~forPrinter=true) + ->ParsetreeCompatibility.signature } } @@ -20551,7 +20564,7 @@ module Driver: { let process = (~reportStyle, parseFn, printFn, recover, filename) => { let (ast, report, parserState) = parseFn(filename) switch report { - | Some(report) when recover == true => + | Some(report) if recover == true => printFn(ast, parserState) prerr_string( Diagnostics.stringOfReport( diff --git a/tests/syntax_benchmarks/data/RedBlackTree.res b/tests/syntax_benchmarks/data/RedBlackTree.res index 409c4feb32..75a887c594 100644 --- a/tests/syntax_benchmarks/data/RedBlackTree.res +++ b/tests/syntax_benchmarks/data/RedBlackTree.res @@ -38,7 +38,7 @@ type rec node<'value> = { mutable right: option>, mutable parent: option>, mutable sum: float, - mutable color : nodeColor, + mutable color: nodeColor, mutable height: float, mutable value: 'value, } @@ -46,15 +46,22 @@ type rec node<'value> = { type t<'value> = { mutable size: int, mutable root: option>, - compare: (. 'value, 'value) => int, + compare: ('value, 'value) => int, } -let createNode = (~color, ~value, ~height) => - {left:None, right:None, parent:None, sum:0., height, value, color} +let createNode = (~color, ~value, ~height) => { + left: None, + right: None, + parent: None, + sum: 0., + height, + value, + color, +} external castNotOption: option<'a> => 'a = "%identity" -let updateSum = (node) => { +let updateSum = node => { let leftSum = switch node.left { | None => 0. | Some(left) => left.sum @@ -71,8 +78,7 @@ let rec updateSumRecursive = (rbt, node) => { updateSum(node) switch node.parent { | None => () - | Some(parent) => - rbt->updateSumRecursive(parent) + | Some(parent) => rbt->updateSumRecursive(parent) } } @@ -91,7 +97,7 @@ let isLeft = node => { } let leftOrRightSet = (~node, x, value) => { - isLeft(node) ? x.left=value : x.right=value + isLeft(node) ? x.left = value : x.right = value } let siblingOf = node => { @@ -118,7 +124,7 @@ let rec findNode = (rbt, node, value) => { switch node { | None => None | Some(node) => - let cmp = rbt.compare(. value, node.value) + let cmp = rbt.compare(value, node.value) if cmp === 0 { Some(node) } else if cmp < 0 { @@ -131,36 +137,32 @@ let rec findNode = (rbt, node, value) => { let has = (rbt, value) => findNode(rbt, rbt.root, value) !== None -let rec peekMinNode = node => switch node { +let rec peekMinNode = node => + switch node { | None => None - | Some(node) => - node.left === None ? Some(node) : node.left->peekMinNode -} + | Some(node) => node.left === None ? Some(node) : node.left->peekMinNode + } -let rec peekMaxNode = node => switch node { +let rec peekMaxNode = node => + switch node { | None => None - | Some(node) => - node.right === None ? Some(node) : node.right->peekMaxNode -} + | Some(node) => node.right === None ? Some(node) : node.right->peekMaxNode + } let rotateLeft = (rbt, node) => { let parent = node.parent let right = node.right switch parent { - | Some(parent) => - parent->leftOrRightSet(~node, right) - | None => - rbt.root = right + | Some(parent) => parent->leftOrRightSet(~node, right) + | None => rbt.root = right } node.parent = right let right = right->castNotOption // precondition let rightLeft = right.left node.right = rightLeft switch rightLeft { - | Some(rightLeft) => - rightLeft.parent = Some(node) - | None => - () + | Some(rightLeft) => rightLeft.parent = Some(node) + | None => () } right.parent = parent right.left = Some(node) @@ -172,20 +174,16 @@ let rotateRight = (rbt, node) => { let parent = node.parent let left = node.left switch parent { - | Some(parent) => - parent->leftOrRightSet(~node, left) - | None => - rbt.root = left + | Some(parent) => parent->leftOrRightSet(~node, left) + | None => rbt.root = left } node.parent = left let left = left->castNotOption // precondition let leftRight = left.right node.left = leftRight switch leftRight { - | Some(leftRight) => - leftRight.parent = Some(node) - | None => - () + | Some(leftRight) => leftRight.parent = Some(node) + | None => () } left.parent = parent left.right = Some(node) @@ -195,29 +193,25 @@ let rotateRight = (rbt, node) => { let rec findInsert = (rbt, node, nodeToInsert, value) => { switch node { - | None => None - | Some(node) => { - let cmp = rbt.compare(. value, node.value) + | None => None + | Some(node) => { + let cmp = rbt.compare(value, node.value) if cmp === 0 { Some(node) - } else { - if cmp < 0 { - if node.left !== None { - rbt->findInsert(node.left, nodeToInsert, value) - } else { - nodeToInsert.parent = Some(node) - node.left = Some(nodeToInsert) - None - } + } else if cmp < 0 { + if node.left !== None { + rbt->findInsert(node.left, nodeToInsert, value) } else { - if node.right !== None { - rbt->findInsert(node.right, nodeToInsert, value) - } else { - nodeToInsert.parent = Some(node) - node.right = Some(nodeToInsert) - None - } + nodeToInsert.parent = Some(node) + node.left = Some(nodeToInsert) + None } + } else if node.right !== None { + rbt->findInsert(node.right, nodeToInsert, value) + } else { + nodeToInsert.parent = Some(node) + node.right = Some(nodeToInsert) + None } } } @@ -232,44 +226,38 @@ let rec _addLoop = (rbt, currentNode) => { // Case 1: node is root. Violates 1. Paint it black. if Some(currentNode) === rbt.root { currentNode.color = Black - } - - // Case 2: parent black. No properties violated. After that, parent is sure + } // Case 2: parent black. No properties violated. After that, parent is sure // to be red. else if (currentNode.parent->castNotOption).color === Black { () - } - - // Case 3: if node's parent and uncle are red, they are painted black. + } // Case 3: if node's parent and uncle are red, they are painted black. // Their parent (node's grandparent) should be painted red, and the // grandparent red. Note that node certainly has a grandparent, since at // this point, its parent's red, which can't be the root. // After the painting, the grandparent might violate 2 or 4. - else if({ - let uncle = uncleOf(currentNode) - uncle !== None && (uncle->castNotOption).color === Red - }) { + else if { + let uncle = uncleOf(currentNode) + uncle !== None && (uncle->castNotOption).color === Red + } { (currentNode.parent->castNotOption).color = Black (uncleOf(currentNode)->castNotOption).color = Black (grandParentOf(currentNode)->castNotOption).color = Red _addLoop(rbt, grandParentOf(currentNode)->castNotOption) - } - else { + } else { // At this point, uncle is either black or doesn't exist. // Case 4: parent red, uncle black, node is right child, parent is left // child. Do a left rotation. Then, former parent passes through case 5. - let currentNode = - if !isLeft(currentNode) && isLeft(currentNode.parent->castNotOption) { - rotateLeft(rbt, currentNode.parent->castNotOption) - currentNode.left->castNotOption - } else if isLeft(currentNode) && !isLeft(currentNode.parent->castNotOption) { - rotateRight(rbt, currentNode.parent->castNotOption) - currentNode.right->castNotOption - } else { - currentNode - } + let currentNode = if !isLeft(currentNode) && isLeft(currentNode.parent->castNotOption) { + rotateLeft(rbt, currentNode.parent->castNotOption) + currentNode.left->castNotOption + } else if isLeft(currentNode) && !isLeft(currentNode.parent->castNotOption) { + rotateRight(rbt, currentNode.parent->castNotOption) + currentNode.right->castNotOption + } else { + currentNode + } // Case 5: parent red, uncle black, node is left child, parent is left // child. Right rotation. Switch parent and grandparent's color. @@ -289,15 +277,13 @@ let add = (rbt, value, ~height) => { // _Returns:_ value added. rbt.size = rbt.size + 1 let nodeToInsert = createNode(~value, ~color=Red, ~height) - let inserted = - if rbt.root === None { - rbt.root = Some(nodeToInsert) - true - } - else { - let foundNode = findInsert(rbt, rbt.root, nodeToInsert, value) - foundNode === None - } + let inserted = if rbt.root === None { + rbt.root = Some(nodeToInsert) + true + } else { + let foundNode = findInsert(rbt, rbt.root, nodeToInsert, value) + foundNode === None + } if inserted { rbt->updateSumRecursive(nodeToInsert) @@ -308,7 +294,6 @@ let add = (rbt, value, ~height) => { } } - // To simplify removal cases, we can notice this: // 1. Node has no child. // 2. Node has two children. Select the smallest child on the right branch @@ -318,34 +303,31 @@ let add = (rbt, value, ~height) => { // 3. Node has one child. // They all come down to removing a node with maximum one child. let removeNode = (rbt, node) => { - let nodeToRemove = - switch (node.left, node.right) { - | (Some(_), Some(_)) => - let successor = peekMinNode(node.right)->castNotOption - node.value = successor.value - node.height = successor.height - successor - | _ => node - } + let nodeToRemove = switch (node.left, node.right) { + | (Some(_), Some(_)) => + let successor = peekMinNode(node.right)->castNotOption + node.value = successor.value + node.height = successor.height + successor + | _ => node + } // At this point, the node to remove has only one child. let successor = switch nodeToRemove.left { | None => nodeToRemove.right | left => left } let (successor, isLeaf) = switch successor { - | None => - let leaf = createNode(~value=%raw("0"), ~color=Black, ~height=0.) - let isLeaf = (. x) => x === leaf; - (leaf, isLeaf) - | Some(successor) => - (successor, (. _) => false) + | None => + let leaf = createNode(~value=%raw("0"), ~color=Black, ~height=0.) + let isLeaf = x => x === leaf + (leaf, isLeaf) + | Some(successor) => (successor, _ => false) } let nodeParent = nodeToRemove.parent successor.parent = nodeParent switch nodeParent { | None => () - | Some(parent) => - parent->leftOrRightSet(~node=nodeToRemove, Some(successor)) + | Some(parent) => parent->leftOrRightSet(~node=nodeToRemove, Some(successor)) } rbt->updateSumRecursive(successor) @@ -386,57 +368,54 @@ let removeNode = (rbt, node) => { // sibling red. Rebalance parent. let sibling = siblingOf(successor) let siblingNN = sibling->castNotOption - if + if ( successorParent.color === Black && - ( sibling === None || - ( siblingNN.color === Black && - ( siblingNN.left === None || - (siblingNN.left->castNotOption).color === Black ) && - ( siblingNN.right === None || - (siblingNN.right->castNotOption).color === Black))) - { + (sibling === None || + (siblingNN.color === Black && + (siblingNN.left === None || (siblingNN.left->castNotOption).color === Black) && + (siblingNN.right === None || (siblingNN.right->castNotOption).color === Black))) + ) { if sibling !== None { siblingNN.color = Red } successorRef.contents = successorParent // continue - } else if + } else if ( // Case 4: sibling and sibling children black. Node parent red. Swap // color of sibling and node parent. successorParent.color === Red && - ( sibling === None || - ( siblingNN.color === Black && - ( siblingNN.left === None || - (siblingNN.left->castNotOption).color === Black) && - ( siblingNN.right === None || - (siblingNN.right->castNotOption).color === Black))) - { + (sibling === None || + (siblingNN.color === Black && + (siblingNN.left === None || (siblingNN.left->castNotOption).color === Black) && + (siblingNN.right === None || (siblingNN.right->castNotOption).color === Black))) + ) { if sibling !== None { siblingNN.color = Red } successorParent.color = Black break.contents = true - } else if + } else if ( // Case 5: sibling black, sibling left child red, right child black, // node is left child. Rotate right sibling. Swap color of sibling and // its new parent. sibling !== None && (sibling->castNotOption).color === Black - { + ) { let sibling = sibling->castNotOption - if + if ( isLeft(successor) && (sibling.right === None || (sibling.right->castNotOption).color === Black) && sibling.left !== None && - (sibling.left->castNotOption).color === Red { + (sibling.left->castNotOption).color === Red + ) { sibling.color = Red (sibling.left->castNotOption).color = Black rotateRight(rbt, sibling) - } else if + } else if ( !isLeft(successor) && (sibling.left === None || (sibling.left->castNotOption).color === Black) && sibling.right !== None && (sibling.right->castNotOption).color === Red - { + ) { sibling.color = Red (sibling.right->castNotOption).color = Black rotateLeft(rbt, sibling) @@ -461,27 +440,26 @@ let removeNode = (rbt, node) => { } } } + // Don't forget to detatch the artificially created leaf. - if isLeaf(. successor) { + if isLeaf(successor) { if rbt.root === Some(successor) { rbt.root = None } switch successor.parent { | None => () - | Some(parent) => - parent->leftOrRightSet(~node=successor, None) + | Some(parent) => parent->leftOrRightSet(~node=successor, None) } } } let remove = (rbt, value) => { switch findNode(rbt, rbt.root, value) { - | Some(node) => - rbt->removeNode(node) - rbt.size = rbt.size - 1 - true - | None => - false + | Some(node) => + rbt->removeNode(node) + rbt.size = rbt.size - 1 + true + | None => false } } @@ -489,7 +467,7 @@ let rec findNodeThroughCallback = (rbt, node, cb) => { switch node { | None => None | Some(node) => - let cmp = cb(. node) + let cmp = cb(node) if cmp === 0 { Some(node) } else if cmp < 0 { @@ -502,12 +480,11 @@ let rec findNodeThroughCallback = (rbt, node, cb) => { let removeThroughCallback = (rbt, cb) => { switch findNodeThroughCallback(rbt, rbt.root, cb) { - | Some(node) => - rbt->removeNode(node) - rbt.size = rbt.size - 1 - true - | None => - false + | Some(node) => + rbt->removeNode(node) + rbt.size = rbt.size - 1 + true + | None => false } } @@ -515,7 +492,7 @@ let make = (~compare) => {size: 0, root: None, compare} let makeWith = (array, ~compare) => { let rbt = make(~compare) - array->Js.Array2.forEach(((value, height)) => add(rbt,value, ~height)->ignore) + array->Js.Array2.forEach(((value, height)) => add(rbt, value, ~height)->ignore) rbt } @@ -528,10 +505,10 @@ let rec heightOfInterval = (rbt, node, lhs, rhs) => { //Js.log4("heightOfInterval n:", n.value, lhs, rhs) if lhs === None && rhs === None { n.sum - } else if lhs !== None && rbt.compare(. n.value, lhs->castNotOption) < 0 { + } else if lhs !== None && rbt.compare(n.value, lhs->castNotOption) < 0 { // to the lhs of the interval rbt->heightOfInterval(n.right, lhs, rhs) - } else if rhs !== None && rbt.compare(. n.value, rhs->castNotOption) > 0 { + } else if rhs !== None && rbt.compare(n.value, rhs->castNotOption) > 0 { // to the rhs of the interval rbt->heightOfInterval(n.left, lhs, rhs) } else { @@ -555,13 +532,13 @@ let rec firstVisibleNode = (node, top) => { | Some(node) => //Js.log4("firstVisibleNode", node.value, "top:", top) if node.sum <= top { - // no node is visible - None + // no node is visible + None } else { let nodeHeight = node.height let sumLeft = switch node.left { - | None => 0.0 - | Some(left) => left.sum + | None => 0.0 + | Some(left) => left.sum } if sumLeft > top { firstVisibleNode(node.left, top) @@ -578,8 +555,7 @@ let rec firstVisibleNode = (node, top) => { let lastVisibleNode = (node, top) => { switch firstVisibleNode(node, top) { - | None => - node->peekMaxNode + | None => node->peekMaxNode | first => first } } @@ -589,84 +565,84 @@ let firstVisibleValue = (rbt, ~top) => switch firstVisibleNode(rbt.root, top) { | None => None | Some(node) => Some(node.value) -} + } -let rec leftmost = node => switch node.left { +let rec leftmost = node => + switch node.left { | None => node | Some(node) => node->leftmost -} + } let rec firstRightParent = node => { switch node.parent { - | None => None - | Some(parent) => - isLeft(node) ? Some(parent) : parent->firstRightParent + | None => None + | Some(parent) => isLeft(node) ? Some(parent) : parent->firstRightParent } } let nextNode = node => { switch node.right { - | None => - node->firstRightParent - | Some(right) => - Some(right->leftmost) + | None => node->firstRightParent + | Some(right) => Some(right->leftmost) } } let rec sumLeftSpine = (node, ~fromRightChild) => { let leftSpine = switch node.left { - | None => node.height - | Some(left) => fromRightChild ? node.height +. left.sum : 0.0 + | None => node.height + | Some(left) => fromRightChild ? node.height +. left.sum : 0.0 } switch node.parent { - | None => - leftSpine - | Some(parent) => - leftSpine +. parent->sumLeftSpine(~fromRightChild = parent.right === Some(node)) + | None => leftSpine + | Some(parent) => leftSpine +. parent->sumLeftSpine(~fromRightChild=parent.right === Some(node)) } } -let getY = node => - node->sumLeftSpine(~fromRightChild=true) -. node.height +let getY = node => node->sumLeftSpine(~fromRightChild=true) -. node.height let rec iterate = (~inclusive, firstNode, lastNode, ~callback) => { switch firstNode { - | None => () - | Some(node) => - if inclusive { callback(. node) } - if firstNode !== lastNode { - if !inclusive { callback (.node) } - iterate(~inclusive, node->nextNode, lastNode, ~callback) + | None => () + | Some(node) => + if inclusive { + callback(node) + } + if firstNode !== lastNode { + if !inclusive { + callback(node) } + iterate(~inclusive, node->nextNode, lastNode, ~callback) + } } } let rec iterateWithY = (~y=?, ~inclusive, firstNode, lastNode, ~callback) => { switch firstNode { - | None => () - | Some(node) => - let y = switch y { - | None => node->getY - | Some(y) => y - } - if inclusive { - callback(. node, y) - } - if firstNode !== lastNode { - if !inclusive { - callback (.node, y) - } - iterateWithY(~y=y+.node.height, ~inclusive, node->nextNode, lastNode, ~callback) + | None => () + | Some(node) => + let y = switch y { + | None => node->getY + | Some(y) => y + } + if inclusive { + callback(node, y) + } + if firstNode !== lastNode { + if !inclusive { + callback(node, y) } + iterateWithY(~y=y +. node.height, ~inclusive, node->nextNode, lastNode, ~callback) + } } } -let rec updateSum = (node, ~delta) => switch node { +let rec updateSum = (node, ~delta) => + switch node { | None => () | Some(node) => node.sum = node.sum +. delta node.parent->updateSum(~delta) -} + } let updateHeight = (node, ~height) => { let delta = height -. node.height @@ -677,31 +653,29 @@ let updateHeight = (node, ~height) => { type oldNewVisible<'value> = { mutable old: array<'value>, mutable new: array<'value>, -}; +} let getAnchorDelta = (rbt, ~anchor) => { switch anchor { + | None => 0.0 + | Some((value, y)) => + switch rbt->findNode(rbt.root, value) { + | Some(node) => y -. node->getY | None => 0.0 - | Some((value, y)) => - switch rbt->findNode(rbt.root, value) { - | Some(node) => y -. node->getY - | None => 0.0 - } + } } } -let onChangedVisible = - ( - ~anchor=None, - rbt, - ~oldNewVisible, - ~top as top_, - ~bottom as bottom_, - ~appear, - ~remained, - ~disappear, - ) => - { +let onChangedVisible = ( + ~anchor=None, + rbt, + ~oldNewVisible, + ~top as top_, + ~bottom as bottom_, + ~appear, + ~remained, + ~disappear, +) => { let old = oldNewVisible.new let new = oldNewVisible.old // empty new @@ -720,32 +694,33 @@ let onChangedVisible = let oldLen = old->Js.Array2.length let oldIter = ref(0) - iterateWithY(~inclusive=true, first, last, ~callback=(. node, y_) => { + iterateWithY(~inclusive=true, first, last, ~callback=(node, y_) => { let y = y_ +. anchorDelta - if y >= 0.0 { // anchoring can make y negative + if y >= 0.0 { + // anchoring can make y negative while ( oldIter.contents < oldLen && - rbt.compare(. Js.Array2.unsafe_get(old, oldIter.contents), node.value) < 0 + rbt.compare(Js.Array2.unsafe_get(old, oldIter.contents), node.value) < 0 ) { - disappear(. Js.Array2.unsafe_get(old, oldIter.contents)) + disappear(Js.Array2.unsafe_get(old, oldIter.contents)) oldIter.contents = oldIter.contents + 1 } new->Js.Array2.push(node.value)->ignore - if (oldIter.contents < oldLen) { - let cmp = rbt.compare(. Js.Array2.unsafe_get(old, oldIter.contents), node.value) + if oldIter.contents < oldLen { + let cmp = rbt.compare(Js.Array2.unsafe_get(old, oldIter.contents), node.value) if cmp == 0 { - remained(. node, y) + remained(node, y) oldIter.contents = oldIter.contents + 1 } else { - appear(. node, y) + appear(node, y) } } else { - appear(. node, y) + appear(node, y) } } }) - while (oldIter.contents < oldLen) { - disappear(. Js.Array2.unsafe_get(old, oldIter.contents)) + while oldIter.contents < oldLen { + disappear(Js.Array2.unsafe_get(old, oldIter.contents)) oldIter.contents = oldIter.contents + 1 } -}; +} diff --git a/tests/syntax_benchmarks/data/RedBlackTreeNoComments.res b/tests/syntax_benchmarks/data/RedBlackTreeNoComments.res index 6f68764300..9842a002b8 100644 --- a/tests/syntax_benchmarks/data/RedBlackTreeNoComments.res +++ b/tests/syntax_benchmarks/data/RedBlackTreeNoComments.res @@ -15,7 +15,7 @@ type rec node<'value> = { type t<'value> = { mutable size: int, mutable root: option>, - compare: (. 'value, 'value) => int, + compare: ('value, 'value) => int, } let createNode = (~color, ~value, ~height) => { @@ -23,9 +23,9 @@ let createNode = (~color, ~value, ~height) => { right: None, parent: None, sum: 0., - height: height, - value: value, - color: color, + height, + value, + color, } external castNotOption: option<'a> => 'a = "%identity" @@ -62,10 +62,7 @@ let isLeft = node => | Some(parent) => Some(node) === parent.left } -let leftOrRightSet = (~node, x, value) => - isLeft(node) - ? x.left = value - : x.right = value +let leftOrRightSet = (~node, x, value) => isLeft(node) ? x.left = value : x.right = value let siblingOf = node => if isLeft(node) { @@ -89,7 +86,7 @@ let rec findNode = (rbt, node, value) => switch node { | None => None | Some(node) => - let cmp = rbt.compare(. value, node.value) + let cmp = rbt.compare(value, node.value) if cmp === 0 { Some(node) } else if cmp < 0 { @@ -159,7 +156,7 @@ let rec findInsert = (rbt, node, nodeToInsert, value) => switch node { | None => None | Some(node) => - let cmp = rbt.compare(. value, node.value) + let cmp = rbt.compare(value, node.value) if cmp === 0 { Some(node) } else if cmp < 0 { @@ -193,14 +190,10 @@ let rec _addLoop = (rbt, currentNode) => (grandParentOf(currentNode)->castNotOption).color = Red _addLoop(rbt, grandParentOf(currentNode)->castNotOption) } else { - let currentNode = if ( - !isLeft(currentNode) && isLeft(currentNode.parent->castNotOption) - ) { + let currentNode = if !isLeft(currentNode) && isLeft(currentNode.parent->castNotOption) { rotateLeft(rbt, currentNode.parent->castNotOption) currentNode.left->castNotOption - } else if ( - isLeft(currentNode) && !isLeft(currentNode.parent->castNotOption) - ) { + } else if isLeft(currentNode) && !isLeft(currentNode.parent->castNotOption) { rotateRight(rbt, currentNode.parent->castNotOption) currentNode.right->castNotOption } else { @@ -253,9 +246,9 @@ let removeNode = (rbt, node) => { let (successor, isLeaf) = switch successor { | None => let leaf = createNode(~value=%raw("0"), ~color=Black, ~height=0.) - let isLeaf = (. x) => x === leaf + let isLeaf = x => x === leaf (leaf, isLeaf) - | Some(successor) => (successor, (. _) => false) + | Some(successor) => (successor, _ => false) } let nodeParent = nodeToRemove.parent successor.parent = nodeParent @@ -300,10 +293,8 @@ let removeNode = (rbt, node) => { successorParent.color === Black && (sibling === None || (siblingNN.color === Black && - (siblingNN.left === None || - (siblingNN.left->castNotOption).color === Black) && - (siblingNN.right === None || - (siblingNN.right->castNotOption).color === Black))) + (siblingNN.left === None || (siblingNN.left->castNotOption).color === Black) && + (siblingNN.right === None || (siblingNN.right->castNotOption).color === Black))) ) { if sibling !== None { siblingNN.color = Red @@ -313,24 +304,19 @@ let removeNode = (rbt, node) => { successorParent.color === Red && (sibling === None || (siblingNN.color === Black && - (siblingNN.left === None || - (siblingNN.left->castNotOption).color === Black) && - (siblingNN.right === None || - (siblingNN.right->castNotOption).color === Black))) + (siblingNN.left === None || (siblingNN.left->castNotOption).color === Black) && + (siblingNN.right === None || (siblingNN.right->castNotOption).color === Black))) ) { if sibling !== None { siblingNN.color = Red } successorParent.color = Black break.contents = true - } else if ( - sibling !== None && (sibling->castNotOption).color === Black - ) { + } else if sibling !== None && (sibling->castNotOption).color === Black { let sibling = sibling->castNotOption if ( isLeft(successor) && - (sibling.right === None || - (sibling.right->castNotOption).color === Black) && + (sibling.right === None || (sibling.right->castNotOption).color === Black) && sibling.left !== None && (sibling.left->castNotOption).color === Red ) { @@ -339,8 +325,7 @@ let removeNode = (rbt, node) => { rotateRight(rbt, sibling) } else if ( !isLeft(successor) && - (sibling.left === None || - (sibling.left->castNotOption).color === Black) && + (sibling.left === None || (sibling.left->castNotOption).color === Black) && sibling.right !== None && (sibling.right->castNotOption).color === Red ) { @@ -366,7 +351,7 @@ let removeNode = (rbt, node) => { } } - if isLeaf(. successor) { + if isLeaf(successor) { if rbt.root === Some(successor) { rbt.root = None } @@ -390,7 +375,7 @@ let rec findNodeThroughCallback = (rbt, node, cb) => switch node { | None => None | Some(node) => - let cmp = cb(. node) + let cmp = cb(node) if cmp === 0 { Some(node) } else if cmp < 0 { @@ -409,13 +394,11 @@ let removeThroughCallback = (rbt, cb) => | None => false } -let make = (~compare) => {size: 0, root: None, compare: compare} +let make = (~compare) => {size: 0, root: None, compare} let makeWith = (array, ~compare) => { let rbt = make(~compare) - array->Js.Array2.forEach(((value, height)) => - add(rbt, value, ~height)->ignore - ) + array->Js.Array2.forEach(((value, height)) => add(rbt, value, ~height)->ignore) rbt } @@ -425,9 +408,9 @@ let rec heightOfInterval = (rbt, node, lhs, rhs) => | Some(n) => if lhs === None && rhs === None { n.sum - } else if lhs !== None && rbt.compare(. n.value, lhs->castNotOption) < 0 { + } else if lhs !== None && rbt.compare(n.value, lhs->castNotOption) < 0 { rbt->heightOfInterval(n.right, lhs, rhs) - } else if rhs !== None && rbt.compare(. n.value, rhs->castNotOption) > 0 { + } else if rhs !== None && rbt.compare(n.value, rhs->castNotOption) > 0 { rbt->heightOfInterval(n.left, lhs, rhs) } else { n.height +. @@ -436,8 +419,7 @@ let rec heightOfInterval = (rbt, node, lhs, rhs) => } } -let heightOfInterval = (rbt, lhs, rhs) => - heightOfInterval(rbt, rbt.root, lhs, rhs) +let heightOfInterval = (rbt, lhs, rhs) => heightOfInterval(rbt, rbt.root, lhs, rhs) let rec firstVisibleNode = (node, top) => switch node { @@ -499,9 +481,7 @@ let rec sumLeftSpine = (node, ~fromRightChild) => { } switch node.parent { | None => leftSpine - | Some(parent) => - leftSpine +. - parent->sumLeftSpine(~fromRightChild=parent.right === Some(node)) + | Some(parent) => leftSpine +. parent->sumLeftSpine(~fromRightChild=parent.right === Some(node)) } } @@ -512,11 +492,11 @@ let rec iterate = (~inclusive, firstNode, lastNode, ~callback) => | None => () | Some(node) => if inclusive { - callback(. node) + callback(node) } if firstNode !== lastNode { if !inclusive { - callback(. node) + callback(node) } iterate(~inclusive, node->nextNode, lastNode, ~callback) } @@ -531,19 +511,13 @@ let rec iterateWithY = (~y=?, ~inclusive, firstNode, lastNode, ~callback) => | Some(y) => y } if inclusive { - callback(. node, y) + callback(node, y) } if firstNode !== lastNode { if !inclusive { - callback(. node, y) + callback(node, y) } - iterateWithY( - ~y=y +. node.height, - ~inclusive, - node->nextNode, - lastNode, - ~callback, - ) + iterateWithY(~y=y +. node.height, ~inclusive, node->nextNode, lastNode, ~callback) } } @@ -606,38 +580,32 @@ let onChangedVisible = ( let oldLen = old->Js.Array2.length let oldIter = ref(0) - iterateWithY(~inclusive=true, first, last, (. node, y_) => { + iterateWithY(~inclusive=true, first, last, (node, y_) => { let y = y_ +. anchorDelta if y >= 0.0 { while ( oldIter.contents < oldLen && - rbt.compare(. - Js.Array2.unsafe_get(old, oldIter.contents), - node.value, - ) < 0 + rbt.compare(Js.Array2.unsafe_get(old, oldIter.contents), node.value) < 0 ) { - disappear(. Js.Array2.unsafe_get(old, oldIter.contents)) + disappear(Js.Array2.unsafe_get(old, oldIter.contents)) oldIter.contents = oldIter.contents + 1 } new->Js.Array2.push(node.value)->ignore if oldIter.contents < oldLen { - let cmp = rbt.compare(. - Js.Array2.unsafe_get(old, oldIter.contents), - node.value, - ) + let cmp = rbt.compare(Js.Array2.unsafe_get(old, oldIter.contents), node.value) if cmp == 0 { - remained(. node, y) + remained(node, y) oldIter.contents = oldIter.contents + 1 } else { - appear(. node, y) + appear(node, y) } } else { - appear(. node, y) + appear(node, y) } } }) while oldIter.contents < oldLen { - disappear(. Js.Array2.unsafe_get(old, oldIter.contents)) + disappear(Js.Array2.unsafe_get(old, oldIter.contents)) oldIter.contents = oldIter.contents + 1 } }