Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial commit to svn

  • Loading branch information...
commit df938fc2b21eeb1a969594682e4672755c008fe7 0 parents
Alessandro Warth authored
1  bs-js-compiler.js
... ... @@ -0,0 +1 @@
  1 +{BSJSParser=Parser.delegated({"fromTo":function(){var $elf=this,x,y;return (function(){x=$elf._apply("anything");y=$elf._apply("anything");$elf._applyWithArgs("seq",x);$elf._many(function(){return (function(){$elf._not(function(){return $elf._applyWithArgs("seq",y)});return $elf._apply("char")})()});return $elf._applyWithArgs("seq",y)})()},"space":function(){var $elf=this;return $elf._or((function(){return Parser._superApplyWithArgs($elf,"space")}),(function(){return $elf._applyWithArgs("fromTo","//","\n")}),(function(){return $elf._applyWithArgs("fromTo","/*","*/")}))},"nameFirst":function(){var $elf=this;return $elf._or((function(){return $elf._apply("letter")}),(function(){return $elf._applyWithArgs("exactly","$")}),(function(){return $elf._applyWithArgs("exactly","_")}))},"nameRest":function(){var $elf=this;return $elf._or((function(){return $elf._apply("nameFirst")}),(function(){return $elf._apply("digit")}))},"iName":function(){var $elf=this,r;return (function(){r=$elf._applyWithArgs("firstAndRest","nameFirst","nameRest");return r.join("")})()},"isKeyword":function(){var $elf=this,x;return (function(){x=$elf._apply("anything");return $elf._pred(BSJSParser._isKeyword(x))})()},"name":function(){var $elf=this,n;return (function(){n=$elf._apply("iName");$elf._not(function(){return $elf._applyWithArgs("isKeyword",n)});return ["name",(n == "self")?"$elf":n]})()},"keyword":function(){var $elf=this,k;return (function(){k=$elf._apply("iName");$elf._applyWithArgs("isKeyword",k);return [k,k]})()},"number":function(){var $elf=this,ws,fs;return (function(){ws=$elf._many1(function(){return $elf._apply("digit")});fs=$elf._or((function(){return (function(){$elf._applyWithArgs("exactly",".");return $elf._many1(function(){return $elf._apply("digit")})})()}),(function(){return (function(){$elf._apply("empty");return []})()}));return ["number",parseFloat(((ws.join("") + ".") + fs.join("")))]})()},"escapeChar":function(){var $elf=this,c;return (function(){$elf._applyWithArgs("exactly","\\");c=$elf._apply("char");return unescape(("\\" + c))})()},"str":function(){var $elf=this,cs,cs,cs,n;return $elf._or((function(){return (function(){$elf._applyWithArgs("seq","\"\"\"");cs=$elf._many(function(){return $elf._or((function(){return $elf._apply("escapeChar")}),(function(){return (function(){$elf._not(function(){return $elf._applyWithArgs("seq","\"\"\"")});return $elf._apply("char")})()}))});$elf._applyWithArgs("seq","\"\"\"");return ["string",cs.join("")]})()}),(function(){return (function(){$elf._applyWithArgs("exactly","\'");cs=$elf._many(function(){return $elf._or((function(){return $elf._apply("escapeChar")}),(function(){return (function(){$elf._not(function(){return $elf._applyWithArgs("exactly","\'")});return $elf._apply("char")})()}))});$elf._applyWithArgs("exactly","\'");return ["string",cs.join("")]})()}),(function(){return (function(){$elf._applyWithArgs("exactly","\"");cs=$elf._many(function(){return $elf._or((function(){return $elf._apply("escapeChar")}),(function(){return (function(){$elf._not(function(){return $elf._applyWithArgs("exactly","\"")});return $elf._apply("char")})()}))});$elf._applyWithArgs("exactly","\"");return ["string",cs.join("")]})()}),(function(){return (function(){$elf._or((function(){return $elf._applyWithArgs("exactly","#")}),(function(){return $elf._applyWithArgs("exactly","`")}));n=$elf._apply("iName");return ["string",n]})()}))},"special":function(){var $elf=this,s;return (function(){s=$elf._or((function(){return $elf._applyWithArgs("exactly","(")}),(function(){return $elf._applyWithArgs("exactly",")")}),(function(){return $elf._applyWithArgs("exactly","{")}),(function(){return $elf._applyWithArgs("exactly","}")}),(function(){return $elf._applyWithArgs("exactly","[")}),(function(){return $elf._applyWithArgs("exactly","]")}),(function(){return $elf._applyWithArgs("exactly",",")}),(function(){return $elf._applyWithArgs("exactly",";")}),(function(){return $elf._applyWithArgs("exactly","?")}),(function(){return $elf._applyWithArgs("exactly",":")}),(function(){return $elf._applyWithArgs("seq","!==")}),(function(){return $elf._applyWithArgs("seq","!=")}),(function(){return $elf._applyWithArgs("seq","===")}),(function(){return $elf._applyWithArgs("seq","==")}),(function(){return $elf._applyWithArgs("seq","=")}),(function(){return $elf._applyWithArgs("seq",">=")}),(function(){return $elf._applyWithArgs("exactly",">")}),(function(){return $elf._applyWithArgs("seq","<=")}),(function(){return $elf._applyWithArgs("exactly","<")}),(function(){return $elf._applyWithArgs("seq","++")}),(function(){return $elf._applyWithArgs("seq","+=")}),(function(){return $elf._applyWithArgs("exactly","+")}),(function(){return $elf._applyWithArgs("seq","--")}),(function(){return $elf._applyWithArgs("seq","-=")}),(function(){return $elf._applyWithArgs("exactly","-")}),(function(){return $elf._applyWithArgs("seq","*=")}),(function(){return $elf._applyWithArgs("exactly","*")}),(function(){return $elf._applyWithArgs("seq","/=")}),(function(){return $elf._applyWithArgs("exactly","/")}),(function(){return $elf._applyWithArgs("seq","%=")}),(function(){return $elf._applyWithArgs("exactly","%")}),(function(){return $elf._applyWithArgs("seq","&&=")}),(function(){return $elf._applyWithArgs("seq","&&")}),(function(){return $elf._applyWithArgs("seq","||=")}),(function(){return $elf._applyWithArgs("seq","||")}),(function(){return $elf._applyWithArgs("exactly",".")}),(function(){return $elf._applyWithArgs("exactly","!")}));return [s,s]})()},"tok":function(){var $elf=this;return (function(){$elf._apply("spaces");return $elf._or((function(){return $elf._apply("name")}),(function(){return $elf._apply("keyword")}),(function(){return $elf._apply("number")}),(function(){return $elf._apply("str")}),(function(){return $elf._apply("special")}))})()},"toks":function(){var $elf=this,ts;return (function(){ts=$elf._many(function(){return $elf._apply("token")});$elf._apply("spaces");$elf._apply("end");return ts})()},"token":function(){var $elf=this,tt,t;return (function(){tt=$elf._apply("anything");t=$elf._apply("tok");$elf._pred((t[(0)] == tt));return t[(1)]})()},"spacesNoNl":function(){var $elf=this;return $elf._many(function(){return (function(){$elf._not(function(){return $elf._applyWithArgs("exactly","\n")});return $elf._apply("space")})()})},"expr":function(){var $elf=this,e,t,f,rhs,rhs,rhs,rhs,rhs,rhs,rhs,rhs;return (function(){e=$elf._apply("orExpr");return $elf._or((function(){return (function(){$elf._applyWithArgs("token","?");t=$elf._apply("expr");$elf._applyWithArgs("token",":");f=$elf._apply("expr");return ["condExpr",e,t,f]})()}),(function(){return (function(){$elf._applyWithArgs("token","=");rhs=$elf._apply("expr");return ["set",e,rhs]})()}),(function(){return (function(){$elf._applyWithArgs("token","+=");rhs=$elf._apply("expr");return ["mset",e,"+",rhs]})()}),(function(){return (function(){$elf._applyWithArgs("token","-=");rhs=$elf._apply("expr");return ["mset",e,"-",rhs]})()}),(function(){return (function(){$elf._applyWithArgs("token","*=");rhs=$elf._apply("expr");return ["mset",e,"*",rhs]})()}),(function(){return (function(){$elf._applyWithArgs("token","/=");rhs=$elf._apply("expr");return ["mset",e,"/",rhs]})()}),(function(){return (function(){$elf._applyWithArgs("token","%=");rhs=$elf._apply("expr");return ["mset",e,"%",rhs]})()}),(function(){return (function(){$elf._applyWithArgs("token","&&=");rhs=$elf._apply("expr");return ["mset",e,"&&",rhs]})()}),(function(){return (function(){$elf._applyWithArgs("token","||=");rhs=$elf._apply("expr");return ["mset",e,"||",rhs]})()}),(function(){return (function(){$elf._apply("empty");return e})()}))})()},"orExpr":function(){var $elf=this,x,y;return $elf._or((function(){return (function(){x=$elf._apply("orExpr");$elf._applyWithArgs("token","||");y=$elf._apply("andExpr");return ["binop","||",x,y]})()}),(function(){return $elf._apply("andExpr")}))},"andExpr":function(){var $elf=this,x,y;return $elf._or((function(){return (function(){x=$elf._apply("andExpr");$elf._applyWithArgs("token","&&");y=$elf._apply("eqExpr");return ["binop","&&",x,y]})()}),(function(){return $elf._apply("eqExpr")}))},"eqExpr":function(){var $elf=this,x,y,y,y,y;return $elf._or((function(){return (function(){x=$elf._apply("eqExpr");return $elf._or((function(){return (function(){$elf._applyWithArgs("token","==");y=$elf._apply("relExpr");return ["binop","==",x,y]})()}),(function(){return (function(){$elf._applyWithArgs("token","!=");y=$elf._apply("relExpr");return ["binop","!=",x,y]})()}),(function(){return (function(){$elf._applyWithArgs("token","===");y=$elf._apply("relExpr");return ["binop","===",x,y]})()}),(function(){return (function(){$elf._applyWithArgs("token","!==");y=$elf._apply("relExpr");return ["binop","!==",x,y]})()}))})()}),(function(){return $elf._apply("relExpr")}))},"relExpr":function(){var $elf=this,x,y,y,y,y,y;return $elf._or((function(){return (function(){x=$elf._apply("relExpr");return $elf._or((function(){return (function(){$elf._applyWithArgs("token",">");y=$elf._apply("addExpr");return ["binop",">",x,y]})()}),(function(){return (function(){$elf._applyWithArgs("token",">=");y=$elf._apply("addExpr");return ["binop",">=",x,y]})()}),(function(){return (function(){$elf._applyWithArgs("token","<");y=$elf._apply("addExpr");return ["binop","<",x,y]})()}),(function(){return (function(){$elf._applyWithArgs("token","<=");y=$elf._apply("addExpr");return ["binop","<=",x,y]})()}),(function(){return (function(){$elf._applyWithArgs("token","instanceof");y=$elf._apply("addExpr");return ["binop","instanceof",x,y]})()}))})()}),(function(){return $elf._apply("addExpr")}))},"addExpr":function(){var $elf=this,x,y,x,y;return $elf._or((function(){return (function(){x=$elf._apply("addExpr");$elf._applyWithArgs("token","+");y=$elf._apply("mulExpr");return ["binop","+",x,y]})()}),(function(){return (function(){x=$elf._apply("addExpr");$elf._applyWithArgs("token","-");y=$elf._apply("mulExpr");return ["binop","-",x,y]})()}),(function(){return $elf._apply("mulExpr")}))},"mulExpr":function(){var $elf=this,x,y,x,y,x,y;return $elf._or((function(){return (function(){x=$elf._apply("mulExpr");$elf._applyWithArgs("token","*");y=$elf._apply("mulExpr");return ["binop","*",x,y]})()}),(function(){return (function(){x=$elf._apply("mulExpr");$elf._applyWithArgs("token","/");y=$elf._apply("mulExpr");return ["binop","/",x,y]})()}),(function(){return (function(){x=$elf._apply("mulExpr");$elf._applyWithArgs("token","%");y=$elf._apply("mulExpr");return ["binop","%",x,y]})()}),(function(){return $elf._apply("unary")}))},"unary":function(){var $elf=this,p,p,p,p,p;return $elf._or((function(){return (function(){$elf._applyWithArgs("token","-");p=$elf._apply("postfix");return ["unop","-",p]})()}),(function(){return (function(){$elf._applyWithArgs("token","+");p=$elf._apply("postfix");return p})()}),(function(){return (function(){$elf._applyWithArgs("token","++");p=$elf._apply("postfix");return ["preop","++",p]})()}),(function(){return (function(){$elf._applyWithArgs("token","--");p=$elf._apply("postfix");return ["preop","--",p]})()}),(function(){return (function(){$elf._applyWithArgs("token","!");p=$elf._apply("postfix");return ["unop","!",p]})()}),(function(){return $elf._apply("postfix")}))},"postfix":function(){var $elf=this,p;return (function(){p=$elf._apply("primExpr");return $elf._or((function(){return (function(){$elf._apply("spacesNoNl");$elf._applyWithArgs("token","++");return ["postop","++",p]})()}),(function(){return (function(){$elf._apply("spacesNoNl");$elf._applyWithArgs("token","--");return ["postop","--",p]})()}),(function(){return (function(){$elf._apply("empty");return p})()}))})()},"primExpr":function(){var $elf=this,p,i,m,as,f,as;return $elf._or((function(){return (function(){p=$elf._apply("primExpr");return $elf._or((function(){return (function(){$elf._applyWithArgs("token","[");i=$elf._apply("expr");$elf._applyWithArgs("token","]");return ["getp",i,p]})()}),(function(){return (function(){$elf._applyWithArgs("token",".");m=$elf._applyWithArgs("token","name");$elf._applyWithArgs("token","(");as=$elf._applyWithArgs("listOf","expr",",");$elf._applyWithArgs("token",")");return ["send",m,p].concat(as)})()}),(function(){return (function(){$elf._applyWithArgs("token",".");f=$elf._applyWithArgs("token","name");return ["getp",["string",f],p]})()}),(function(){return (function(){$elf._applyWithArgs("token","(");as=$elf._applyWithArgs("listOf","expr",",");$elf._applyWithArgs("token",")");return ["call",p].concat(as)})()}))})()}),(function(){return $elf._apply("primExprHd")}))},"primExprHd":function(){var $elf=this,e,n,n,s,n,as,es;return $elf._or((function(){return (function(){$elf._applyWithArgs("token","(");e=$elf._apply("expr");$elf._applyWithArgs("token",")");return e})()}),(function(){return (function(){$elf._applyWithArgs("token","this");return ["this"]})()}),(function(){return (function(){n=$elf._applyWithArgs("token","name");return ["get",n]})()}),(function(){return (function(){n=$elf._applyWithArgs("token","number");return ["number",n]})()}),(function(){return (function(){s=$elf._applyWithArgs("token","string");return ["string",s]})()}),(function(){return (function(){$elf._applyWithArgs("token","function");return $elf._apply("funcRest")})()}),(function(){return (function(){$elf._applyWithArgs("token","new");n=$elf._applyWithArgs("token","name");$elf._applyWithArgs("token","(");as=$elf._applyWithArgs("listOf","expr",",");$elf._applyWithArgs("token",")");return ["new",n].concat(as)})()}),(function(){return (function(){$elf._applyWithArgs("token","[");es=$elf._applyWithArgs("listOf","expr",",");$elf._applyWithArgs("token","]");return ["arr"].concat(es)})()}),(function(){return $elf._apply("json")}))},"json":function(){var $elf=this,bs;return (function(){$elf._applyWithArgs("token","{");bs=$elf._applyWithArgs("listOf","jsonBinding",",");$elf._applyWithArgs("token","}");return ["json"].concat(bs)})()},"jsonBinding":function(){var $elf=this,n,v;return (function(){n=$elf._apply("jsonPropName");$elf._applyWithArgs("token",":");v=$elf._apply("expr");return ["binding",n,v]})()},"jsonPropName":function(){var $elf=this;return $elf._or((function(){return $elf._applyWithArgs("token","name")}),(function(){return $elf._applyWithArgs("token","number")}),(function(){return $elf._applyWithArgs("token","string")}))},"formal":function(){var $elf=this;return (function(){$elf._apply("spaces");return $elf._applyWithArgs("token","name")})()},"funcRest":function(){var $elf=this,fs,body;return (function(){$elf._applyWithArgs("token","(");fs=$elf._applyWithArgs("listOf","formal",",");$elf._applyWithArgs("token",")");$elf._applyWithArgs("token","{");body=$elf._apply("srcElems");$elf._applyWithArgs("token","}");return ["func",fs,body]})()},"sc":function(){var $elf=this;return $elf._or((function(){return (function(){$elf._apply("spacesNoNl");return $elf._or((function(){return $elf._applyWithArgs("exactly","\n")}),(function(){return $elf._lookahead(function(){return $elf._applyWithArgs("exactly","}")})}),(function(){return $elf._apply("end")}))})()}),(function(){return $elf._applyWithArgs("token",";")}))},"binding":function(){var $elf=this,n,v;return (function(){n=$elf._applyWithArgs("token","name");v=$elf._or((function(){return (function(){$elf._applyWithArgs("token","=");return $elf._apply("expr")})()}),(function(){return (function(){$elf._apply("empty");return ["get","undefined"]})()}));return ["var",n,v]})()},"block":function(){var $elf=this,ss;return (function(){$elf._applyWithArgs("token","{");ss=$elf._apply("srcElems");$elf._applyWithArgs("token","}");return ss})()},"stmt":function(){var $elf=this,bs,c,t,f,c,s,s,c,i,c,u,s,n,v,e,s,e,c,cs,cs,cs,e,t,e,c,f,e,x,s,e;return $elf._or((function(){return $elf._apply("block")}),(function(){return (function(){$elf._applyWithArgs("token","var");bs=$elf._applyWithArgs("listOf","binding",",");$elf._apply("sc");return ["begin"].concat(bs)})()}),(function(){return (function(){$elf._applyWithArgs("token","if");$elf._applyWithArgs("token","(");c=$elf._apply("expr");$elf._applyWithArgs("token",")");t=$elf._apply("stmt");f=$elf._or((function(){return (function(){$elf._applyWithArgs("token","else");return $elf._apply("stmt")})()}),(function(){return (function(){$elf._apply("empty");return ["get","undefined"]})()}));return ["if",c,t,f]})()}),(function(){return (function(){$elf._applyWithArgs("token","while");$elf._applyWithArgs("token","(");c=$elf._apply("expr");$elf._applyWithArgs("token",")");s=$elf._apply("stmt");return ["while",c,s]})()}),(function(){return (function(){$elf._applyWithArgs("token","do");s=$elf._apply("stmt");$elf._applyWithArgs("token","while");$elf._applyWithArgs("token","(");c=$elf._apply("expr");$elf._applyWithArgs("token",")");$elf._apply("sc");return ["doWhile",s,c]})()}),(function(){return (function(){$elf._applyWithArgs("token","for");$elf._applyWithArgs("token","(");i=$elf._or((function(){return (function(){$elf._applyWithArgs("token","var");return $elf._apply("binding")})()}),(function(){return $elf._apply("expr")}),(function(){return (function(){$elf._apply("empty");return ["get","undefined"]})()}));$elf._applyWithArgs("token",";");c=$elf._or((function(){return $elf._apply("expr")}),(function(){return (function(){$elf._apply("empty");return ["get","true"]})()}));$elf._applyWithArgs("token",";");u=$elf._or((function(){return $elf._apply("expr")}),(function(){return (function(){$elf._apply("empty");return ["get","undefined"]})()}));$elf._applyWithArgs("token",")");s=$elf._apply("stmt");return ["for",i,c,u,s]})()}),(function(){return (function(){$elf._applyWithArgs("token","for");$elf._applyWithArgs("token","(");v=$elf._or((function(){return (function(){$elf._applyWithArgs("token","var");n=$elf._applyWithArgs("token","name");return ["var",n,["get","undefined"]]})()}),(function(){return $elf._apply("expr")}));$elf._applyWithArgs("token","in");e=$elf._apply("expr");$elf._applyWithArgs("token",")");s=$elf._apply("stmt");return ["forIn",v,e,s]})()}),(function(){return (function(){$elf._applyWithArgs("token","switch");$elf._applyWithArgs("token","(");e=$elf._apply("expr");$elf._applyWithArgs("token",")");$elf._applyWithArgs("token","{");cs=$elf._many(function(){return $elf._or((function(){return (function(){$elf._applyWithArgs("token","case");c=$elf._apply("expr");$elf._applyWithArgs("token",":");cs=$elf._apply("srcElems");return ["case",c,cs]})()}),(function(){return (function(){$elf._applyWithArgs("token","default");$elf._applyWithArgs("token",":");cs=$elf._apply("srcElems");return ["default",cs]})()}))});$elf._applyWithArgs("token","}");return ["switch",e].concat(cs)})()}),(function(){return (function(){$elf._applyWithArgs("token","break");$elf._apply("sc");return ["break"]})()}),(function(){return (function(){$elf._applyWithArgs("token","continue");$elf._apply("sc");return ["continue"]})()}),(function(){return (function(){$elf._applyWithArgs("token","throw");$elf._apply("spacesNoNl");e=$elf._apply("expr");$elf._apply("sc");return ["throw",e]})()}),(function(){return (function(){$elf._applyWithArgs("token","try");t=$elf._apply("block");$elf._applyWithArgs("token","catch");$elf._applyWithArgs("token","(");e=$elf._applyWithArgs("token","name");$elf._applyWithArgs("token",")");c=$elf._apply("block");f=$elf._or((function(){return (function(){$elf._applyWithArgs("token","finally");return $elf._apply("block")})()}),(function(){return (function(){$elf._apply("empty");return ["get","undefined"]})()}));return ["try",t,e,c,f]})()}),(function(){return (function(){$elf._applyWithArgs("token","return");e=$elf._or((function(){return $elf._apply("expr")}),(function(){return (function(){$elf._apply("empty");return ["get","undefined"]})()}));$elf._apply("sc");return ["return",e]})()}),(function(){return (function(){$elf._applyWithArgs("token","with");$elf._applyWithArgs("token","(");x=$elf._apply("expr");$elf._applyWithArgs("token",")");s=$elf._apply("stmt");return ["with",x,s]})()}),(function(){return (function(){e=$elf._apply("expr");$elf._apply("sc");return e})()}))},"srcElem":function(){var $elf=this,n,f;return $elf._or((function(){return (function(){$elf._applyWithArgs("token","function");n=$elf._applyWithArgs("token","name");f=$elf._apply("funcRest");return ["var",n,f]})()}),(function(){return $elf._apply("stmt")}))},"srcElems":function(){var $elf=this,ss;return (function(){ss=$elf._many(function(){return $elf._apply("srcElem")});return ["begin"].concat(ss)})()},"topLevel":function(){var $elf=this,r;return (function(){r=$elf._apply("srcElems");$elf._apply("spaces");$elf._apply("end");return r})()},"curlySemAction":function(){var $elf=this,s,ss,r,r;return $elf._or((function(){return (function(){$elf._applyWithArgs("token","{");ss=$elf._many1(function(){return (function(){s=$elf._apply("srcElem");$elf._lookahead(function(){return $elf._apply("srcElem")});return s})()});r=$elf._apply("expr");$elf._apply("sc");$elf._applyWithArgs("token","}");$elf._apply("spaces");return (function (){ss.push(["return",r]);return ["call",["func",[],["begin"].concat(ss)]]})()})()}),(function(){return (function(){$elf._applyWithArgs("token","{");r=$elf._apply("expr");$elf._applyWithArgs("token","}");$elf._apply("spaces");return r})()}))},"semAction":function(){var $elf=this,r;return $elf._or((function(){return $elf._apply("curlySemAction")}),(function(){return (function(){r=$elf._apply("primExpr");$elf._apply("spaces");return r})()}))}});BSJSParser["keywords"]=({});keywords=["break","case","catch","continue","default","delete","do","else","finally","for","function","if","in","instanceof","new","return","switch","this","throw","try","typeof","var","void","while","with","ometa"];for(var idx=(0);(idx < keywords["length"]);idx++){BSJSParser["keywords"][keywords[idx]]=true}BSJSParser["_isKeyword"]=(function (k){return (this["keywords"].hasProperty(k) && (!Object["prototype"].hasProperty(k)))});BSJSTranslator=OMeta.delegated({"trans":function(){var $elf=this,t,ans;return (function(){$elf._form(function(){return (function(){t=$elf._apply("anything");return ans=$elf._applyWithArgs("apply",t)})()});return ans})()},"curlyTrans":function(){var $elf=this,r,rs,r;return $elf._or((function(){return (function(){$elf._form(function(){return (function(){$elf._applyWithArgs("exactly","begin");return r=$elf._apply("curlyTrans")})()});return r})()}),(function(){return (function(){$elf._form(function(){return (function(){$elf._applyWithArgs("exactly","begin");return rs=$elf._many(function(){return $elf._apply("trans")})})()});return (("{" + rs.join(";")) + "}")})()}),(function(){return (function(){r=$elf._apply("trans");return (("{" + r) + "}")})()}))},"this":function(){var $elf=this;return "this"},"break":function(){var $elf=this;return "break"},"continue":function(){var $elf=this;return "continue"},"number":function(){var $elf=this,n;return (function(){n=$elf._apply("anything");return (("(" + n) + ")")})()},"string":function(){var $elf=this,s;return (function(){s=$elf._apply("anything");return s.toProgramString()})()},"arr":function(){var $elf=this,xs;return (function(){xs=$elf._many(function(){return $elf._apply("trans")});return (("[" + xs.join(",")) + "]")})()},"unop":function(){var $elf=this,op,x;return (function(){op=$elf._apply("anything");x=$elf._apply("trans");return ((("(" + op) + x) + ")")})()},"getp":function(){var $elf=this,fd,x;return (function(){fd=$elf._apply("trans");x=$elf._apply("trans");return (((x + "[") + fd) + "]")})()},"get":function(){var $elf=this,x;return (function(){x=$elf._apply("anything");return x})()},"set":function(){var $elf=this,lhs,rhs;return (function(){lhs=$elf._apply("trans");rhs=$elf._apply("trans");return ((lhs + "=") + rhs)})()},"mset":function(){var $elf=this,lhs,op,rhs;return (function(){lhs=$elf._apply("trans");op=$elf._apply("anything");rhs=$elf._apply("trans");return (((lhs + op) + "=") + rhs)})()},"binop":function(){var $elf=this,op,x,y;return (function(){op=$elf._apply("anything");x=$elf._apply("trans");y=$elf._apply("trans");return (((((("(" + x) + " ") + op) + " ") + y) + ")")})()},"preop":function(){var $elf=this,op,x;return (function(){op=$elf._apply("anything");x=$elf._apply("trans");return (op + x)})()},"postop":function(){var $elf=this,op,x;return (function(){op=$elf._apply("anything");x=$elf._apply("trans");return (x + op)})()},"return":function(){var $elf=this,x;return (function(){x=$elf._apply("trans");return ("return " + x)})()},"with":function(){var $elf=this,x,s;return (function(){x=$elf._apply("trans");s=$elf._apply("curlyTrans");return ((("with(" + x) + ")") + s)})()},"if":function(){var $elf=this,cond,t,e;return (function(){cond=$elf._apply("trans");t=$elf._apply("curlyTrans");e=$elf._apply("curlyTrans");return ((((("if(" + cond) + ")") + t) + "else") + e)})()},"condExpr":function(){var $elf=this,cond,t,e;return (function(){cond=$elf._apply("trans");t=$elf._apply("trans");e=$elf._apply("trans");return ((((cond + "?") + t) + ":") + e)})()},"while":function(){var $elf=this,cond,body;return (function(){cond=$elf._apply("trans");body=$elf._apply("curlyTrans");return ((("while(" + cond) + ")") + body)})()},"doWhile":function(){var $elf=this,body,cond;return (function(){body=$elf._apply("curlyTrans");cond=$elf._apply("trans");return (((("do" + body) + "while(") + cond) + ")")})()},"for":function(){var $elf=this,init,cond,upd,body;return (function(){init=$elf._apply("trans");cond=$elf._apply("trans");upd=$elf._apply("trans");body=$elf._apply("curlyTrans");return ((((((("for(" + init) + ";") + cond) + ";") + upd) + ")") + body)})()},"forIn":function(){var $elf=this,x,arr,body;return (function(){x=$elf._apply("trans");arr=$elf._apply("trans");body=$elf._apply("curlyTrans");return ((((("for(" + x) + " in ") + arr) + ")") + body)})()},"begin":function(){var $elf=this,x,x,xs;return $elf._or((function(){return (function(){x=$elf._apply("trans");$elf._apply("end");return x})()}),(function(){return (function(){xs=$elf._many(function(){return (function(){x=$elf._apply("trans");return $elf._or((function(){return (function(){$elf._or((function(){return $elf._pred((x[(x["length"] - (1))] == "}"))}),(function(){return $elf._apply("end")}));return x})()}),(function(){return (function(){$elf._apply("empty");return (x + ";")})()}))})()});return (("{" + xs.join("")) + "}")})()}))},"func":function(){var $elf=this,args,body;return (function(){args=$elf._apply("anything");body=$elf._apply("curlyTrans");return (((("(function (" + args.join(",")) + ")") + body) + ")")})()},"call":function(){var $elf=this,fn,args;return (function(){fn=$elf._apply("trans");args=$elf._many(function(){return $elf._apply("trans")});return (((fn + "(") + args.join(",")) + ")")})()},"send":function(){var $elf=this,msg,recv,args;return (function(){msg=$elf._apply("anything");recv=$elf._apply("trans");args=$elf._many(function(){return $elf._apply("trans")});return (((((recv + ".") + msg) + "(") + args.join(",")) + ")")})()},"new":function(){var $elf=this,cls,args;return (function(){cls=$elf._apply("anything");args=$elf._many(function(){return $elf._apply("trans")});return (((("new " + cls) + "(") + args.join(",")) + ")")})()},"var":function(){var $elf=this,name,val;return (function(){name=$elf._apply("anything");val=$elf._apply("trans");return ((("var " + name) + "=") + val)})()},"throw":function(){var $elf=this,x;return (function(){x=$elf._apply("trans");return ("throw " + x)})()},"try":function(){var $elf=this,x,name,c,f;return (function(){x=$elf._apply("curlyTrans");name=$elf._apply("anything");c=$elf._apply("curlyTrans");f=$elf._apply("curlyTrans");return ((((((("try " + x) + "catch(") + name) + ")") + c) + "finally") + f)})()},"json":function(){var $elf=this,props;return (function(){props=$elf._many(function(){return $elf._apply("trans")});return (("({" + props.join(",")) + "})")})()},"binding":function(){var $elf=this,name,val;return (function(){name=$elf._apply("anything");val=$elf._apply("trans");return ((name.toProgramString() + ": ") + val)})()},"switch":function(){var $elf=this,x,cases;return (function(){x=$elf._apply("trans");cases=$elf._many(function(){return $elf._apply("trans")});return (((("switch(" + x) + "){") + cases.join(";")) + "}")})()},"case":function(){var $elf=this,x,y;return (function(){x=$elf._apply("trans");y=$elf._apply("trans");return ((("case " + x) + ": ") + y)})()}})}
