Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

replaced monkey-patched'' methods (e.g., delegated and isImmutable) w…

…ith functions to fix incompatibilities between OMeta/JS and libraries like JQuery
  • Loading branch information...
commit 62b83a954c1a668c390031d87e23d9159a4d60fb 1 parent 5c56a03
@alexwarth authored
View
6 bs-js-compiler.js
@@ -1,4 +1,4 @@
-{BSJSParser=Parser.delegated({
+{BSJSParser=objectThatDelegatesTo(Parser,{
"fromTo":function(){var $elf=this,_fromIdx=this.input.idx,x,y;return (function(){x=this._apply("anything");y=this._apply("anything");this._applyWithArgs("seq",x);this._many((function(){return (function(){this._not((function(){return this._applyWithArgs("seq",y)}));return this._apply("char")}).call(this)}));return this._applyWithArgs("seq",y)}).call(this)},
"space":function(){var $elf=this,_fromIdx=this.input.idx;return this._or((function(){return Parser._superApplyWithArgs(this,'space')}),(function(){return this._applyWithArgs("fromTo","//","\n")}),(function(){return this._applyWithArgs("fromTo","/*","*/")}))},
"nameFirst":function(){var $elf=this,_fromIdx=this.input.idx;return this._or((function(){return this._apply("letter")}),(function(){return (function(){switch(this._apply('anything')){case "$":return "$";case "_":return "_";default: throw fail}}).call(this)}))},
@@ -39,9 +39,9 @@
"stmt":function(){var $elf=this,_fromIdx=this.input.idx,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 this._or((function(){return this._apply("block")}),(function(){return (function(){this._applyWithArgs("token","var");bs=this._applyWithArgs("listOf","binding",",");this._apply("sc");return ["begin"].concat(bs)}).call(this)}),(function(){return (function(){this._applyWithArgs("token","if");this._applyWithArgs("token","(");c=this._apply("expr");this._applyWithArgs("token",")");t=this._apply("stmt");f=this._or((function(){return (function(){this._applyWithArgs("token","else");return this._apply("stmt")}).call(this)}),(function(){return (function(){this._apply("empty");return ["get","undefined"]}).call(this)}));return ["if",c,t,f]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","while");this._applyWithArgs("token","(");c=this._apply("expr");this._applyWithArgs("token",")");s=this._apply("stmt");return ["while",c,s]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","do");s=this._apply("stmt");this._applyWithArgs("token","while");this._applyWithArgs("token","(");c=this._apply("expr");this._applyWithArgs("token",")");this._apply("sc");return ["doWhile",s,c]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","for");this._applyWithArgs("token","(");i=this._or((function(){return (function(){this._applyWithArgs("token","var");return this._apply("binding")}).call(this)}),(function(){return this._apply("expr")}),(function(){return (function(){this._apply("empty");return ["get","undefined"]}).call(this)}));this._applyWithArgs("token",";");c=this._or((function(){return this._apply("expr")}),(function(){return (function(){this._apply("empty");return ["get","true"]}).call(this)}));this._applyWithArgs("token",";");u=this._or((function(){return this._apply("expr")}),(function(){return (function(){this._apply("empty");return ["get","undefined"]}).call(this)}));this._applyWithArgs("token",")");s=this._apply("stmt");return ["for",i,c,u,s]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","for");this._applyWithArgs("token","(");v=this._or((function(){return (function(){this._applyWithArgs("token","var");n=this._applyWithArgs("token","name");return ["var",n,["get","undefined"]]}).call(this)}),(function(){return this._apply("expr")}));this._applyWithArgs("token","in");e=this._apply("expr");this._applyWithArgs("token",")");s=this._apply("stmt");return ["forIn",v,e,s]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","switch");this._applyWithArgs("token","(");e=this._apply("expr");this._applyWithArgs("token",")");this._applyWithArgs("token","{");cs=this._many((function(){return this._or((function(){return (function(){this._applyWithArgs("token","case");c=this._apply("expr");this._applyWithArgs("token",":");cs=this._apply("srcElems");return ["case",c,cs]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","default");this._applyWithArgs("token",":");cs=this._apply("srcElems");return ["default",cs]}).call(this)}))}));this._applyWithArgs("token","}");return ["switch",e].concat(cs)}).call(this)}),(function(){return (function(){this._applyWithArgs("token","break");this._apply("sc");return ["break"]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","continue");this._apply("sc");return ["continue"]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","throw");this._apply("spacesNoNl");e=this._apply("expr");this._apply("sc");return ["throw",e]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","try");t=this._apply("block");this._applyWithArgs("token","catch");this._applyWithArgs("token","(");e=this._applyWithArgs("token","name");this._applyWithArgs("token",")");c=this._apply("block");f=this._or((function(){return (function(){this._applyWithArgs("token","finally");return this._apply("block")}).call(this)}),(function(){return (function(){this._apply("empty");return ["get","undefined"]}).call(this)}));return ["try",t,e,c,f]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","return");e=this._or((function(){return this._apply("expr")}),(function(){return (function(){this._apply("empty");return ["get","undefined"]}).call(this)}));this._apply("sc");return ["return",e]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","with");this._applyWithArgs("token","(");x=this._apply("expr");this._applyWithArgs("token",")");s=this._apply("stmt");return ["with",x,s]}).call(this)}),(function(){return (function(){e=this._apply("expr");this._apply("sc");return e}).call(this)}),(function(){return (function(){this._applyWithArgs("token",";");return ["get","undefined"]}).call(this)}))},
"srcElem":function(){var $elf=this,_fromIdx=this.input.idx,n,f;return this._or((function(){return (function(){this._applyWithArgs("token","function");n=this._applyWithArgs("token","name");f=this._apply("funcRest");return ["var",n,f]}).call(this)}),(function(){return this._apply("stmt")}))},
"srcElems":function(){var $elf=this,_fromIdx=this.input.idx,ss;return (function(){ss=this._many((function(){return this._apply("srcElem")}));return ["begin"].concat(ss)}).call(this)},
-"topLevel":function(){var $elf=this,_fromIdx=this.input.idx,r;return (function(){r=this._apply("srcElems");this._apply("spaces");this._apply("end");return r}).call(this)}});(BSJSParser["hexDigits"]="0123456789abcdef");(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)))}));BSSemActionParser=BSJSParser.delegated({
+"topLevel":function(){var $elf=this,_fromIdx=this.input.idx,r;return (function(){r=this._apply("srcElems");this._apply("spaces");this._apply("end");return r}).call(this)}});(BSJSParser["hexDigits"]="0123456789abcdef");(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"].hasOwnProperty(k))}));BSSemActionParser=objectThatDelegatesTo(BSJSParser,{
"curlySemAction":function(){var $elf=this,_fromIdx=this.input.idx,r,s,ss,r,s;return this._or((function(){return (function(){this._applyWithArgs("token","{");r=this._apply("expr");this._apply("sc");this._applyWithArgs("token","}");this._apply("spaces");return r}).call(this)}),(function(){return (function(){this._applyWithArgs("token","{");ss=this._many((function(){return (function(){s=this._apply("srcElem");this._lookahead((function(){return this._apply("srcElem")}));return s}).call(this)}));s=this._or((function(){return (function(){r=this._apply("expr");this._apply("sc");return ["return",r]}).call(this)}),(function(){return this._apply("srcElem")}));ss.push(s);this._applyWithArgs("token","}");this._apply("spaces");return ["send","call",["func",[],["begin"].concat(ss)],["this"]]}).call(this)}))},
-"semAction":function(){var $elf=this,_fromIdx=this.input.idx,r;return this._or((function(){return this._apply("curlySemAction")}),(function(){return (function(){r=this._apply("primExpr");this._apply("spaces");return r}).call(this)}))}});BSJSTranslator=OMeta.delegated({
+"semAction":function(){var $elf=this,_fromIdx=this.input.idx,r;return this._or((function(){return this._apply("curlySemAction")}),(function(){return (function(){r=this._apply("primExpr");this._apply("spaces");return r}).call(this)}))}});BSJSTranslator=objectThatDelegatesTo(OMeta,{
"trans":function(){var $elf=this,_fromIdx=this.input.idx,t,ans;return (function(){this._form((function(){return (function(){t=this._apply("anything");return ans=this._applyWithArgs("apply",t)}).call(this)}));return ans}).call(this)},
"curlyTrans":function(){var $elf=this,_fromIdx=this.input.idx,r,rs,r;return this._or((function(){return (function(){this._form((function(){return (function(){this._applyWithArgs("exactly","begin");return r=this._apply("curlyTrans")}).call(this)}));return r}).call(this)}),(function(){return (function(){this._form((function(){return (function(){this._applyWithArgs("exactly","begin");return rs=this._many((function(){return this._apply("trans")}))}).call(this)}));return (("{" + rs.join(";")) + "}")}).call(this)}),(function(){return (function(){r=this._apply("trans");return (("{" + r) + "}")}).call(this)}))},
"this":function(){var $elf=this,_fromIdx=this.input.idx;return "this"},
View
2  bs-js-compiler.txt
@@ -147,7 +147,7 @@ keywords = ["break", "case", "catch", "continue", "default", "delete", "do", "el
"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) }
+BSJSParser._isKeyword = function(k) { return this.keywords.hasOwnProperty(k) }
ometa BSSemActionParser <: BSJSParser {
View
6 bs-ometa-compiler.js
@@ -1,4 +1,4 @@
-{BSOMetaParser=Parser.delegated({
+{BSOMetaParser=objectThatDelegatesTo(Parser,{
"fromTo":function(){var $elf=this,_fromIdx=this.input.idx,x,y;return (function(){x=this._apply("anything");y=this._apply("anything");this._applyWithArgs("seq",x);this._many((function(){return (function(){this._not((function(){return this._applyWithArgs("seq",y)}));return this._apply("char")}).call(this)}));return this._applyWithArgs("seq",y)}).call(this)},
"space":function(){var $elf=this,_fromIdx=this.input.idx;return this._or((function(){return Parser._superApplyWithArgs(this,'space')}),(function(){return this._applyWithArgs("fromTo","//","\n")}),(function(){return this._applyWithArgs("fromTo","/*","*/")}))},
"nameFirst":function(){var $elf=this,_fromIdx=this.input.idx;return this._or((function(){return (function(){switch(this._apply('anything')){case "_":return "_";case "$":return "$";default: throw fail}}).call(this)}),(function(){return this._apply("letter")}))},
@@ -34,7 +34,7 @@
"ruleName":function(){var $elf=this,_fromIdx=this.input.idx;return this._or((function(){return this._apply("name")}),(function(){return (function(){this._apply("spaces");return this._apply("tsString")}).call(this)}))},
"rule":function(){var $elf=this,_fromIdx=this.input.idx,n,x,xs;return (function(){this._lookahead((function(){return n=this._apply("ruleName")}));(this["locals"]=["$elf=this","_fromIdx=this.input.idx"]);x=this._applyWithArgs("rulePart",n);xs=this._many((function(){return (function(){this._applyWithArgs("token",",");return this._applyWithArgs("rulePart",n)}).call(this)}));return ["Rule",n,this["locals"],["Or",x].concat(xs)]}).call(this)},
"rulePart":function(){var $elf=this,_fromIdx=this.input.idx,rn,n,b1,b2;return (function(){rn=this._apply("anything");n=this._apply("ruleName");this._pred((n == rn));b1=this._apply("expr4");return this._or((function(){return (function(){this._applyWithArgs("token","=");b2=this._apply("expr");return ["And",b1,b2]}).call(this)}),(function(){return (function(){this._apply("empty");return b1}).call(this)}))}).call(this)},
-"grammar":function(){var $elf=this,_fromIdx=this.input.idx,n,sn,rs;return (function(){this._applyWithArgs("keyword","ometa");n=this._apply("name");sn=this._or((function(){return (function(){this._applyWithArgs("token","<:");return this._apply("name")}).call(this)}),(function(){return (function(){this._apply("empty");return "OMeta"}).call(this)}));this._applyWithArgs("token","{");rs=this._applyWithArgs("listOf","rule",",");this._applyWithArgs("token","}");return this._applyWithArgs("foreign",BSOMetaOptimizer,'optimizeGrammar',["Grammar",n,sn].concat(rs))}).call(this)}});BSOMetaTranslator=OMeta.delegated({
+"grammar":function(){var $elf=this,_fromIdx=this.input.idx,n,sn,rs;return (function(){this._applyWithArgs("keyword","ometa");n=this._apply("name");sn=this._or((function(){return (function(){this._applyWithArgs("token","<:");return this._apply("name")}).call(this)}),(function(){return (function(){this._apply("empty");return "OMeta"}).call(this)}));this._applyWithArgs("token","{");rs=this._applyWithArgs("listOf","rule",",");this._applyWithArgs("token","}");return this._applyWithArgs("foreign",BSOMetaOptimizer,'optimizeGrammar',["Grammar",n,sn].concat(rs))}).call(this)}});BSOMetaTranslator=objectThatDelegatesTo(OMeta,{
"App":function(){var $elf=this,_fromIdx=this.input.idx,args,rule,args,rule;return this._or((function(){return (function(){switch(this._apply('anything')){case "super":return (function(){args=this._many1((function(){return this._apply("anything")}));return [this["sName"],"._superApplyWithArgs(this,",args.join(","),")"].join("")}).call(this);default: throw fail}}).call(this)}),(function(){return (function(){rule=this._apply("anything");args=this._many1((function(){return this._apply("anything")}));return ["this._applyWithArgs(\"",rule,"\",",args.join(","),")"].join("")}).call(this)}),(function(){return (function(){rule=this._apply("anything");return ["this._apply(\"",rule,"\")"].join("")}).call(this)}))},
"Act":function(){var $elf=this,_fromIdx=this.input.idx,expr;return (function(){expr=this._apply("anything");return expr}).call(this)},
"Pred":function(){var $elf=this,_fromIdx=this.input.idx,expr;return (function(){expr=this._apply("anything");return ["this._pred(",expr,")"].join("")}).call(this)},
@@ -53,7 +53,7 @@
"JumpTable":function(){var $elf=this,_fromIdx=this.input.idx,cases;return (function(){cases=this._many((function(){return this._apply("jtCase")}));return this.jumpTableCode(cases)}).call(this)},
"Interleave":function(){var $elf=this,_fromIdx=this.input.idx,xs;return (function(){xs=this._many((function(){return this._apply("intPart")}));return ["this._interleave(",xs.join(","),")"].join("")}).call(this)},
"Rule":function(){var $elf=this,_fromIdx=this.input.idx,name,ls,body;return (function(){name=this._apply("anything");(this["rName"]=name);ls=this._apply("locals");body=this._apply("trans");return ["\n\"",name,"\":function(){",ls,"return ",body,"}"].join("")}).call(this)},
-"Grammar":function(){var $elf=this,_fromIdx=this.input.idx,name,sName,rules;return (function(){name=this._apply("anything");sName=this._apply("anything");(this["name"]=name);(this["sName"]=sName);rules=this._many((function(){return this._apply("trans")}));return [name,"=",sName,".delegated({",rules.join(","),"})"].join("")}).call(this)},
+"Grammar":function(){var $elf=this,_fromIdx=this.input.idx,name,sName,rules;return (function(){name=this._apply("anything");sName=this._apply("anything");(this["name"]=name);(this["sName"]=sName);rules=this._many((function(){return this._apply("trans")}));return [name,"=objectThatDelegatesTo(",sName,",{",rules.join(","),"})"].join("")}).call(this)},
"intPart":function(){var $elf=this,_fromIdx=this.input.idx,mode,part;return (function(){this._form((function(){return (function(){mode=this._apply("anything");return part=this._apply("transFn")}).call(this)}));return ((mode.toProgramString() + ",") + part)}).call(this)},
"jtCase":function(){var $elf=this,_fromIdx=this.input.idx,x,e;return (function(){this._form((function(){return (function(){x=this._apply("anything");return e=this._apply("trans")}).call(this)}));return [x.toProgramString(),e]}).call(this)},
"locals":function(){var $elf=this,_fromIdx=this.input.idx,vs;return this._or((function(){return (function(){this._form((function(){return vs=this._many1((function(){return this._apply("string")}))}));return ["var ",vs.join(","),";"].join("")}).call(this)}),(function(){return (function(){this._form((function(){return undefined}));return ""}).call(this)}))},
View
42 bs-ometa-compiler.txt
@@ -77,41 +77,41 @@ ometa BSOMetaParser <: Parser {
// By dispatching on the head of a list, the following idiom allows translators to avoid doing a linear search.
// (Note that the "=" in a rule definition is optional, so you can give your rules an "ML feel".)
ometa BSOMetaTranslator {
- App 'super' anything+:args -> [this.sName, '._superApplyWithArgs(this,', args.join(','), ')'].join(''),
- App :rule anything+:args -> ['this._applyWithArgs("', rule, '",', args.join(','), ')'].join(''),
- App :rule -> ['this._apply("', rule, '")'] .join(''),
+ App 'super' anything+:args -> [this.sName, '._superApplyWithArgs(this,', args.join(','), ')'] .join(''),
+ App :rule anything+:args -> ['this._applyWithArgs("', rule, '",', args.join(','), ')'] .join(''),
+ App :rule -> ['this._apply("', rule, '")'] .join(''),
Act :expr -> expr,
- Pred :expr -> ['this._pred(', expr, ')'] .join(''),
- Or transFn*:xs -> ['this._or(', xs.join(','), ')'] .join(''),
+ Pred :expr -> ['this._pred(', expr, ')'] .join(''),
+ Or transFn*:xs -> ['this._or(', xs.join(','), ')'] .join(''),
XOr transFn*:xs {xs.unshift((this.name + "." + this.rName).toProgramString())}
- -> ['this._xor(', xs.join(','), ')'] .join(''),
+ -> ['this._xor(', xs.join(','), ')'] .join(''),
And notLast(#trans)*:xs trans:y
- {xs.push('return ' + y)} -> ['(function(){', xs.join(';'), '}).call(this)'] .join(''),
+ {xs.push('return ' + y)} -> ['(function(){', xs.join(';'), '}).call(this)'] .join(''),
And -> 'undefined',
- Opt transFn:x -> ['this._opt(', x, ')'] .join(''),
- Many transFn:x -> ['this._many(', x, ')'] .join(''),
- Many1 transFn:x -> ['this._many1(', x, ')'] .join(''),
- Set :n trans:v -> [n, '=', v].join(''),
- Not transFn:x -> ['this._not(', x, ')'] .join(''),
- Lookahead transFn:x -> ['this._lookahead(', x, ')'] .join(''),
- Form transFn:x -> ['this._form(', x, ')'] .join(''),
- ConsBy transFn:x -> ['this._consumedBy(', x, ')'] .join(''),
- IdxConsBy transFn:x -> ['this._idxConsumedBy(', x, ')'] .join(''),
+ Opt transFn:x -> ['this._opt(', x, ')'] .join(''),
+ Many transFn:x -> ['this._many(', x, ')'] .join(''),
+ Many1 transFn:x -> ['this._many1(', x, ')'] .join(''),
+ Set :n trans:v -> [n, '=', v] .join(''),
+ Not transFn:x -> ['this._not(', x, ')'] .join(''),
+ Lookahead transFn:x -> ['this._lookahead(', x, ')'] .join(''),
+ Form transFn:x -> ['this._form(', x, ')'] .join(''),
+ ConsBy transFn:x -> ['this._consumedBy(', x, ')'] .join(''),
+ IdxConsBy transFn:x -> ['this._idxConsumedBy(', x, ')'] .join(''),
JumpTable jtCase*:cases -> this.jumpTableCode(cases),
- Interleave intPart*:xs -> ['this._interleave(', xs.join(','), ')'] .join(''),
+ Interleave intPart*:xs -> ['this._interleave(', xs.join(','), ')'] .join(''),
Rule :name {this.rName = name}
- locals:ls trans:body -> ['\n"', name, '":function(){', ls, 'return ', body, '}'] .join(''),
+ locals:ls trans:body -> ['\n"', name, '":function(){', ls, 'return ', body, '}'] .join(''),
Grammar :name :sName
{this.name = name}
{this.sName = sName}
- trans*:rules -> [name, '=', sName, '.delegated({', rules.join(','), '})'] .join(''),
+ trans*:rules -> [name, '=objectThatDelegatesTo(', sName, ',{', rules.join(','), '})'].join(''),
intPart = [:mode transFn:part] -> (mode.toProgramString() + "," + part),
jtCase = [:x trans:e] -> [x.toProgramString(), e],
- locals = [string+:vs] -> ['var ', vs.join(','), ';'] .join('')
+ locals = [string+:vs] -> ['var ', vs.join(','), ';'] .join('')
| [] -> '',
trans = [:t apply(t):ans] -> ans,
- transFn = trans:x -> ['(function(){return ', x, '})'] .join('')
+ transFn = trans:x -> ['(function(){return ', x, '})'] .join('')
}
BSOMetaTranslator.jumpTableCode = function(cases) {
var buf = new StringBuffer()
View
4 bs-ometa-js-compiler.js
@@ -1,3 +1,3 @@
-{BSOMetaJSParser=BSJSParser.delegated({
-"srcElem":function(){var $elf=this,_fromIdx=this.input.idx,r;return this._or((function(){return (function(){this._apply("spaces");r=this._applyWithArgs("foreign",BSOMetaParser,'grammar');this._apply("sc");return r}).call(this)}),(function(){return BSJSParser._superApplyWithArgs(this,'srcElem')}))}});BSOMetaJSTranslator=BSJSTranslator.delegated({
+{BSOMetaJSParser=objectThatDelegatesTo(BSJSParser,{
+"srcElem":function(){var $elf=this,_fromIdx=this.input.idx,r;return this._or((function(){return (function(){this._apply("spaces");r=this._applyWithArgs("foreign",BSOMetaParser,'grammar');this._apply("sc");return r}).call(this)}),(function(){return BSJSParser._superApplyWithArgs(this,'srcElem')}))}});BSOMetaJSTranslator=objectThatDelegatesTo(BSJSTranslator,{
"Grammar":function(){var $elf=this,_fromIdx=this.input.idx;return this._applyWithArgs("foreign",BSOMetaTranslator,'Grammar')}})}
View
12 bs-ometa-optimizer.js
@@ -1,7 +1,7 @@
-{BSNullOptimization=OMeta.delegated({
+{BSNullOptimization=objectThatDelegatesTo(OMeta,{
"setHelped":function(){var $elf=this,_fromIdx=this.input.idx;return (this["_didSomething"]=true)},
"helped":function(){var $elf=this,_fromIdx=this.input.idx;return this._pred(this["_didSomething"])},
-"trans":function(){var $elf=this,_fromIdx=this.input.idx,t,ans;return (function(){this._form((function(){return (function(){t=this._apply("anything");this._pred(this.hasProperty(t));return ans=this._applyWithArgs("apply",t)}).call(this)}));return ans}).call(this)},
+"trans":function(){var $elf=this,_fromIdx=this.input.idx,t,ans;return (function(){this._form((function(){return (function(){t=this._apply("anything");this._pred((this[t] != undefined));return ans=this._applyWithArgs("apply",t)}).call(this)}));return ans}).call(this)},
"optimize":function(){var $elf=this,_fromIdx=this.input.idx,x;return (function(){x=this._apply("trans");this._apply("helped");return x}).call(this)},
"App":function(){var $elf=this,_fromIdx=this.input.idx,rule,args;return (function(){rule=this._apply("anything");args=this._many((function(){return this._apply("anything")}));return ["App",rule].concat(args)}).call(this)},
"Act":function(){var $elf=this,_fromIdx=this.input.idx,expr;return (function(){expr=this._apply("anything");return ["Act",expr]}).call(this)},
@@ -20,18 +20,18 @@
"IdxConsBy":function(){var $elf=this,_fromIdx=this.input.idx,x;return (function(){x=this._apply("trans");return ["IdxConsBy",x]}).call(this)},
"JumpTable":function(){var $elf=this,_fromIdx=this.input.idx,c,e,ces;return (function(){ces=this._many((function(){return (function(){this._form((function(){return (function(){c=this._apply("anything");return e=this._apply("trans")}).call(this)}));return [c,e]}).call(this)}));return ["JumpTable"].concat(ces)}).call(this)},
"Interleave":function(){var $elf=this,_fromIdx=this.input.idx,m,p,xs;return (function(){xs=this._many((function(){return (function(){this._form((function(){return (function(){m=this._apply("anything");return p=this._apply("trans")}).call(this)}));return [m,p]}).call(this)}));return ["Interleave"].concat(xs)}).call(this)},
-"Rule":function(){var $elf=this,_fromIdx=this.input.idx,name,ls,body;return (function(){name=this._apply("anything");ls=this._apply("anything");body=this._apply("trans");return ["Rule",name,ls,body]}).call(this)}});(BSNullOptimization["initialize"]=(function (){(this["_didSomething"]=false)}));BSAssociativeOptimization=BSNullOptimization.delegated({
+"Rule":function(){var $elf=this,_fromIdx=this.input.idx,name,ls,body;return (function(){name=this._apply("anything");ls=this._apply("anything");body=this._apply("trans");return ["Rule",name,ls,body]}).call(this)}});(BSNullOptimization["initialize"]=(function (){(this["_didSomething"]=false)}));BSAssociativeOptimization=objectThatDelegatesTo(BSNullOptimization,{
"And":function(){var $elf=this,_fromIdx=this.input.idx,x,xs;return this._or((function(){return (function(){x=this._apply("trans");this._apply("end");this._apply("setHelped");return x}).call(this)}),(function(){return (function(){xs=this._applyWithArgs("transInside","And");return ["And"].concat(xs)}).call(this)}))},
"Or":function(){var $elf=this,_fromIdx=this.input.idx,x,xs;return this._or((function(){return (function(){x=this._apply("trans");this._apply("end");this._apply("setHelped");return x}).call(this)}),(function(){return (function(){xs=this._applyWithArgs("transInside","Or");return ["Or"].concat(xs)}).call(this)}))},
"XOr":function(){var $elf=this,_fromIdx=this.input.idx,x,xs;return this._or((function(){return (function(){x=this._apply("trans");this._apply("end");this._apply("setHelped");return x}).call(this)}),(function(){return (function(){xs=this._applyWithArgs("transInside","XOr");return ["XOr"].concat(xs)}).call(this)}))},
-"transInside":function(){var $elf=this,_fromIdx=this.input.idx,t,xs,ys,x,xs;return (function(){t=this._apply("anything");return this._or((function(){return (function(){this._form((function(){return (function(){this._applyWithArgs("exactly",t);return xs=this._applyWithArgs("transInside",t)}).call(this)}));ys=this._applyWithArgs("transInside",t);this._apply("setHelped");return xs.concat(ys)}).call(this)}),(function(){return (function(){x=this._apply("trans");xs=this._applyWithArgs("transInside",t);return [x].concat(xs)}).call(this)}),(function(){return []}))}).call(this)}});BSSeqInliner=BSNullOptimization.delegated({
+"transInside":function(){var $elf=this,_fromIdx=this.input.idx,t,xs,ys,x,xs;return (function(){t=this._apply("anything");return this._or((function(){return (function(){this._form((function(){return (function(){this._applyWithArgs("exactly",t);return xs=this._applyWithArgs("transInside",t)}).call(this)}));ys=this._applyWithArgs("transInside",t);this._apply("setHelped");return xs.concat(ys)}).call(this)}),(function(){return (function(){x=this._apply("trans");xs=this._applyWithArgs("transInside",t);return [x].concat(xs)}).call(this)}),(function(){return []}))}).call(this)}});BSSeqInliner=objectThatDelegatesTo(BSNullOptimization,{
"App":function(){var $elf=this,_fromIdx=this.input.idx,s,cs,rule,args;return this._or((function(){return (function(){switch(this._apply('anything')){case "seq":return (function(){s=this._apply("anything");this._apply("end");cs=this._applyWithArgs("seqString",s);this._apply("setHelped");return ["And"].concat(cs).concat([["Act",s]])}).call(this);default: throw fail}}).call(this)}),(function(){return (function(){rule=this._apply("anything");args=this._many((function(){return this._apply("anything")}));return ["App",rule].concat(args)}).call(this)}))},
"inlineChar":function(){var $elf=this,_fromIdx=this.input.idx,c;return (function(){c=this._applyWithArgs("foreign",BSOMetaParser,'eChar');this._not((function(){return this._apply("end")}));return ["App","exactly",c.toProgramString()]}).call(this)},
-"seqString":function(){var $elf=this,_fromIdx=this.input.idx,s,cs,cs;return (function(){this._lookahead((function(){return (function(){s=this._apply("anything");return this._pred(((typeof s) === "string"))}).call(this)}));return this._or((function(){return (function(){this._form((function(){return (function(){this._applyWithArgs("exactly","\"");cs=this._many((function(){return this._apply("inlineChar")}));return this._applyWithArgs("exactly","\"")}).call(this)}));return cs}).call(this)}),(function(){return (function(){this._form((function(){return (function(){this._applyWithArgs("exactly","\'");cs=this._many((function(){return this._apply("inlineChar")}));return this._applyWithArgs("exactly","\'")}).call(this)}));return cs}).call(this)}))}).call(this)}});(JumpTable=(function (choiceOp,choice){(this["choiceOp"]=choiceOp);(this["choices"]=({}));this.add(choice)}));(JumpTable["prototype"]["add"]=(function (choice){{var c=choice[(0)];var t=choice[(1)]};if(this["choices"][c]){if((this["choices"][c][(0)] == this["choiceOp"])){this["choices"][c].push(t)}else{(this["choices"][c]=[this["choiceOp"],this["choices"][c],t])}}else{(this["choices"][c]=t)}}));(JumpTable["prototype"]["toTree"]=(function (){{var r=["JumpTable"];var choiceKeys=this["choices"].ownPropertyNames()};for(var i=(0);(i < choiceKeys["length"]);(i+=(1))){r.push([choiceKeys[i],this["choices"][choiceKeys[i]]])};return r}));BSJumpTableOptimization=BSNullOptimization.delegated({
+"seqString":function(){var $elf=this,_fromIdx=this.input.idx,s,cs,cs;return (function(){this._lookahead((function(){return (function(){s=this._apply("anything");return this._pred(((typeof s) === "string"))}).call(this)}));return this._or((function(){return (function(){this._form((function(){return (function(){this._applyWithArgs("exactly","\"");cs=this._many((function(){return this._apply("inlineChar")}));return this._applyWithArgs("exactly","\"")}).call(this)}));return cs}).call(this)}),(function(){return (function(){this._form((function(){return (function(){this._applyWithArgs("exactly","\'");cs=this._many((function(){return this._apply("inlineChar")}));return this._applyWithArgs("exactly","\'")}).call(this)}));return cs}).call(this)}))}).call(this)}});(JumpTable=(function (choiceOp,choice){(this["choiceOp"]=choiceOp);(this["choices"]=({}));this.add(choice)}));(JumpTable["prototype"]["add"]=(function (choice){{var c=choice[(0)];var t=choice[(1)]};if(this["choices"][c]){if((this["choices"][c][(0)] == this["choiceOp"])){this["choices"][c].push(t)}else{(this["choices"][c]=[this["choiceOp"],this["choices"][c],t])}}else{(this["choices"][c]=t)}}));(JumpTable["prototype"]["toTree"]=(function (){{var r=["JumpTable"];var choiceKeys=ownPropertyNames(this["choices"])};for(var i=(0);(i < choiceKeys["length"]);(i+=(1))){r.push([choiceKeys[i],this["choices"][choiceKeys[i]]])};return r}));BSJumpTableOptimization=objectThatDelegatesTo(BSNullOptimization,{
"Or":function(){var $elf=this,_fromIdx=this.input.idx,cs;return (function(){cs=this._many((function(){return this._or((function(){return this._applyWithArgs("jtChoices","Or")}),(function(){return this._apply("trans")}))}));return ["Or"].concat(cs)}).call(this)},
"XOr":function(){var $elf=this,_fromIdx=this.input.idx,cs;return (function(){cs=this._many((function(){return this._or((function(){return this._applyWithArgs("jtChoices","XOr")}),(function(){return this._apply("trans")}))}));return ["XOr"].concat(cs)}).call(this)},
"quotedString":function(){var $elf=this,_fromIdx=this.input.idx,c,cs,c,cs;return (function(){this._lookahead((function(){return this._apply("string")}));this._form((function(){return (function(){switch(this._apply('anything')){case "\"":return (function(){cs=this._many((function(){return (function(){c=this._applyWithArgs("foreign",BSOMetaParser,'eChar');this._not((function(){return this._apply("end")}));return c}).call(this)}));return this._applyWithArgs("exactly","\"")}).call(this);case "\'":return (function(){cs=this._many((function(){return (function(){c=this._applyWithArgs("foreign",BSOMetaParser,'eChar');this._not((function(){return this._apply("end")}));return c}).call(this)}));return this._applyWithArgs("exactly","\'")}).call(this);default: throw fail}}).call(this)}));return cs.join("")}).call(this)},
"jtChoice":function(){var $elf=this,_fromIdx=this.input.idx,x,rest,x;return this._or((function(){return (function(){this._form((function(){return (function(){this._applyWithArgs("exactly","And");this._form((function(){return (function(){this._applyWithArgs("exactly","App");this._applyWithArgs("exactly","exactly");return x=this._apply("quotedString")}).call(this)}));return rest=this._many((function(){return this._apply("anything")}))}).call(this)}));return [x,["And"].concat(rest)]}).call(this)}),(function(){return (function(){this._form((function(){return (function(){this._applyWithArgs("exactly","App");this._applyWithArgs("exactly","exactly");return x=this._apply("quotedString")}).call(this)}));return [x,["Act",x.toProgramString()]]}).call(this)}))},
-"jtChoices":function(){var $elf=this,_fromIdx=this.input.idx,op,c,jt,c;return (function(){op=this._apply("anything");c=this._apply("jtChoice");jt=new JumpTable(op,c);this._many((function(){return (function(){c=this._apply("jtChoice");return jt.add(c)}).call(this)}));this._apply("setHelped");return jt.toTree()}).call(this)}});BSOMetaOptimizer=OMeta.delegated({
+"jtChoices":function(){var $elf=this,_fromIdx=this.input.idx,op,c,jt,c;return (function(){op=this._apply("anything");c=this._apply("jtChoice");jt=new JumpTable(op,c);this._many((function(){return (function(){c=this._apply("jtChoice");return jt.add(c)}).call(this)}));this._apply("setHelped");return jt.toTree()}).call(this)}});BSOMetaOptimizer=objectThatDelegatesTo(OMeta,{
"optimizeGrammar":function(){var $elf=this,_fromIdx=this.input.idx,n,sn,rs;return (function(){this._form((function(){return (function(){this._applyWithArgs("exactly","Grammar");n=this._apply("anything");sn=this._apply("anything");return rs=this._many((function(){return this._apply("optimizeRule")}))}).call(this)}));return ["Grammar",n,sn].concat(rs)}).call(this)},
"optimizeRule":function(){var $elf=this,_fromIdx=this.input.idx,r,r,r,r;return (function(){r=this._apply("anything");this._or((function(){return r=this._applyWithArgs("foreign",BSSeqInliner,'optimize',r)}),(function(){return this._apply("empty")}));this._many((function(){return this._or((function(){return r=this._applyWithArgs("foreign",BSAssociativeOptimization,'optimize',r)}),(function(){return r=this._applyWithArgs("foreign",BSJumpTableOptimization,'optimize',r)}))}));return r}).call(this)}})}
View
4 bs-ometa-optimizer.txt
@@ -4,7 +4,7 @@
ometa BSNullOptimization {
setHelped = !(this._didSomething = true),
helped = ?this._didSomething,
- trans = [:t ?this.hasProperty(t) apply(t):ans] -> ans,
+ trans = [:t ?(this[t] != undefined) apply(t):ans] -> ans,
optimize = trans:x helped -> x,
App :rule anything*:args -> ['App', rule].concat(args),
@@ -68,7 +68,7 @@ JumpTable.prototype.add = function(choice) {
this.choices[c] = t
}
JumpTable.prototype.toTree = function() {
- var r = ['JumpTable'], choiceKeys = this.choices.ownPropertyNames()
+ var r = ['JumpTable'], choiceKeys = ownPropertyNames(this.choices)
for (var i = 0; i < choiceKeys.length; i += 1)
r.push([choiceKeys[i], this.choices[choiceKeys[i]]])
return r
View
2  bs-project-list-parser.js
@@ -1,3 +1,3 @@
-ProjectListParser=BSJSParser.delegated({
+ProjectListParser=objectThatDelegatesTo(BSJSParser,{
"proj":function(){var $elf=this,_fromIdx=this.input.idx;return (function(){this._apply("spaces");return this._apply("iName")}).call(this)},
"projs":function(){var $elf=this,_fromIdx=this.input.idx,x,xs;return (function(){x=this._apply("proj");xs=this._many((function(){return (function(){this._apply("spaces");this._applyWithArgs("exactly",",");return this._apply("proj")}).call(this)}));return [x].concat(xs)}).call(this)}})
View
81 lib.js
@@ -39,28 +39,26 @@ String.prototype.writeStream = function() { return new StringBuffer(this) }
printOn = function(x, ws) {
if (x === undefined || x === null)
ws.nextPutAll("" + x)
+ else if (x.constructor === Array) {
+ ws.nextPutAll("[")
+ for (var idx = 0; idx < x.length; idx++) {
+ if (idx > 0)
+ ws.nextPutAll(", ")
+ printOn(x[idx], ws)
+ }
+ ws.nextPutAll("]")
+ }
else
- x.printOn(ws)
+ ws.nextPutAll(x.toString())
}
-Object.prototype.printOn = function(ws) { ws.nextPutAll(this.toString()) }
-
-Array.prototype.toString = function() { var ws = "".writeStream(); this.printOn(ws); return ws.contents() }
-Array.prototype.printOn = function(ws) {
- ws.nextPutAll("[")
- for (var idx = 0; idx < this.length; idx++) {
- if (idx > 0)
- ws.nextPutAll(", ")
- printOn(this[idx], ws)
- }
- ws.nextPutAll("]")
-}
+Array.prototype.toString = function() { var ws = "".writeStream(); printOn(this, ws); return ws.contents() }
// delegation
-Object.prototype.delegated = function(props) {
+objectThatDelegatesTo = function(x, props) {
var f = function() { }
- f.prototype = this
+ f.prototype = x
var r = new f()
for (var p in props)
if (props.hasOwnProperty(p))
@@ -70,41 +68,21 @@ Object.prototype.delegated = function(props) {
// some reflective stuff
-Object.prototype.ownPropertyNames = function() {
+ownPropertyNames = function(x) {
var r = []
- for (name in this)
- if (this.hasOwnProperty(name))
+ for (name in x)
+ if (x.hasOwnProperty(name))
r.push(name)
return r
}
-Object.prototype.hasProperty = function(p) { return this[p] != undefined }
-
-isImmutable = function(x) { return x === null || x === undefined || x.isImmutable() }
-Object.prototype.isImmutable = function() { return false }
-Boolean.prototype.isImmutable = function() { return true }
-Number.prototype.isImmutable = function() { return true }
-String.prototype.isImmutable = function() { return true }
-
-Object.prototype.isNumber = function() { return false }
-Number.prototype.isNumber = function() { return true }
-
-Object.prototype.isString = function() { return false }
-String.prototype.isString = function() { return true }
-
-Object.prototype.isCharacter = function() { return false }
+isImmutable = function(x) {
+ return x === null || x === undefined || typeof x === "boolean" || typeof x === "number" || typeof x === "string"
+}
-String.prototype.isCharacter = function() { return this.length == 1 }
-String.prototype.isSpace = function() { return this.isCharacter() && this.charCodeAt(0) <= 32 }
-String.prototype.isDigit = function() { return this.isCharacter() && this >= "0" && this <= "9" }
-String.prototype.isLower = function() { return this.isCharacter() && this >= "a" && this <= "z" }
-String.prototype.isUpper = function() { return this.isCharacter() && this >= "A" && this <= "Z" }
-
String.prototype.digitValue = function() { return this.charCodeAt(0) - "0".charCodeAt(0) }
-Object.prototype.isSequenceable = false
-Array.prototype.isSequenceable = true
-String.prototype.isSequenceable = true
+isSequenceable = function(x) { return typeof x == "string" || x.constructor === Array }
// some functional programming stuff
@@ -186,12 +164,17 @@ tempnam.n = 0
// unique tags for objects (useful for making "hash tables")
-getTag = function(x) { return (x === null || x === undefined) ? x : x.getTag() }
-Object.prototype.getTag = (function() {
- var numIds = 0
- return function() { return this.hasOwnProperty("_id_") ? this._id_ : this._id_ = "R" + numIds++ }
+getTag = (function() {
+ var numIdx = 0
+ return function(x) {
+ if (x === null || x === undefined)
+ return x
+ switch (typeof x) {
+ case "boolean": return x == true ? "Btrue" : "Bfalse"
+ case "string": return "S" + x
+ case "number": return "N" + x
+ default: return x.hasOwnProperty("_id_") ? this._id_ : this._id_ = "R" + numIdx++
+ }
+ }
})()
-Boolean.prototype.getTag = function() { return this == true ? "Btrue" : "Bfalse" }
-String.prototype.getTag = function() { return "S" + this }
-Number.prototype.getTag = function() { return "N" + this }
View
20 ometa-base.js
@@ -43,7 +43,7 @@ ometa M {
translates to...
-M = OMeta.delegated({
+M = objectThatDelegatesTo(OMeta, {
number: function() {
return this._or(function() {
var n = this._apply("number"),
@@ -90,7 +90,7 @@ function OMInputStreamEnd(lst, idx) {
this.lst = lst
this.idx = idx
}
-OMInputStreamEnd.prototype = OMInputStream.prototype.delegated()
+OMInputStreamEnd.prototype = objectThatDelegatesTo(OMInputStream.prototype)
OMInputStreamEnd.prototype.head = function() { throw fail }
OMInputStreamEnd.prototype.tail = function() { throw fail }
@@ -104,7 +104,7 @@ function ListOMInputStream(lst, idx) {
this.idx = idx
this.hd = lst.at(idx)
}
-ListOMInputStream.prototype = OMInputStream.prototype.delegated()
+ListOMInputStream.prototype = objectThatDelegatesTo(OMInputStream.prototype)
ListOMInputStream.prototype.head = function() { return this.hd }
ListOMInputStream.prototype.tail = function() { return this.tl || (this.tl = makeListOMInputStream(this.lst, this.idx + 1)) }
@@ -114,7 +114,7 @@ Array.prototype.toOMInputStream = function() { return makeListOMInputStream(thi
String.prototype.toOMInputStream = function() { return makeListOMInputStream(this, 0) }
function makeOMInputStreamProxy(target) {
- return target.delegated({
+ return objectThatDelegatesTo(target, {
memo: { },
target: target,
tail: function() { return makeOMInputStreamProxy(target.tail()) }
@@ -293,7 +293,7 @@ OMeta = {
_many1: function(x) { return this._many(x, x.call(this)) },
_form: function(x) {
var v = this._apply("anything")
- if (!v.isSequenceable)
+ if (!isSequenceable(v))
throw fail
var origInput = this.input
this.input = v.toOMInputStream()
@@ -370,7 +370,7 @@ OMeta = {
foreign: function() {
var g = this._apply("anything"),
r = this._apply("anything"),
- gi = g.delegated({input: makeOMInputStreamProxy(this.input)})
+ gi = objectThatDelegatesTo(g, {input: makeOMInputStreamProxy(this.input)})
var ans = gi._apply(r)
this.input = gi.input.target
return ans
@@ -423,17 +423,17 @@ OMeta = {
},
digit: function() {
var r = this._apply("char")
- this._pred(r.isDigit())
+ this._pred(r >= "0" && r <= "9")
return r
},
lower: function() {
var r = this._apply("char")
- this._pred(r.isLower())
+ this._pred(r >= "a" && r <= "z")
return r
},
upper: function() {
var r = this._apply("char")
- this._pred(r.isUpper())
+ this._pred(r >= "A" && r <= "Z")
return r
},
letter: function() {
@@ -470,7 +470,7 @@ OMeta = {
var realArgs = [rule]
for (var idx = 0; idx < args.length; idx++)
realArgs.push(args[idx])
- var m = this.delegated({input: input})
+ var m = objectThatDelegatesTo(this, {input: input})
m.initialize()
try { return realArgs.length == 1 ? m._apply.call(m, realArgs[0]) : m._applyWithArgs.apply(m, realArgs) }
catch (f) {
View
3  ometa-rhino.js
@@ -10,7 +10,8 @@ alert = print
translateCode = function(s) {
var translationError = function(m, i) { alert("Translation error - please tell Alex about this!"); throw fail },
- tree = BSOMetaJSParser.matchAll(s, "topLevel", undefined, function(m, i) { throw fail.delegated({errorPos: i}) })
+ tree = BSOMetaJSParser.matchAll(s, "topLevel", undefined, function(m, i) {
+ throw objectThatDelegatesTo(fail, {errorPos: i}) })
return BSOMetaJSTranslator.match(tree, "trans", undefined, translationError)
}
View
3  ometa-script-tag.js
@@ -1,6 +1,7 @@
function translateCode(s) {
var translationError = function(m, i) { alert("Translation error - please tell Alex about this!"); throw fail },
- tree = BSOMetaJSParser.matchAll(s, "topLevel", undefined, function(m, i) { throw fail.delegated({errorPos: i}) })
+ tree = BSOMetaJSParser.matchAll(s, "topLevel", undefined, function(m, i) {
+ throw objectThatDelegatesTo(fail, {errorPos: i}) })
return BSOMetaJSTranslator.match(tree, "trans", undefined, translationError)
}
View
2  parser.js
@@ -23,7 +23,7 @@
OTHER DEALINGS IN THE SOFTWARE.
*/
-Parser = OMeta.delegated({
+Parser = objectThatDelegatesTo(OMeta, {
listOf: function() {
var rule = this._apply("anything"),
delim = this._apply("anything")
Please sign in to comment.
Something went wrong with that request. Please try again.