summaryrefslogtreecommitdiff
path: root/src/www/syntax-editor/ast.js
diff options
context:
space:
mode:
authorjohn.j.camilleri <john.j.camilleri@chalmers.se>2012-11-27 12:00:41 +0000
committerjohn.j.camilleri <john.j.camilleri@chalmers.se>2012-11-27 12:00:41 +0000
commit59b9676fd1b85500b7a66e957542af6dea66c53e (patch)
tree41da285a1881809a1bef9a725cec9c9a0777dd20 /src/www/syntax-editor/ast.js
parente9e29aab678f0ff6b4939c938ea91f82df2fa435 (diff)
Syntax editor; add new helper for parsing type signatures (not complete)
Diffstat (limited to 'src/www/syntax-editor/ast.js')
-rw-r--r--src/www/syntax-editor/ast.js34
1 files changed, 33 insertions, 1 deletions
diff --git a/src/www/syntax-editor/ast.js b/src/www/syntax-editor/ast.js
index 2cad53600..5a80930d9 100644
--- a/src/www/syntax-editor/ast.js
+++ b/src/www/syntax-editor/ast.js
@@ -58,7 +58,7 @@ function ASTNode(data) {
function AST(fun, cat) {
// local helper function for building ASTNodes
- newNode = function(fun, cat) {
+ var newNode = function(fun, cat) {
return new ASTNode({
"fun": fun,
"cat": cat,
@@ -205,4 +205,36 @@ function AST(fun, cat) {
}
}
+
+// Parse type signature into a JSON object
+// (This probably needs a better home)
+AST.parse_type_signature = function(str) {
+ var obj = {
+ type: undefined,
+ name: [],
+ deps: [],
+ args: [],
+ ret: undefined
+ };
+ var ix = str.indexOf(":");
+
+ // judgement type
+ var bits = str.substr(0, ix).split(" ");
+ obj.type = bits[0];
+
+ // name (possibly with constructors)
+ obj.name = bits.slice(1);
+
+ // function args (possibly with type dependency)
+ var bits = map(function(s){return s.trim()}, str.substr(ix+1).split("->"));
+ for (var i=0 ; i<bits.length-1; i++) {
+ var bit = bits[i];
+ obj.args.push(bit);
+ }
+
+ // return type
+ obj.ret = bits[bits.length-1];
+
+ return obj;
+}