diff --git a/src/dune/string_with_vars.ml b/src/dune/string_with_vars.ml index 8826bf89b07..7993f944394 100644 --- a/src/dune/string_with_vars.ml +++ b/src/dune/string_with_vars.ml @@ -23,7 +23,7 @@ let make template = { template; syntax_version = make_syntax } let make_text ?(quoted = false) loc s = make { parts = [ Text s ]; quoted; loc } let make_var ?(quoted = false) loc ?payload name = - let var = { loc; name; payload; syntax = Percent } in + let var = { loc; name; payload } in make { parts = [ Var var ]; quoted; loc } let literal ~quoted ~loc s = { parts = [ Text s ]; quoted; loc } @@ -52,10 +52,7 @@ let virt_var ?(quoted = false) pos s = | _ -> true) ); let loc = Loc.of_pos pos in let template = - { parts = [ Var { payload = None; name = s; syntax = Percent; loc } ] - ; loc - ; quoted - } + { parts = [ Var { payload = None; name = s; loc } ]; loc; quoted } in { template; syntax_version = make_syntax } @@ -315,18 +312,13 @@ module Make (A : Applicative_intf.S1) = struct partial_expand t ~mode ~dir ~f:(fun var syntax_version -> let+ exp = f var syntax_version in match exp with - | None -> ( - match var.syntax with - | Percent -> - if Var.is_macro var then - User_error.raise ~loc:var.loc - [ Pp.textf "Unknown macro %s" (Var.describe var) ] - else - User_error.raise ~loc:var.loc - [ Pp.textf "Unknown variable %S" (Var.name var) ] - | Dollar_brace - | Dollar_paren -> - Some [ Value.String (string_of_var var) ] ) + | None -> + if Var.is_macro var then + User_error.raise ~loc:var.loc + [ Pp.textf "Unknown macro %s" (Var.describe var) ] + else + User_error.raise ~loc:var.loc + [ Pp.textf "Unknown variable %S" (Var.name var) ] | s -> s) in match exp with diff --git a/src/dune_lang/lexer.mll b/src/dune_lang/lexer.mll index 77da5d8125c..24de3715df6 100644 --- a/src/dune_lang/lexer.mll +++ b/src/dune_lang/lexer.mll @@ -369,7 +369,6 @@ and template_variable = parse } ; name ; payload - ; syntax = Percent } } | '}' | eof diff --git a/src/dune_lang/template.ml b/src/dune_lang/template.ml index c0ff093a203..dd8c524e300 100644 --- a/src/dune_lang/template.ml +++ b/src/dune_lang/template.ml @@ -1,15 +1,9 @@ open! Stdune -type var_syntax = - | Dollar_brace - | Dollar_paren - | Percent - type var = { loc : Loc.t ; name : string ; payload : string option - ; syntax : var_syntax } type part = @@ -22,24 +16,10 @@ type t = ; loc : Loc.t } -let compare_var_syntax x y = - match (x, y) with - | Percent, Percent - | Dollar_brace, Dollar_brace - | Dollar_paren, Dollar_paren -> - Ordering.Eq - | Percent, (Dollar_brace | Dollar_paren) -> Ordering.Lt - | (Dollar_brace | Dollar_paren), Percent -> Ordering.Gt - | Dollar_brace, Dollar_paren -> Ordering.Lt - | Dollar_paren, Dollar_brace -> Ordering.Gt - let compare_var_no_loc v1 v2 = match String.compare v1.name v2.name with | (Ordering.Lt | Gt) as a -> a - | Eq -> ( - match Option.compare String.compare v1.payload v2.payload with - | (Ordering.Lt | Gt) as a -> a - | Eq -> compare_var_syntax v1.syntax v2.syntax ) + | Eq -> Option.compare String.compare v1.payload v2.payload let compare_part p1 p2 = match (p1, p2) with @@ -58,7 +38,7 @@ module Pp : sig end = struct let buf = Buffer.create 16 - let add_var { loc = _; syntax = _; name; payload } = + let add_var { loc = _; name; payload } = let before, after = ("%{", "}") in Buffer.add_string buf before; Buffer.add_string buf name; @@ -107,7 +87,7 @@ end let to_string = Pp.to_string -let string_of_var { loc = _; syntax = _; name; payload } = +let string_of_var { loc = _; name; payload } = let before, after = ("%{", "}") in match payload with | None -> before ^ name ^ after @@ -145,20 +125,9 @@ let remove_locs t = | Text _ as s -> s) } -let dyn_of_var_syntax = - let open Dyn.Encoder in - function - | Dollar_brace -> constr "Dollar_brace" [] - | Dollar_paren -> constr "Dollar_paren" [] - | Percent -> constr "Percent" [] - -let dyn_of_var { loc = _; name; payload; syntax } = +let dyn_of_var { loc = _; name; payload } = let open Dyn.Encoder in - record - [ ("name", string name) - ; ("payload", option string payload) - ; ("syntax", dyn_of_var_syntax syntax) - ] + record [ ("name", string name); ("payload", option string payload) ] let dyn_of_part = let open Dyn.Encoder in diff --git a/src/dune_lang/template.mli b/src/dune_lang/template.mli index 9b3bccb06c0..fe6bd3314d3 100644 --- a/src/dune_lang/template.mli +++ b/src/dune_lang/template.mli @@ -1,15 +1,9 @@ open! Stdune -type var_syntax = - | Dollar_brace - | Dollar_paren - | Percent - type var = { loc : Loc.t ; name : string ; payload : string option - ; syntax : var_syntax } type part = diff --git a/src/jbuild_support/string_with_vars.ml b/src/jbuild_support/string_with_vars.ml index 4027fcd2893..1c101136950 100644 --- a/src/jbuild_support/string_with_vars.ml +++ b/src/jbuild_support/string_with_vars.ml @@ -4,6 +4,17 @@ type var_syntax = | Parens | Braces +type var = + { loc : Loc.t + ; name : string + ; payload : string option + ; syntax : var_syntax + } + +type part = + | Text of string + | Var of var + module Token = struct type t = | String of string @@ -44,7 +55,7 @@ module Token = struct end (* Remark: Consecutive [Text] items are concatenated. *) -let rec of_tokens : Loc.t -> Token.t list -> Dune_lang.Template.part list = +let rec of_tokens : Loc.t -> Token.t list -> part list = fun loc -> function | [] -> [] | Open a :: String s :: Close b :: rest when a = b -> @@ -53,16 +64,7 @@ let rec of_tokens : Loc.t -> Token.t list -> Dune_lang.Template.part list = | None -> (s, None) | Some (n, p) -> (n, Some p) in - Var - { loc - ; name - ; payload - ; syntax = - ( match a with - | Parens -> Dollar_paren - | Braces -> Dollar_brace ) - } - :: of_tokens loc rest + Var { loc; name; payload; syntax = a } :: of_tokens loc rest | token :: rest -> ( let s = Token.to_string token in match of_tokens loc rest with @@ -145,8 +147,17 @@ module Upgrade_var = struct (List.concat [ macros; static_vars; lowercased; uppercased; other ]) end +let string_of_var { loc = _; name; payload; syntax } = + let s = + match payload with + | None -> name + | Some p -> sprintf "%s:%s" name p + in + match syntax with + | Parens -> sprintf "$(%s)" s + | Braces -> sprintf "${%s}" s + let upgrade_to_dune s ~loc ~quoted ~allow_first_dep_var = - let open Dune_lang.Template in let map_var v = match String.Map.find Upgrade_var.map v.name with | None -> None @@ -162,11 +173,11 @@ let upgrade_to_dune s ~loc ~quoted ~allow_first_dep_var = | Renamed_to new_name -> Some new_name ) in let map_part = function - | Text _ as part -> part + | Text s -> Dune_lang.Template.Text s | Var v -> ( match map_var v with | None -> Text (string_of_var v) - | Some name -> Var { v with name; syntax = Percent } ) + | Some name -> Var { name; payload = v.payload; loc = v.loc } ) in let parts = List.map (parse ~loc s) ~f:map_part in { Dune_lang.Template.quoted; parts; loc } diff --git a/test/blackbox-tests/test-cases/output-obj/run.t b/test/blackbox-tests/test-cases/output-obj/run.t index edb0ad8b8a1..45406dfbfff 100644 --- a/test/blackbox-tests/test-cases/output-obj/run.t +++ b/test/blackbox-tests/test-cases/output-obj/run.t @@ -3,10 +3,10 @@ static alias runtest OK: ./static.exe dynamic alias runtest + OK: ./dynamic.exe ./test.bc$ext_dll + dynamic alias runtest OK: ./dynamic.exe ./test$ext_dll static alias runtest OK: ./static.bc - dynamic alias runtest - OK: ./dynamic.exe ./test.bc$ext_dll # static alias runtest # OK: ./static.bc.c.exe diff --git a/test/blackbox-tests/test-cases/tests-stanza/run.t b/test/blackbox-tests/test-cases/tests-stanza/run.t index a501d6eafdd..bfb6e46d72d 100644 --- a/test/blackbox-tests/test-cases/tests-stanza/run.t +++ b/test/blackbox-tests/test-cases/tests-stanza/run.t @@ -12,10 +12,6 @@ ocamldep .expect_test.eobjs/expect_test.ml.d ocamldep .expect_test.eobjs/regular_test.ml.d ocamldep .expect_test.eobjs/regular_test2.ml.d - ocamlc .expect_test.eobjs/byte/expect_test.{cmi,cmo,cmt} - ocamlopt .expect_test.eobjs/native/expect_test.{cmx,o} - ocamlopt expect_test.exe - expect_test expect_test.output ocamlc .expect_test.eobjs/byte/regular_test.{cmi,cmo,cmt} ocamlopt .expect_test.eobjs/native/regular_test.{cmx,o} ocamlopt regular_test.exe @@ -26,6 +22,10 @@ ocamlopt regular_test2.exe regular_test2 alias runtest regular test2 + ocamlc .expect_test.eobjs/byte/expect_test.{cmi,cmo,cmt} + ocamlopt .expect_test.eobjs/native/expect_test.{cmx,o} + ocamlopt expect_test.exe + expect_test expect_test.output $ dune runtest --root generated --display short Entering directory 'generated' ocamldep .generated.eobjs/generated.ml.d diff --git a/test/expect-tests/dune_lang/sexp_tests.ml b/test/expect-tests/dune_lang/sexp_tests.ml index 36744fdde03..a0ae1d73384 100644 --- a/test/expect-tests/dune_lang/sexp_tests.ml +++ b/test/expect-tests/dune_lang/sexp_tests.ml @@ -258,8 +258,7 @@ let tq x = Dune_lang.Template { quoted = true; parts = x; loc } let l x = Dune_lang.List x -let var ?(syntax = Dune_lang.Template.Percent) ?payload name = - { Dune_lang.Template.loc; name; payload; syntax } +let var ?payload name = { Dune_lang.Template.loc; name; payload } type syntax = | Dune