From c9b4318e9e8519b4b2a05cb48060cfdf353e1d49 Mon Sep 17 00:00:00 2001 From: "John J. Camilleri" Date: Fri, 7 Jun 2019 09:34:13 +0200 Subject: Clean up whitespace in [old] gflib.js --- src/runtime/javascript/gflib.js | 802 ++++++++++++++++++++-------------------- 1 file changed, 401 insertions(+), 401 deletions(-) (limited to 'src/runtime/javascript') diff --git a/src/runtime/javascript/gflib.js b/src/runtime/javascript/gflib.js index 97e98aab2..45bb32d3d 100644 --- a/src/runtime/javascript/gflib.js +++ b/src/runtime/javascript/gflib.js @@ -1,38 +1,38 @@ function GFGrammar(abstract, concretes) { - this.abstract = abstract; - this.concretes = concretes; + this.abstract = abstract; + this.concretes = concretes; } -/* Translates a string from any concrete syntax to all concrete syntaxes. +/* Translates a string from any concrete syntax to all concrete syntaxes. Uses the start category of the grammar. */ GFGrammar.prototype.translate = function (input, fromLang, toLang) { - var outputs = new Object(); - var fromConcs = this.concretes; - if (fromLang) { - fromConcs = new Object(); - fromConcs[fromLang] = this.concretes[fromLang]; - } - var toConcs = this.concretes; - if (toLang) { - toConcs = new Object(); - toConcs[toLang] = this.concretes[toLang]; - } - for (var c1 in fromConcs) { - var concrete = this.concretes[c1]; - var trees = concrete.parseString(input, this.abstract.startcat); - if (trees.length > 0) { - outputs[c1] = new Array(); - for (var i in trees) { - outputs[c1][i] = new Object(); - for (var c2 in toConcs) { - outputs[c1][i][c2] = this.concretes[c2].linearize(trees[i]); - } - } - } - } - return outputs; + var outputs = new Object(); + var fromConcs = this.concretes; + if (fromLang) { + fromConcs = new Object(); + fromConcs[fromLang] = this.concretes[fromLang]; + } + var toConcs = this.concretes; + if (toLang) { + toConcs = new Object(); + toConcs[toLang] = this.concretes[toLang]; + } + for (var c1 in fromConcs) { + var concrete = this.concretes[c1]; + var trees = concrete.parseString(input, this.abstract.startcat); + if (trees.length > 0) { + outputs[c1] = new Array(); + for (var i in trees) { + outputs[c1][i] = new Object(); + for (var c2 in toConcs) { + outputs[c1][i][c2] = this.concretes[c2].linearize(trees[i]); + } + } + } + } + return outputs; } @@ -47,56 +47,56 @@ String.prototype.setTag = function (tag) { this.tag = tag; }; /* Abstract syntax trees */ function Fun(name) { - this.name = name; - this.args = new Array(); - for (var i = 1; i < arguments.length; i++) { - this.args[i-1] = arguments[i]; - } + this.name = name; + this.args = new Array(); + for (var i = 1; i < arguments.length; i++) { + this.args[i-1] = arguments[i]; + } } Fun.prototype.print = function () { return this.show(0); } ; Fun.prototype.show = function (prec) { - if (this.isMeta()) { - if (isUndefined(this.type)) { - return '?'; - } else { - var s = '?:' + this.type; - if (prec > 0) { - s = "(" + s + ")" ; - } - return s; - } - } else { - var s = this.name; - var cs = this.args; - for (var i in cs) { - s += " " + (isUndefined(cs[i]) ? "undefined" : cs[i].show(1)); - } - if (prec > 0 && cs.length > 0) { - s = "(" + s + ")" ; - } - return s; - } + if (this.isMeta()) { + if (isUndefined(this.type)) { + return '?'; + } else { + var s = '?:' + this.type; + if (prec > 0) { + s = "(" + s + ")" ; + } + return s; + } + } else { + var s = this.name; + var cs = this.args; + for (var i in cs) { + s += " " + (isUndefined(cs[i]) ? "undefined" : cs[i].show(1)); + } + if (prec > 0 && cs.length > 0) { + s = "(" + s + ")" ; + } + return s; + } }; Fun.prototype.getArg = function (i) { - return this.args[i]; + return this.args[i]; }; Fun.prototype.setArg = function (i,c) { - this.args[i] = c; + this.args[i] = c; }; Fun.prototype.isMeta = function() { - return this.name == '?'; + return this.name == '?'; } ; Fun.prototype.isComplete = function() { - if (this.isMeta()) { - return false; - } else { - for (var i in this.args) { - if (!this.args[i].isComplete()) { - return false; - } - } - return true; - } + if (this.isMeta()) { + return false; + } else { + for (var i in this.args) { + if (!this.args[i].isComplete()) { + return false; + } + } + return true; + } } ; Fun.prototype.isLiteral = function() { return (/^[\"\-\d]/).test(this.name); @@ -120,146 +120,146 @@ Fun.prototype.isEqual = function(obj) { if (!this.args[i].isEqual(obj.args[i])) return false; } - + return true; } /* Type annotation */ function GFAbstract(startcat, types) { - this.startcat = startcat; - this.types = types; + this.startcat = startcat; + this.types = types; } GFAbstract.prototype.addType = function(fun, args, cat) { - this.types[fun] = new Type(args, cat); + this.types[fun] = new Type(args, cat); } ; GFAbstract.prototype.getArgs = function(fun) { - return this.types[fun].args; + return this.types[fun].args; } GFAbstract.prototype.getCat = function(fun) { - return this.types[fun].cat; + return this.types[fun].cat; }; GFAbstract.prototype.annotate = function(tree, type) { - if (tree.name == '?') { - tree.type = type; - } else { - var typ = this.types[tree.name]; - for (var i in tree.args) { - this.annotate(tree.args[i], typ.args[i]); - } - } - return tree; + if (tree.name == '?') { + tree.type = type; + } else { + var typ = this.types[tree.name]; + for (var i in tree.args) { + this.annotate(tree.args[i], typ.args[i]); + } + } + return tree; } ; GFAbstract.prototype.handleLiterals = function(tree, type) { - if (tree.name != '?') { - if (type == "String" || type == "Int" || type == "Float") { - tree.name = type + "_Literal_" + tree.name; - } else { - var typ = this.types[tree.name]; - for (var i in tree.args) { - this.handleLiterals(tree.args[i], typ.args[i]); - } - } - } - return tree; + if (tree.name != '?') { + if (type == "String" || type == "Int" || type == "Float") { + tree.name = type + "_Literal_" + tree.name; + } else { + var typ = this.types[tree.name]; + for (var i in tree.args) { + this.handleLiterals(tree.args[i], typ.args[i]); + } + } + } + return tree; } ; /* Hack to get around the fact that our SISR doesn't build real Fun objects. */ GFAbstract.prototype.copyTree = function(x) { - var t = new Fun(x.name); - if (!isUndefined(x.type)) { - t.type = x.type; - } - var cs = x.args; - if (!isUndefined(cs)) { - for (var i in cs) { - t.setArg(i, this.copyTree(cs[i])); - } - } - return t; + var t = new Fun(x.name); + if (!isUndefined(x.type)) { + t.type = x.type; + } + var cs = x.args; + if (!isUndefined(cs)) { + for (var i in cs) { + t.setArg(i, this.copyTree(cs[i])); + } + } + return t; } ; -GFAbstract.prototype.parseTree = function(str, type) { - return this.annotate(this.parseTree_(str.match(/[\w\'\.\"]+|\(|\)|\?|\:/g), 0), type); +GFAbstract.prototype.parseTree = function(str, type) { + return this.annotate(this.parseTree_(str.match(/[\w\'\.\"]+|\(|\)|\?|\:/g), 0), type); } ; GFAbstract.prototype.parseTree_ = function(tokens, prec) { - if (tokens.length == 0 || tokens[0] == ")") { return null; } - var t = tokens.shift(); - if (t == "(") { - var tree = this.parseTree_(tokens, 0); - tokens.shift(); - return tree; - } else if (t == '?') { - var tree = this.parseTree_(tokens, 0); - return new Fun('?'); - } else { - var tree = new Fun(t); - if (prec == 0) { - var c, i; - for (i = 0; (c = this.parseTree_(tokens, 1)) !== null; i++) { - tree.setArg(i,c); - } - } - return tree; - } + if (tokens.length == 0 || tokens[0] == ")") { return null; } + var t = tokens.shift(); + if (t == "(") { + var tree = this.parseTree_(tokens, 0); + tokens.shift(); + return tree; + } else if (t == '?') { + var tree = this.parseTree_(tokens, 0); + return new Fun('?'); + } else { + var tree = new Fun(t); + if (prec == 0) { + var c, i; + for (i = 0; (c = this.parseTree_(tokens, 1)) !== null; i++) { + tree.setArg(i,c); + } + } + return tree; + } } ; function Type(args, cat) { - this.args = args; - this.cat = cat; + this.args = args; + this.cat = cat; } /* Linearization */ function GFConcrete(flags, productions, functions, sequences, startCats, totalFIds) { - this.flags = flags; - this.productions = productions; - this.functions = functions; - this.sequences = sequences; - this.startCats = startCats; - this.totalFIds = totalFIds; + this.flags = flags; + this.productions = productions; + this.functions = functions; + this.sequences = sequences; + this.startCats = startCats; + this.totalFIds = totalFIds; - this.pproductions = productions; - this.lproductions = new Object(); + this.pproductions = productions; + this.lproductions = new Object(); for (var fid in productions) { for (var i in productions[fid]) { var rule = productions[fid][i]; - + if (rule.id == "Apply") { var fun = this.functions[rule.fun]; var lproductions = this.lproductions; - + rule.fun = fun; var register = function (args, key, i) { - if (i < args.length) { - var c = 0; - var arg = args[i].fid; - - for (var k in productions[arg]) { + if (i < args.length) { + var c = 0; + var arg = args[i].fid; + + for (var k in productions[arg]) { var rule = productions[arg][k]; if (rule.id == "Coerce") { register(args,key + "_" + rule.arg,i+1); c++; } } - + if (c == 0) register(args,key + "_" + arg,i+1); - } else { - var set = lproductions[key]; + } else { + var set = lproductions[key]; if (set == null) { set = new Array(); lproductions[key] = set; } - set.push({fun: fun, fid: fid}); - } - } + set.push({fun: fun, fid: fid}); + } + } register(rule.args,rule.fun.name,0); - } + } } } - + for (var i in functions) { var fun = functions[i]; for (var j in fun.lins) { @@ -267,30 +267,30 @@ function GFConcrete(flags, productions, functions, sequences, startCats, totalFI } } } -GFConcrete.prototype.linearizeSyms = function (tree, tag) { +GFConcrete.prototype.linearizeSyms = function (tree, tag) { var res = new Array(); - + if (tree.isString()) { - var sym = new SymKS(tree.name); - sym.tag = tag; - res.push({fid: -1, table: [[sym]]}); + var sym = new SymKS(tree.name); + sym.tag = tag; + res.push({fid: -1, table: [[sym]]}); } else if (tree.isInt()) { - var sym = new SymKS(tree.name); - sym.tag = tag; - res.push({fid: -2, table: [[sym]]}); + var sym = new SymKS(tree.name); + sym.tag = tag; + res.push({fid: -2, table: [[sym]]}); } else if (tree.isFloat()) { - var sym = new SymKS(tree.name); - sym.tag = tag; - res.push({fid: -3, table: [[sym]]}); + var sym = new SymKS(tree.name); + sym.tag = tag; + res.push({fid: -3, table: [[sym]]}); } else if (tree.isMeta()) { - // TODO: Use lindef here + // TODO: Use lindef here var cat = this.startCats[tree.type]; - + var sym = new SymKS(tree.name); - sym.tag = tag; - - for (var fid = cat.s; fid <= cat.e; fid++) { - res.push({fid: fid, table: [[sym]]}); + sym.tag = tag; + + for (var fid = cat.s; fid <= cat.e; fid++) { + res.push({fid: fid, table: [[sym]]}); } } else { var cs = new Array(); @@ -310,7 +310,7 @@ GFConcrete.prototype.linearizeSyms = function (tree, tag) { var lin = rule.fun.lins[j]; var toks = new Array(); row.table[j] = toks; - + for (var k in lin) { var sym = lin[k]; switch (sym.id) { @@ -331,7 +331,7 @@ GFConcrete.prototype.linearizeSyms = function (tree, tag) { res.push(row); } } - + return res; }; GFConcrete.prototype.syms2toks = function (syms) { @@ -347,7 +347,7 @@ GFConcrete.prototype.syms2toks = function (syms) { case "KP": for (var j in sym.tokens) { ts.push(this.tagIt(sym.tokens[j],sym.tag)); - } + } break; } } @@ -365,37 +365,37 @@ GFConcrete.prototype.linearize = function (tree) { var res = this.linearizeSyms(tree,"0"); return this.unlex(this.syms2toks(res[0].table[0])); } -GFConcrete.prototype.tagAndLinearize = function (tree) { +GFConcrete.prototype.tagAndLinearize = function (tree) { var res = this.linearizeSyms(tree,"0"); return this.syms2toks(res[0].table[0]); } GFConcrete.prototype.unlex = function (ts) { - if (ts.length == 0) { - return ""; - } - - var noSpaceAfter = /^[\(\-\[]/; - var noSpaceBefore = /^[\.\,\?\!\)\:\;\-\]]/; - - var s = ""; - for (var i = 0; i < ts.length; i++) { - var t = ts[i]; - var after = i < ts.length-1 ? ts[i+1] : null; - s += t; - if (after != null && !t.match(noSpaceAfter) - && !after.match(noSpaceBefore)) { - s += " "; - } - } - return s; + if (ts.length == 0) { + return ""; + } + + var noSpaceAfter = /^[\(\-\[]/; + var noSpaceBefore = /^[\.\,\?\!\)\:\;\-\]]/; + + var s = ""; + for (var i = 0; i < ts.length; i++) { + var t = ts[i]; + var after = i < ts.length-1 ? ts[i+1] : null; + s += t; + if (after != null && !t.match(noSpaceAfter) + && !after.match(noSpaceBefore)) { + s += " "; + } + } + return s; }; GFConcrete.prototype.tagIt = function (obj, tag) { if (isString(obj)) { - var o = new String(obj); - o.setTag(tag); - return o; + var o = new String(obj); + o.setTag(tag); + return o; } else { - var me = arguments.callee; + var me = arguments.callee; if (arguments.length == 2) { me.prototype = obj; var o = new me(); @@ -416,28 +416,28 @@ function isNumber(a) { return typeof a == 'number' && isFinite(a); } function isFunction(a) { return typeof a == 'function'; } function dumpObject (obj) { - if (isUndefined(obj)) { - return "undefined"; - } else if (isString(obj)) { - return '"' + obj.toString() + '"'; // FIXME: escape - } else if (isBoolean(obj) || isNumber(obj)) { - return obj.toString(); - } else if (isArray(obj)) { - var x = "["; - for (var i in obj) { - x += dumpObject(obj[i]); - if (i < obj.length-1) { - x += ","; - } - } - return x + "]"; - } else { - var x = "{"; - for (var y in obj) { - x += y + "=" + dumpObject(obj[y]) + ";" ; - } - return x + "}"; - } + if (isUndefined(obj)) { + return "undefined"; + } else if (isString(obj)) { + return '"' + obj.toString() + '"'; // FIXME: escape + } else if (isBoolean(obj) || isNumber(obj)) { + return obj.toString(); + } else if (isArray(obj)) { + var x = "["; + for (var i in obj) { + x += dumpObject(obj[i]); + if (i < obj.length-1) { + x += ","; + } + } + return x + "]"; + } else { + var x = "{"; + for (var y in obj) { + x += y + "=" + dumpObject(obj[y]) + ";" ; + } + return x + "}"; + } } /* ------------------------------------------------------------------------- */ @@ -447,11 +447,11 @@ function dumpObject (obj) { GFConcrete.prototype.showRules = function () { var ruleStr = new Array(); - ruleStr.push(""); - for (var i = 0, j = this.rules.length; i < j; i++) { - ruleStr.push(this.rules[i].show()); - } - return ruleStr.join(""); + ruleStr.push(""); + for (var i = 0, j = this.rules.length; i < j; i++) { + ruleStr.push(this.rules[i].show()); + } + return ruleStr.join(""); }; GFConcrete.prototype.tokenize = function (string) { var inToken = false; @@ -460,125 +460,125 @@ GFConcrete.prototype.tokenize = function (string) { for (var i = 0; i < string.length; i++) { if ( string.charAt(i) == ' ' // space - || string.charAt(i) == '\f' // form feed - || string.charAt(i) == '\n' // newline - || string.charAt(i) == '\r' // return - || string.charAt(i) == '\t' // horizontal tab - || string.charAt(i) == '\v' // vertical tab + || string.charAt(i) == '\f' // form feed + || string.charAt(i) == '\n' // newline + || string.charAt(i) == '\r' // return + || string.charAt(i) == '\t' // horizontal tab + || string.charAt(i) == '\v' // vertical tab || string.charAt(i) == String.fromCharCode(160) //   ) { - if (inToken) { + if (inToken) { end = i-1; inToken = false; - + tokens.push(string.substr(start,end-start+1)); } - } else { + } else { if (!inToken) { start = i; inToken = true; } } } - + if (inToken) { end = i-1; inToken = false; - + tokens.push(string.substr(start,end-start+1)); } return tokens; }; GFConcrete.prototype.parseString = function (string, cat) { - var tokens = this.tokenize(string); - - var ps = new ParseState(this, cat); - for (var i in tokens) { - if (!ps.next(tokens[i])) + var tokens = this.tokenize(string); + + var ps = new ParseState(this, cat); + for (var i in tokens) { + if (!ps.next(tokens[i])) return new Array(); - } - return ps.extractTrees(); + } + return ps.extractTrees(); }; /** * Generate list of suggestions given an input string */ GFConcrete.prototype.complete = function (input, cat) { - // Parameter defaults - if (input == null) input = ""; - if (cat == null) cat = grammar.abstract.startcat; - - // Tokenise input string & remove empty tokens - tokens = input.trim().split(' '); - for (var i = tokens.length - 1; i >= 0; i--) { - if (tokens[i] == "") { tokens.splice(i, 1); } - } - - // Capture last token as it may be partial - current = tokens.pop(); - if (current == null) current = ""; - - // Init parse state objects. - // ps2 is used for testing whether the final token is parsable or not. - var ps = new ParseState(this, cat); - var ps2 = new ParseState(this, cat); - - // Iterate over tokens, feed one by one to parser - for (var i = 0; i < tokens.length ; i++) { - if (!ps.next(tokens[i])) { - return new Array(); // Incorrect parse, nothing to suggest - } - ps2.next(tokens[i]); // also consume token in ps2 - } - - // Attempt to also parse current, knowing it may be incomplete - if (ps2.next(current)) { - ps.next(current); - tokens.push(current); - current = ""; - } - delete(ps2); // don't need this anymore - - // Parse is successful so far, now get suggestions - var acc = ps.complete(current); - - // Format into just a list of strings & return - // (I know the multiple nesting looks horrible) - var suggs = new Array(); - if (acc.value) { - // Iterate over all acc.value[] - for (var v = 0; v < acc.value.length; v++) { - // Iterate over all acc.value[].seq[] - for (var s = 0; s < acc.value[v].seq.length; s++) { - if (acc.value[v].seq[s].tokens == null) continue; - // Iterate over all acc.value[].seq[].tokens - for (var t = 0; t < acc.value[v].seq[s].tokens.length; t++) { - suggs.push( acc.value[v].seq[s].tokens[t] ); - } - } - } - } - - // Note: return used tokens too - return { 'consumed' : tokens, 'suggestions' : suggs }; + // Parameter defaults + if (input == null) input = ""; + if (cat == null) cat = grammar.abstract.startcat; + + // Tokenise input string & remove empty tokens + tokens = input.trim().split(' '); + for (var i = tokens.length - 1; i >= 0; i--) { + if (tokens[i] == "") { tokens.splice(i, 1); } + } + + // Capture last token as it may be partial + current = tokens.pop(); + if (current == null) current = ""; + + // Init parse state objects. + // ps2 is used for testing whether the final token is parsable or not. + var ps = new ParseState(this, cat); + var ps2 = new ParseState(this, cat); + + // Iterate over tokens, feed one by one to parser + for (var i = 0; i < tokens.length ; i++) { + if (!ps.next(tokens[i])) { + return new Array(); // Incorrect parse, nothing to suggest + } + ps2.next(tokens[i]); // also consume token in ps2 + } + + // Attempt to also parse current, knowing it may be incomplete + if (ps2.next(current)) { + ps.next(current); + tokens.push(current); + current = ""; + } + delete(ps2); // don't need this anymore + + // Parse is successful so far, now get suggestions + var acc = ps.complete(current); + + // Format into just a list of strings & return + // (I know the multiple nesting looks horrible) + var suggs = new Array(); + if (acc.value) { + // Iterate over all acc.value[] + for (var v = 0; v < acc.value.length; v++) { + // Iterate over all acc.value[].seq[] + for (var s = 0; s < acc.value[v].seq.length; s++) { + if (acc.value[v].seq[s].tokens == null) continue; + // Iterate over all acc.value[].seq[].tokens + for (var t = 0; t < acc.value[v].seq[s].tokens.length; t++) { + suggs.push( acc.value[v].seq[s].tokens[t] ); + } + } + } + } + + // Note: return used tokens too + return { 'consumed' : tokens, 'suggestions' : suggs }; } // Apply Object Definition function Apply(fun, args) { - this.id = "Apply"; - this.fun = fun; - this.args = args; + this.id = "Apply"; + this.fun = fun; + this.args = args; } Apply.prototype.show = function (cat) { - var recStr = new Array(); - recStr.push(cat, " -> ", fun.name, " [", this.args, "]"); - return recStr.join(""); + var recStr = new Array(); + recStr.push(cat, " -> ", fun.name, " [", this.args, "]"); + return recStr.join(""); }; Apply.prototype.isEqual = function (obj) { - if (this.id != obj.id || this.fun != obj.fun || this.args.length != obj.args.length) + if (this.id != obj.id || this.fun != obj.fun || this.args.length != obj.args.length) return false; - + for (var i in this.args) { if (this.args[i] != obj.args[i]) return false; @@ -588,39 +588,39 @@ Apply.prototype.isEqual = function (obj) { }; function PArg() { - this.fid = arguments[arguments.length-1]; - if (arguments.length > 1) - this.hypos = arguments.slice(0,arguments.length-1); + this.fid = arguments[arguments.length-1]; + if (arguments.length > 1) + this.hypos = arguments.slice(0,arguments.length-1); } // Coerce Object Definition function Coerce(arg) { this.id = "Coerce"; - this.arg = arg; + this.arg = arg; } Coerce.prototype.show = function (cat) { - var recStr = new Array(); - recStr.push(cat, " -> _ [", this.args, "]"); - return recStr.join(""); + var recStr = new Array(); + recStr.push(cat, " -> _ [", this.args, "]"); + return recStr.join(""); }; // Const Object Definition function Const(lit, toks) { this.id = "Const"; - this.lit = lit; - this.toks = toks; + this.lit = lit; + this.toks = toks; } Const.prototype.show = function (cat) { - var recStr = new Array(); - recStr.push(cat, " -> ", lit.print()); - return recStr.join(""); + var recStr = new Array(); + recStr.push(cat, " -> ", lit.print()); + return recStr.join(""); }; Const.prototype.isEqual = function (obj) { - if (this.id != obj.id || this.lit.isEqual(obj.lit) || this.toks.length != obj.toks.length) + if (this.id != obj.id || this.lit.isEqual(obj.lit) || this.toks.length != obj.toks.length) return false; - + for (var i in this.toks) { if (this.toks[i] != obj.toks[i]) return false; @@ -638,41 +638,41 @@ function CncFun(name,lins) { // Object to represent argument projections in grammar rules function SymCat(i, label) { - this.id = "Arg"; - this.i = i; - this.label = label; + this.id = "Arg"; + this.i = i; + this.label = label; } SymCat.prototype.getId = function () { return this.id; }; SymCat.prototype.getArgNum = function () { return this.i }; SymCat.prototype.show = function () { - var argStr = new Array(); - argStr.push(this.i, this.label); - return argStr.join("."); + var argStr = new Array(); + argStr.push(this.i, this.label); + return argStr.join("."); }; // Object to represent terminals in grammar rules function SymKS() { - this.id = "KS"; - this.tokens = arguments; + this.id = "KS"; + this.tokens = arguments; } SymKS.prototype.getId = function () { return this.id; }; SymKS.prototype.show = function () { - var terminalStr = new Array(); - terminalStr.push('"', this.tokens, '"'); - return terminalStr.join(""); + var terminalStr = new Array(); + terminalStr.push('"', this.tokens, '"'); + return terminalStr.join(""); }; // Object to represent pre in grammar rules function SymKP(tokens,alts) { - this.id = "KP"; - this.tokens = tokens; + this.id = "KP"; + this.tokens = tokens; this.alts = alts; } SymKP.prototype.getId = function () { return this.id; }; SymKP.prototype.show = function () { - var terminalStr = new Array(); - terminalStr.push('"', this.tokens, '"'); - return terminalStr.join(""); + var terminalStr = new Array(); + terminalStr.push('"', this.tokens, '"'); + return terminalStr.join(""); }; function Alt(tokens, prefixes) { @@ -682,15 +682,15 @@ function Alt(tokens, prefixes) { // Object to represent pre in grammar rules function SymLit(i,label) { - this.id = "Lit"; - this.i = i; - this.label = label; + this.id = "Lit"; + this.i = i; + this.label = label; } SymLit.prototype.getId = function () { return this.id; }; SymLit.prototype.show = function () { - var argStr = new Array(); - argStr.push(this.i, this.label); - return argStr.join("."); + var argStr = new Array(); + argStr.push(this.i, this.label); + return argStr.join("."); }; // Parsing @@ -732,11 +732,11 @@ Trie.prototype.lookup = function(key,obj) { Trie.prototype.isEmpty = function() { if (this.value != null) return false; - + for (var i in this.items) { return false; } - + return true; } @@ -747,7 +747,7 @@ function ParseState(concrete, startCat) { this.chart = new Chart(concrete); var items = new Array(); - + var fids = concrete.startCats[startCat]; if (fids != null) { var fid; @@ -762,7 +762,7 @@ function ParseState(concrete, startCat) { } } } - + this.items.insertChain(new Array(), items); } ParseState.prototype.next = function (token) { @@ -785,7 +785,7 @@ ParseState.prototype.next = function (token) { else return null; } - + return null; } , function (tokens, item) { @@ -802,7 +802,7 @@ ParseState.prototype.next = function (token) { this.items = acc; this.chart.shift(); - + return !this.items.isEmpty(); } /** @@ -812,35 +812,35 @@ ParseState.prototype.next = function (token) { */ ParseState.prototype.complete = function (currentToken) { - // Initialise accumulator for suggestions - var acc = this.items.lookup(currentToken); - if (acc == null) - acc = new Trie(); - - this.process( - // Items - this.items.value, - - // Deal with literal categories - function (fid) { - // Always return null, as suggested by Krasimir - return null; - }, - - // Takes an array of tokens and populates the accumulator - function (tokens, item) { - if (currentToken == "" || tokens[0].indexOf(currentToken) == 0) { //if begins with... - var tokens1 = new Array(); - for (var i = 1; i < tokens.length; i++) { - tokens1[i-1] = tokens[i]; - } - acc.insertChain1(tokens1, item); - } - } - ); - - // Return matches - return acc; + // Initialise accumulator for suggestions + var acc = this.items.lookup(currentToken); + if (acc == null) + acc = new Trie(); + + this.process( + // Items + this.items.value, + + // Deal with literal categories + function (fid) { + // Always return null, as suggested by Krasimir + return null; + }, + + // Takes an array of tokens and populates the accumulator + function (tokens, item) { + if (currentToken == "" || tokens[0].indexOf(currentToken) == 0) { //if begins with... + var tokens1 = new Array(); + for (var i = 1; i < tokens.length; i++) { + tokens1[i-1] = tokens[i]; + } + acc.insertChain1(tokens1, item); + } + } + ); + + // Return matches + return acc; } ParseState.prototype.extractTrees = function() { this.process( this.items.value @@ -850,11 +850,11 @@ ParseState.prototype.extractTrees = function() { , function (tokens, item) { } ); - - + + var totalFIds = this.concrete.totalFIds; var forest = this.chart.forest; - + function go(fid) { if (fid < totalFIds) { return [new Fun("?")]; @@ -864,24 +864,24 @@ ParseState.prototype.extractTrees = function() { var rules = forest[fid]; for (var j in rules) { var rule = rules[j]; - + if (rule.id == "Const") { trees.push(rule.lit); - } else { + } else { var arg_ix = new Array(); var arg_ts = new Array(); for (var k in rule.args) { arg_ix[k] = 0; arg_ts[k] = go(rule.args[k].fid); } - + while (true) { var t = new Fun(rule.fun.name); for (var k in arg_ts) { t.setArg(k,arg_ts[k][arg_ix[k]]); } trees.push(t); - + var i = 0; while (i < arg_ts.length) { arg_ix[i]++; @@ -889,26 +889,26 @@ ParseState.prototype.extractTrees = function() { break; arg_ix[i] = 0; - i++; + i++; } - + if (i >= arg_ts.length) break; } } } - + return trees; } } - + var trees = new Array(); var fids = this.concrete.startCats[this.startCat]; if (fids != null) { var fid0; for (fid0 = fids.s; fid0 <= fids.e; fid0++) { - + var labels = new Object(); var rules = this.chart.expandForest(fid0); for (var i in rules) { @@ -916,7 +916,7 @@ ParseState.prototype.extractTrees = function() { labels[lbl] = true; } } - + for (var lbl in labels) { var fid = this.chart.lookupPC(fid0,lbl,0); var arg_ts = go(fid); @@ -928,14 +928,14 @@ ParseState.prototype.extractTrees = function() { break; } } - + if (!isMember) trees.push(arg_ts[i]); } } } - } - + } + return trees; } ParseState.prototype.process = function (agenda,literalCallback,tokenCallback) { @@ -966,10 +966,10 @@ ParseState.prototype.process = function (agenda,literalCallback,tokenCallback) { break; } } - + if (!isMember) { items.push(item); - + var fid2 = this.chart.lookupPC(fid,label,this.chart.offset); if (fid2 != null) { agenda.push(item.shiftOverArg(sym.i,fid2)); @@ -1004,7 +1004,7 @@ ParseState.prototype.process = function (agenda,literalCallback,tokenCallback) { var fid = this.chart.lookupPC(item.fid,item.lbl,item.offset); if (fid == null) { fid = this.chart.nextId++; - + var items = this.chart.lookupACo(item.offset,item.fid,item.lbl); if (items != null) { for (var j in items) { @@ -1013,7 +1013,7 @@ ParseState.prototype.process = function (agenda,literalCallback,tokenCallback) { agenda.push(pitem.shiftOverArg(i,fid)); } } - + this.chart.insertPC(item.fid,item.lbl,item.offset,fid); this.chart.forest[fid] = [new Apply(item.fun,item.args)]; } else { @@ -1023,16 +1023,16 @@ ParseState.prototype.process = function (agenda,literalCallback,tokenCallback) { agenda.push(new ActiveItem(this.chart.offset,0,item.fun,item.fun.lins[lbl],item.args,fid,lbl)); } } - + var rules = this.chart.forest[fid]; var rule = new Apply(item.fun,item.args); - + var isMember = false; for (var j in rules) { if (rules[j].isEqual(rule)) isMember = true; } - + if (!isMember) rules.push(rule); } @@ -1048,7 +1048,7 @@ function Chart(concrete) { this.forest = new Object(); this.nextId = concrete.totalFIds; this.offset = 0; - + for (var fid in concrete.pproductions) { this.forest[fid] = concrete.pproductions[fid]; } @@ -1061,7 +1061,7 @@ Chart.prototype.lookupAC = function (fid,label) { } Chart.prototype.lookupACo = function (offset,fid,label) { var tmp; - + if (offset == this.offset) tmp = this.active[fid]; else @@ -1094,15 +1094,15 @@ Chart.prototype.insertPC = function (fid1,label,offset,fid2) { Chart.prototype.shift = function () { this.actives.push(this.active); this.active = new Object(); - + this.passive = new Object(); - + this.offset++; } Chart.prototype.expandForest = function (fid) { var rules = new Array(); var forest = this.forest; - + var go = function (rules0) { for (var i in rules0) { var rule = rules0[i]; -- cgit v1.2.3 From 63606fd2d0eaeb374d22116a5a6159a9ae7c12f5 Mon Sep 17 00:00:00 2001 From: "John J. Camilleri" Date: Mon, 10 Jun 2019 09:29:43 +0200 Subject: Minor indentation fixes in gflib.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Despite it being deprecated 🙈 --- src/runtime/javascript/gflib.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/runtime/javascript') diff --git a/src/runtime/javascript/gflib.js b/src/runtime/javascript/gflib.js index 45bb32d3d..0dc5a2ff3 100644 --- a/src/runtime/javascript/gflib.js +++ b/src/runtime/javascript/gflib.js @@ -391,11 +391,11 @@ GFConcrete.prototype.unlex = function (ts) { }; GFConcrete.prototype.tagIt = function (obj, tag) { if (isString(obj)) { - var o = new String(obj); - o.setTag(tag); - return o; + var o = new String(obj); + o.setTag(tag); + return o; } else { - var me = arguments.callee; + var me = arguments.callee; if (arguments.length == 2) { me.prototype = obj; var o = new me(); -- cgit v1.2.3 From 9ba4a42426b43ff7f7f19ca0ba4f74b175b9b84a Mon Sep 17 00:00:00 2001 From: "John J. Camilleri" Date: Mon, 10 Jun 2019 10:15:03 +0200 Subject: Add generated gflib.js under `typescript/js`. Add deprecation notice in `javascript`. --- src/runtime/javascript/DEPRECATED.md | 7 + src/runtime/typescript/.gitignore | 1 - src/runtime/typescript/js/gflib.js | 1114 ++++++++++++++++++++++++++++++++++ src/runtime/typescript/tsconfig.json | 4 +- 4 files changed, 1124 insertions(+), 2 deletions(-) create mode 100644 src/runtime/javascript/DEPRECATED.md delete mode 100644 src/runtime/typescript/.gitignore create mode 100644 src/runtime/typescript/js/gflib.js (limited to 'src/runtime/javascript') diff --git a/src/runtime/javascript/DEPRECATED.md b/src/runtime/javascript/DEPRECATED.md new file mode 100644 index 000000000..a4a8993c6 --- /dev/null +++ b/src/runtime/javascript/DEPRECATED.md @@ -0,0 +1,7 @@ +# Deprecation notice + +As of June 2019, this JavaScript version of the GF runtime is considered deprecated, +in favour of the TypeScript version in `../typescript/gflib.ts`. + +If you don't want/need TypeScript and are just looking for a ready-to-use JavaScript version, +see `../typescript/js/gflib.js` which is generated directly from the TypeScript version. diff --git a/src/runtime/typescript/.gitignore b/src/runtime/typescript/.gitignore deleted file mode 100644 index a6c7c2852..000000000 --- a/src/runtime/typescript/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.js diff --git a/src/runtime/typescript/js/gflib.js b/src/runtime/typescript/js/gflib.js new file mode 100644 index 000000000..04f78acd1 --- /dev/null +++ b/src/runtime/typescript/js/gflib.js @@ -0,0 +1,1114 @@ +var GFGrammar = (function () { + function GFGrammar(abstract, concretes) { + this.abstract = abstract; + this.concretes = concretes; + } + GFGrammar.prototype.translate = function (input, fromLang, toLang) { + var outputs = {}; + var fromConcs = this.concretes; + if (fromLang) { + fromConcs = {}; + fromConcs[fromLang] = this.concretes[fromLang]; + } + var toConcs = this.concretes; + if (toLang) { + toConcs = {}; + toConcs[toLang] = this.concretes[toLang]; + } + for (var c1 in fromConcs) { + var concrete = this.concretes[c1]; + var trees = concrete.parseString(input, this.abstract.startcat); + if (trees.length > 0) { + outputs[c1] = []; + for (var i in trees) { + outputs[c1][i] = {}; + for (var c2 in toConcs) { + outputs[c1][i][c2] = this.concretes[c2].linearize(trees[i]); + } + } + } + } + return outputs; + }; + return GFGrammar; +}()); +var Fun = (function () { + function Fun(name) { + var args = []; + for (var _i = 1; _i < arguments.length; _i++) { + args[_i - 1] = arguments[_i]; + } + this.name = name; + this.args = []; + for (var i = 1; i < args.length; i++) { + this.args[i - 1] = args[i]; + } + } + Fun.prototype.print = function () { + return this.show(0); + }; + Fun.prototype.show = function (prec) { + if (this.isMeta()) { + if (isUndefined(this.type)) { + return '?'; + } + else { + var s = '?:' + this.type; + if (prec > 0) { + s = '(' + s + ')'; + } + return s; + } + } + else { + var s = this.name; + var cs = this.args; + for (var i in cs) { + s += ' ' + (isUndefined(cs[i]) ? 'undefined' : cs[i].show(1)); + } + if (prec > 0 && cs.length > 0) { + s = '(' + s + ')'; + } + return s; + } + }; + Fun.prototype.getArg = function (i) { + return this.args[i]; + }; + Fun.prototype.setArg = function (i, c) { + this.args[i] = c; + }; + Fun.prototype.isMeta = function () { + return this.name == '?'; + }; + Fun.prototype.isComplete = function () { + if (this.isMeta()) { + return false; + } + else { + for (var i in this.args) { + if (!this.args[i].isComplete()) { + return false; + } + } + return true; + } + }; + Fun.prototype.isLiteral = function () { + return (/^[\"\-\d]/).test(this.name); + }; + Fun.prototype.isString = function () { + return (/^\".*\"$/).test(this.name); + }; + Fun.prototype.isInt = function () { + return (/^\-?\d+$/).test(this.name); + }; + Fun.prototype.isFloat = function () { + return (/^\-?\d*(\.\d*)?$/).test(this.name) && this.name != '.' && this.name != '-.'; + }; + Fun.prototype.isEqual = function (obj) { + if (this.name != obj.name) + return false; + for (var i in this.args) { + if (!this.args[i].isEqual(obj.args[i])) + return false; + } + return true; + }; + return Fun; +}()); +var GFAbstract = (function () { + function GFAbstract(startcat, types) { + this.startcat = startcat; + this.types = types; + } + GFAbstract.prototype.addType = function (fun, args, cat) { + this.types[fun] = new Type(args, cat); + }; + GFAbstract.prototype.getArgs = function (fun) { + return this.types[fun].args; + }; + GFAbstract.prototype.getCat = function (fun) { + return this.types[fun].cat; + }; + GFAbstract.prototype.annotate = function (tree, type) { + if (tree.name == '?') { + tree.type = type; + } + else { + var typ = this.types[tree.name]; + for (var i in tree.args) { + this.annotate(tree.args[i], typ.args[i]); + } + } + return tree; + }; + GFAbstract.prototype.handleLiterals = function (tree, type) { + if (tree.name != '?') { + if (type == 'String' || type == 'Int' || type == 'Float') { + tree.name = type + '_Literal_' + tree.name; + } + else { + var typ = this.types[tree.name]; + for (var i in tree.args) { + this.handleLiterals(tree.args[i], typ.args[i]); + } + } + } + return tree; + }; + GFAbstract.prototype.copyTree = function (x) { + var t = new Fun(x.name); + if (!isUndefined(x.type)) { + t.type = x.type; + } + var cs = x.args; + if (!isUndefined(cs)) { + for (var i = 0; i < cs.length; i++) { + t.setArg(i, this.copyTree(cs[i])); + } + } + return t; + }; + GFAbstract.prototype.parseTree = function (str, type) { + return this.annotate(this.parseTree_(str.match(/[\w\'\.\"]+|\(|\)|\?|\:/g), 0), type); + }; + GFAbstract.prototype.parseTree_ = function (tokens, prec) { + if (tokens.length == 0 || tokens[0] == ')') { + return null; + } + var t = tokens.shift(); + if (t == '(') { + var tree = this.parseTree_(tokens, 0); + tokens.shift(); + return tree; + } + else if (t == '?') { + return new Fun('?'); + } + else { + var tree = new Fun(t); + if (prec == 0) { + var c = void 0; + var i = void 0; + for (i = 0; (c = this.parseTree_(tokens, 1)) !== null; i++) { + tree.setArg(i, c); + } + } + return tree; + } + }; + return GFAbstract; +}()); +var Type = (function () { + function Type(args, cat) { + this.args = args; + this.cat = cat; + } + return Type; +}()); +var GFConcrete = (function () { + function GFConcrete(flags, productions, functions, sequences, startCats, totalFIds) { + this.flags = flags; + this.functions = functions; + this.startCats = startCats; + this.totalFIds = totalFIds; + this.pproductions = productions; + this.lproductions = {}; + var _loop_1 = function (fid0) { + var fid = parseInt(fid0); + var _loop_2 = function (i) { + var rule = productions[fid][i]; + if (rule.id == 'Apply') { + rule = rule; + var fun_1 = this_1.functions[rule.fun]; + var lproductions_1 = this_1.lproductions; + rule.fun = fun_1; + var register_1 = function (args, key, i) { + if (i < args.length) { + var c = 0; + var arg = args[i].fid; + for (var k in productions[arg]) { + var rule_1 = productions[arg][k]; + if (rule_1.id == 'Coerce') { + rule_1 = rule_1; + register_1(args, key + '_' + rule_1.arg, i + 1); + c++; + } + } + if (c == 0) { + register_1(args, key + '_' + arg, i + 1); + } + } + else { + var set = lproductions_1[key]; + if (set == null) { + set = []; + lproductions_1[key] = set; + } + set.push({ fun: fun_1, fid: fid }); + } + }; + register_1(rule.args, rule.fun.name, 0); + } + }; + for (var i in productions[fid]) { + _loop_2(i); + } + }; + var this_1 = this; + for (var fid0 in productions) { + _loop_1(fid0); + } + for (var _i = 0, functions_1 = functions; _i < functions_1.length; _i++) { + var fun = functions_1[_i]; + for (var j in fun.lins) { + fun.lins[j] = sequences[fun.lins[j]]; + } + } + } + GFConcrete.prototype.linearizeSyms = function (tree, tag) { + var res = []; + if (tree.isString()) { + var sym = new SymKS(tree.name); + sym.tag = tag; + res.push({ fid: -1, table: [[sym]] }); + } + else if (tree.isInt()) { + var sym = new SymKS(tree.name); + sym.tag = tag; + res.push({ fid: -2, table: [[sym]] }); + } + else if (tree.isFloat()) { + var sym = new SymKS(tree.name); + sym.tag = tag; + res.push({ fid: -3, table: [[sym]] }); + } + else if (tree.isMeta()) { + var cat = this.startCats[tree.type]; + var sym = new SymKS(tree.name); + sym.tag = tag; + for (var fid = cat.s; fid <= cat.e; fid++) { + res.push({ fid: fid, table: [[sym]] }); + } + } + else { + var cs_1 = []; + for (var i in tree.args) { + cs_1.push(this.linearizeSyms(tree.args[i], tag + '-' + i)[0]); + } + var key = tree.name; + for (var i in cs_1) { + key = key + '_' + cs_1[i].fid; + } + for (var i in this.lproductions[key]) { + var rule = this.lproductions[key][i]; + var row = { + fid: rule.fid, + table: [] + }; + var _loop_3 = function (j) { + var lin = rule.fun.lins[j]; + var toks = []; + row.table[j] = toks; + lin.forEach(function (sym0) { + switch (sym0.id) { + case 'Arg': + case 'Lit': { + var sym = sym0; + var ts = cs_1[sym.i].table[sym.label]; + for (var l in ts) { + toks.push(ts[l]); + } + break; + } + case 'KS': + case 'KP': { + var sym = sym0; + toks.push(sym.tagWith(tag)); + break; + } + } + }); + }; + for (var j in rule.fun.lins) { + _loop_3(j); + } + res.push(row); + } + } + return res; + }; + GFConcrete.prototype.syms2toks = function (syms) { + var ts = []; + var _loop_4 = function (i) { + var sym0 = syms[i]; + switch (sym0.id) { + case 'KS': { + var sym = sym0; + for (var j in sym.tokens) { + ts.push(sym.tokens[j].tagWith(sym.tag)); + } + break; + } + case 'KP': { + var sym_1 = sym0; + var addedAlt_1 = false; + if (i < syms.length - 1) { + var nextSym = syms[i + 1]; + if (nextSym.id == 'KS') { + var nextToken_1 = nextSym.tokens[0]; + sym_1.alts.forEach(function (alt) { + if (alt.prefixes.some(function (p) { return nextToken_1.startsWith(p); })) { + alt.tokens.forEach(function (symks) { + symks.tokens.forEach(function (t) { + ts.push(t.tagWith(sym_1.tag)); + }); + }); + addedAlt_1 = true; + return; + } + }); + } + } + if (addedAlt_1) + break; + sym_1.tokens.forEach(function (symks) { + symks.tokens.forEach(function (t) { + ts.push(t.tagWith(sym_1.tag)); + }); + }); + break; + } + } + }; + for (var i = 0; i < syms.length; i++) { + _loop_4(i); + } + return ts; + }; + GFConcrete.prototype.linearizeAll = function (tree) { + var _this = this; + return this.linearizeSyms(tree, '0').map(function (r) { + return _this.unlex(_this.syms2toks(r.table[0])); + }); + }; + GFConcrete.prototype.linearize = function (tree) { + var res = this.linearizeSyms(tree, '0'); + return this.unlex(this.syms2toks(res[0].table[0])); + }; + GFConcrete.prototype.tagAndLinearize = function (tree) { + var res = this.linearizeSyms(tree, '0'); + return this.syms2toks(res[0].table[0]); + }; + GFConcrete.prototype.unlex = function (ts) { + if (ts.length == 0) { + return ''; + } + var noSpaceAfter = /^[\(\-\[]/; + var noSpaceBefore = /^[\.\,\?\!\)\:\;\-\]]/; + var s = ''; + for (var i = 0; i < ts.length; i++) { + var t = ts[i]; + var after = i < ts.length - 1 ? ts[i + 1] : null; + s += t; + if (after != null + && !t.match(noSpaceAfter) + && !after.match(noSpaceBefore)) { + s += ' '; + } + } + return s; + }; + GFConcrete.prototype.tokenize = function (string) { + var inToken = false; + var start, end; + var tokens = []; + var i; + for (i = 0; i < string.length; i++) { + if (string.charAt(i) == ' ' + || string.charAt(i) == '\f' + || string.charAt(i) == '\n' + || string.charAt(i) == '\r' + || string.charAt(i) == '\t' + || string.charAt(i) == '\v' + || string.charAt(i) == String.fromCharCode(160)) { + if (inToken) { + end = i - 1; + inToken = false; + tokens.push(string.substr(start, end - start + 1)); + } + } + else { + if (!inToken) { + start = i; + inToken = true; + } + } + } + if (inToken) { + end = i - 1; + inToken = false; + tokens.push(string.substr(start, end - start + 1)); + } + return tokens; + }; + GFConcrete.prototype.parseString = function (string, cat) { + var tokens = this.tokenize(string); + var ps = new ParseState(this, cat); + for (var i in tokens) { + if (!ps.next(tokens[i])) + return []; + } + return ps.extractTrees(); + }; + GFConcrete.prototype.complete = function (input, cat) { + if (input == null) + input = ''; + var tokens = input.trim().split(' '); + for (var i = tokens.length - 1; i >= 0; i--) { + if (tokens[i] == '') { + tokens.splice(i, 1); + } + } + var current = tokens.pop(); + if (current == null) + current = ''; + var ps = new ParseState(this, cat); + var ps2 = new ParseState(this, cat); + for (var i = 0; i < tokens.length; i++) { + if (!ps.next(tokens[i])) { + return { 'consumed': [], 'suggestions': [] }; + } + ps2.next(tokens[i]); + } + if (ps2.next(current)) { + ps.next(current); + tokens.push(current); + current = ''; + } + var acc = ps.complete(current); + var suggs = []; + if (acc.value) { + acc.value.forEach(function (a) { + a.seq.forEach(function (s) { + if (s.tokens == null) + return; + switch (s.id) { + case 'KS': { + s.tokens.forEach(function (t) { + suggs.push(t); + }); + break; + } + case 'KP': { + s.tokens.forEach(function (symks) { + symks.tokens.forEach(function (t) { + suggs.push(t); + }); + }); + break; + } + } + }); + }); + } + return { 'consumed': tokens, 'suggestions': suggs }; + }; + return GFConcrete; +}()); +String.prototype.tagWith = function (tag) { + var s2 = this; + s2.tag = tag; + return s2; +}; +var Apply = (function () { + function Apply(fun, args) { + this.id = 'Apply'; + this.fun = fun; + this.args = args; + } + Apply.prototype.show = function (cat) { + var recStr = []; + recStr.push(cat, ' -> ', this.fun.name, ' [', this.args, ']'); + return recStr.join(''); + }; + Apply.prototype.isEqual = function (obj) { + if (this.id != obj.id || this.fun != obj.fun || this.args.length != obj.args.length) + return false; + for (var i in this.args) { + if (this.args[i] != obj.args[i]) + return false; + } + return true; + }; + return Apply; +}()); +var Coerce = (function () { + function Coerce(arg) { + this.id = 'Coerce'; + this.arg = arg; + } + Coerce.prototype.show = function (cat) { + var recStr = []; + recStr.push(cat, ' -> _ [', this.arg, ']'); + return recStr.join(''); + }; + return Coerce; +}()); +var PArg = (function () { + function PArg() { + var hypos = []; + for (var _i = 0; _i < arguments.length; _i++) { + hypos[_i] = arguments[_i]; + } + this.fid = hypos[hypos.length - 1]; + if (hypos.length > 1) + this.hypos = hypos.slice(0, hypos.length - 1); + } + return PArg; +}()); +var Const = (function () { + function Const(lit, toks) { + this.id = 'Const'; + this.lit = lit; + this.toks = toks; + } + Const.prototype.show = function (cat) { + var recStr = []; + recStr.push(cat, ' -> ', this.lit.print()); + return recStr.join(''); + }; + Const.prototype.isEqual = function (obj) { + if (this.id != obj.id || this.lit.isEqual(obj.lit) || this.toks.length != obj.toks.length) + return false; + for (var i in this.toks) { + if (this.toks[i] != obj.toks[i]) + return false; + } + return true; + }; + return Const; +}()); +var CncFun = (function () { + function CncFun(name, lins) { + this.name = name; + this.lins = lins; + } + return CncFun; +}()); +var SymCat = (function () { + function SymCat(i, label) { + this.id = 'Arg'; + this.i = i; + this.label = label; + } + SymCat.prototype.show = function () { + var argStr = []; + argStr.push(this.i, this.label); + return argStr.join('.'); + }; + return SymCat; +}()); +var SymKS = (function () { + function SymKS() { + var tokens = []; + for (var _i = 0; _i < arguments.length; _i++) { + tokens[_i] = arguments[_i]; + } + this.id = 'KS'; + this.tokens = tokens; + } + SymKS.prototype.show = function () { + var terminalStr = []; + terminalStr.push('"', this.tokens, '"'); + return terminalStr.join(''); + }; + SymKS.prototype.tagWith = function (tag) { + var s = new SymKS(); + s.tokens = this.tokens.slice(); + s.tag = tag; + return s; + }; + return SymKS; +}()); +var SymKP = (function () { + function SymKP(tokens, alts) { + this.id = 'KP'; + this.tokens = tokens; + this.alts = alts; + } + SymKP.prototype.show = function () { + var terminalStr = []; + terminalStr.push('"', this.tokens, '"'); + return terminalStr.join(''); + }; + SymKP.prototype.tagWith = function (tag) { + var s = new SymKP(this.tokens.slice(), this.alts.slice()); + s.tag = tag; + return s; + }; + return SymKP; +}()); +var Alt = (function () { + function Alt(tokens, prefixes) { + this.tokens = tokens; + this.prefixes = prefixes; + } + return Alt; +}()); +var SymLit = (function () { + function SymLit(i, label) { + this.id = 'Lit'; + this.i = i; + this.label = label; + } + SymLit.prototype.getId = function () { + return this.id; + }; + SymLit.prototype.show = function () { + var argStr = []; + argStr.push(this.i, this.label); + return argStr.join('.'); + }; + return SymLit; +}()); +var Trie = (function () { + function Trie() { + this.value = null; + this.items = {}; + } + Trie.prototype.insertChain = function (keys, obj) { + var node = this; + keys.forEach(function (key) { + var nnode = node.items[key]; + if (nnode == null) { + nnode = new Trie(); + node.items[key] = nnode; + } + node = nnode; + }); + node.value = obj; + }; + Trie.prototype.insertChain1 = function (keys, obj) { + var node = this; + keys.forEach(function (key) { + var nnode = node.items[key]; + if (nnode == null) { + nnode = new Trie(); + node.items[key] = nnode; + } + node = nnode; + }); + if (node.value == null) + node.value = [obj]; + else + node.value.push(obj); + }; + Trie.prototype.lookup = function (key) { + return this.items[key]; + }; + Trie.prototype.isEmpty = function () { + if (this.value != null) + return false; + for (var _ in this.items) { + return false; + } + return true; + }; + return Trie; +}()); +var ParseState = (function () { + function ParseState(concrete, startCat) { + this.concrete = concrete; + this.startCat = startCat; + this.items = new Trie(); + this.chart = new Chart(concrete); + var items = []; + var fids = concrete.startCats[startCat]; + if (fids != null) { + var fid = void 0; + for (fid = fids.s; fid <= fids.e; fid++) { + var exProds = this.chart.expandForest(fid); + for (var j in exProds) { + var rule = exProds[j]; + var fun = rule.fun; + for (var lbl in fun.lins) { + items.push(new ActiveItem(0, 0, rule.fun, fun.lins[lbl], rule.args, fid, parseInt(lbl))); + } + } + } + } + this.items.insertChain([], items); + } + ParseState.prototype.next = function (token) { + var acc = this.items.lookup(token); + if (acc == null) { + acc = new Trie(); + } + this.process(this.items.value, function (fid) { + switch (fid) { + case -1: + return new Const(new Fun('"' + token + '"'), [token]); + case -2: { + var x = parseInt(token, 10); + if (token == '0' || (x != 0 && !isNaN(x))) + return new Const(new Fun(token), [token]); + else + return null; + } + case -3: { + var x = parseFloat(token); + if (token == '0' || token == '0.0' || (x != 0 && !isNaN(x))) + return new Const(new Fun(token), [token]); + else + return null; + } + } + return null; + }, function (tokens, item) { + if (tokens[0] == token) { + var tokens1 = []; + for (var i = 1; i < tokens.length; i++) { + tokens1[i - 1] = tokens[i]; + } + acc.insertChain1(tokens1, item); + } + }); + this.items = acc; + this.chart.shift(); + return !this.items.isEmpty(); + }; + ParseState.prototype.complete = function (currentToken) { + var acc = this.items.lookup(currentToken); + if (acc == null) + acc = new Trie(); + this.process(this.items.value, function (_fid) { + return null; + }, function (tokens, item) { + if (currentToken == '' || tokens[0].indexOf(currentToken) == 0) { + var tokens1 = []; + for (var i = 1; i < tokens.length; i++) { + tokens1[i - 1] = tokens[i]; + } + acc.insertChain1(tokens1, item); + } + }); + return acc; + }; + ParseState.prototype.extractTrees = function () { + this.process(this.items.value, function (_fid) { + return null; + }, function (_tokens, _item) { + }); + var totalFIds = this.concrete.totalFIds; + var forest = this.chart.forest; + function go(fid) { + if (fid < totalFIds) { + return [new Fun('?')]; + } + else { + var trees_1 = []; + var rules = forest[fid]; + for (var j in rules) { + var rule = rules[j]; + if (rule.id == 'Const') { + trees_1.push(rule.lit); + } + else { + rule = rule; + var arg_ix = []; + var arg_ts = []; + for (var k in rule.args) { + arg_ix[k] = 0; + arg_ts[k] = go(rule.args[k].fid); + } + while (true) { + var t = new Fun(rule.fun.name); + for (var k = 0; k < arg_ts.length; k++) { + t.setArg(k, arg_ts[k][arg_ix[k]]); + } + trees_1.push(t); + var i = 0; + while (i < arg_ts.length) { + arg_ix[i]++; + if (arg_ix[i] < arg_ts[i].length) + break; + arg_ix[i] = 0; + i++; + } + if (i >= arg_ts.length) + break; + } + } + } + return trees_1; + } + } + var trees = []; + var fids = this.concrete.startCats[this.startCat]; + if (fids != null) { + var _loop_5 = function (fid0) { + var labels = {}; + var rules = this_2.chart.expandForest(fid0); + rules.forEach(function (rule) { + for (var lbl in rule.fun.lins) { + labels[lbl] = true; + } + }); + for (var lbl0 in labels) { + var lbl = parseInt(lbl0); + var fid = this_2.chart.lookupPC(fid0, lbl, 0); + var arg_ts = go(fid); + for (var i in arg_ts) { + var isMember = false; + for (var j in trees) { + if (arg_ts[i].isEqual(trees[j])) { + isMember = true; + break; + } + } + if (!isMember) + trees.push(arg_ts[i]); + } + } + }; + var this_2 = this; + for (var fid0 = fids.s; fid0 <= fids.e; fid0++) { + _loop_5(fid0); + } + } + return trees; + }; + ParseState.prototype.process = function (agenda, literalCallback, tokenCallback) { + if (agenda != null) { + var _loop_6 = function () { + var item = agenda.pop(); + var lin = item.seq; + if (item.dot < lin.length) { + var sym0 = lin[item.dot]; + switch (sym0.id) { + case 'Arg': { + var sym = sym0; + var fid = item.args[sym.i].fid; + var label = sym.label; + var items = this_3.chart.lookupAC(fid, label); + if (items == null) { + var rules = this_3.chart.expandForest(fid); + for (var j in rules) { + var rule = rules[j]; + agenda.push(new ActiveItem(this_3.chart.offset, 0, rule.fun, rule.fun.lins[label], rule.args, fid, label)); + } + this_3.chart.insertAC(fid, label, [item]); + } + else { + var isMember = false; + for (var j in items) { + if (items[j].isEqual(item)) { + isMember = true; + break; + } + } + if (!isMember) { + items.push(item); + var fid2 = this_3.chart.lookupPC(fid, label, this_3.chart.offset); + if (fid2 != null) { + agenda.push(item.shiftOverArg(sym.i, fid2)); + } + } + } + break; + } + case 'KS': { + var sym = sym0; + tokenCallback(sym.tokens, item.shiftOverTokn()); + break; + } + case 'KP': { + var sym = sym0; + var pitem_1 = item.shiftOverTokn(); + sym.tokens.forEach(function (symks) { + tokenCallback(symks.tokens, pitem_1); + }); + sym.alts.forEach(function (alt) { + alt.tokens.forEach(function (symks) { + tokenCallback(symks.tokens, pitem_1); + }); + }); + break; + } + case 'Lit': { + var sym = sym0; + var fid = item.args[sym.i].fid; + var rules = this_3.chart.forest[fid]; + if (rules != null) { + tokenCallback(rules[0].toks, item.shiftOverTokn()); + } + else { + var rule = literalCallback(fid); + if (rule != null) { + fid = this_3.chart.nextId++; + this_3.chart.forest[fid] = [rule]; + tokenCallback(rule.toks, item.shiftOverArg(sym.i, fid)); + } + } + break; + } + } + } + else { + var fid_1 = this_3.chart.lookupPC(item.fid, item.lbl, item.offset); + if (fid_1 == null) { + fid_1 = this_3.chart.nextId++; + var items = this_3.chart.lookupACo(item.offset, item.fid, item.lbl); + if (items != null) { + items.forEach(function (pitem) { + var i = pitem.seq[pitem.dot].i; + agenda.push(pitem.shiftOverArg(i, fid_1)); + }); + } + this_3.chart.insertPC(item.fid, item.lbl, item.offset, fid_1); + this_3.chart.forest[fid_1] = [new Apply(item.fun, item.args)]; + } + else { + var labels = this_3.chart.labelsAC(fid_1); + if (labels != null) { + for (var lbl in labels) { + agenda.push(new ActiveItem(this_3.chart.offset, 0, item.fun, item.fun.lins[lbl], item.args, fid_1, parseInt(lbl))); + } + } + var rules = this_3.chart.forest[fid_1]; + var rule_2 = new Apply(item.fun, item.args); + var isMember_1 = false; + rules.forEach(function (rule1) { + if (rule1.isEqual(rule_2)) + isMember_1 = true; + }); + if (!isMember_1) + rules.push(rule_2); + } + } + }; + var this_3 = this; + while (agenda.length > 0) { + _loop_6(); + } + } + }; + return ParseState; +}()); +var Chart = (function () { + function Chart(concrete) { + this.active = {}; + this.actives = []; + this.passive = {}; + this.forest = {}; + this.nextId = concrete.totalFIds; + this.offset = 0; + for (var fid in concrete.pproductions) { + this.forest[fid] = concrete.pproductions[fid]; + } + } + Chart.prototype.lookupAC = function (fid, label) { + var tmp = this.active[fid]; + if (tmp == null) + return null; + return tmp[label]; + }; + Chart.prototype.lookupACo = function (offset, fid, label) { + var tmp; + if (offset == this.offset) + tmp = this.active[fid]; + else + tmp = this.actives[offset][fid]; + if (tmp == null) + return null; + return tmp[label]; + }; + Chart.prototype.labelsAC = function (fid) { + return this.active[fid]; + }; + Chart.prototype.insertAC = function (fid, label, items) { + var tmp = this.active[fid]; + if (tmp == null) { + tmp = {}; + this.active[fid] = tmp; + } + tmp[label] = items; + }; + Chart.prototype.lookupPC = function (fid, label, offset) { + var key = fid + '.' + label + '-' + offset; + return this.passive[key]; + }; + Chart.prototype.insertPC = function (fid1, label, offset, fid2) { + var key = fid1 + '.' + label + '-' + offset; + this.passive[key] = fid2; + }; + Chart.prototype.shift = function () { + this.actives.push(this.active); + this.active = {}; + this.passive = {}; + this.offset++; + }; + Chart.prototype.expandForest = function (fid) { + var rules = []; + var forest = this.forest; + var go = function (rules0) { + for (var i in rules0) { + var rule = rules0[i]; + switch (rule.id) { + case 'Apply': + rules.push(rule); + break; + case 'Coerce': + go(forest[rule.arg]); + break; + } + } + }; + go(this.forest[fid]); + return rules; + }; + return Chart; +}()); +var ActiveItem = (function () { + function ActiveItem(offset, dot, fun, seq, args, fid, lbl) { + this.offset = offset; + this.dot = dot; + this.fun = fun; + this.seq = seq; + this.args = args; + this.fid = fid; + this.lbl = lbl; + } + ActiveItem.prototype.isEqual = function (obj) { + return (this.offset == obj.offset && + this.dot == obj.dot && + this.fun == obj.fun && + this.seq == obj.seq && + this.args == obj.args && + this.fid == obj.fid && + this.lbl == obj.lbl); + }; + ActiveItem.prototype.shiftOverArg = function (i, fid) { + var nargs = []; + for (var k in this.args) { + nargs[k] = this.args[k]; + } + nargs[i] = new PArg(fid); + return new ActiveItem(this.offset, this.dot + 1, this.fun, this.seq, nargs, this.fid, this.lbl); + }; + ActiveItem.prototype.shiftOverTokn = function () { + return new ActiveItem(this.offset, this.dot + 1, this.fun, this.seq, this.args, this.fid, this.lbl); + }; + return ActiveItem; +}()); +function isUndefined(a) { + return typeof a == 'undefined'; +} +Object.defineProperty(String.prototype, 'startsWith', { + value: function (search, pos) { + pos = !pos || pos < 0 ? 0 : +pos; + return this.substring(pos, pos + search.length) === search; + } +}); diff --git a/src/runtime/typescript/tsconfig.json b/src/runtime/typescript/tsconfig.json index 9a6cb7ac6..ab8f984db 100644 --- a/src/runtime/typescript/tsconfig.json +++ b/src/runtime/typescript/tsconfig.json @@ -2,7 +2,9 @@ "compilerOptions": { "module": "none", "target": "es3", - "noImplicitAny": true + "noImplicitAny": true, + "removeComments": true, + "outDir": "js" }, "compileOnSave": true } -- cgit v1.2.3