-
Notifications
You must be signed in to change notification settings - Fork 8
/
jsRealB-server.mjs
59 lines (53 loc) · 1.84 KB
/
jsRealB-server.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/// Simplistic node.js jsRealBServer
import { createServer } from "http";
import { parse } from 'url';
/////////
// load jsRealB file
import jsRealB from "./jsRealB.js";
// "evaluate" the exports (Constructors for terminals and non-terminal) in the current context
// so that they can be used directly
Object.assign(globalThis,jsRealB);
loadEn(true);
createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
var req = parse(request.url, true);
var query = req.query;
var lang = query.lang
var exp = query.exp
if (lang=="en"){
let errorType,sentence;
try {
if (exp.startsWith("{")){
errorType="JSON";
const jsonExp=JSON.parse(exp);
sentence=fromJSON(jsonExp).toString();
} else {
errorType="jsRealB expression";
sentence=eval(exp).toString();
}
response.end(sentence)
} catch (e) {
const mess=`${e}\nErroneous realization from ${errorType}\n`
if (errorType=="JSON"){
try { // pretty-print if possible... i.e. not a JSON error
response.end(mess+ppJSON(JSON.parse(exp)))
} catch(e){ // print line as is
response.end(mess+exp);
}
} else {
response.end(mess+exp)
}
}
} else {
response.end('Language should be "en", but '+lang+' received\n')
}
}).listen(8081);
// Console will print the message
console.log('jsRealB server [built on %s] running at http://127.0.0.1:8081/',jsRealB_dateCreated);
/*
start server with : node ./dist/jsRealB-server.js
try these examples in a browser
http://127.0.0.1:8081/?lang=en&exp=S(NP(D("the"),N("man")),VP(V("love")))
that should display:
The man loves.
*/