Given this type:
type 'a variable = { name: string; mutable value: 'a }
I'm trying to create a syntax extension that would accept this syntax:
var foo = true
...and convert it to:
let foo = { name = "foo"; value = true }
Here's my attempt:
open Camlp4.PreCast
open Syntax
type 'a variable = { name: string; mutable value: 'a }
EXTEND Gram
expr: LEVEL "top"
[ [ "var"; v = a_LIDENT; "="; e = expr ->
<:expr< let $lid:v$ = { name= $`str:v$ ; value = $e$ } in $e$ >>
] ]
;
END
(I'm pretty sure it needs that $e$ at the end of the substitution as a way of saying "the rest", but it also looks kind of suspect given that we want the value field of the record to have the value of the expression on the right - initially I did not have the ending $e$ there and got the same error)
I try compiling with:
ocamlc -I +camlp4 camlp4lib.cma -pp camlp4orf -c pa_var.ml
Which results in:
File "pa_var.ml", line 10, characters 50-51:
While expanding quotation "expr" in a position of "expr":
Parse error: "}" expected after [label_expr_list] (in [expr])
File "pa_var.ml", line 1, characters 0-1:
Error: Preprocessor error
I do not know know why it seems to want to have a "}" after the name field of the record. (Otherwise, am I on the right track here?)