How to insert code verbatim into SWIG interface? - common-lisp

I need to insert code into the header of the generated file, but the inserted code is written in Lisp. Since SWIG tries to parse it, it breaks. I need something analogous to %pythoncode command, but for CFFI (Lisp).
#ifdef SWIGCFFI
%begin
%{
(in-package :some-package)
%}
#endif /* SWIGCFFI */
Also, looking at this: https://github.com/swig/swig/blob/master/Source/Modules/cffi.cxx#L140 am I right to assume that the contents of %begin will be thrown away unless CPlusPlus || CWrap is true? (but I don't need C wrapper and the API is for C, not C++)?

Answering my onw question:
%insert("lisphead") %{
(in-package :some-package)
%}
This appears to be the way to do it. Thanks this blog post: http://www.bnikolic.co.uk/blog/cpp-swig-insert-directive.html

Related

Custom Injections in Neovim Treesitter with Tagged Template Literals

I'm trying to write a custom injection for Neovim's Treesitter that will highlight tagged template literals as SQL code.
The code in question looks like this:
import sql from "postgres"
const query = sql` SELECT * FROM my_table`
I'd like to tell Treesitter to parse this tagged template literal as SQL code. This is possible using Treesitter's custom injections; in fact the library uses this same technique to inject syntax for GraphQL tagged template literals, which I believe happens in this file here.
I've written a configuration file at ~/.config/nvim/after/queries/ecma/injections.scm that's quite similar to the GraphQL query, and although Treesitter is recognizing the file, the injection isn't working at all. Does anyone have a solution to this? Ideally, Treesitter would see the sql tagged template literal and interpret the body of the function with the SQL parser.
to make tree sitter use injections in your after/ folder, your injections.scm file should contain ; extends as first line.
Took me many hours to figure it out, it's now documented right here
Similar to what #brotify-force said, but my own SQL injection for Python was not working until I realized what the issue was.
~/.config/nvim/after/queries/python/injections.scm
; extends
(assignment
left: (identifier) #_id (#match? #_id "sql")
right: (string) #sql
)
My original query used #id, but it seems like if you have any captures not starting with an underscore besides the language capture, the Neovim Treesitter machinery gets confused.
I've been trying to do something similar but for template strings that just happen to contain SQL. I was inspired by a VSCode plugin that syntax highlights any template string that has a comment like /*sql*/ in front of it. Here's what I came up with.
; extends
(
(comment) #_comment-text
(#match? #_comment-text "sql")
(template_string) #ql
)
I do wish there was a proper sql module for tree-sitter, and it looks like one is in the works, but the ql module will do for now.

Meteor how to save templates in mongo

I want to give my users the possibility to create document templates (contracts, emails, etc.)
The best option I figured out would be to store these document templates in mongo (maybe I'm wrong...)
I've been searching for a couple of hours now but I can't figure out how to render these document template with their data context.
Example:
Template stored in Mongo: "Dear {{firstname}}"
data context: {firstname: "Tom"}
On Tom's website, He should read: "Dear Tom"
How can I do this?
EDIT
After some researches, I discovered a package called spacebars-compiler that brings the option to compile to the client:
meteor add spacebars-compiler
I then tried something like this:
Template.doctypesList.rendered = ->
content = "<div>" + this.data.content + "</div>"
template = Spacebars.compile content
rendered = UI.dynamic(template,{name:"nicolas"})
UI.insert(rendered, $(this).closest(".widget-body"))
but it doesn't work.
the template gets compiled but then, I don't know how to interpret it with its data context and to send it back to the web page.
EDIT 2
I'm getting closer thanks to Tom.
This is what I did:
Template.doctypesList.rendered = ->
content = this.data.content
console.log content
templateName = "template_#{this.data._id}"
Template.__define__(templateName, () -> content)
rendered = UI.renderWithData(eval("Template.#{templateName}"),{name:"nicolas"})
UI.insert(rendered, $("#content_" + this.data._id).get(0))
This works excepted the fact that the name is not injected into the template. UI.renderWithData renders the template but without the data context...
The thing your are missing is the call to (undocumented!) Template.__define__ which requires the template name (pick something unique and clever) as the first argument and the render function which you get from your space bars compiler. When it is done you can use {{> UI.dynamic}} as #Slava suggested.
There is also another way to do it, by using UI.Component API, but I guess it's pretty unstable at the moment, so maybe I will skip this, at least for now.
Use UI.dynamic: https://www.discovermeteor.com/blog/blaze-dynamic-template-includes/
It is fairly new and didn't make its way to docs for some reason.
There are few ways to achieve what you want, but I would do it like this:
You're probably already using underscore.js, if not Meteor has core package for it.
You could use underscore templates (http://underscorejs.org/#template) like this:
var templateString = 'Dear <%= firstname %>'
and later compile it using
_.template(templateString, {firstname: "Tom"})
to get Dear Tom.
Of course you can store templateString in MongoDB in the meantime.
You can set delimiters to whatever you want, <%= %> is just the default.
Compiled template is essentially htmljs notation Meteor uses (or so I suppose) and it uses Template.template_name.lookup to render correct data. Check in console if Template.template_name.lookup("data_helper")() returns the correct data.
I recently had to solve this exact (or similar) problem of compiling templates client side. You need to make sure the order of things is like this:
Compiled template is present on client
Template data is present (verify with Template.template_name.lookup("data_name")() )
Render the template on page now
To compile the template, as #apendua have suggested, use (this is how I use it and it works for me)
Template.__define__(name, eval(Spacebars.compile(
newHtml, {
isTemplate: true,
sourceName: 'Template "' + name + '"'
}
)));
After this you need to make sure the data you want to render in template is available before you actually render the template on page. This is what I use for rendering template on page:
UI.DomRange.insert(UI.render(Template.template_name).dom, document.body);
Although my use case for rendering templates client side is somewhat different (my task was to live update the changed template overriding meteor's hot code push), but this worked best among different methods of rendering the template.
You can check my very early stage package which does this here: https://github.com/channikhabra/meteor-live-update/blob/master/js/live-update.js
I am fairly new to real-world programming so my code might be ugly, but may be it'll give you some pointers to solve your problem. (If you find me doing something stupid in there, or see something which is better done some other way, please feel free to drop a comment. That's the only way I get feedback for improvement as I am new and essentially code alone sitting in my dark corner).

Is it possible to declare a default associative array in Smarty?

In Smarty, I know you can declare a string:
{$somevar|default:'some string'}
or even an array:
{$somevar|default:array('someval')}
How do you/Is it possible to set an associative array as a default value? as this doesn't seem to work:
{$somevar|default:array('default'=>array('subkey'=>'subval'))}
I just tried:
{$somevar|default:array('key'=>'val')}
It's the '=>' smarty doesn't like
I know its probably not the solution you're looking for, but you can always just use the {php} feature. However, I will try a few things and see if I can work the format out.
Just out of interest, why are you trying to do this in the tpl file and not in the calling PHP script?
Edit
From takeing a read, it doesn't seem like it is possible. However, there is a "set" plugin which allows it, see here (bottom example).

How to work with hook_nodeapi after image thumbnail creation with ImageCache

A bit of a followup from a previous question.
As I mentioned in that question, my overall goal is to call a Ruby script after ImageCache does its magic with generating thumbnails and whatnot.
Sebi's suggestion from this question involved using hook_nodeapi.
Sadly, my Drupal knowledge of creating modules and/or hacking into existing modules is pretty limited.
So, for this question:
Should I create my own module or attempt to modify the ImageCache module?
How do I go about getting the generated thumbnail path (from ImageCache) to pass into my Ruby script?
edit
I found this question searching through SO...
Is it possible to do something similar in the _imagecache_cache function that would do what I want?
ie
function _imagecache_cache($presetname, $path) {
...
...
// check if deriv exists... (file was created between apaches request handler and reaching this code)
// otherwise try to create the derivative.
if (file_exists($dst) || imagecache_build_derivative($preset['actions'], $src, $dst)) {
imagecache_transfer($dst);
// call ruby script here
call('MY RUBY SCRIPT');
}
Don't hack into imagecache, remember every time you hack core/contrib modules god kills a kitten ;)
You should create a module that invokes hook_nodeapi, look at the api documentation to find the correct entry point for your script, nodeapi works on various different levels of the node process so you have to pick the correct one for you (it should become clear when you check the link out) http://api.drupal.org/api/function/hook_nodeapi
You won't be able to call the function you've shown because it is private so you'll have to find another route.
You could try and build the path up manually, you should be able to pull out the filename of the uploaded file and then append it to the directory structure, ugly but it should work. e.g.
If the uploaded file is called test123.jpg then it should be in /files/imagecache/thumbnails/test123/jpg (or something similar).
Hope it helps.

Is this code written in Pl/Sql, and if not, then what language is it?

I have encountered this code... Is this Pl/Sql? What do you think it is?
[Script 1.0]
script package up is
import native def_1;
procedure p(
i_g text
)
is
l_txt text;
begin
with mem_m(idx) as msg do
with book_aud(evt_id) as book do
book.upd_pkt(
evt_nr => i__nr
,ref_nr => msg.h.id
,account_nr => msg.h.id
,status => '1'
);
end with;
end with;
end p;
I am surprised by import and end with;
It is not the full code. It is reduced version of it.
It also contained familiar elements such as:
c_max constant number := 95;
c_VE_BA constant text := 'A07000';
-- comment
if i_mt is null then
return rpad('/',16);
else
if i_id = zconst_.c_JPY then
l_fmt := '9999999999999999';
else
l_fmt := '9999999999999D99';
end if;
end if;
case i_typ_id
when def_typ.contr then
l_zuonr := zfx2.c_avqt;
when def_typ.fx then
l_zuonr := zfx2.c_avqd;
when def_typ.fxswap then
l_zuonr := zfx2.c_avqd;
when def_typ.forex then
l_zuonr := zfx2.c_avqd;
when def_typ.xfer then
l_zuonr := zfx2.c_avqd;
when def_typ.intr then
l_zuonr := zfx2.c_avqt;
else
assert(false,'Meta Typ');
end case;
It looks like an extension of Pl/Sql.
Based on the responses and my own research, I guess it is Avaloq+PL/Sql.
I contacted Avaloq,I am still waiting for official answer.
It looks like Avaloq Script, used by (ahem) Swiss banks, and while there is very little about it online, I found a grammar which perfectly matches the terms in your samples.
Avaloq Script, the Avaloq Banking
System's script language, facilitates
entering specific business logic. The
structure of the data that can be
accessed through Avaloq Script is
defined in a DDIC (Data Dictionary),
making it unnecessary to know the data
storage structure.
yes it is avaloq script. its some kind of pl/sql pre compiler, you should be able to find a package called s#up where the real pl/sql code resides.
It definitely is Avaloq Script. The code snippet is a script package that Avaloq compiler compiles into PL/SQL. The point of Avaloq Script is to disallow direct database access and make the customizer of the Avaloq product to use the Avaloq API instead. The API is the Avaloq script language and a whole array of other ways like setting up rule tables to be loaded or special syntax to define forms, reports, workflows etc. often allowing snippets of Avaloq script within that other kind of sources.
Avaloq script has many PL/SQL elements but also some VB language concepts can be found. Here are some comments in the code to give some idea what the code means.
[Script 1.0] -- Have not seen other than 1.0 version
script package up is -- The PL/SQL package name is going to be s#up
import native def_1; -- import native means a PL/SQL package named
-- def_1 can be used, without native it is
-- another Avaloq script package
procedure p( -- declares a procedure with the name "p"
i_g text -- input variable i_g defined text.
-- in PL/SQL this becomes a VARCHAR2
)
is
l_txt text; -- local variable VARCHAR2(4000) in PL/SQL
begin
with mem_m(idx) as msg do -- mem_m is a DDIC (Data Dictionary)
-- It actually is a kind of "class" with
-- fields and methods
-- "with" is like in VB to avoid writing
-- mem_m(idx) all the time e.g. mem_m(idx).h.id
with book_aud(evt_id) as book do -- book_aud is another DDIC that it is not
-- prefixed with mem implies this is not a
-- in memory structure but direct access
-- to a Oracle table book_aud with index
-- evt_id which looks undefined to me and
-- should bring a compiler error
book.upd_pkt( -- method call in the book_aud DDIC
evt_nr => i__nr -- like in PL/SQL named parameters
,ref_nr => msg.h.id
,account_nr => msg.h.id
,status => '1'
);
end with;
end with;
end p;
I could also comment on the other code snippet above but I think you already get out the general concept. Neither mem_m nor book_aud is a known DDIC in the Avaloq version I am working with, wonder where you got it from. Since your post is many years old I suppose this was a very old Avaloq release.
I'm sure that isn't PL/SQL.
I know this doesn't directly answer your question but I might suggest that you go though the list here. It might be listed in here. There are several examples of programs in different programming languages. It may be hard to 100% identify the language unless someone happens to recognize it and finds a "finger print" to prove the language... Do you have more examples you could post?
http://www.ntecs.de/old-hp/uu9r/lang/html/lang.en.html
I don't think that is a functional language. Knowing this might help narrow your search.
The only language I can think of offhand that has "with...end with" syntax is visual basic. Could this be some scripting form of VB?

Resources