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.
This commit is contained in:
Christophe Troestler 2018-01-29 00:14:42 +01:00
parent 3e2dc2517d
commit e8e3698e15
3 changed files with 13 additions and 8 deletions

View File

@ -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)

View File

@ -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

View File

@ -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