Skip to content

Commit

Permalink
Remove some vestigial jbuilder code related to variable syntax (#3274)
Browse files Browse the repository at this point in the history
Signed-off-by: Jeremie Dimino <jeremie@dimino.org>
  • Loading branch information
jeremiedimino authored Mar 18, 2020
1 parent 70c1dee commit acbf560
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 82 deletions.
26 changes: 9 additions & 17 deletions src/dune/string_with_vars.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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 }

Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/dune_lang/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ and template_variable = parse
}
; name
; payload
; syntax = Percent
}
}
| '}' | eof
Expand Down
41 changes: 5 additions & 36 deletions src/dune_lang/template.ml
Original file line number Diff line number Diff line change
@@ -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 =
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions src/dune_lang/template.mli
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down
39 changes: 25 additions & 14 deletions src/jbuild_support/string_with_vars.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 }
4 changes: 2 additions & 2 deletions test/blackbox-tests/test-cases/output-obj/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions test/blackbox-tests/test-cases/tests-stanza/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions test/expect-tests/dune_lang/sexp_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit acbf560

Please sign in to comment.