package embedded_ocaml_templates

  1. Overview
  2. Docs
EML is a simple templating language that lets you generate text with plain OCaml

Install

Dune Dependency

Authors

Maintainers

Sources

embedded_ocaml_templates-0.8.tbz
sha256=c2ff640ab8123e5403f2b6ee480a07603ed2f1a8a97432f40d9ddec38d315321
sha512=9159e0f62f0aa1d7fb566c1b057f73ce5aba7c749af3af03ecf9c23591256d19dd664deb11c067a2514cafa2fe483125d95a65c2b2342a84d78447c6205a72c3

doc/src/ppx_eml/ppx_eml.ml.html

Source file ppx_eml.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
module Parser_ = Parser
open Ppxlib

(* Not present before 4.11 *)
let set_position lexbuf position =
  Lexing.(
    lexbuf.lex_curr_p <- {position with pos_fname= lexbuf.lex_curr_p.pos_fname} ;
    lexbuf.lex_abs_pos <- position.pos_cnum )

let name = "eml"

let expand ~loc:_ ~path:_ (s : string) loc _delim =
  let startpos = loc.loc_start in
  (* let startpos = {loc.loc_start with pos_lnum= loc.loc_start.pos_lnum - 1} in *)
  match Common.Template_builder.of_string ~startpos s with
  | Error e ->
      Common.Lexer.pp_error Format.err_formatter e ;
      exit 1
  | Ok template ->
      let code = Common.Compile.compile_to_string template in
      (* print_endline code ; *)
      let buffer = Lexing.from_string code in
      set_position buffer loc.loc_start ;
      Parser_.parse_expression Lexer.token buffer
      |> Selected_ast.Of_ocaml.copy_expression

let ext =
  Extension.declare name Extension.Context.expression
    Ast_pattern.(single_expr_payload (pexp_constant (pconst_string __ __ __)))
    expand

let () = Ppxlib.Driver.register_transformation name ~extensions:[ext]
OCaml

Innovation. Community. Security.