OCaml toplevel with syntax extensions - syntax

I don't know how to accomplish this in general, but I'll ask about one instance in particular for clarity:
Sexplib looks interesting to me. I want to play around with it. I've downloaded it, installed it just fine (I'm pretty sure, anyway), etc. I want to use the "with sexp" syntax extension in a toplevel. How would I go about doing this? All the examples I've found of its use assume you already know how to make the toplevel and/or compile with the syntax extensions.
My best shot at it was something like this:
ocamlmktop -I +site-lib/sexplib -pp "camlp4 -I +site-lib/sexplib pa_sexp_conv.cma" -o sexplib-top
When I run this toplevel, I can open Sexplib just fine, but when I try using the with sexp syntax extension, I get a syntax error.

It is XXI century already - use ocamlfind :
Objective Caml version 3.11.2
# #use "topfind";;
- : unit = ()
# #camlp4o;;
/usr/lib/ocaml/dynlink.cma: loaded
/usr/lib/ocaml/camlp4: added to search path
/usr/lib/ocaml/camlp4/camlp4o.cma: loaded
Camlp4 Parsing version 3.11.2
# #require "sexplib.syntax";;
/usr/lib/ocaml/unix.cma: loaded
/usr/lib/ocaml/bigarray.cma: loaded
/usr/lib/ocaml/nums.cma: loaded
/usr/lib/ocaml/num-top: added to search path
/usr/lib/ocaml/num-top/num_top.cma: loaded
/usr/lib/ocaml/sexplib: added to search path
/usr/lib/ocaml/sexplib/sexplib.cma: loaded
/usr/lib/ocaml/type-conv: added to search path
/usr/lib/ocaml/type-conv/pa_type_conv.cmo: loaded
/usr/lib/ocaml/sexplib/pa_sexp_conv.cmo: loaded
# type t = { x : int; y : float; } with sexp;;
type t = { x : int; y : float; }
val t_of_sexp__ : Sexplib.Sexp.t -> t = <fun>
val t_of_sexp : Sexplib.Sexp.t -> t = <fun>
val sexp_of_t : t -> Sexplib.Sexp.t = <fun>

Related

Declare additional dependency to sphinx-build in an extension

TL,DR: From a Sphinx extension, how do I tell sphinx-build to treat an additional file as a dependency? In my immediate use case, this is the extension's source code, but the question could equally apply to some auxiliary file used by the extension.
I'm generating documentation with Sphinx using a custom extension. I'm using sphinx-build to build the documentation. For example, I use this command to generate the HTML (this is the command in the makefile generated by sphinx-quickstart):
sphinx-build -b html -d _build/doctrees . _build/html
Since my custom extension is maintained together with the source of the documentation, I want sphinx-build to treat it as a dependency of the generated HTML (and LaTeX, etc.). So whenever I change my extension's source code, I want sphinx-build to regenerate the output.
How do I tell sphinx-build to treat an additional file as a dependency? That is not mentioned in the toctree, since it isn't part of the source. Logically, this should be something I do from my extension's setup function.
Sample extension (my_extension.py):
from docutils import nodes
from docutils.parsers.rst import Directive
class Foo(Directive):
def run(self):
node = nodes.paragraph(text='Hello world\n')
return [node]
def setup(app):
app.add_directive('foo', Foo)
Sample source (index.rst):
.. toctree::
:maxdepth: 2
.. foo::
Sample conf.py (basically the output of sphinx-quickstart plus my extension):
import sys
import os
sys.path.insert(0, os.path.abspath('.'))
extensions = ['my_extension']
templates_path = ['_templates']
source_suffix = '.rst'
master_doc = 'index'
project = 'Hello directive'
copyright = '2019, Gilles'
author = 'Gilles'
version = '1'
release = '1'
language = None
exclude_patterns = ['_build']
pygments_style = 'sphinx'
todo_include_todos = False
html_theme = 'alabaster'
html_static_path = ['_static']
htmlhelp_basename = 'Hellodirectivedoc'
latex_elements = {
}
latex_documents = [
(master_doc, 'Hellodirective.tex', 'Hello directive Documentation',
'Gilles', 'manual'),
]
man_pages = [
(master_doc, 'hellodirective', 'Hello directive Documentation',
[author], 1)
]
texinfo_documents = [
(master_doc, 'Hellodirective', 'Hello directive Documentation',
author, 'Hellodirective', 'One line description of project.',
'Miscellaneous'),
]
Validation of a solution:
Run make html (or sphinx-build as above).
Modify my_extension.py to replace Hello world by Hello again.
Run make html again.
The generated HTML (_build/html/index.html) must now contain Hello again instead of Hello world.
It looks like the note_dependency method in the build environment API should do what I want. But when should I call it? I tried various events but none seemed to hit the environment object in the right state. What did work was to call it from a directive.
import os
from docutils import nodes
from docutils.parsers.rst import Directive
import sphinx.application
class Foo(Directive):
def run(self):
self.state.document.settings.env.note_dependency(__file__)
node = nodes.paragraph(text='Hello done\n')
return [node]
def setup(app):
app.add_directive('foo', Foo)
If a document contains at least one foo directive, it'll get marked as stale when the extension that introduces this directive changes. This makes sense, although it could get tedious if an extension adds many directives or makes different changes. I don't know if there's a better way.
Inspired by Luc Van Oostenryck's autodoc-C.
As far as I know app.env.note_dependency can be called within the doctree-read to add any file as a dependency to the document currently being read.
So in your use case, I assume this would work:
from typing import Any, Dict
from sphinx.application import Sphinx
import docutils.nodes as nodes
def doctree-read(app: Sphinx, doctree: nodes.document):
app.env.note_dependency(file)
def setup(app: Sphinx):
app.connect("doctree-read", doctree-read)

