From e8e3698e154eaecb1a16863484c2a4f843c5d3f2 Mon Sep 17 00:00:00 2001 From: Christophe Troestler Date: Mon, 29 Jan 2018 00:14:42 +0100 Subject: [PATCH] String_with_vars: represent quoted vars differently from unquoted ones Define the representation for quoted variables, adapt the test of strings made of a single variable, and add a constructor. [String_with_vars.t] is not yet able to use that representation because the necessary information is not available from the parser. --- src/action.ml | 4 ++-- src/string_with_vars.ml | 6 +++++- src/string_with_vars.mli | 11 ++++++----- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/action.ml b/src/action.ml index 00fe164e..22b5bb96 100644 --- a/src/action.ml +++ b/src/action.ml @@ -363,7 +363,7 @@ module Unexpanded = struct let expand ~generic ~special ~map ~dir ~f = function | Inl x -> map x | Inr template as x -> - match SW.just_a_var template with + match SW.unquoted_var template with | None -> generic ~dir (string ~dir ~f x) | Some var -> match f (SW.loc template) var with @@ -452,7 +452,7 @@ module Unexpanded = struct | Some e -> Some (VE.to_string ~dir e)) let expand ~generic ~special ~dir ~f template = - match SW.just_a_var template with + match SW.unquoted_var template with | None -> begin match string ~dir ~f template with | Inl x -> Inl (generic ~dir x) diff --git a/src/string_with_vars.ml b/src/string_with_vars.ml index 0ce1353d..8e8f73d7 100644 --- a/src/string_with_vars.ml +++ b/src/string_with_vars.ml @@ -6,6 +6,8 @@ type item = | Text of string | Var of var_syntax * string +(* A single unquoted variable is encoded as the list [Var v]. A + quoted variable is encoded as [Var v; Text ""]. *) type t = { items : item list ; loc : Loc.t @@ -67,9 +69,11 @@ let loc t = t.loc let virt pos s = of_string ~loc:(Loc.of_pos pos) s let virt_var pos s = { loc = Loc.of_pos pos; items = [Var (Braces, s)] } +let virt_quoted_var pos s = { loc = Loc.of_pos pos; + items = [Var (Braces, s); Text ""] } let virt_text pos s = { loc = Loc.of_pos pos; items = [Text s] } -let just_a_var t = +let unquoted_var t = match t.items with | [Var (_, s)] -> Some s | _ -> None diff --git a/src/string_with_vars.mli b/src/string_with_vars.mli index de02a0bc..ed22a78f 100644 --- a/src/string_with_vars.mli +++ b/src/string_with_vars.mli @@ -21,12 +21,13 @@ val to_string : t -> string (** [t] generated by the OCaml code. The first argument should be [__POS__]. The second is either a string to parse, a variable name or plain text. *) -val virt : (string * int * int * int) -> string -> t -val virt_var : (string * int * int * int) -> string -> t -val virt_text : (string * int * int * int) -> string -> t +val virt : (string * int * int * int) -> string -> t +val virt_var : (string * int * int * int) -> string -> t +val virt_quoted_var : (string * int * int * int) -> string -> t +val virt_text : (string * int * int * int) -> string -> t -val just_a_var : t -> string option -(** [just_a_var t] return the [Some name] where [name] is the name of +val unquoted_var : t -> string option +(** [unquoted_var t] return the [Some name] where [name] is the name of the variable if [t] is solely made of a variable and [None] otherwise. *) val vars : t -> String_set.t