mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-22 09:32:53 -06:00
Syntax editor; add new helper for parsing type signatures (not complete)
This commit is contained in:
@@ -58,7 +58,7 @@ function ASTNode(data) {
|
|||||||
function AST(fun, cat) {
|
function AST(fun, cat) {
|
||||||
|
|
||||||
// local helper function for building ASTNodes
|
// local helper function for building ASTNodes
|
||||||
newNode = function(fun, cat) {
|
var newNode = function(fun, cat) {
|
||||||
return new ASTNode({
|
return new ASTNode({
|
||||||
"fun": fun,
|
"fun": fun,
|
||||||
"cat": cat,
|
"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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -198,14 +198,12 @@ Editor.prototype.select_refinement=function(fun) {
|
|||||||
|
|
||||||
// Parse out function arguments
|
// Parse out function arguments
|
||||||
var def = t.grammar_constructors.funs[fun].def;
|
var def = t.grammar_constructors.funs[fun].def;
|
||||||
def = def.substr(def.lastIndexOf(":")+1);
|
var typeobj = AST.parse_type_signature(def);
|
||||||
var fun_args = map(function(s){return s.trim()}, def.split("->"))
|
|
||||||
fun_args = fun_args.slice(0,-1);
|
|
||||||
|
|
||||||
if (fun_args.length > 0) {
|
if (typeobj.args.length > 0) {
|
||||||
// Add placeholders
|
// Add placeholders
|
||||||
for (ci in fun_args) {
|
for (var i in typeobj.args) {
|
||||||
t.ast.add(null, fun_args[ci]);
|
t.ast.add(null, typeobj.args[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user