How can I use erl_lint to syntax check an Erlang module?

I'm trying to use erl_lint() to build a simple Erlang syntax and style checker. I've gotten far enough to load the file and parse it into Forms and to get erl_lint to partially understand it, but then erl_lint complains about undefined functions that are defined. What am I doing wrong?
erlint.erl :
-module(erlint).
-export([lint/1]).
% based on http://stackoverflow.com/a/28086396/13675
lint(File) ->
{ok, B} = file:read_file(File),
Forms = scan(erl_scan:tokens([],binary_to_list(B),1),[]),
F = fun(X) -> {ok,Y} = erl_parse:parse_form(X), Y end,
erl_lint:module([F(X) || X <- Forms],File).
scan({done,{ok,T,N},S},Res) ->
scan(erl_scan:tokens([],S,N),[T|Res]);
scan(_,Res) ->
lists:reverse(Res).
hello.erl :
-module(hello).
-export([hello_world/0]).
hello_world() -> io:fwrite("hello, world\n").
attempt to use :
1> c(erlint).
{ok,erlint}
2> erlint:lint("hello.erl").
{error,[{"hello.erl",
[{2,erl_lint,{undefined_function,{hello_world,0}}}]}],
[]}
I'm not sure this approach fits with your overall plan, but you could instead compile the input file, extract its abstract forms from the resulting beam, and pass them to erl_lint:
-module(erlint).
-export([lint/1]).
lint(File) ->
{ok,_,Bin} = compile:file(File,[debug_info,binary]),
{ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks(Bin,[abstract_code]),
erl_lint:module(AC,File).
Let's change your hello.erl to include an unused variable:
-module(hello).
-export([hello_world/0]).
hello_world() ->
X = io:fwrite("hello, world\n").
We see that this version of erlint:lint/1 correctly reports it:
1> erlint:lint("hello.erl").
{ok,[{"hello.erl",[{5,erl_lint,{unused_var,'X'}}]}]}
If you need them for your overall purposes, note that you can retrieve source code forms from the abstract forms variable AC by calling erl_syntax:form_list(AC).

Why cannot do I get syntax errors when trying to define functions in Erlang?

I am try to learn Erlang. I've installed a runtime but cannot get it working. The following code:
X = 3.
works, but none of the following statements work:
f(X)->X.
F() ->0.
F([])->[].
I get back 1: syntax error before: '->'. I tried the word_count from this tutorial. And I get the same error.
What is wrong here?
In REPL you have to use fun(...) -> ... end:
1> F = fun(X) -> X end.
#Fun<erl_eval.6.80484245>
2> F(42).
42
If you have code in file use c command:
1> c(word_count).
{ok,word_count}
2> word_count:word_count([]).
0
There is difference in sytax when writing functions in Erlang module and Erlang shell (REPL). As P_A mentioned you need to call as F = fun(X) -> X end, F("Echo").
Also note that function names are atoms so has to start with lowercase when you are writing in Erlang module. If you are serious about learning Erlang I would suggest you go through this.
You mentioned that you worked on F#. The basic difference between F# and Erlang in this case is that expression
let Lilo = [|5; 3; -3; 0; 0.5|];; Can be written directly in the file and executed. In Erlang it can only be done in Erlang shell and not inside a file.
So the expression you are trying should be inside a function inside a module with the same name as file. Consider test.erl file. Any function you export can be called from outside (shell).
-module(test).
-export([test/0]).
test() ->
Lilo = [5, 3, -3, 0, 0.5],
[X*2 || X <-Lilo].

Opa : give a name to a binary resource

I'm trying to download a file from an opa database. I've used the following code :
case {path:[], query:[("download", filename)], ...} : Resource.binary(/myDatabase[filename], "application/txt")
It's working fine, but the file I download is always named "download.txt". How can I change this name ?
Thanks
case {path:[], query:[("download", filename)], ...} : Resource.binary(/myDatabase[filename], "application/txt") |> Resource.add_header(_, {content_disposition={attachment=filename}})

Ruby libxml: format XMLParser to expand closing tags [duplicate]

libxml2 (for C) is not preserving empty elements in their original form on a save. It replaces <tag></tag> with <tag/> which is technically correct but causes problems for us.
xmlDocPtr doc = xmlParseFile("myfile.xml");
xmlNodePtr root = xmlSaveFile("mynewfile.xml", doc);
I've tried playing with the various options (using xlmReadFile) but none seem to affect the output. One post here mentioned disabling tag compression but the example was for PERL and I've found no analog for C.
Is there an option to disable this behavior?
Just found this enum in the xmlsave module documentation:
Enum xmlSaveOption {
XML_SAVE_FORMAT = 1 : format save output
XML_SAVE_NO_DECL = 2 : drop the xml declaration
XML_SAVE_NO_EMPTY = 4 : no empty tags
XML_SAVE_NO_XHTML = 8 : disable XHTML1 specific rules
XML_SAVE_XHTML = 16 : force XHTML1 specific rules
XML_SAVE_AS_XML = 32 : force XML serialization on HTML doc
XML_SAVE_AS_HTML = 64 : force HTML serialization on XML doc
XML_SAVE_WSNONSIG = 128 : format with non-significant whitespace
}
Maybe you can refactor your application to use this module for serialization, and play a little with these options. Specially with XML_SAVE_NO_EMPTY.
Your code may look like this:
xmlSaveCtxt *ctxt = xmlSaveToFilename("mynewfile.xml", "UTF-8", XML_SAVE_FORMAT | XML_SAVE_NO_EMPTY);
if (!ctxt || xmlSaveDoc(ctxt, doc) < 0 || xmlSaveClose(ctxt) < 0)
//...deal with the error

Resources