package pla
Pla is a simple library and ppx syntax extension to create composable templates based on verbatim strings
Install
Dune Dependency
Authors
Maintainers
Sources
v2.1.tar.gz
md5=48d8a9c4f3476375bcc685bf23864d62
sha512=f2b59d3563c59a320b1410417e8aa68c325bb63f2a5dd9488e8973b663590a2bbe57d5642eee9a933a862471fa826cb16b21f1b139023910fa55c5b8a1200a5b
doc/src/pla_ppx/pla_ppx.ml.html
Source file pla_ppx.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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
(* The MIT License (MIT) Copyright (c) 2016 Leonardo Laguna Ruiz Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *) open Ppxlib module Pla_tokens = Pla__.Pla_tokens module Pla_lex = Pla__.Pla_lex module PlaMapper = struct open Ast_helper let buffer_id = "__buffer__" let makeLident (str : string) : Longident.t Location.loc = Longident.parse str |> Ocaml_common.Location.mknoloc let buffer = Exp.ident (makeLident buffer_id) let newline = Exp.ident (makeLident "Pla.buffer_newline") let indent = Exp.ident (makeLident "Pla.buffer_indent") let outdent = Exp.ident (makeLident "Pla.buffer_outdent") let append = Exp.ident (makeLident "Pla.buffer_append") let papply = Exp.ident (makeLident "Pla.buffer_apply") let pint = Exp.ident (makeLident "Pla.int") let pfloat = Exp.ident (makeLident "Pla.float") let pstring = Exp.ident (makeLident "Pla.string") let unit = Exp.construct (makeLident "()") None let offsetPosition (displacement : int) (pos1 : Lexing.position) (pos2 : Lexing.position) : Lexing.position = Lexing. { pos1 with pos_lnum = pos1.pos_lnum + pos2.pos_lnum - 1 ; pos_bol = pos1.pos_bol + pos2.pos_bol ; pos_cnum = pos1.pos_cnum + pos2.pos_cnum + displacement } ;; let offsetLocation (displacement : int) (loc1 : Location.t) (loc2 : Location.t) : Location.t = Location. { loc1 with loc_start = offsetPosition displacement loc1.loc_start loc2.loc_start ; loc_end = offsetPosition displacement loc1.loc_start loc2.loc_end } ;; let mkVar (var_loc : Location.t) (v : string) = Ocaml_common.Location.mkloc (Longident.parse v) var_loc let template_type (t : Pla_tokens.vartype) = match t with | Pla_tokens.Int -> Typ.constr (makeLident "int") [] | Pla_tokens.Float -> Typ.constr (makeLident "float") [] | Pla_tokens.String -> Typ.constr (makeLident "string") [] | Pla_tokens.Template -> Typ.constr (makeLident "Pla.t") [] ;; let constString s = Const.string s let makeExp (loc : Location.t) (displacement : int) (s : Pla_tokens.s) = match s with | Pla_tokens.N -> Exp.apply ~loc newline [ Nolabel, buffer ] | Pla_tokens.I -> Exp.apply ~loc indent [ Nolabel, buffer ] | Pla_tokens.O -> Exp.apply ~loc outdent [ Nolabel, buffer ] | Pla_tokens.T txt -> Exp.apply ~loc append [ Nolabel, buffer; Nolabel, Exp.constant (constString txt) ] | Pla_tokens.V (v, vartype, loc_ref) -> let var_loc = offsetLocation displacement loc loc_ref in let v_exp = Exp.constraint_ ~loc:var_loc (Exp.ident ~loc:var_loc (mkVar var_loc v)) (template_type vartype) in (match vartype with | Pla_tokens.Template -> Exp.apply ~loc papply [ Nolabel, v_exp; Nolabel, buffer ] | Pla_tokens.Int -> Exp.apply ~loc papply [ Nolabel, Exp.apply ~loc pint [ Nolabel, v_exp ]; Nolabel, buffer ] | Pla_tokens.Float -> Exp.apply ~loc papply [ Nolabel, Exp.apply ~loc pfloat [ Nolabel, v_exp ]; Nolabel, buffer ] | Pla_tokens.String -> Exp.apply ~loc papply [ Nolabel, Exp.apply ~loc pstring [ Nolabel, v_exp ]; Nolabel, buffer ]) ;; let makeExpSeq (loc : Location.t) (displacement : int) (sl : Pla_tokens.s list) = List.fold_right (fun a s -> Exp.sequence (makeExp loc displacement a) s) sl unit ;; let makeTemplateExp (loc : Location.t) (displacement : int) (sl : Pla_tokens.s list) : expression = let pat = Pat.var (Ocaml_common.Location.mknoloc buffer_id) in let fun_ = Exp.fun_ ~loc Nolabel None pat (makeExpSeq loc displacement sl) in Exp.apply ~loc (Exp.ident (makeLident "Pla.make")) [ Nolabel, fun_ ] ;; let mapper ~loc ~path (expr : expression) : expression = let _ = loc in let _ = path in match expr with | { pexp_desc = Pexp_constant (Pconst_string (text, loc, Some "pla")); _ } -> let tokens = Pla_lex.tokenize text in let displacement = 5 in let pla_exp = makeTemplateExp loc displacement tokens in pla_exp | { pexp_desc = Pexp_constant (Pconst_string (text, loc, _)); _ } -> let tokens = Pla_lex.tokenize text in let displacement = 2 in let pla_exp = makeTemplateExp loc displacement tokens in pla_exp | _ -> expr ;; end let pla_ext = Extension.declare "pla" Ppxlib.Extension.Context.Expression Ast_pattern.(single_expr_payload __) PlaMapper.mapper ;; let pla_rule = Ppxlib.Context_free.Rule.extension pla_ext let () = Driver.register_transformation ~rules:[ pla_rule ] "pla"
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>