194 bs-js-compiler.txt
... ... @@ -0,0 +1,194 @@
  1 +ometa BSJSParser <: Parser {
  2 + fromTo :x :y = seq(x) (~seq(y) char)* seq(y),
  3 + space = super(#space) | fromTo('//', '\n') | fromTo('/*', '*/'),
  4 + nameFirst = letter | '$' | '_',
  5 + nameRest = nameFirst | digit,
  6 + iName = firstAndRest(#nameFirst, #nameRest):r -> r.join(''),
  7 + isKeyword :x = ?BSJSParser._isKeyword(x),
  8 + name = iName:n ~isKeyword(n) -> [#name, n=='self' ? '$elf' : n],
  9 + keyword = iName:k isKeyword(k) -> [k, k],
  10 + number = digit+:ws ('.' digit+ | empty -> []):fs -> [#number,
  11 + parseFloat(ws.join('') + '.' +
  12 + fs.join(''))],
  13 + escapeChar = '\\' char:c -> unescape('\\' + c),
  14 + str = seq('"""') (escapeChar | ~seq('"""') char)*:cs seq('"""') -> [`string, cs.join('')]
  15 + | '\'' (escapeChar | ~'\'' char)*:cs '\'' -> [#string, cs.join('')]
  16 + | '"' (escapeChar | ~'"' char)*:cs '"' -> [#string, cs.join('')]
  17 + | ('#' | '`') iName:n -> [#string, n],
  18 + special = ( '(' | ')' | '{' | '}' | '[' | ']' | ',' | ';'
  19 + | '?' | ':' | ``!=='' | ``!='' | ``==='' | ``=='' | ``='' | ``>=''
  20 + | '>' | ``<='' | '<' | ``++'' | ``+='' | '+' | ``--'' | ``-=''
  21 + | '-' | ``*='' | '*' | ``/='' | '/' | ``%='' | '%' | ``&&=''
  22 + | ``&&'' | ``||='' | ``||'' | '.' | '!' ):s -> [s, s],
  23 + tok = spaces (name | keyword | number | str | special),
  24 + toks = token*:ts spaces end -> ts,
  25 + token :tt = tok:t ?(t[0] == tt) -> t[1],
  26 + spacesNoNl = (~'\n' space)*,
  27 +
  28 + expr = orExpr:e ( "?" expr:t ":" expr:f -> [#condExpr, e, t, f]
  29 + | "=" expr:rhs -> [#set, e, rhs]
  30 + | "+=" expr:rhs -> [#mset, e, "+", rhs]
  31 + | "-=" expr:rhs -> [#mset, e, "-", rhs]
  32 + | "*=" expr:rhs -> [#mset, e, "*", rhs]
  33 + | "/=" expr:rhs -> [#mset, e, "/", rhs]
  34 + | "%=" expr:rhs -> [#mset, e, "%", rhs]
  35 + | "&&=" expr:rhs -> [#mset, e, "&&", rhs]
  36 + | "||=" expr:rhs -> [#mset, e, "||", rhs]
  37 + | empty -> e
  38 + ),
  39 + orExpr = orExpr:x "||" andExpr:y -> [#binop, "||", x, y]
  40 + | andExpr,
  41 + andExpr = andExpr:x "&&" eqExpr:y -> [#binop, "&&", x, y]
  42 + | eqExpr,
  43 + eqExpr = eqExpr:x ( "==" relExpr:y -> [#binop, "==", x, y]
  44 + | "!=" relExpr:y -> [#binop, "!=", x, y]
  45 + | "===" relExpr:y -> [#binop, "===", x, y]
  46 + | "!==" relExpr:y -> [#binop, "!==", x, y]
  47 + )
  48 + | relExpr,
  49 + relExpr = relExpr:x ( ">" addExpr:y -> [#binop, ">", x, y]
  50 + | ">=" addExpr:y -> [#binop, ">=", x, y]
  51 + | "<" addExpr:y -> [#binop, "<", x, y]
  52 + | "<=" addExpr:y -> [#binop, "<=", x, y]
  53 + | "instanceof" addExpr:y -> [#binop, "instanceof", x, y]
  54 + )
  55 + | addExpr,
  56 + addExpr = addExpr:x "+" mulExpr:y -> [#binop, "+", x, y]
  57 + | addExpr:x "-" mulExpr:y -> [#binop, "-", x, y]
  58 + | mulExpr,
  59 + mulExpr = mulExpr:x "*" mulExpr:y -> [#binop, "*", x, y]
  60 + | mulExpr:x "/" mulExpr:y -> [#binop, "/", x, y]
  61 + | mulExpr:x "%" mulExpr:y -> [#binop, "%", x, y]
  62 + | unary,
  63 + unary = "-" postfix:p -> [#unop, "-", p]
  64 + | "+" postfix:p -> p
  65 + | "++" postfix:p -> [#preop, "++", p]
  66 + | "--" postfix:p -> [#preop, "--", p]
  67 + | "!" postfix:p -> [#unop, "!", p]
  68 + | postfix,
  69 + postfix = primExpr:p ( spacesNoNl "++" -> [#postop, "++", p]
  70 + | spacesNoNl "--" -> [#postop, "--", p]
  71 + | empty -> p
  72 + ),
  73 + primExpr = primExpr:p ( "[" expr:i "]" -> [#getp, i, p]
  74 + | "." "name":m "(" listOf(#expr, ','):as ")" -> [#send, m, p].concat(as)
  75 + | "." "name":f -> [#getp, [#string, f], p]
  76 + | "(" listOf(#expr, ','):as ")" -> [#call, p].concat(as)
  77 + )
  78 + | primExprHd,
  79 + primExprHd = "(" expr:e ")" -> e
  80 + | "this" -> [#this]
  81 + | "name":n -> [#get, n]
  82 + | "number":n -> [#number, n]
  83 + | "string":s -> [#string, s]
  84 + | "function" funcRest
  85 + | "new" "name":n "(" listOf(#expr, ','):as ")" -> [#new, n].concat(as)
  86 + | "[" listOf(#expr, ','):es "]" -> [#arr].concat(es)
  87 + | json,
  88 + json = "{" listOf(#jsonBinding, ','):bs "}" -> [#json].concat(bs),
  89 + jsonBinding = jsonPropName:n ":" expr:v -> [#binding, n, v],
  90 + jsonPropName = "name" | "number" | "string",
  91 + formal = spaces "name",
  92 + funcRest = "(" listOf(#formal, ','):fs ")" "{" srcElems:body "}" -> [#func, fs, body],
  93 + sc = spacesNoNl ('\n' | &'}' | end)
  94 + | ";",
  95 + binding = "name":n ( "=" expr
  96 + | empty -> [#get, 'undefined'] ):v -> [#var, n, v],
  97 + block = "{" srcElems:ss "}" -> ss,
  98 + stmt = block
  99 + | "var" listOf(#binding, ','):bs sc -> [#begin].concat(bs)
  100 + | "if" "(" expr:c ")" stmt:t ( "else" stmt
  101 + | empty -> [#get, 'undefined'] ):f -> [#if, c, t, f]
  102 + | "while" "(" expr:c ")" stmt:s -> [#while, c, s]
  103 + | "do" stmt:s "while" "(" expr:c ")" sc -> [#doWhile, s, c]
  104 + | "for" "(" ( "var" binding
  105 + | expr
  106 + | empty -> [#get, 'undefined'] ):i
  107 + ";" ( expr
  108 + | empty -> [#get, 'true'] ):c
  109 + ";" ( expr
  110 + | empty -> [#get, 'undefined'] ):u
  111 + ")" stmt:s -> [#for, i, c, u, s]
  112 + | "for" "(" ( "var" "name":n -> [#var, n, [#get, 'undefined']]
  113 + | expr ):v
  114 + "in" expr:e
  115 + ")" stmt:s -> [#forIn, v, e, s]
  116 + | "switch" "(" expr:e ")" "{"
  117 + ( "case" expr:c ":" srcElems:cs -> [#case, c, cs]
  118 + | "default" ":" srcElems:cs -> [#default, cs] )*:cs
  119 + "}" -> [#switch, e].concat(cs)
  120 + | "break" sc -> [#break]
  121 + | "continue" sc -> [#continue]
  122 + | "throw" spacesNoNl expr:e sc -> [#throw, e]
  123 + | "try" block:t "catch" "(" "name":e ")" block:c
  124 + ( "finally" block
  125 + | empty -> [#get, 'undefined'] ):f -> [#try, t, e, c, f]
  126 + | "return" ( expr
  127 + | empty -> [#get, 'undefined'] ):e sc -> [#return, e]
  128 + | "with" "(" expr:x ")" stmt:s -> [#with, x, s]
  129 + | expr:e sc -> e,
  130 + srcElem = "function" "name":n funcRest:f -> [#var, n, f]
  131 + | stmt,
  132 + srcElems = srcElem*:ss -> [#begin].concat(ss),
  133 +
  134 + topLevel = srcElems:r spaces end -> r,
  135 + curlySemAction = "{" (srcElem:s &srcElem -> s)+:ss expr:r sc "}" spaces -> { ss.push([#return, r])
  136 + [#call, [#func, [], [#begin].concat(ss)]] }
  137 + | "{" expr:r "}" spaces -> r,
  138 + semAction = curlySemAction
  139 + | primExpr:r spaces -> r
  140 +}
  141 +BSJSParser.keywords = { }
  142 +keywords = ["break", "case", "catch", "continue", "default", "delete", "do", "else", "finally", "for", "function", "if", "in",
  143 + "instanceof", "new", "return", "switch", "this", "throw", "try", "typeof", "var", "void", "while", "with", "ometa"]
  144 +for (var idx = 0; idx < keywords.length; idx++)
  145 + BSJSParser.keywords[keywords[idx]] = true
  146 +BSJSParser._isKeyword = function(k) { return this.keywords.hasProperty(k) && !Object.prototype.hasProperty(k) }
  147 +
  148 +ometa BSJSTranslator {
  149 + trans = [:t apply(t):ans] -> ans,
  150 + curlyTrans = [#begin curlyTrans:r] -> r
  151 + | [#begin trans*:rs] -> ('{' + rs.join(';') + '}')
  152 + | trans:r -> ('{' + r + '}'),
  153 +
  154 + this -> 'this',
  155 + break -> 'break',
  156 + continue -> 'continue',
  157 + number :n -> ('(' + n + ')'),
  158 + string :s -> s.toProgramString(),
  159 + arr trans*:xs -> ('[' + xs.join(',') + ']'),
  160 + unop :op trans:x -> ('(' + op + x + ')'),
  161 + getp trans:fd trans:x -> (x + '[' + fd + ']'),
  162 + get :x -> x,
  163 + set trans:lhs trans:rhs -> (lhs + '=' + rhs),
  164 + mset trans:lhs :op trans:rhs -> (lhs + op + '=' + rhs),
  165 + binop :op trans:x trans:y -> ('(' + x + ' ' + op + ' ' + y + ')'),
  166 + preop :op trans:x -> (op + x),
  167 + postop :op trans:x -> (x + op),
  168 + return trans:x -> ('return ' + x),
  169 + with trans:x curlyTrans:s -> ('with(' + x + ')' + s),
  170 + if trans:cond curlyTrans:t curlyTrans:e -> ('if(' + cond + ')' + t + 'else' + e),
  171 + condExpr trans:cond trans:t trans:e -> (cond + '?' + t + ':' + e),
  172 + while trans:cond curlyTrans:body -> ('while(' + cond + ')' + body),
  173 + doWhile curlyTrans:body trans:cond -> ('do' + body + 'while(' + cond + ')'),
  174 + for trans:init trans:cond trans:upd
  175 + curlyTrans:body -> ('for(' + init + ';' + cond + ';' + upd + ')' + body),
  176 + forIn trans:x trans:arr curlyTrans:body -> ('for(' + x + ' in ' + arr + ')' + body),
  177 + begin trans:x end -> x,
  178 + begin (trans:x
  179 + ( (?(x[x.length - 1] == '}') | end) -> x
  180 + | empty -> (x + ';')
  181 + )
  182 + )*:xs -> ('{' + xs.join('') + '}'),
  183 + func :args curlyTrans:body -> ('(function (' + args.join(',') + ')' + body + ')'),
  184 + call trans:fn trans*:args -> (fn + '(' + args.join(',') + ')'),
  185 + send :msg trans:recv trans*:args -> (recv + '.' + msg + '(' + args.join(',') + ')'),
  186 + new :cls trans*:args -> ('new ' + cls + '(' + args.join(',') + ')'),
  187 + var :name trans:val -> ('var ' + name + '=' + val),
  188 + throw trans:x -> ('throw ' + x),
  189 + try curlyTrans:x :name curlyTrans:c curlyTrans:f -> ('try ' + x + 'catch(' + name + ')' + c + 'finally' + f),
  190 + json trans*:props -> ('({' + props.join(',') + '})'),
  191 + binding :name trans:val -> (name.toProgramString() + ': ' + val),
  192 + switch trans:x trans*:cases -> ('switch(' + x + '){' + cases.join(';') + '}'),
  193 + case trans:x trans:y -> ('case ' + x + ': '+ y)
  194 +}
1  bs-ometa-compiler.js
... ... @@ -0,0 +1 @@
  1 +{BSOMetaParser=Parser.delegated({"nameFirst":function(){var $elf=this;return $elf._or((function(){return $elf._applyWithArgs("exactly","_")}),(function(){return $elf._applyWithArgs("exactly","$")}),(function(){return $elf._apply("letter")}))},"nameRest":function(){var $elf=this;return $elf._or((function(){return $elf._apply("nameFirst")}),(function(){return $elf._apply("digit")}))},"tsName":function(){var $elf=this,xs;return (function(){xs=$elf._applyWithArgs("firstAndRest","nameFirst","nameRest");return xs.join("")})()},"name":function(){var $elf=this;return (function(){$elf._apply("spaces");return $elf._apply("tsName")})()},"eChar":function(){var $elf=this,c;return $elf._or((function(){return (function(){$elf._applyWithArgs("exactly","\\");c=$elf._apply("char");return unescape(("\\" + c))})()}),(function(){return $elf._apply("char")}))},"tsString":function(){var $elf=this,xs;return (function(){$elf._applyWithArgs("exactly","\'");xs=$elf._many(function(){return (function(){$elf._not(function(){return $elf._applyWithArgs("exactly","\'")});return $elf._apply("eChar")})()});$elf._applyWithArgs("exactly","\'");return xs.join("")})()},"characters":function(){var $elf=this,xs;return (function(){$elf._applyWithArgs("exactly","`");$elf._applyWithArgs("exactly","`");xs=$elf._many(function(){return (function(){$elf._not(function(){return (function(){$elf._applyWithArgs("exactly","\'");return $elf._applyWithArgs("exactly","\'")})()});return $elf._apply("eChar")})()});$elf._applyWithArgs("exactly","\'");$elf._applyWithArgs("exactly","\'");return ["App","seq",xs.join("").toProgramString()]})()},"sCharacters":function(){var $elf=this,xs;return (function(){$elf._applyWithArgs("exactly","\"");xs=$elf._many(function(){return (function(){$elf._not(function(){return $elf._applyWithArgs("exactly","\"")});return $elf._apply("eChar")})()});$elf._applyWithArgs("exactly","\"");return ["App","token",xs.join("").toProgramString()]})()},"string":function(){var $elf=this,xs;return (function(){xs=$elf._or((function(){return (function(){$elf._or((function(){return $elf._applyWithArgs("exactly","#")}),(function(){return $elf._applyWithArgs("exactly","`")}));return $elf._apply("tsName")})()}),(function(){return $elf._apply("tsString")}));return ["App","exactly",xs.toProgramString()]})()},"number":function(){var $elf=this,sign,ds;return (function(){sign=$elf._or((function(){return $elf._applyWithArgs("exactly","-")}),(function(){return (function(){$elf._apply("empty");return ""})()}));ds=$elf._many1(function(){return $elf._apply("digit")});return ["App","exactly",(sign + ds.join(""))]})()},"keyword":function(){var $elf=this,xs;return (function(){xs=$elf._apply("anything");$elf._applyWithArgs("token",xs);$elf._not(function(){return $elf._apply("letterOrDigit")});return xs})()},"args":function(){var $elf=this,xs;return $elf._or((function(){return (function(){$elf._applyWithArgs("token","(");xs=$elf._applyWithArgs("listOf","hostExpr",",");$elf._applyWithArgs("token",")");return xs})()}),(function(){return (function(){$elf._apply("empty");return []})()}))},"application":function(){var $elf=this,rule,as;return (function(){rule=$elf._apply("name");as=$elf._apply("args");return ["App",rule].concat(as)})()},"hostExpr":function(){var $elf=this,r;return (function(){r=$elf._applyWithArgs("foreign",BSJSParser,"expr");return $elf._applyWithArgs("foreign",BSJSTranslator,"trans",r)})()},"atomicHostExpr":function(){var $elf=this,r;return (function(){r=$elf._applyWithArgs("foreign",BSJSParser,"semAction");return $elf._applyWithArgs("foreign",BSJSTranslator,"trans",r)})()},"curlyHostExpr":function(){var $elf=this,r;return (function(){r=$elf._applyWithArgs("foreign",BSJSParser,"curlySemAction");return $elf._applyWithArgs("foreign",BSJSTranslator,"trans",r)})()},"semAction":function(){var $elf=this,x,x;return $elf._or((function(){return (function(){$elf._or((function(){return $elf._applyWithArgs("token","!")}),(function(){return $elf._applyWithArgs("token","->")}));x=$elf._apply("atomicHostExpr");return ["Act",x]})()}),(function(){return (function(){x=$elf._apply("curlyHostExpr");return ["Act",x]})()}))},"semPred":function(){var $elf=this,x;return (function(){$elf._applyWithArgs("token","?");x=$elf._apply("atomicHostExpr");return ["Pred",x]})()},"expr":function(){var $elf=this,xs;return (function(){xs=$elf._applyWithArgs("listOf","expr4","|");return ["Or"].concat(xs)})()},"expr4":function(){var $elf=this,xs;return (function(){xs=$elf._many(function(){return $elf._apply("expr3")});return ["And"].concat(xs)})()},"optIter":function(){var $elf=this,x;return (function(){x=$elf._apply("anything");return $elf._or((function(){return (function(){$elf._applyWithArgs("token","*");return ["Many",x]})()}),(function(){return (function(){$elf._applyWithArgs("token","+");return ["Many1",x]})()}),(function(){return (function(){$elf._apply("empty");return x})()}))})()},"expr3":function(){var $elf=this,x,x,n,n;return $elf._or((function(){return (function(){x=$elf._apply("expr2");x=$elf._applyWithArgs("optIter",x);return $elf._or((function(){return (function(){$elf._applyWithArgs("exactly",":");n=$elf._apply("name");return (function (){$elf["locals"].push(n);return ["Set",n,x]})()})()}),(function(){return (function(){$elf._apply("empty");return x})()}))})()}),(function(){return (function(){$elf._applyWithArgs("token",":");n=$elf._apply("name");return (function (){$elf["locals"].push(n);return ["Set",n,["App","anything"]]})()})()}))},"expr2":function(){var $elf=this,x,x;return $elf._or((function(){return (function(){$elf._applyWithArgs("token","~");x=$elf._apply("expr2");return ["Not",x]})()}),(function(){return (function(){$elf._applyWithArgs("token","&");x=$elf._apply("expr1");return ["Lookahead",x]})()}),(function(){return $elf._apply("expr1")}))},"expr1":function(){var $elf=this,x,x,x;return $elf._or((function(){return $elf._apply("application")}),(function(){return $elf._apply("semAction")}),(function(){return $elf._apply("semPred")}),(function(){return (function(){x=$elf._or((function(){return $elf._applyWithArgs("keyword","undefined")}),(function(){return $elf._applyWithArgs("keyword","nil")}),(function(){return $elf._applyWithArgs("keyword","true")}),(function(){return $elf._applyWithArgs("keyword","false")}));return ["App","exactly",x]})()}),(function(){return (function(){$elf._apply("spaces");return $elf._or((function(){return $elf._apply("characters")}),(function(){return $elf._apply("sCharacters")}),(function(){return $elf._apply("string")}),(function(){return $elf._apply("number")}))})()}),(function(){return (function(){$elf._applyWithArgs("token","[");x=$elf._apply("expr");$elf._applyWithArgs("token","]");return ["Form",x]})()}),(function(){return (function(){$elf._applyWithArgs("token","(");x=$elf._apply("expr");$elf._applyWithArgs("token",")");return x})()}))},"ruleName":function(){var $elf=this;return $elf._or((function(){return $elf._apply("name")}),(function(){return (function(){$elf._apply("spaces");return $elf._apply("tsString")})()}))},"rule":function(){var $elf=this,n,x,xs;return (function(){$elf._lookahead(function(){return n=$elf._apply("ruleName")});$elf["locals"]=["$elf=this"];x=$elf._applyWithArgs("rulePart",n);xs=$elf._many(function(){return (function(){$elf._applyWithArgs("token",",");return $elf._applyWithArgs("rulePart",n)})()});return ["Rule",n,$elf["locals"],["Or",x].concat(xs)]})()},"rulePart":function(){var $elf=this,rn,n,b1,b2;return (function(){rn=$elf._apply("anything");n=$elf._apply("ruleName");$elf._pred((n == rn));b1=$elf._apply("expr4");return $elf._or((function(){return (function(){$elf._applyWithArgs("token","=");b2=$elf._apply("expr");return ["And",b1,b2]})()}),(function(){return (function(){$elf._apply("empty");return b1})()}))})()},"grammar":function(){var $elf=this,n,sn,rs;return (function(){$elf._applyWithArgs("keyword","ometa");n=$elf._apply("name");sn=$elf._or((function(){return (function(){$elf._applyWithArgs("token","<:");return $elf._apply("name")})()}),(function(){return (function(){$elf._apply("empty");return "OMeta"})()}));$elf._applyWithArgs("token","{");rs=$elf._applyWithArgs("listOf","rule",",");$elf._applyWithArgs("token","}");return $elf._applyWithArgs("foreign",BSOMetaOptimizer,"optimizeGrammar",["Grammar",n,sn].concat(rs))})()}});BSOMetaTranslator=OMeta.delegated({"trans":function(){var $elf=this,t,ans;return (function(){$elf._form(function(){return (function(){t=$elf._apply("anything");return ans=$elf._applyWithArgs("apply",t)})()});return ans})()},"App":function(){var $elf=this,args,rule,args,rule;return $elf._or((function(){return (function(){$elf._applyWithArgs("exactly","super");args=$elf._many1(function(){return $elf._apply("anything")});return [$elf["sName"],"._superApplyWithArgs($elf,",args.join(","),")"].join("")})()}),(function(){return (function(){rule=$elf._apply("anything");args=$elf._many1(function(){return $elf._apply("anything")});return ["$elf._applyWithArgs(\"",rule,"\",",args.join(","),")"].join("")})()}),(function(){return (function(){rule=$elf._apply("anything");return ["$elf._apply(\"",rule,"\")"].join("")})()}))},"Act":function(){var $elf=this,expr;return (function(){expr=$elf._apply("anything");return expr})()},"Pred":function(){var $elf=this,expr;return (function(){expr=$elf._apply("anything");return ["$elf._pred(",expr,")"].join("")})()},"Or":function(){var $elf=this,xs;return (function(){xs=$elf._many(function(){return $elf._apply("transFn")});return ["$elf._or(",xs.join(","),")"].join("")})()},"And":function(){var $elf=this,xs,y;return $elf._or((function(){return (function(){xs=$elf._many(function(){return $elf._applyWithArgs("notLast","trans")});y=$elf._apply("trans");return (function (){xs.push(("return " + y));return ["(function(){",xs.join(";"),"})()"].join("")})()})()}),(function(){return "(function(){})"}))},"Many":function(){var $elf=this,x;return (function(){x=$elf._apply("trans");return ["$elf._many(function(){return ",x,"})"].join("")})()},"Many1":function(){var $elf=this,x;return (function(){x=$elf._apply("trans");return ["$elf._many1(function(){return ",x,"})"].join("")})()},"Set":function(){var $elf=this,n,v;return (function(){n=$elf._apply("anything");v=$elf._apply("trans");return [n,"=",v].join("")})()},"Not":function(){var $elf=this,x;return (function(){x=$elf._apply("trans");return ["$elf._not(function(){return ",x,"})"].join("")})()},"Lookahead":function(){var $elf=this,x;return (function(){x=$elf._apply("trans");return ["$elf._lookahead(function(){return ",x,"})"].join("")})()},"Form":function(){var $elf=this,x;return (function(){x=$elf._apply("trans");return ["$elf._form(function(){return ",x,"})"].join("")})()},"Rule":function(){var $elf=this,name,ls,body;return (function(){name=$elf._apply("anything");ls=$elf._apply("locals");body=$elf._apply("trans");return ["\"",name,"\":function(){",ls,"return ",body,"}"].join("")})()},"Grammar":function(){var $elf=this,name,sName,rules;return (function(){name=$elf._apply("anything");sName=$elf._apply("anything");$elf["sName"]=sName;rules=$elf._many(function(){return $elf._apply("trans")});return [name,"=",sName,".delegated({",rules.join(","),"})"].join("")})()},"locals":function(){var $elf=this,vs;return $elf._or((function(){return (function(){$elf._form(function(){return vs=$elf._many1(function(){return $elf._apply("string")})});return ["var ",vs.join(","),";"].join("")})()}),(function(){return (function(){$elf._form(function(){return (function(){})});return ""})()}))},"transFn":function(){var $elf=this,x;return (function(){x=$elf._apply("trans");return ["(function(){return ",x,"})"].join("")})()}})}
80 bs-ometa-compiler.txt
... ... @@ -0,0 +1,80 @@
  1 +ometa BSOMetaParser <: Parser {
  2 + nameFirst = '_' | '$' | letter,
  3 + nameRest = nameFirst | digit,
  4 + tsName = firstAndRest(#nameFirst, #nameRest):xs -> xs.join(''),
  5 + name = spaces tsName,
  6 + eChar = '\\' char:c -> unescape('\\' +c)
  7 + | char,
  8 + tsString = '\'' (~'\'' eChar)*:xs '\'' -> xs.join(''),
  9 + characters = '`' '`' (~('\'' '\'') eChar)*:xs '\'' '\'' -> [#App, #seq, xs.join('').toProgramString()],
  10 + sCharacters = '"' (~'"' eChar)*:xs '"' -> [#App, #token, xs.join('').toProgramString()],
  11 + string = (('#' | '`') tsName | tsString):xs -> [#App, #exactly, xs.toProgramString()],
  12 + number = ('-' | empty -> ''):sign digit+:ds -> [#App, #exactly, sign + ds.join('')],
  13 + keyword :xs = token(xs) ~letterOrDigit -> xs,
  14 + args = "(" listOf(#hostExpr, ','):xs ")" -> xs
  15 + | empty -> [],
  16 + application = name:rule args:as -> [#App, rule].concat(as),
  17 + hostExpr = foreign(BSJSParser, #expr):r foreign(BSJSTranslator, #trans, r),
  18 + atomicHostExpr = foreign(BSJSParser, #semAction):r foreign(BSJSTranslator, #trans, r),
  19 + curlyHostExpr = foreign(BSJSParser, #curlySemAction):r foreign(BSJSTranslator, #trans, r),
  20 + semAction = ("!" | "->") atomicHostExpr:x -> [#Act, x]
  21 + | curlyHostExpr:x -> [#Act, x],
  22 + semPred = "?" atomicHostExpr:x -> [#Pred, x],
  23 + expr = listOf(#expr4, '|'):xs -> [#Or].concat(xs),
  24 + expr4 = expr3*:xs -> [#And].concat(xs),
  25 + optIter :x = "*" -> [#Many, x]
  26 + | "+" -> [#Many1, x]
  27 + | empty -> x,
  28 + expr3 = expr2:x optIter(x):x ( ':' name:n -> { self.locals.push(n); [#Set, n, x] }
  29 + | empty -> x
  30 + )
  31 + | ":" name:n -> { self.locals.push(n); [#Set, n, [#App, #anything]] },
  32 + expr2 = "~" expr2:x -> [#Not, x]
  33 + | "&" expr1:x -> [#Lookahead, x]
  34 + | expr1,
  35 + expr1 = application | semAction | semPred
  36 + | ( keyword('undefined') | keyword('nil')
  37 + | keyword('true') | keyword('false') ):x -> [#App, #exactly, x]
  38 + | spaces (characters | sCharacters | string | number)
  39 + | "[" expr:x "]" -> [#Form, x]
  40 + | "(" expr:x ")" -> x,
  41 + ruleName = name
  42 + | spaces tsString,
  43 + rule = &(ruleName:n) !(self.locals = ['$elf=this'])
  44 + rulePart(n):x ("," rulePart(n))*:xs -> [#Rule, n, self.locals, [#Or, x].concat(xs)],
  45 + rulePart :rn = ruleName:n ?(n == rn) expr4:b1 ( "=" expr:b2 -> [#And, b1, b2]
  46 + | empty -> b1
  47 + ),
  48 + grammar = keyword('ometa') name:n
  49 + ( "<:" name | empty -> 'OMeta' ):sn
  50 + "{" listOf(#rule, ','):rs "}" foreign(BSOMetaOptimizer, #optimizeGrammar,
  51 + [#Grammar, n, sn].concat(rs))
  52 +}
  53 +
  54 +// By dispatching on the head of a list, the following idiom allows translators to avoid doing a linear search.
  55 +// (Note that the "=" in a rule definition is optional, which can be used to get an ML "feel".)
  56 +ometa BSOMetaTranslator {
  57 + trans [:t apply(t):ans] -> ans,
  58 + App 'super' anything+:args -> [self.sName, '._superApplyWithArgs($elf,', args.join(','), ')'].join(''),
  59 + App :rule anything+:args -> ['$elf._applyWithArgs("', rule, '",', args.join(','), ')'].join(''),
  60 + App :rule -> ['$elf._apply("', rule, '")'] .join(''),
  61 + Act :expr -> expr,
  62 + Pred :expr -> ['$elf._pred(', expr, ')'] .join(''),
  63 + Or transFn*:xs -> ['$elf._or(', xs.join(','), ')'] .join(''),
  64 + And notLast(#trans)*:xs trans:y -> { xs.push('return ' + y)
  65 + ['(function(){', xs.join(';'), '})()'].join('') },
  66 + And -> '(function(){})',
  67 + Many trans:x -> ['$elf._many(function(){return ', x, '})'] .join(''),
  68 + Many1 trans:x -> ['$elf._many1(function(){return ', x, '})'] .join(''),
  69 + Set :n trans:v -> [n, '=', v].join(''),
  70 + Not trans:x -> ['$elf._not(function(){return ', x, '})'] .join(''),
  71 + Lookahead trans:x -> ['$elf._lookahead(function(){return ', x, '})'] .join(''),
  72 + Form trans:x -> ['$elf._form(function(){return ', x, '})'] .join(''),
  73 + Rule :name locals:ls trans:body -> ['"', name, '":function(){', ls, 'return ', body, '}'] .join(''),
  74 + Grammar :name :sName
  75 + !(self.sName = sName)
  76 + trans*:rules -> [name, '=', sName, '.delegated({', rules.join(','), '})'] .join(''),
  77 + locals = [string+:vs] -> ['var ', vs.join(','), ';'] .join('')
  78 + | [] -> '',
  79 + transFn = trans:x -> ['(function(){return ', x, '})'] .join('')
  80 +}
1  bs-ometa-js-compiler.js
... ... @@ -0,0 +1 @@
  1 +{BSOMetaJSParser=BSJSParser.delegated();BSOMetaJSParser['srcElem']=function() {var $elf=this,r;return $elf._or((function(){return (function(){$elf._apply("spaces");r=$elf._applyWithArgs("foreign", BSOMetaParser, "grammar");$elf._apply("sc");return r})()}),(function(){return BSJSParser._superApplyWithArgs($elf,"srcElem")}))};BSOMetaJSParser.prototype=BSOMetaJSParser;;BSOMetaJSTranslator=BSJSTranslator.delegated();BSOMetaJSTranslator['Grammar']=function() {var $elf=this;return $elf._applyWithArgs("foreign", BSOMetaTranslator, "Grammar")};BSOMetaJSTranslator.prototype=BSOMetaJSTranslator;}
9 bs-ometa-js-compiler.txt
... ... @@ -0,0 +1,9 @@
  1 +ometa BSOMetaJSParser <: BSJSParser {
  2 + srcElem = spaces foreign(BSOMetaParser, #grammar):r sc -> r
  3 + | super(#srcElem)
  4 +}
  5 +
  6 +ometa BSOMetaJSTranslator <: BSJSTranslator {
  7 + Grammar = foreign(BSOMetaTranslator, #Grammar)
  8 +}
  9 +
1  bs-ometa-optimizer.js
... ... @@ -0,0 +1 @@
  1 +{BSNullOptimization=OMeta.delegated();BSNullOptimization['setHelped']=function() {var $elf=this;return $elf["_didSomething"]=true};BSNullOptimization['helped']=function() {var $elf=this;return $elf._pred($elf["_didSomething"])};BSNullOptimization['trans']=function() {var $elf=this,t,ans;return $elf._or((function(){return (function(){$elf._form(function(){return (function(){t=$elf._apply("anything");$elf._pred($elf.hasProperty(t));return ans=$elf._applyWithArgs("apply", t)})()});return ans})()}),(function(){return $elf._apply("anything")}))};BSNullOptimization['optimize']=function() {var $elf=this,x;return (function(){x=$elf._apply("trans");$elf._apply("helped");return x})()};BSNullOptimization['Or']=function() {var $elf=this,xs;return (function(){xs=$elf._many(function(){return $elf._apply("trans")});return ["Or"].concat(xs)})()};BSNullOptimization['And']=function() {var $elf=this,xs;return (function(){xs=$elf._many(function(){return $elf._apply("trans")});return ["And"].concat(xs)})()};BSNullOptimization['Many']=function() {var $elf=this,x;return (function(){x=$elf._apply("trans");return ["Many",x]})()};BSNullOptimization['Many1']=function() {var $elf=this,x;return (function(){x=$elf._apply("trans");return ["Many1",x]})()};BSNullOptimization['Set']=function() {var $elf=this,n,v;return (function(){n=$elf._apply("anything");v=$elf._apply("trans");return ["Set",n,v]})()};BSNullOptimization['Not']=function() {var $elf=this,x;return (function(){x=$elf._apply("trans");return ["Not",x]})()};BSNullOptimization['Lookahead']=function() {var $elf=this,x;return (function(){x=$elf._apply("trans");return ["Lookahead",x]})()};BSNullOptimization['Form']=function() {var $elf=this,x;return (function(){x=$elf._apply("trans");return ["Form",x]})()};BSNullOptimization['Rule']=function() {var $elf=this,name,ls,body;return (function(){name=$elf._apply("anything");ls=$elf._apply("anything");body=$elf._apply("trans");return ["Rule",name,ls,body]})()};BSNullOptimization.prototype=BSNullOptimization;;BSNullOptimization["initialize"]=(function () {this["_didSomething"]=false});BSAndOrOptimization=BSNullOptimization.delegated();BSAndOrOptimization['And']=function() {var $elf=this,x,xs;return $elf._or((function(){return (function(){x=$elf._apply("trans");$elf._apply("end");$elf._apply("setHelped");return x})()}),(function(){return (function(){xs=$elf._applyWithArgs("transInside", "And");return ["And"].concat(xs)})()}))};BSAndOrOptimization['Or']=function() {var $elf=this,x,xs;return $elf._or((function(){return (function(){x=$elf._apply("trans");$elf._apply("end");$elf._apply("setHelped");return x})()}),(function(){return (function(){xs=$elf._applyWithArgs("transInside", "Or");return ["Or"].concat(xs)})()}))};BSAndOrOptimization['transInside']=function() {var $elf=this,t,xs,ys,x,xs;return (function(){t=$elf._apply("anything");return $elf._or((function(){return (function(){$elf._form(function(){return (function(){$elf._applyWithArgs("exactly", t);return xs=$elf._applyWithArgs("transInside", t)})()});ys=$elf._applyWithArgs("transInside", t);$elf._apply("setHelped");return xs.concat(ys)})()}),(function(){return (function(){x=$elf._apply("trans");xs=$elf._applyWithArgs("transInside", t);return [x].concat(xs)})()}),(function(){return []}))})()};BSAndOrOptimization.prototype=BSAndOrOptimization;;BSOMetaOptimizer=OMeta.delegated();BSOMetaOptimizer['optimizeGrammar']=function() {var $elf=this,n,sn,rs;return (function(){$elf._form(function(){return (function(){$elf._applyWithArgs("exactly", "Grammar");n=$elf._apply("anything");sn=$elf._apply("anything");return rs=$elf._many(function(){return $elf._apply("optimizeRule")})})()});return ["Grammar",n,sn].concat(rs)})()};BSOMetaOptimizer['optimizeRule']=function() {var $elf=this,r,r;return (function(){r=$elf._apply("anything");$elf._many(function(){return r=$elf._applyWithArgs("foreign", BSAndOrOptimization, "optimize", r)});return r})()};BSOMetaOptimizer.prototype=BSOMetaOptimizer;}
35 bs-ometa-optimizer.txt
... ... @@ -0,0 +1,35 @@
  1 +ometa BSNullOptimization {
  2 + setHelped = !($elf._didSomething = true),
  3 + helped = ?$elf._didSomething,
  4 + trans = [:t ?$elf.hasProperty(t) apply(t):ans] -> ans
  5 + | anything,
  6 + optimize = trans:x helped -> x,
  7 +
  8 + Or trans*:xs -> [#Or].concat(xs),
  9 + And trans*:xs -> [#And].concat(xs),
  10 + Many trans:x -> [#Many, x],
  11 + Many1 trans:x -> [#Many1, x],
  12 + Set :n trans:v -> [#Set, n, v],
  13 + Not trans:x -> [#Not, x],
  14 + Lookahead trans:x -> [#Lookahead, x],
  15 + Form trans:x -> [#Form, x],
  16 + Rule :name :ls trans:body -> [#Rule, name, ls, body]
  17 +}
  18 +BSNullOptimization.initialize = function() { this._didSomething = false }
  19 +
  20 +ometa BSAndOrOptimization <: BSNullOptimization {
  21 + And trans:x end setHelped -> x,
  22 + And transInside(#And):xs -> [#And].concat(xs),
  23 + Or trans:x end setHelped -> x,
  24 + Or transInside(#Or):xs -> [#Or].concat(xs),
  25 +
  26 + transInside :t = [exactly(t) transInside(t):xs] transInside(t):ys setHelped -> xs.concat(ys)
  27 + | trans:x transInside(t):xs -> [x].concat(xs)
  28 + | -> []
  29 +}
  30 +
  31 +ometa BSOMetaOptimizer {
  32 + optimizeGrammar = [#Grammar :n :sn optimizeRule*:rs] -> [#Grammar, n, sn].concat(rs),
  33 + optimizeRule = :r (foreign(BSAndOrOptimization, #optimize, r):r)* -> r
  34 +}
  35 +
1  bs-project-list-parser.js
... ... @@ -0,0 +1 @@
  1 +ProjectListParser=BSJSParser.delegated();ProjectListParser['proj']=function() {var $elf=this;return (function(){$elf._apply("spaces");return $elf._apply("iName")})()};ProjectListParser['projs']=function() {var $elf=this,x,xs;return (function(){x=$elf._apply("proj");xs=$elf._many(function(){return (function(){$elf._apply("spaces");$elf._applyWithArgs("exactly", ",");return $elf._apply("proj")})()});return [x].concat(xs)})()};ProjectListParser.prototype=ProjectListParser;
5 bs-project-list-parser.txt
... ... @@ -0,0 +1,5 @@
  1 +ometa ProjectListParser <: BSJSParser {
  2 + proj = spaces iName,
  3 + projs = proj:x (spaces ',' proj)*:xs -> [x].concat(xs)
  4 +}
  5 +
161 index.html
... ... @@ -0,0 +1,161 @@
  1 +<!--
  2 + Copyright (c) 2007, 2008 Alessandro Warth <awarth@cs.ucla.edu>
  3 +
  4 + Permission is hereby granted, free of charge, to any person
  5 + obtaining a copy of this software and associated documentation
  6 + files (the "Software"), to deal in the Software without
  7 + restriction, including without limitation the rights to use,
  8 + copy, modify, merge, publish, distribute, sublicense, and/or sell
  9 + copies of the Software, and to permit persons to whom the
  10 + Software is furnished to do so, subject to the following
  11 + conditions:
  12 +
  13 + The above copyright notice and this permission notice shall be
  14 + included in all copies or substantial portions of the Software.
  15 +
  16 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17 + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18 + OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19 + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20 + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21 + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22 + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23 + OTHER DEALINGS IN THE SOFTWARE.
  24 +!-->
  25 +
  26 +<html>
  27 + <head>
  28 + <title>OMeta/JS 2.0 Workspace</title>
  29 + <script src="prototype.js"></script>
  30 + <script src="prototype-fix.js"></script>
  31 + <script src="workspace.js"></script>
  32 + <script src="lib.js"></script>
  33 + <script src="ometa-base.js"></script>
  34 + <script src="parser.js"></script>
  35 + <script src="bs-js-compiler.js"></script>
  36 + <script src="bs-ometa-compiler.js"></script>
  37 + <script src="bs-ometa-optimizer.js"></script>
  38 + <script src="bs-ometa-js-compiler.js"></script>
  39 + <script src="bs-project-list-parser.js"></script>
  40 + <script src="wiki.js"></script>
  41 + <script>
  42 +
  43 +function toggleVisible(id) {
  44 + var style = document.getElementById(id).style
  45 + style.visibility = style.visibility == "hidden" ? "visible" : "hidden"
  46 + style.display = style.display == "block" ? "none" : "block"
  47 +}
  48 +
  49 +function initializeThisPage() {
  50 + $('workspaceForm').source.onkeydown = onShortCutKey
  51 + var makeProjectSelector = function(id) {
  52 + $(id + "Home").innerHTML =
  53 + "<select id=" + id +
  54 + " onChange='loadProject(this.options[this.selectedIndex].value)'><option value=''>(nevermind)</option></select>"
  55 + }
  56 + var addOption = function(toId, x) {
  57 + var option = document.createElement("option")
  58 + option.innerHTML = x.replace(/_/g, " ")
  59 + option.value = x
  60 + $(toId).options.add(option)
  61 + }
  62 + makeProjectSelector('infoSelector')
  63 + addOption('infoSelector', 'Sample_Project')
  64 + addOption('infoSelector', 'Not_Quite_JS')
  65 + addOption('infoSelector', 'Things_You_Should_Know')
  66 + addOption('infoSelector', 'OMeta_Tutorial')
  67 + addOption('infoSelector', 'Project_Listing')
  68 + makeProjectSelector('projectSelector')
  69 + var ps = $('projectSelector')
  70 + try {
  71 + var projects = ProjectListParser.matchAll(readFile("Project_Listing"), "projs")
  72 + for (var idx = 0; idx < projects.length; idx++)
  73 + addOption('projectSelector', projects[idx])
  74 + }
  75 + catch (e) { }
  76 + $('workspaceForm').source.value = $('workspaceForm').source.origValue = ''
  77 + $('workspaceForm').translation.value = ''
  78 + $('workspaceForm').transcript.value = ''
  79 + loadProject()
  80 +}
  81 +
  82 +saveIt = saveProject
  83 +
  84 +if (!document.location.hash)
  85 + document.location.hash = "#Sample_Project"
  86 +titleRest = " - <span title='In case you are wondering, this is\n" +
  87 + "(OMeta/JS) 2.0, not OMeta/(JS 2.0).\n" +
  88 + "In other words, it has nothing to do\n" +
  89 + "with JS 2 / EcmaScript 2.'>OMeta/JS 2.0</span> Workspace"
  90 +
  91 +function translateCode(s) {
  92 + var translationError = function(m, i) { alert("Translation error - please tell Alex about this!"); throw fail },
  93 + tree = BSOMetaJSParser.matchAll(s, "topLevel", undefined, function(m, i) { throw fail.delegated({errorPos: i}) })
  94 + return BSOMetaJSTranslator.match(tree, "trans", undefined, translationError)
  95 +}
  96 +
  97 + </script>
  98 + </head>
  99 + <body onLoad="initializeThisPage()">
  100 + <table border=0 align=right>
  101 + <tr><td align=right><b>important info: </b></td><td><span id=infoSelectorHome></span></td></tr>
  102 + <tr><td align=right><b>go to project: </b></td><td><span id=projectSelectorHome></span></td></tr>
  103 + <tr><td colspan=2 align=right><input type=button value="previous versions of this project" onClick="document.location = 'http://jarrett.cs.ucla.edu/websvn/log.php?repname=ometa-js-projects&path=%2F' + document.location.hash.substring(1) + '.txt&rev=0&sc=0&isdir=0'"><br>
  104 + </td></tr>
  105 + </table>
  106 + <table border=0><tr><td><h2 id=title>OMeta/JS 2.0 Workspace <small><font color=lightgray>(not to be confused with JS 2 / EcmaScript 4)</font></small></h2></td></tr></table>
  107 +
  108 +
  109 + <b>Instructions</b>
  110 + <input type=button value="+/-" onClick="toggleVisible('instructions')">
  111 + <br>
  112 + <div id=instructions style="visibility: visible; display: block">
  113 + <table bgcolor=#f9f9f9 cellpadding=4
  114 + style="border: 1px solid #333333; padding: .2em .2em .2em .2em; margin-bottom: .4em; margin-top: .1em">
  115 + <tr><td>
  116 + The text area below (<b>source</b>) works like a Smalltalk workspace:
  117 + <bl><li>
  118 + To evaluate some code, just select it and press the <input type=button value="do it" onClick="doIt()"> button.
  119 + </li><li>
  120 + Pressing <input type=button value="do it" onClick="doIt()"> without a selection evaluates the line that the cursor is on.
  121 + </li><li>
  122 + <input type=button value="print it" onClick="printIt()"> is like <input type=button value="do it" onClick="doIt()">,
  123 + but it also prints the result.
  124 + </li></bl>
  125 + Also, you can press <input type=button value="save it" onClick="saveIt()"> to save the current project.
  126 + </td></tr></table>
  127 + </div>
  128 + <b><span title="A useful place to add new HTMLElements - see Canvas project for an example">Play Area</span></b>
  129 + <input type=button value="+/-" onClick="toggleVisible('playArea')">
  130 + <br>
  131 + <div id=playArea style="visibility: visible; display: block"></div>
  132 + <form id=workspaceForm>
  133 + <b>Source</b>
  134 + <input type=button value="+/-" onClick="toggleVisible('source')">
  135 + <br>
  136 + <div id=source style="visibility: visible; display: block">
  137 + <textarea cols=132 rows=24 name=source style="font-family:monaco, monospace; font-size: 10pt"></textarea><br>
  138 + <input type=button value="print it (ctrl+p)" onClick="printIt()" style="margin-bottom: .4em">
  139 + <input type=button value="do it (ctrl+d)" onClick="doIt()" style="margin-bottom: .4em">
  140 + <input type=button value="save it (ctrl + s)" onClick="saveProject()" style="margin-bottom: .4em"><br>
  141 + </div>
  142 + <b><span title="Shows the JavaScript translation of the last thing that was evaluated above">Translation</span></b>
  143 + <input type=button value="+/-" onClick="toggleVisible('translation')">
  144 + <br>
  145 + <div id=translation style="visibility: hidden; display: none">
  146 + <textarea cols=132 rows=4 name=translation style="font-family:monaco, monospace; font-size: 10pt; margin-bottom: .4em">
  147 + </textArea>
  148 + </div>
  149 +
  150 + <b>Transcript</b>
  151 + <input type=button value="+/-" onClick="toggleVisible('transcript')">
  152 + <br>
  153 + <div id=transcript style="visibility: hidden; display: none">
  154 + <textarea cols=132 rows=4 name=transcript
  155 + style="font-family:monaco, monospace; font-size: 10pt; margin-bottom: .4em"></textArea>
  156 + </div>
  157 + </form>
  158 + To learn more about OMeta, click <a href=http://www.cs.ucla.edu/~awarth/ometa/>here</a>.<br>
  159 + </body>
  160 +</html>
  161 +
173 lib.js
... ... @@ -0,0 +1,173 @@
  1 +/*
  2 + Copyright (c) 2007, 2008 Alessandro Warth <awarth@cs.ucla.edu>
  3 +
  4 + Permission is hereby granted, free of charge, to any person
  5 + obtaining a copy of this software and associated documentation
  6 + files (the "Software"), to deal in the Software without
  7 + restriction, including without limitation the rights to use,
  8 + copy, modify, merge, publish, distribute, sublicense, and/or sell
  9 + copies of the Software, and to permit persons to whom the
  10 + Software is furnished to do so, subject to the following
  11 + conditions:
  12 +
  13 + The above copyright notice and this permission notice shall be
  14 + included in all copies or substantial portions of the Software.
  15 +
  16 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17 + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18 + OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19 + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20 + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21 + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22 + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23 + OTHER DEALINGS IN THE SOFTWARE.
  24 +*/
  25 +
  26 +// try to use StringBuffer instead of string concatenation to improve performance
  27 +
  28 +function StringBuffer() {
  29 + this.strings = []
  30 + for (var idx = 0; idx < arguments.length; idx++)
  31 + this.nextPutAll(arguments[idx])
  32 +}
  33 +StringBuffer.prototype.nextPutAll = function(s) { this.strings.push(s) }
  34 +StringBuffer.prototype.contents = function() { return this.strings.join("") }
  35 +String.prototype.writeStream = function() { return new StringBuffer(this) }
  36 +
  37 +// make Arrays print themselves sensibly
  38 +
  39 +Object.prototype.printOn = function(ws) { ws.nextPutAll(this.toString()) }
  40 +
  41 +Array.prototype.toString = function() { var ws = "".writeStream(); this.printOn(ws); return ws.contents() }
  42 +Array.prototype.printOn = function(ws) {
  43 + ws.nextPutAll("[")
  44 + for (var idx = 0; idx < this.length; idx++) {
  45 + if (idx > 0)
  46 + ws.nextPutAll(", ")
  47 + this[idx].printOn(ws)
  48 + }
  49 + ws.nextPutAll("]")
  50 +}
  51 +
  52 +// delegation
  53 +
  54 +Object.prototype.delegated = function(props) {
  55 + var f = function() { }
  56 + f.prototype = this
  57 + var r = new f()
  58 + for (var p in props)
  59 + if (props.hasOwnProperty(p))
  60 + r[p] = props[p]
  61 + return r
  62 +}
  63 +
  64 +// some reflective stuff
  65 +
  66 +Object.prototype.ownPropertyNames = function() {
  67 + var r = []
  68 + for (name in this)
  69 + if (this.hasOwnProperty(name))
  70 + r.push(name)
  71 + return r
  72 +}
  73 +
  74 +Object.prototype.hasProperty = function(p) { return this[p] != undefined }
  75 +
  76 +Object.prototype.isNumber = function() { return false }
  77 +Number.prototype.isNumber = function() { return true }
  78 +
  79 +Object.prototype.isString = function() { return false }
  80 +String.prototype.isString = function() { return true }
  81 +
  82 +Object.prototype.isCharacter = function() { return false }
  83 +
  84 +String.prototype.isCharacter = function() { return this.length == 1 }
  85 +String.prototype.isSpace = function() { return this.isCharacter() && this.charCodeAt(0) <= 32 }
  86 +String.prototype.isDigit = function() { return this.isCharacter() && this >= "0" && this <= "9" }
  87 +String.prototype.isLower = function() { return this.isCharacter() && this >= "a" && this <= "z" }
  88 +String.prototype.isUpper = function() { return this.isCharacter() && this >= "A" && this <= "Z" }
  89 +
  90 +String.prototype.digitValue = function() { return this.charCodeAt(0) - "0".charCodeAt(0) }
  91 +
  92 +Object.prototype.isSequenceable = false
  93 +Array.prototype.isSequenceable = true
  94 +String.prototype.isSequenceable = true
  95 +
  96 +// some functional programming stuff
  97 +
  98 +Array.prototype.map = function(f) {
  99 + var r = []
  100 + for (var idx = 0; idx < this.length; idx++)
  101 + r[idx] = f(this[idx])
  102 + return r
  103 +}
  104 +
  105 +Array.prototype.reduce = function(f, z) {
  106 + var r = z
  107 + for (var idx = 0; idx < this.length; idx++)
  108 + r = f(r, this[idx])
  109 + return r
  110 +}
  111 +
  112 +Array.prototype.delimWith = function(d) {
  113 + return this.reduce(
  114 + function(xs, x) {
  115 + if (xs.length > 0)
  116 + xs.push(d)
  117 + xs.push(x)
  118 + return xs
  119 + },
  120 + [])
  121 +}
  122 +
  123 +// Squeak's ReadStream, kind of
  124 +
  125 +function ReadStream(anArrayOrString) {
  126 + this.src = anArrayOrString
  127 + this.pos = 0
  128 +}
  129 +ReadStream.prototype.atEnd = function() { return this.pos >= this.src.length }
  130 +ReadStream.prototype.next = function() { return this.src.at(this.pos++) }
  131 +
  132 +// escape characters
  133 +
  134 +escapeStringFor = new Object()
  135 +for (var c = 0; c < 256; c++)
  136 + escapeStringFor[c] = String.fromCharCode(c)
  137 +escapeStringFor["\\".charCodeAt(0)] = "\\\\"
  138 +escapeStringFor['"'.charCodeAt(0)] = '\\"'
  139 +escapeStringFor["'".charCodeAt(0)] = "\\'"
  140 +escapeStringFor["\r".charCodeAt(0)] = "\\r"
  141 +escapeStringFor["\n".charCodeAt(0)] = "\\n"
  142 +escapeStringFor["\t".charCodeAt(0)] = "\\t"
  143 +escapeChar = function(c) {
  144 + var charCode = c.charCodeAt(0)
  145 + return charCode > 255 ? String.fromCharCode(charCode) : escapeStringFor[charCode]
  146 +}
  147 +
  148 +function unescape(s) {
  149 + if (s[0] == '\\')
  150 + switch (s[1]) {
  151 + case '\\': return '\\'
  152 + case 'r': return '\r'
  153 + case 'n': return '\n'
  154 + case 't': return '\t'
  155 + default: return s[1]
  156 + }
  157 + else
  158 + return s
  159 +}
  160 +
  161 +String.prototype.toProgramString = function() {
  162 + var ws = "\"".writeStream()
  163 + for (var idx = 0; idx < this.length; idx++)
  164 + ws.nextPutAll(escapeChar(this[idx]))
  165 + ws.nextPutAll("\"")
  166 + return ws.contents()
  167 +}
  168 +
  169 +// C-style tempnam function
  170 +
  171 +function tempnam(s) { return (s ? s : "_tmpnam_") + tempnam.n++ }
  172 +tempnam.n = 0
  173 +
370 ometa-base.js
... ... @@ -0,0 +1,370 @@
  1 +/*
  2 + Copyright (c) 2007, 2008 Alessandro Warth <awarth@cs.ucla.edu>
  3 +
  4 + Permission is hereby granted, free of charge, to any person
  5 + obtaining a copy of this software and associated documentation
  6 + files (the "Software"), to deal in the Software without
  7 + restriction, including without limitation the rights to use,
  8 + copy, modify, merge, publish, distribute, sublicense, and/or sell
  9 + copies of the Software, and to permit persons to whom the
  10 + Software is furnished to do so, subject to the following
  11 + conditions:
  12 +
  13 + The above copyright notice and this permission notice shall be
  14 + included in all copies or substantial portions of the Software.
  15 +
  16 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17 + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18 + OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19 + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20 + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21 + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22 + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23 + OTHER DEALINGS IN THE SOFTWARE.
  24 +*/
  25 +
  26 +/*
  27 + new syntax:
  28 + #foo match the string object 'foo' (should also be accepted in JS)
  29 + 'abc' match the string object 'abc'
  30 + 'c' match the string object 'c'
  31 + ``abc'' match the sequence of string objects 'a', 'b', 'c'
  32 + "abc" token('abc')
  33 + [1 2 3] match the array object [1, 2, 3]
  34 + foo(bar) apply rule foo with argument bar
  35 + -> ... do semantic actions in JS (no more ST). allow multiple statements, but no declarations
  36 + probably don't even need {}s, because newlines can terminate it!
  37 +*/
  38 +
  39 +/*
  40 +// ometa M {
  41 +// number = number:n digit:d -> { n * 10 + d.digitValue() }
  42 +// | digit:d -> { d.digitValue() }.
  43 +// }
  44 +
  45 +try {
  46 + M = OMeta.delegated({
  47 + number: function() {
  48 + var $elf = this
  49 + return $elf._or(
  50 + function() {
  51 + var n, d
  52 + n = $elf._apply("number")
  53 + d = $elf._apply("digit")
  54 + return n * 10 + d.digitValue()
  55 + },
  56 + function() {
  57 + var d
  58 + d = $elf._apply("digit")
  59 + return d.digitValue()
  60 + }
  61 + )
  62 + }
  63 + })
  64 + M.matchAll("123456789", "number")
  65 +} catch (f) { alert(f) }
  66 +*/
  67 +
  68 +// the failure exception
  69 +
  70 +fail = { toString: function() { return "match failed" } }
  71 +
  72 +// streams and memoization
  73 +
  74 +function OMInputStream(hd, tl) {
  75 + this.memo = { }
  76 + this.hd = hd
  77 + this.tl = tl
  78 +}
  79 +OMInputStream.prototype.head = function() { return this.hd }
  80 +OMInputStream.prototype.tail = function() { return this.tl }
  81 +
  82 +function OMInputStreamEnd() { this.memo = { } }
  83 +OMInputStreamEnd.prototype.head = function() { throw fail }
  84 +OMInputStreamEnd.prototype.tail = function() { throw fail }
  85 +
  86 +Array.prototype.toOMInputStream = function() { return makeArrayOMInputStream(this, 0) }
  87 +String.prototype.toOMInputStream = Array.prototype.toOMInputStream
  88 +
  89 +function makeArrayOMInputStream(arr, idx) { return idx < arr.length ? new ArrayOMInputStream(arr, idx) : new OMInputStreamEnd() }
  90 +
  91 +function ArrayOMInputStream(arr, idx) {
  92 + this.memo = { }
  93 + this.arr = arr
  94 + this.idx = idx
  95 + this.hd = arr[idx]
  96 +}
  97 +ArrayOMInputStream.prototype.head = function() { return this.hd }
  98 +ArrayOMInputStream.prototype.tail = function() {
  99 + if (this.tl == undefined)
  100 + this.tl = makeArrayOMInputStream(this.arr, this.idx + 1)
  101 + return this.tl
  102 +}
  103 +
  104 +function makeOMInputStreamProxy(target) {
  105 + return target.delegated({
  106 + memo: { },
  107 + target: target,
  108 + tail: function() { return makeOMInputStreamProxy(target.tail()) }
  109 + })
  110 +}
  111 +
  112 +// Failer (i.e., that which makes things fail) is used to detect (direct) left recursion and memoize failures
  113 +
  114 +function Failer() { }
  115 +Failer.prototype.used = false
  116 +
  117 +// the OMeta "class" and basic functionality
  118 +
  119 +OMeta = {
  120 + _apply: function(rule) {
  121 + var memoRec = this.input.memo[rule]
  122 + if (memoRec == undefined) {
  123 + var origInput = this.input,
  124 + failer = new Failer()
  125 + this.input.memo[rule] = failer
  126 + this.input.memo[rule] = memoRec = {ans: this[rule].apply(this), nextInput: this.input}
  127 + if (failer.used) {
  128 + var sentinel = this.input
  129 + while (true) {
  130 + try {
  131 + this.input = origInput
  132 + var ans = this[rule].apply(this)
  133 + if (this.input == sentinel)
  134 + throw fail
  135 + memoRec.ans = ans
  136 + memoRec.nextInput = this.input
  137 + }
  138 + catch (f) {
  139 + if (f != fail)
  140 + throw f
  141 + break
  142 + }
  143 + }
  144 + }
  145 + }
  146 + else if (memoRec instanceof Failer) {
  147 + memoRec.used = true
  148 + throw fail
  149 + }
  150 + this.input = memoRec.nextInput
  151 + return memoRec.ans
  152 + },
  153 +
  154 + // note: _applyWithArgs and _superApplyWithArgs are not memoized, so they can't be left-recursive
  155 + _applyWithArgs: function(rule) {
  156 + for (var idx = arguments.length - 1; idx > 0; idx--)
  157 + this.input = new OMInputStream(arguments[idx], this.input)
  158 + return this[rule].apply(this)
  159 + },
  160 + _superApplyWithArgs: function($elf, rule) {
  161 + for (var idx = arguments.length - 1; idx > 1; idx--)
  162 + $elf.input = new OMInputStream(arguments[idx], $elf.input)
  163 + return this[rule].apply($elf)
  164 + },
  165 + _pred: function(b) {
  166 + if (b)
  167 + return true
  168 + throw fail
  169 + },
  170 + _not: function(x) {
  171 + var origInput = this.input
  172 + try { x() }
  173 + catch (f) {
  174 + if (f != fail)
  175 + throw f
  176 + this.input = origInput
  177 + return true
  178 + }
  179 + throw fail
  180 + },
  181 + _lookahead: function(x) {
  182 + var origInput = this.input,
  183 + r = x()
  184 + this.input = origInput
  185 + return r
  186 + },
  187 + _or: function() {
  188 + var origInput = this.input
  189 + for (var idx = 0; idx < arguments.length; idx++)
  190 + try { this.input = origInput; return arguments[idx]() }
  191 + catch (f) {
  192 + if (f != fail)
  193 + throw f
  194 + }
  195 + throw fail
  196 + },
  197 + _many: function(x) {
  198 + var ans = arguments[1] != undefined ? [arguments[1]] : []
  199 + while (true) {
  200 + var origInput = this.input
  201 + try { ans.push(x()) }
  202 + catch (f) {
  203 + if (f != fail)
  204 + throw f
  205 + this.input = origInput
  206 + break
  207 + }
  208 + }
  209 + return ans
  210 + },
  211 + _many1: function(x) { return this._many(x, x()) },
  212 + _form: function(x) {
  213 + var v = this._apply("anything")
  214 + if (!v.isSequenceable)
  215 + throw fail
  216 + var origInput = this.input
  217 + this.input = makeArrayOMInputStream(v, 0)
  218 + var r = x()
  219 + this._apply("end")
  220 + this.input = origInput
  221 + return v
  222 + },
  223 +
  224 + // some basic rules
  225 + anything: function() {
  226 + var r = this.input.head()
  227 + this.input = this.input.tail()
  228 + return r
  229 + },
  230 + end: function() {
  231 + var $elf = this
  232 + return this._not(function() { return $elf._apply("anything") })
  233 + },
  234 + empty: function() { return true },
  235 + apply: function() {
  236 + var r = this._apply("anything")
  237 + return this._apply(r)
  238 + },
  239 + foreign: function() {
  240 + var g = this._apply("anything"),
  241 + r = this._apply("anything"),
  242 + gi = g.delegated({input: makeOMInputStreamProxy(this.input)})
  243 + var ans = gi._apply(r)
  244 + this.input = gi.input.target
  245 + return ans
  246 + },
  247 +
  248 + // some useful "derived" rules
  249 + exactly: function() {
  250 + var wanted = this._apply("anything")
  251 + if (wanted === this._apply("anything"))
  252 + return wanted
  253 + throw fail
  254 + },
  255 + "true": function() {
  256 + var r = this._apply("anything")
  257 + this._pred(r == true)
  258 + return r
  259 + },
  260 + "false": function() {
  261 + var r = this._apply("anything")
  262 + this._pred(r == false)
  263 + return r
  264 + },
  265 + "undefined": function() {
  266 + var r = this._apply("anything")
  267 + this._pred(r == undefined)
  268 + return r
  269 + },
  270 + number: function() {
  271 + var r = this._apply("anything")
  272 + this._pred(r.isNumber())
  273 + return r
  274 + },
  275 + string: function() {
  276 + var r = this._apply("anything")
  277 + this._pred(r.isString())
  278 + return r
  279 + },
  280 + char: function() {
  281 + var r = this._apply("anything")
  282 + this._pred(r.isCharacter())
  283 + return r
  284 + },
  285 + space: function() {
  286 + var r = this._apply("char")
  287 + this._pred(r.charCodeAt(0) <= 32)
  288 + return r
  289 + },
  290 + spaces: function() {
  291 + var $elf = this
  292 + return this._many(function() { return $elf._apply("space") })
  293 + },
  294 + digit: function() {
  295 + var r = this._apply("char")
  296 + this._pred(r.isDigit())
  297 + return r
  298 + },
  299 + lower: function() {
  300 + var r = this._apply("char")
  301 + this._pred(r.isLower())
  302 + return r
  303 + },
  304 + upper: function() {
  305 + var r = this._apply("char")
  306 + this._pred(r.isUpper())
  307 + return r
  308 + },
  309 + letter: function() {
  310 + var $elf = this
  311 + return this._or(function() { return $elf._apply("lower") },
  312 + function() { return $elf._apply("upper") })
  313 + },
  314 + letterOrDigit: function() {
  315 + var $elf = this
  316 + return this._or(function() { return $elf._apply("letter") },
  317 + function() { return $elf._apply("digit") })
  318 + },
  319 + firstAndRest: function() {
  320 + var $elf = this,
  321 + first = this._apply("anything"),
  322 + rest = this._apply("anything")
  323 + return this._many(function() { return $elf._apply(rest) }, this._apply(first))
  324 + },
  325 + seq: function() {
  326 + var xs = this._apply("anything")
  327 + for (var idx = 0; idx < xs.length; idx++)
  328 + this._applyWithArgs("exactly", xs[idx])
  329 + return xs
  330 + },
  331 + notLast: function() {
  332 + var $elf = this,
  333 + rule = this._apply("anything"),
  334 + r = this._apply(rule)
  335 + this._lookahead(function() { return $elf._apply(rule) })
  336 + return r
  337 + },
  338 +
  339 + initialize: function() { },
  340 + // match and matchAll are a grammar's "public interface"
  341 + _genericMatch: function(input, rule, args, matchFailed) {
  342 + if (args == undefined)
  343 + args = []
  344 + var realArgs = [rule]
  345 + for (var idx = 0; idx < args.length; idx++)
  346 + realArgs.push(args[idx])
  347 + var m = this.delegated({input: input})
  348 + m.initialize()
  349 + try { return realArgs.length == 1 ? m._apply.call(m, realArgs[0]) : m._applyWithArgs.apply(m, realArgs) }
  350 + catch (f) {
  351 + if (f == fail && matchFailed != undefined) {
  352 + var input = m.input
  353 + if (input.idx != undefined) {
  354 + while (input.tl != undefined && input.tl.idx != undefined)
  355 + input = input.tl
  356 + input.idx--
  357 + }
  358 + return matchFailed(m, input.idx)
  359 + }
  360 + throw f
  361 + }
  362 + },
  363 + match: function(obj, rule, args, matchFailed) {
  364 + return this._genericMatch([obj].toOMInputStream(), rule, args, matchFailed)
  365 + },
  366 + matchAll: function(listyObj, rule, args, matchFailed) {
  367 + return this._genericMatch(listyObj.toOMInputStream(), rule, args, matchFailed)
  368 + }
  369 +}
  370 +
47 parser.js
... ... @@ -0,0 +1,47 @@
  1 +/*
  2 + Copyright (c) 2007, 2008 Alessandro Warth <awarth@cs.ucla.edu>
  3 +
  4 + Permission is hereby granted, free of charge, to any person
  5 + obtaining a copy of this software and associated documentation
  6 + files (the "Software"), to deal in the Software without
  7 + restriction, including without limitation the rights to use,
  8 + copy, modify, merge, publish, distribute, sublicense, and/or sell
  9 + copies of the Software, and to permit persons to whom the
  10 + Software is furnished to do so, subject to the following
  11 + conditions:
  12 +
  13 + The above copyright notice and this permission notice shall be
  14 + included in all copies or substantial portions of the Software.
  15 +
  16 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17 + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18 + OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19 + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20 + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21 + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22 + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23 + OTHER DEALINGS IN THE SOFTWARE.
  24 +*/
  25 +
  26 +Parser = OMeta.delegated({
  27 + listOf: function() {
  28 + var $elf = this,
  29 + rule = this._apply("anything"),
  30 + delim = this._apply("anything")
  31 + return this._or(function() {
  32 + var r = $elf._apply(rule)
  33 + return $elf._many(function() {
  34 + $elf._applyWithArgs("token", delim)
  35 + return $elf._apply(rule)
  36 + },
  37 + r)
  38 + },
  39 + function() { return [] })
  40 + },
  41 + token: function() {
  42 + var cs = this._apply("anything")
  43 + this._apply("spaces")
  44 + return this._applyWithArgs("seq", cs)
  45 + }
  46 +})
  47 +
91 prototype-fix.js
... ... @@ -0,0 +1,91 @@
  1 +// Override prototype.js' Ajax
  2 +
  3 +// To allow PUT and PROPFIND method
  4 +
  5 +Ajax.Request.prototype.request = function(url) {
  6 + this.url = url;
  7 + this.method = this.options.method;
  8 + var params = Object.clone(this.options.parameters);
  9 +
  10 + if (!['get', 'post', 'put', 'propfind'].include(this.method)) {
  11 +// if (!['get', 'post'].include(this.method)) {
  12 + // simulate other verbs over post
  13 + params['_method'] = this.method;
  14 + this.method = 'post';
  15 + }
  16 +
  17 + this.parameters = params;
  18 +
  19 + if (params = Hash.toQueryString(params)) {
  20 + // when GET, append parameters to URL
  21 + if (this.method == 'get')
  22 + this.url += (this.url.include('?') ? '&' : '?') + params;
  23 + else if (/Konqueror|Safari|KHTML/.test(navigator.userAgent))
  24 + params += '&_=';
  25 + }
  26 +
  27 + try {
  28 + if (this.options.onCreate) this.options.onCreate(this.transport);
  29 + Ajax.Responders.dispatch('onCreate', this, this.transport);
  30 +
  31 + this.transport.open(this.method.toUpperCase(), this.url,
  32 + this.options.asynchronous);
  33 +
  34 + if (this.options.asynchronous)
  35 + setTimeout(function() { this.respondToReadyState(1) }.bind(this), 10);
  36 +
  37 + this.transport.onreadystatechange = this.onStateChange.bind(this);
  38 + this.setRequestHeaders();
  39 +
  40 + this.body = (['post', 'put'].include(this.method)) ? (this.options.postBody || params) : null;
  41