123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158(* Copyright (C) 2023--2024 Petter A. Urkedal <paurkedal@gmail.com>
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version, with the LGPL-3.0 Linking Exception.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* and the LGPL-3.0 Linking Exception along with this library. If not, see
* <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively.
*)type'at=Format.formatter->'a->unittypeFormat.stag+=Stag_queryofQuery.tletqueryppfq=Format.pp_open_stagppf(Stag_queryq);Format.pp_print_stringppf"... SQL FRAGMENT ...";Format.pp_close_stagppf()letquoteppfq=queryppf(Query.quoteq)letenvppfe=queryppf(Query.vare)letparamppfp=queryppf(Query.paramp)typemode=Mode_literal|Mode_ignore|Mode_rawof(string->Query.t)letkqprintfkfmt=(* The formatter can be in three different modes, which is determined
depending on the currently open tags (we exploit the fact that
[mark_open_stag] and [mark_close_tag] are called immediately before the tag
would actually be output to the formatter, and so can we can effectively
use them as control directives for the behavior of [output_string]).
Internally, the formatter holds a list of query elements in the reverse
order that they have been produced in. Once we have finished parsing, that
list is reversed, wrapped into the [S] constructor, and returned. We will
call this list of elements we are building "the queue" in the following.
Initially, the formatter starts in literal mode. In literal mode, we simply
push each string that we receive from the formatter into the queue
unmodified as a literal.
When we encounter a tag in literal mode, we enter one of two auxiliary
modes:
- If the tag is the string tag "Q" or "E" (denoting a nested quote or env
var format, respectively), we enter raw mode, parameterized by a
function that returns either [Q] or [E]. In raw mode, we accumulate the
content that gets printed into a buffer, and when exiting raw mode
(i.e. when the corresponding tag gets closed), we wrap the accumulated
content as a string into either the [Q] or [E] constructor, and push it
into the queue. Then, we exit go back to literal mode.
- If the tag is custom tag [Stag_query], added by the {!query} printer, we
push the embedded query without modifications onto the queue, then
switch to ignore mode. In ignore mode, we simply discard what is printed
until we exit ignore mode by closing the corresponding tag. This is
because, in ignore mode, the content that we are interested in is the
parameter of the [Stag_query], not the textual content within the tag.
Nesting of tags is not supported, so if we see one of the tags above in
either of these modes, we raise a [Failure] exception.
*)letelems=ref[]inletbuf=Buffer.create512inletpushq=elems:=q::!elemsinletmode=refMode_literalinletflush_raw()=letmk=match!modewithMode_rawf->f|_->assertfalseinpush@@mk@@Buffer.contentsbuf;Buffer.resetbuf;mode:=Mode_literalandflush_ignore()=match!modewithMode_ignore->mode:=Mode_literal|_->assertfalseandflush_literal()=(* Unlike the similar cases in [flush_raw] and [flush_ignore], it is actually
possible to call [flush_literal] while not in literal mode by nesting <Q>
and/or <E> semantic tags.
There is actually nothing to do here, because we push output strings into
the queue on the fly. *)beginmatch!modewith|Mode_literal->()|_->failwith"invalid nesting of query tags; did you forget a `@}`?"endandoutput_stringspn=match!modewith|Mode_literal->ifn>0thenifp=0&&n=String.lengthsthenpush(Query.lits)elsepush(Query.lit(String.subspn))|Mode_raw_->Buffer.add_substringbufspn|Mode_ignore->()inletppf=Format.make_formatteroutput_stringflush_literalinletFormat.{mark_open_stag;mark_close_stag;print_open_stag;print_close_stag}=Format.pp_get_formatter_stag_functionsppf()in(* Note that we call [flush_literal] when *opening* tags but the other [flush]
functions when *closing* tags. Since [Format] enforces the
well-parenthesising of tags, we are guaranteed to always be in the correct
mode when calling [flush_raw] and [flush_ignore], but that is *not* the
case for [flush_litera].
*)letmark_open_stag=function|Format.String_tag"Q"->flush_literal();mode:=Mode_raw(funs->Query.quotes);""|Format.String_tag"E"->flush_literal();mode:=Mode_raw(funs->Query.vars);""|Stag_queryq->flush_literal();pushq;mode:=Mode_ignore;""|t->mark_open_stagtandmark_close_stag=function|Format.String_tag("Q"|"E")->flush_raw();""|Stag_query_->flush_ignore();""|t->mark_close_stagtinFormat.pp_set_formatter_stag_functionsppf{mark_open_stag;mark_close_stag;print_open_stag;print_close_stag};Format.pp_set_mark_tagsppftrue;Format.kfprintf(funppf->Format.pp_print_flushppf();k(Query.concat(List.rev!elems)))ppffmtletqprintffmt=kqprintfFun.idfmtletboolppfx=queryppf(Query.boolx)letintppfx=queryppf(Query.intx)letfloatppfx=queryppf(Query.floatx)letstringppfx=queryppf(Query.stringx)letoctetsppfx=queryppf(Query.octetsx)letpdateppfx=queryppf(Query.pdatex)letptimeppfx=queryppf(Query.ptimex)letptime_spanppfx=queryppf(Query.ptime_spanx)