Skip to content

Commit 75e27d2

Browse files
committed
initial effort to replace camlp4
1 parent 780f0a0 commit 75e27d2

20 files changed

+600001
-0
lines changed

ocaml-tree/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
build
3+
data
4+
index.js
5+
*.cm*

ocaml-tree/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Max Brunsfeld and Pieter Goetschalckx
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

ocaml-tree/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
# License
3+
4+
The grammar.js file belongs to tree-sitter/tree-sitter-ocaml, it fails to compile with the
5+
latest Node, so it's vendored here, its license goes to [./LICENSE](LICENSE)
6+
7+
Those C libraries are not needed since we made a snasphot of wasm.
8+
9+
# build wasm
10+
npx tree-sitter build-wasm .
11+
# build native
12+
node-gyp config
13+
node-gyp build

ocaml-tree/binding.gyp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "tree_sitter_ocaml_binding",
5+
"include_dirs": [
6+
"<!(node -e \"require('nan')\")",
7+
"src"
8+
],
9+
"sources": [
10+
"src/parser.c",
11+
"src/binding.cc",
12+
"src/scanner.cc"
13+
],
14+
"cflags_c": [
15+
"-std=c99",
16+
]
17+
}
18+
]
19+
}

ocaml-tree/data2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type inline =
2+
| A of { x : int }
3+
| B of int

ocaml-tree/fold_maker.js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
//@ts-check
2+
var assert = require("assert");
3+
/**
4+
*
5+
* @typedef {import('./node_types').Node} Node
6+
*/
7+
8+
/**
9+
*
10+
* @param {{name:string, def:Node}} typedef
11+
* @returns {string}
12+
*/
13+
function mkMethod({ name, def }) {
14+
return `method ${name} : ${name} -> 'self_type = ${mkBody(def)} `;
15+
}
16+
17+
function init(n, fn) {
18+
var arr = Array(n);
19+
for (let i = 0; i < n; ++i) {
20+
arr[i] = fn(i);
21+
}
22+
return arr;
23+
}
24+
25+
/**
26+
* @param {Node} def
27+
*/
28+
function mkBody(def) {
29+
// @ts-ignore
30+
assert(def !== undefined);
31+
switch (def.type) {
32+
case "type_constructor_path":
33+
if (def.children.length === 1) {
34+
return `o#${def.children[0].text}`;
35+
} else {
36+
// [ extended_module_path ..]
37+
return `o#unknown`;
38+
}
39+
case "constructed_type":
40+
// FIXME
41+
var [list, base] = [...def.children].reverse();
42+
return `${mkBody(list)} (fun o -> ${mkBody(base)})`;
43+
case "record_declaration":
44+
var len = def.children.length;
45+
var args = init(len, (i) => `_x${i}`);
46+
var pat_exp = init(len, (i) => {
47+
return `${def.children[i].children[0].text} = ${args[i]}`;
48+
});
49+
50+
/**
51+
* @type {string[]}
52+
*/
53+
var body = args.map((x, i) => {
54+
var ty = def.children[i].children[1];
55+
return `let o = ${mkBody(ty)} ${x} in`;
56+
});
57+
return `fun { ${pat_exp.join(";")}} -> ${body.join("\n")} o`;
58+
case "variant_declaration":
59+
var len = def.children.length;
60+
var branches = def.children.map((branch) => mkBranch(branch));
61+
return `function \n| ${branches.join("\n|")}`;
62+
case "tuple_type":
63+
var len = def.children.length;
64+
var args = init(len, (i) => `_x${i}`);
65+
var body = args.map(
66+
(x, i) => `let o = ${mkBody(def.children[i])} ${x} in`
67+
);
68+
return `fun ( ${args.join(",")}) -> ${body.join(" ")} o`;
69+
default:
70+
throw new Error(`unkonwn ${def.type}`);
71+
}
72+
}
73+
/**
74+
*
75+
* @param {Node} branch
76+
* branch is constructor_declaration
77+
* @returns {string}
78+
*/
79+
function mkBranch(branch) {
80+
// @ts-ignore
81+
assert(branch?.type === "constructor_declaration");
82+
var [{ text }, ...rest] = branch.children;
83+
// TODO: add inline record support
84+
var len = rest.length;
85+
if (len !== 0) {
86+
var args = init(len, (i) => `_x${i}`);
87+
var pat_exp = `${text} ( ${args.join(",")}) `;
88+
var body = args.map((x, i) => {
89+
var ty = rest[i];
90+
return `let o = ${mkBody(ty)} ${x} in`;
91+
});
92+
return `${pat_exp} -> \n${body.join("\n")}\n o`;
93+
} else {
94+
return `${text} -> o`;
95+
}
96+
}
97+
98+
/**
99+
*
100+
* @param {{name : string, def: Node}[]} typedefs
101+
* @returns {string}
102+
*/
103+
function make(typedefs) {
104+
var output = typedefs.map(mkMethod);
105+
var o = `
106+
open J
107+
class virtual fold =
108+
object ((o : 'self_type))
109+
method unknown : 'a. 'a -> 'self_type = fun _ -> o
110+
method string : string -> 'self_type = fun _ -> o
111+
method option :
112+
'a. ('self_type -> 'a -> 'self_type) -> 'a option -> 'self_type =
113+
fun _f_a -> function | None -> o | Some _x -> let o = _f_a o _x in o
114+
method list :
115+
'a. ('self_type -> 'a -> 'self_type) -> 'a list -> 'self_type =
116+
fun _f_a ->
117+
function
118+
| [] -> o
119+
| _x :: _x_i1 -> let o = _f_a o _x in let o = o#list _f_a _x_i1 in o
120+
method int32 : int32 -> 'self_type = fun _ -> o
121+
method int : int -> 'self_type = fun _ -> o
122+
method bool : bool -> 'self_type = fun _ -> o
123+
${output.join("\n")}
124+
end
125+
`;
126+
return o;
127+
}
128+
exports.make = make;

0 commit comments

Comments
 (0)