Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit83d8b34

Browse files
committed
Add a web endpoint for apidsl.
1 parent51c20d3 commit83d8b34

File tree

21 files changed

+395
-40
lines changed

21 files changed

+395
-40
lines changed

‎.travis.yml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
language:generic
33
os:linux
4+
dist:xenial
45

56
addons:
67
apt:
@@ -15,9 +16,8 @@ cache:
1516
-$HOME/.opam
1617

1718
install:
18-
-opam init -y
19-
-eval `opam env`
20-
-opam install -y menhir ppx_deriving bisect_ppx dune
19+
-bin/install-deps
20+
-eval $(opam env)
2121

2222
script:
2323
-make coveralls

‎Makefile‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ check: $(patsubst %,%dune,$(dir $(wildcard test/*/*.api.h)))
1111
BISECT_ENABLE=yes dune runtest
1212

1313
coverage: check
14-
bisect-ppx-report -html _coverage/ -I _build/default _build/default/test/*/bisect*.out
14+
bisect-ppx-report -html _coverage/ -I _build/default _build/default/test/*/bisect*.coverage
1515

1616
coveralls: check
1717
bisect-ppx-report\
1818
-coveralls coverage.json\
1919
-service-name travis-ci\
2020
-service-job-id"${TRAVIS_JOB_ID}"\
21-
-I _build/default _build/default/test/*/bisect*.out
21+
-I _build/default _build/default/test/*/bisect*.coverage
2222
curl -L -F json_file=@coverage.json https://coveralls.io/api/v1/jobs
2323

2424
clean:

‎Procfile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: _build/default/web/apigen.exe -p$PORT

‎apigen.ml‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@ type outlang =
66
|C
77
|Haskellofstring
88

9-
letdump_apipreapipost=
10-
Option.may (Format.pp_print_stringFormat.str_formatter) pre;
11-
Format.fprintfFormat.str_formatter"%a\n"
12-
ApiCodegen.Api.cg_decls api;
13-
Option.may (Format.fprintfFormat.str_formatter"\n%s\n") post;
14-
15-
Format.flush_str_formatter()
16-
179

1810
letmaininput=
1911
let api=ApiPasses.parse_file inputin
@@ -22,7 +14,7 @@ let main input =
2214
|C -> print_string (ApiPasses.all pre ast post)
2315
|Haskellmodname -> print_string (ApiPasses.haskell modname ast)
2416
|Ast -> print_endline (ApiAst.show_apiFormat.pp_print_string api)
25-
|Api -> print_endline (dump_api pre ast post)
17+
|Api -> print_endline (ApiPasses.dump_api pre ast post)
2618

2719

2820
let()=

‎bin/compile‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
set -eu
4+
5+
BUILD_DIR="$1"
6+
CACHE_DIR="$2"
7+
ENV_DIR="$1"
8+
9+
export PATH="/tmp/opam:$PATH"
10+
export OPAMROOT="$CACHE_DIR/opam"
11+
12+
# Download opam.
13+
echo"/tmp/opam
14+
Y"| sh<(curl -sL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh)
15+
16+
# Enter source directory.
17+
cd"$BUILD_DIR"
18+
19+
."$BUILD_DIR/bin/install-deps"
20+
21+
# Build our app.
22+
dune build --profile release

‎bin/detect‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
exit 0

‎bin/install-deps‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
set -eux
4+
5+
# Set up opam, install dune and menhir (it's not a library dependency).
6+
opam init --disable-sandboxing -n
7+
opam install dune menhir -y
8+
9+
eval$(opam env)
10+
11+
# Install dependencies: we keep installing dependencies until dune stops telling
12+
# us to install more.
13+
DUNE_CMD='dune external-lib-deps --missing --profile release @@default |& grep -o "opam install .*" || true'
14+
INSTALL_CMD="$(eval$DUNE_CMD)"
15+
while [-n"$INSTALL_CMD" ];do
16+
$INSTALL_CMD -y
17+
INSTALL_CMD="$(eval$DUNE_CMD)"
18+
done

‎bin/release‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
exit 0

‎js/apidsl-support.js‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
functionprocessResult(result,error,output){
2+
if(result[0]===1){
3+
error.innerText=result[1];
4+
output.innerText="";
5+
}else{
6+
error.innerText="";
7+
output.innerText=result[1];
8+
}
9+
}
10+
11+
functionprocessLocal(input,format,output,error){
12+
letresult=apidsl.parse("input.api.h",input);
13+
if(result[0]===0){
14+
if(format==="haskell"){
15+
result=apidsl.haskell("MyAPI",result[1]);
16+
}else{
17+
result=apidsl[format](result[1]);
18+
}
19+
}
20+
21+
processResult(result,error,output);
22+
}
23+
24+
functionprocessRemote(input,format,output,error){
25+
constapiUrl="https://apidsl2.herokuapp.com";
26+
fetch(apiUrl+"/parse",{
27+
method:"POST",
28+
body:JSON.stringify(["Request",input])
29+
}).then((data)=>data.json()).then((result)=>{
30+
if(result[0]===0){
31+
fetch(apiUrl+"/"+format,{
32+
method:"POST",
33+
body:JSON.stringify(["Request",result[1]])
34+
}).then((data)=>data.json()).then((result)=>{
35+
processResult(result,error,output);
36+
});
37+
}else{
38+
processResult(result,error,output);
39+
}
40+
});
41+
}
42+
43+
functionexceptionType(err){
44+
if(errinstanceofArray){
45+
if(err[1]instanceofObject&&"c"inerr[1]){
46+
returnerr[1].c;
47+
}
48+
}
49+
returnnull;
50+
}
51+
52+
functionisStackOverflow(err){
53+
returnexceptionType(err)==="Stack_overflow"||
54+
(exceptionType(err)==="Js_of_ocaml__Js.Error"&&
55+
err[2].includes("Maximum call stack size exceeded"));
56+
}
57+
58+
functionprocess(){
59+
leterror=document.getElementById("error");
60+
letinput=document.getElementById("input").value;
61+
letoutput=document.getElementById("output");
62+
letformat=document.querySelector("input[name="format"]:checked").value;
63+
64+
try{
65+
processLocal(input,format,output,error);
66+
}catch(err){
67+
if(isStackOverflow(err)){
68+
processRemote(input,format,output,error);
69+
}else{
70+
console.log("Unhandled exception ",err);
71+
}
72+
}
73+
}
74+
75+
functionload(snippet){
76+
constsnippets={
77+
"simple":"static int main();",
78+
"comment":
79+
`/**
80+
* The main function.
81+
*/
82+
static int main();`,
83+
"namespace":
84+
`namespace foo {
85+
/**
86+
* This is $main.
87+
*/
88+
static int32_t main();
89+
}`,
90+
"large":
91+
`class foo {
92+
/**
93+
* The "this" type for all non-static functions.
94+
*/
95+
struct this;
96+
97+
/**
98+
* This is $main.
99+
*/
100+
int32_t main();
101+
102+
namespace bar {
103+
uint32_t blep();
104+
}
105+
106+
uint8_t some_property { get(); set(); }
107+
}`
108+
};
109+
110+
constfileUrl="https://raw.githubusercontent.com/TokTok/c-toxcore/master/toxcore/tox.api.h";
111+
112+
letinput=document.getElementById("input");
113+
if(snippet==="tox"){
114+
fetch(fileUrl).then(data=>data.text()).then(text=>{
115+
input.value=text;
116+
process();
117+
});
118+
}else{
119+
input.value=snippets[snippet];
120+
process();
121+
}
122+
}

‎js/apigen.ml‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
openApidsl
2+
openJs_of_ocaml
3+
4+
let()=
5+
Js.Unsafe.global##.apidsl:= (object%js
6+
val parse=Js.wrap_callback (funfilecontents ->
7+
try
8+
Ok (ApiPasses.parse_string
9+
(Js.to_string file)
10+
(Js.to_string contents))
11+
with
12+
|Failure(x) ->Error(Js.string x)
13+
|ApiLexer.Lexing_error(start_p,token) ->
14+
let error=ApiPasses.format_lex_error start_p tokenin
15+
Error(Js.string error))
16+
17+
val ast=Js.wrap_callback (funapi ->
18+
tryOk (Js.string (ApiAst.show_apiFormat.pp_print_string api))
19+
withFailure(x) ->Error(Js.string x))
20+
21+
val api=Js.wrap_callback (funapi ->
22+
letApiAst.Api (pre, ast, post)= apiin
23+
tryOk (Js.string (ApiPasses.dump_api pre ast post))
24+
withFailure(x) ->Error(Js.string x))
25+
26+
val c=Js.wrap_callback (funapi ->
27+
letApiAst.Api (pre, ast, post)= apiin
28+
tryOk (Js.string (ApiPasses.all pre ast post))
29+
withFailure(x) ->Error(Js.string x))
30+
31+
val haskell=Js.wrap_callback (funmodnameapi ->
32+
letApiAst.Api (_, ast, _)= apiin
33+
tryOk (Js.string (ApiPasses.haskell (Js.to_string modname) ast))
34+
withFailure(x) ->Error(Js.string x))
35+
end)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp