From 1a792a6692ce6ab68aa8c30427391376fbffb3f0 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Sun, 3 Jul 2022 03:28:30 +0200 Subject: [PATCH 1/3] remove "import" --- src/res_core.ml | 94 -------------- src/res_grammar.ml | 12 +- src/res_js_ffi.ml | 121 ------------------ src/res_token.ml | 9 +- .../grammar/ffi/expected/import.res.txt | 12 -- tests/parsing/grammar/ffi/import.res | 7 - .../structure/expected/jsFfiSugar.res.txt | 37 ++---- .../parsing/grammar/structure/jsFfiSugar.res | 16 --- tests/printer/ffi/expected/import.res.txt | 23 ++-- tests/printer/ffi/import.res | 11 -- 10 files changed, 23 insertions(+), 319 deletions(-) delete mode 100644 src/res_js_ffi.ml delete mode 100644 tests/parsing/grammar/ffi/expected/import.res.txt delete mode 100644 tests/parsing/grammar/ffi/import.res delete mode 100644 tests/parsing/grammar/structure/jsFfiSugar.res delete mode 100644 tests/printer/ffi/import.res diff --git a/src/res_core.ml b/src/res_core.ml index 31258253..8c29149c 100644 --- a/src/res_core.ml +++ b/src/res_core.ml @@ -5,7 +5,6 @@ module Diagnostics = Res_diagnostics module CommentTable = Res_comments_table module ResPrinter = Res_printer module Scanner = Res_scanner -module JsFfi = Res_js_ffi module Parser = Res_parser let mkLoc startLoc endLoc = @@ -721,30 +720,6 @@ let parseModuleLongIdent ~lowercase p = (* Parser.eatBreadcrumb p; *) moduleIdent -(* `window.location` or `Math` or `Foo.Bar` *) -let parseIdentPath p = - let rec loop p acc = - match p.Parser.token with - | Uident ident | Lident ident -> ( - Parser.next p; - let lident = Longident.Ldot (acc, ident) in - match p.Parser.token with - | Dot -> - Parser.next p; - loop p lident - | _ -> lident) - | _t -> acc - in - match p.Parser.token with - | Lident ident | Uident ident -> ( - Parser.next p; - match p.Parser.token with - | Dot -> - Parser.next p; - loop p (Longident.Lident ident) - | _ -> Longident.Lident ident) - | _ -> Longident.Lident "_" - let verifyJsxOpeningClosingName p nameExpr = let closing = match p.Parser.token with @@ -5336,12 +5311,6 @@ and parseStructureItemRegion p = parseNewlineOrSemicolonStructure p; let loc = mkLoc startPos p.prevEndPos in Some (Ast_helper.Str.primitive ~loc externalDef) - | Import -> - let importDescr = parseJsImport ~startPos ~attrs p in - parseNewlineOrSemicolonStructure p; - let loc = mkLoc startPos p.prevEndPos in - let structureItem = JsFfi.toParsetree importDescr in - Some {structureItem with pstr_loc = loc} | Exception -> let exceptionDef = parseExceptionDef ~attrs p in parseNewlineOrSemicolonStructure p; @@ -5403,23 +5372,6 @@ and parseStructureItemRegion p = | _ -> None) [@@progress Parser.next, Parser.expect] -and parseJsImport ~startPos ~attrs p = - Parser.expect Token.Import p; - let importSpec = - match p.Parser.token with - | Token.Lident _ | Token.At -> - let decl = - match parseJsFfiDeclaration p with - | Some decl -> decl - | None -> assert false - in - JsFfi.Default decl - | _ -> JsFfi.Spec (parseJsFfiDeclarations p) - in - let scope = parseJsFfiScope p in - let loc = mkLoc startPos p.prevEndPos in - JsFfi.importDescr ~attrs ~importSpec ~scope ~loc - and parseJsExport ~attrs p = let exportStart = p.Parser.startPos in Parser.expect Token.Export p; @@ -5457,49 +5409,6 @@ and parseSignJsExport ~attrs p = let loc = mkLoc exportStart p.prevEndPos in Ast_helper.Sig.value valueDesc ~loc -and parseJsFfiScope p = - match p.Parser.token with - | Token.Lident "from" -> ( - Parser.next p; - match p.token with - | String s -> - Parser.next p; - JsFfi.Module s - | Uident _ | Lident _ -> - let value = parseIdentPath p in - JsFfi.Scope value - | _ -> JsFfi.Global) - | _ -> JsFfi.Global - -and parseJsFfiDeclarations p = - Parser.expect Token.Lbrace p; - let decls = - parseCommaDelimitedRegion ~grammar:Grammar.JsFfiImport ~closing:Rbrace - ~f:parseJsFfiDeclaration p - in - Parser.expect Rbrace p; - decls - -and parseJsFfiDeclaration p = - let startPos = p.Parser.startPos in - let attrs = parseAttributes p in - match p.Parser.token with - | Lident _ -> - let ident, _ = parseLident p in - let alias = - match p.token with - | As -> - Parser.next p; - let ident, _ = parseLident p in - ident - | _ -> ident - in - Parser.expect Token.Colon p; - let typ = parseTypExpr p in - let loc = mkLoc startPos p.prevEndPos in - Some (JsFfi.decl ~loc ~alias ~attrs ~name:ident ~typ) - | _ -> None - (* include-statement ::= include module-expr *) and parseIncludeStatement ~attrs p = let startPos = p.Parser.startPos in @@ -6113,9 +6022,6 @@ and parseSignatureItemRegion p = parseNewlineOrSemicolonSignature p; let loc = mkLoc startPos p.prevEndPos in Some (Ast_helper.Sig.extension ~attrs ~loc extension) - | Import -> - Parser.next p; - parseSignatureItemRegion p | _ -> ( match attrs with | (({Asttypes.loc = attrLoc}, _) as attr) :: _ -> diff --git a/src/res_grammar.ml b/src/res_grammar.ml index 4c9cb942..63cf3e55 100644 --- a/src/res_grammar.ml +++ b/src/res_grammar.ml @@ -56,7 +56,6 @@ type t = | TypeConstraint | AtomicTypExpr | ListExpr - | JsFfiImport | Pattern | AttributePayload | TagNames @@ -116,7 +115,6 @@ let toString = function | AtomicTypExpr -> "a type" | ListExpr -> "an ocaml list expr" | PackageConstraint -> "a package constraint" - | JsFfiImport -> "js ffi import" | JsxChild -> "jsx child" | Pattern -> "pattern" | ExprFor -> "a for expression" @@ -162,8 +160,8 @@ let isJsxAttributeStart = function | _ -> false let isStructureItemStart = function - | Token.Open | Let | Typ | External | Import | Export | Exception | Include - | Module | AtAt | PercentPercent | At -> + | Token.Open | Let | Typ | External | Export | Exception | Include | Module + | AtAt | PercentPercent | At -> true | t when isExprStart t -> true | _ -> false @@ -254,10 +252,6 @@ let isAttributeStart = function | Token.At -> true | _ -> false -let isJsFfiImportStart = function - | Token.Lident _ | At -> true - | _ -> false - let isJsxChildStart = isAtomicExprStart let isBlockExprStart = function @@ -296,7 +290,6 @@ let isListElement grammar token = | PackageConstraint -> token = And | ConstructorDeclaration -> token = Bar | JsxAttribute -> isJsxAttributeStart token - | JsFfiImport -> isJsFfiImportStart token | AttributePayload -> token = Lparen | TagNames -> token = Hash | _ -> false @@ -318,7 +311,6 @@ let isListTerminator grammar token = | TypeParams, Rparen | ParameterList, (EqualGreater | Lbrace) | JsxAttribute, (Forwardslash | GreaterThan) - | JsFfiImport, Rbrace | StringFieldDeclarations, Rbrace -> true | Attribute, token when token <> At -> true diff --git a/src/res_js_ffi.ml b/src/res_js_ffi.ml deleted file mode 100644 index 3d02fb10..00000000 --- a/src/res_js_ffi.ml +++ /dev/null @@ -1,121 +0,0 @@ -(* AST for js externals *) -type scope = - | Global - | Module of string (* bs.module("path") *) - | Scope of Longident.t (* bs.scope(/"window", "location"/) *) - -type label_declaration = { - jld_attributes: Parsetree.attributes; [@live] - jld_name: string; - jld_alias: string; - jld_type: Parsetree.core_type; - jld_loc: Location.t; -} - -type importSpec = - | Default of label_declaration - | Spec of label_declaration list - -type import_description = { - jid_loc: Location.t; - jid_spec: importSpec; - jid_scope: scope; - jid_attributes: Parsetree.attributes; -} - -let decl ~attrs ~loc ~name ~alias ~typ = - { - jld_loc = loc; - jld_attributes = attrs; - jld_name = name; - jld_alias = alias; - jld_type = typ; - } - -let importDescr ~attrs ~scope ~importSpec ~loc = - { - jid_loc = loc; - jid_spec = importSpec; - jid_scope = scope; - jid_attributes = attrs; - } - -let toParsetree importDescr = - let bsVal = (Location.mknoloc "val", Parsetree.PStr []) in - let attrs = - match importDescr.jid_scope with - | Global -> [bsVal] - (* @genType.import("./MyMath"), - * @genType.import(/"./MyMath", "default"/) *) - | Module s -> - let structure = - [ - Parsetree.Pconst_string (s, None) - |> Ast_helper.Exp.constant |> Ast_helper.Str.eval; - ] - in - let genType = - (Location.mknoloc "genType.import", Parsetree.PStr structure) - in - [genType] - | Scope longident -> - let structureItem = - let expr = - match - Longident.flatten longident - |> List.map (fun s -> - Ast_helper.Exp.constant (Parsetree.Pconst_string (s, None))) - with - | [expr] -> expr - | ([] as exprs) | (_ as exprs) -> exprs |> Ast_helper.Exp.tuple - in - Ast_helper.Str.eval expr - in - let bsScope = - (Location.mknoloc "scope", Parsetree.PStr [structureItem]) - in - [bsVal; bsScope] - in - let valueDescrs = - match importDescr.jid_spec with - | Default decl -> - let prim = [decl.jld_name] in - let allAttrs = - List.concat [attrs; importDescr.jid_attributes] - |> List.map (fun attr -> - match attr with - | ( ({Location.txt = "genType.import"} as id), - Parsetree.PStr - [{pstr_desc = Parsetree.Pstr_eval (moduleName, _)}] ) -> - let default = - Parsetree.Pconst_string ("default", None) - |> Ast_helper.Exp.constant - in - let structureItem = - [moduleName; default] |> Ast_helper.Exp.tuple - |> Ast_helper.Str.eval - in - (id, Parsetree.PStr [structureItem]) - | attr -> attr) - in - [ - Ast_helper.Val.mk ~loc:importDescr.jid_loc ~prim ~attrs:allAttrs - (Location.mknoloc decl.jld_alias) - decl.jld_type - |> Ast_helper.Str.primitive; - ] - | Spec decls -> - List.map - (fun decl -> - let prim = [decl.jld_name] in - let allAttrs = List.concat [attrs; decl.jld_attributes] in - Ast_helper.Val.mk ~loc:importDescr.jid_loc ~prim ~attrs:allAttrs - (Location.mknoloc decl.jld_alias) - decl.jld_type - |> Ast_helper.Str.primitive ~loc:decl.jld_loc) - decls - in - let jsFfiAttr = (Location.mknoloc "ns.jsFfi", Parsetree.PStr []) in - Ast_helper.Mod.structure ~loc:importDescr.jid_loc valueDescrs - |> Ast_helper.Incl.mk ~attrs:[jsFfiAttr] ~loc:importDescr.jid_loc - |> Ast_helper.Str.include_ ~loc:importDescr.jid_loc diff --git a/src/res_token.ml b/src/res_token.ml index 2656448f..a0263160 100644 --- a/src/res_token.ml +++ b/src/res_token.ml @@ -93,7 +93,6 @@ type t = | Backtick | BarGreater | Try - | Import | Export | DocComment of Location.t * string | ModuleComment of Location.t * string @@ -205,7 +204,6 @@ let toString = function | Backtick -> "`" | BarGreater -> "|>" | Try -> "try" - | Import -> "import" | Export -> "export" | DocComment (_loc, s) -> "DocComment " ^ s | ModuleComment (_loc, s) -> "ModuleComment " ^ s @@ -222,7 +220,6 @@ let keywordTable = function | "false" -> False | "for" -> For | "if" -> If - | "import" -> Import | "in" -> In | "include" -> Include | "lazy" -> Lazy @@ -245,9 +242,9 @@ let keywordTable = function let isKeyword = function | And | As | Assert | Constraint | Else | Exception | Export | External - | False | For | If | Import | In | Include | Land | Lazy | Let | List | Lor - | Module | Mutable | Of | Open | Private | Rec | Switch | True | Try | Typ - | When | While -> + | False | For | If | In | Include | Land | Lazy | Let | List | Lor | Module + | Mutable | Of | Open | Private | Rec | Switch | True | Try | Typ | When + | While -> true | _ -> false diff --git a/tests/parsing/grammar/ffi/expected/import.res.txt b/tests/parsing/grammar/ffi/expected/import.res.txt deleted file mode 100644 index 96b867df..00000000 --- a/tests/parsing/grammar/ffi/expected/import.res.txt +++ /dev/null @@ -1,12 +0,0 @@ -include - struct - external realValue : complexNumber -> float = "realValue"[@@genType.import - "./MyMath"] - external maxInt : unit -> int = "maxInt"[@@genType.import "./MyMath"] - end[@@ns.jsFfi ] -include - struct - external realValue : complexNumber -> float = "realValue"[@@genType.import - ("./MyMath", - "default")] - end[@@ns.jsFfi ] \ No newline at end of file diff --git a/tests/parsing/grammar/ffi/import.res b/tests/parsing/grammar/ffi/import.res deleted file mode 100644 index 0931349d..00000000 --- a/tests/parsing/grammar/ffi/import.res +++ /dev/null @@ -1,7 +0,0 @@ -import { - realValue: complexNumber => float, - maxInt: unit => int -} from "./MyMath" - -// default -import realValue: complexNumber => float from "./MyMath" diff --git a/tests/parsing/grammar/structure/expected/jsFfiSugar.res.txt b/tests/parsing/grammar/structure/expected/jsFfiSugar.res.txt index e64a2bd2..c70b19cd 100644 --- a/tests/parsing/grammar/structure/expected/jsFfiSugar.res.txt +++ b/tests/parsing/grammar/structure/expected/jsFfiSugar.res.txt @@ -1,27 +1,10 @@ -include - struct - external setTimeout : (unit -> unit) -> unit -> float = "setTimeout" - [@@val ] - end[@@ns.jsFfi ] -include - struct - external timeout : (unit -> unit) -> unit -> float = "setTimeout"[@@val ] - end[@@ns.jsFfi ] -include - struct - external setTimeout : (unit -> unit) -> unit -> float = "setTimeout" - [@@val ] - external clearTimeout : float -> unit = "clearTimeout"[@@val ] - end[@@ns.jsFfi ] -include - struct - external random : unit -> float = "random"[@@val ][@@scope "Math"] - end[@@ns.jsFfi ] -include - struct - external href : string = "href"[@@val ][@@scope ("window", "location")] - end[@@ns.jsFfi ] -include - struct - external dirname : string -> string = "dirname"[@@genType.import "path"] - end[@@ns.jsFfi ] \ No newline at end of file + + Syntax error! + tests/parsing/grammar/structure/jsFfiSugar.res:1:7-17 + + 1 │ import setTimeout: (unit => unit, unit) => float + 2 │ + 3 │ import setTimeout as timeout: (unit => unit, unit) => float + + consecutive statements on a line must be separated by ';' or a newline + diff --git a/tests/parsing/grammar/structure/jsFfiSugar.res b/tests/parsing/grammar/structure/jsFfiSugar.res deleted file mode 100644 index 688c3bf5..00000000 --- a/tests/parsing/grammar/structure/jsFfiSugar.res +++ /dev/null @@ -1,16 +0,0 @@ -import setTimeout: (unit => unit, unit) => float - -import setTimeout as timeout: (unit => unit, unit) => float - -import { - setTimeout: (unit => unit, unit) => float, - clearTimeout: float => unit -} - -import { random: unit => float } from Math - -import { href: string } from window.location - -import { - dirname: string => string -} from "path" diff --git a/tests/printer/ffi/expected/import.res.txt b/tests/printer/ffi/expected/import.res.txt index 29203141..ecd5cef0 100644 --- a/tests/printer/ffi/expected/import.res.txt +++ b/tests/printer/ffi/expected/import.res.txt @@ -1,17 +1,10 @@ -@ns.jsFfi -include { - @genType.import("path") @disableGc @disableJit external delimiter: string = "delimiter" - @genType.import("path") external currentWorkingDirectory: unit => string = "cwd" - @genType.import("path") external isAbsolute: string => bool = "isAbsolute" - @genType.import("path") external \"ToNamespacedPath": string => string = "toNamespacedPath" -} -@ns.jsFfi -include { - @genType.import("firebase/app") external crazyString: float => timestamp = "*crazy_string*" -} + Syntax error! + tests/printer/ffi/import.res:1:7-8 + + 1 │ import { + 2 │ @disableGc @disableJit + 3 │ delimiter: string, + + consecutive statements on a line must be separated by ';' or a newline -@ns.jsFfi -include { - @val external document: Dom.document = "document" -} diff --git a/tests/printer/ffi/import.res b/tests/printer/ffi/import.res deleted file mode 100644 index 789ebf9c..00000000 --- a/tests/printer/ffi/import.res +++ /dev/null @@ -1,11 +0,0 @@ -import { - @disableGc @disableJit - delimiter: string, - cwd as currentWorkingDirectory : unit => string, - isAbsolute: string => bool, - toNamespacedPath as \"ToNamespacedPath": string => string, -} from "path" - -import {\"*crazy_string*" as crazyString : float => timestamp } from "firebase/app" - -import document: Dom.document From 6e76368bbc09ab8f0c64936f6e87e5ab00a507b9 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Sun, 3 Jul 2022 03:33:55 +0200 Subject: [PATCH 2/3] Delete jsFfiSugar.res.txt --- .../grammar/structure/expected/jsFfiSugar.res.txt | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 tests/parsing/grammar/structure/expected/jsFfiSugar.res.txt diff --git a/tests/parsing/grammar/structure/expected/jsFfiSugar.res.txt b/tests/parsing/grammar/structure/expected/jsFfiSugar.res.txt deleted file mode 100644 index c70b19cd..00000000 --- a/tests/parsing/grammar/structure/expected/jsFfiSugar.res.txt +++ /dev/null @@ -1,10 +0,0 @@ - - Syntax error! - tests/parsing/grammar/structure/jsFfiSugar.res:1:7-17 - - 1 │ import setTimeout: (unit => unit, unit) => float - 2 │ - 3 │ import setTimeout as timeout: (unit => unit, unit) => float - - consecutive statements on a line must be separated by ';' or a newline - From dc666ece783cf511f27dff74c2cf176aba789cdb Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Sun, 3 Jul 2022 17:47:23 +0200 Subject: [PATCH 3/3] Delete import.res.txt --- tests/printer/ffi/expected/import.res.txt | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 tests/printer/ffi/expected/import.res.txt diff --git a/tests/printer/ffi/expected/import.res.txt b/tests/printer/ffi/expected/import.res.txt deleted file mode 100644 index ecd5cef0..00000000 --- a/tests/printer/ffi/expected/import.res.txt +++ /dev/null @@ -1,10 +0,0 @@ - - Syntax error! - tests/printer/ffi/import.res:1:7-8 - - 1 │ import { - 2 │ @disableGc @disableJit - 3 │ delimiter: string, - - consecutive statements on a line must be separated by ';' or a newline -