-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrparse.js
160 lines (139 loc) · 4.16 KB
/
rparse.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict';
if (typeof(module) !== 'undefined') {
var LocalStorage = require('./storage.js');
var localStorage = new LocalStorage('./parser.cache', {strict: false});
var http = require('http');
var ParseError = require('./errors.js').ParseError;
var utils = require('./util.js');
var determine_beats = utils.determine_beats;
var translate_mml = utils.translate_mml;
var convert = utils.convert;
var count = utils.count;
}
var useCache = false;
var set_time_signature = function (top, bottom) {
utils.set_time_signature(top,bottom);
}
var rparse = function(inputArray, grammar, serverGrammar, callback) {
localStorage.clear();
var inputs = [];
for (var i = 0; i<inputArray.length; i++){
var input = inputArray[i];
// check if input is in cache
var cachedValue = null;
if (useCache)
cachedValue = localStorage.getItem(input);
if (!cachedValue){
inputs.push(input);
}
}
if (inputs.length == 0){
setTimeout(function() {
handleResponse("[]");
}, 0);
return;
}
var server = 'cs164parsertest.appspot.com';
var port = '80';
var path = '/parser_emit';
// convert program data into parameters
var paramsList = [];
if (serverGrammar) {
paramsList.push(encodeURIComponent('server_grammar') + '=' +
encodeURIComponent(serverGrammar));
} else if (grammar) {
paramsList.push(encodeURIComponent('grammar') + '=' +
encodeURIComponent(grammar));
} else {
paramsList.push(encodeURIComponent('server_grammar') + '=' +
encodeURIComponent('cs164c.grm'));
}
for (var i in inputs) {
paramsList.push(encodeURIComponent('input') + '=' +
encodeURIComponent(inputs[i]));
}
var encodedParams = paramsList.join('&');
// check if we are in nodejs so we can use http instead of XMLHttpRequest
if (typeof(module) !== 'undefined') {
// An object of options to indicate where to post to
var postOptions = {
host: server,
port: port,
path: path,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': encodedParams.length
}
};
var postReq = http.request(postOptions, function(response) {
var data = '';
response.setEncoding('utf8');
response.on('data', function(chunk) {
data += chunk.toString();
});
response.on('end', function() {
handleResponse(data);
});
});
postReq.write(encodedParams);
postReq.end();
// we are not in node, so issue an XLMHttpRequest
} else {
var str = 'http://' + server;
if (port)
str += ':' + port;
if (path)
str += path;
var r = new XMLHttpRequest();
r.open('POST', str, true);
r.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
r.onload = function(x) {
if (r.status === 200)
handleResponse(r.responseText);
};
r.send(encodedParams);
}
// Handle the server response
function handleResponse(response) {
// Parse JSON data
try {
var parsedResponse = JSON.parse(response);
} catch (e) {
console.error('Bad response: ', inputs, response);
throw new ParseError('Error parsing JSON response from remote parser.');
}
// update cache
for (var input in parsedResponse){
var result = parsedResponse[input];
if (result.ast) {
var ast = result.ast;
} else if (result.emit) {
var ast = eval(result.emit);
} else if (result.output) {
console.error(result.output);
} else {
throw new ParseError('Unknown parsed response from remote parser.');
}
localStorage.setItem(input,JSON.stringify(ast));
}
var trees = [];
for (var i=0; i<inputArray.length; i++) {
var input = inputArray[i];
var result = JSON.parse(localStorage.getItem(input));
// value contains the parse tree
var tree = result;
if (tree == 'Error') {
throw new ParseError('Parsing error.');
}
trees.push(tree);
}
callback(trees);
}
};
if (typeof(module) !== 'undefined') {
module.exports = {
'rparse': rparse,
'set_time_signature': set_time_signature
};
}