Permalink
Browse files

made regular expression support a little more ECMAScript-262 compliant

  • Loading branch information...
1 parent 43c6a74 commit 896ea65723da15bb46632dd270b256b7c29bb384 @alexwarth alexwarth committed Jun 30, 2011
Showing with 24 additions and 18 deletions.
  1. +10 −1 bs-js-compiler.js
  2. +14 −17 bs-js-compiler.txt
View
11 bs-js-compiler.js
@@ -26,10 +26,18 @@
"unary":function(){var $elf=this,_fromIdx=this.input.idx,p,p,p,p,p,p,p,p;return this._or((function(){return (function(){this._applyWithArgs("token","-");p=this._apply("postfix");return ["unop","-",p]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","+");p=this._apply("postfix");return ["unop","+",p]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","++");p=this._apply("postfix");return ["preop","++",p]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","--");p=this._apply("postfix");return ["preop","--",p]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","!");p=this._apply("unary");return ["unop","!",p]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","void");p=this._apply("unary");return ["unop","void",p]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","delete");p=this._apply("unary");return ["unop","delete",p]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","typeof");p=this._apply("unary");return ["unop","typeof",p]}).call(this)}),(function(){return this._apply("postfix")}))},
"postfix":function(){var $elf=this,_fromIdx=this.input.idx,p;return (function(){p=this._apply("primExpr");return this._or((function(){return (function(){this._apply("spacesNoNl");this._applyWithArgs("token","++");return ["postop","++",p]}).call(this)}),(function(){return (function(){this._apply("spacesNoNl");this._applyWithArgs("token","--");return ["postop","--",p]}).call(this)}),(function(){return (function(){this._apply("empty");return p}).call(this)}))}).call(this)},
"primExpr":function(){var $elf=this,_fromIdx=this.input.idx,p,i,m,as,f,as;return this._or((function(){return (function(){p=this._apply("primExpr");return this._or((function(){return (function(){this._applyWithArgs("token","[");i=this._apply("expr");this._applyWithArgs("token","]");return ["getp",i,p]}).call(this)}),(function(){return (function(){this._applyWithArgs("token",".");m=this._applyWithArgs("token","name");this._applyWithArgs("token","(");as=this._applyWithArgs("listOf","expr",",");this._applyWithArgs("token",")");return ["send",m,p].concat(as)}).call(this)}),(function(){return (function(){this._applyWithArgs("token",".");f=this._applyWithArgs("token","name");return ["getp",["string",f],p]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","(");as=this._applyWithArgs("listOf","expr",",");this._applyWithArgs("token",")");return ["call",p].concat(as)}).call(this)}))}).call(this)}),(function(){return this._apply("primExprHd")}))},
-"primExprHd":function(){var $elf=this,_fromIdx=this.input.idx,e,n,n,s,n,as,es;return this._or((function(){return (function(){this._applyWithArgs("token","(");e=this._apply("expr");this._applyWithArgs("token",")");return e}).call(this)}),(function(){return (function(){this._applyWithArgs("token","this");return ["this"]}).call(this)}),(function(){return (function(){n=this._applyWithArgs("token","name");return ["get",n]}).call(this)}),(function(){return (function(){n=this._applyWithArgs("token","number");return ["number",n]}).call(this)}),(function(){return (function(){s=this._applyWithArgs("token","string");return ["string",s]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","function");return this._apply("funcRest")}).call(this)}),(function(){return (function(){this._applyWithArgs("token","new");n=this._applyWithArgs("token","name");this._applyWithArgs("token","(");as=this._applyWithArgs("listOf","expr",",");this._applyWithArgs("token",")");return ["new",n].concat(as)}).call(this)}),(function(){return (function(){this._applyWithArgs("token","[");es=this._applyWithArgs("listOf","expr",",");this._applyWithArgs("token","]");return ["arr"].concat(es)}).call(this)}),(function(){return this._apply("json")}))},
+"primExprHd":function(){var $elf=this,_fromIdx=this.input.idx,e,n,n,s,n,as,es;return this._or((function(){return (function(){this._applyWithArgs("token","(");e=this._apply("expr");this._applyWithArgs("token",")");return e}).call(this)}),(function(){return (function(){this._applyWithArgs("token","this");return ["this"]}).call(this)}),(function(){return (function(){n=this._applyWithArgs("token","name");return ["get",n]}).call(this)}),(function(){return (function(){n=this._applyWithArgs("token","number");return ["number",n]}).call(this)}),(function(){return (function(){s=this._applyWithArgs("token","string");return ["string",s]}).call(this)}),(function(){return (function(){this._applyWithArgs("token","function");return this._apply("funcRest")}).call(this)}),(function(){return (function(){this._applyWithArgs("token","new");n=this._applyWithArgs("token","name");this._applyWithArgs("token","(");as=this._applyWithArgs("listOf","expr",",");this._applyWithArgs("token",")");return ["new",n].concat(as)}).call(this)}),(function(){return (function(){this._applyWithArgs("token","[");es=this._applyWithArgs("listOf","expr",",");this._applyWithArgs("token","]");return ["arr"].concat(es)}).call(this)}),(function(){return this._apply("json")}),(function(){return this._apply("re")}))},
"json":function(){var $elf=this,_fromIdx=this.input.idx,bs;return (function(){this._applyWithArgs("token","{");bs=this._applyWithArgs("listOf","jsonBinding",",");this._applyWithArgs("token","}");return ["json"].concat(bs)}).call(this)},
"jsonBinding":function(){var $elf=this,_fromIdx=this.input.idx,n,v;return (function(){n=this._apply("jsonPropName");this._applyWithArgs("token",":");v=this._apply("expr");return ["binding",n,v]}).call(this)},
"jsonPropName":function(){var $elf=this,_fromIdx=this.input.idx;return this._or((function(){return this._applyWithArgs("token","name")}),(function(){return this._applyWithArgs("token","number")}),(function(){return this._applyWithArgs("token","string")}))},
+"re":function(){var $elf=this,_fromIdx=this.input.idx,x;return (function(){this._apply("spaces");x=this._consumedBy((function(){return (function(){this._applyWithArgs("exactly","/");this._apply("reBody");this._applyWithArgs("exactly","/");return this._many((function(){return this._apply("reFlag")}))}).call(this)}));return ["regExp",x]}).call(this)},
+"reBody":function(){var $elf=this,_fromIdx=this.input.idx;return (function(){this._apply("re1stChar");return this._many((function(){return this._apply("reChar")}))}).call(this)},
+"re1stChar":function(){var $elf=this,_fromIdx=this.input.idx;return this._or((function(){return (function(){this._not((function(){return (function(){switch(this._apply('anything')){case "*":return "*";case "\\":return "\\";case "/":return "/";case "[":return "[";default: throw fail}}).call(this)}));return this._apply("reNonTerm")}).call(this)}),(function(){return this._apply("escapeChar")}),(function(){return this._apply("reClass")}))},
+"reChar":function(){var $elf=this,_fromIdx=this.input.idx;return this._or((function(){return this._apply("re1stChar")}),(function(){return (function(){switch(this._apply('anything')){case "*":return "*";default: throw fail}}).call(this)}))},
+"reNonTerm":function(){var $elf=this,_fromIdx=this.input.idx;return (function(){this._not((function(){return (function(){switch(this._apply('anything')){case "\n":return "\n";case "\r":return "\r";default: throw fail}}).call(this)}));return this._apply("char")}).call(this)},
+"reClass":function(){var $elf=this,_fromIdx=this.input.idx;return (function(){this._applyWithArgs("exactly","[");this._many((function(){return this._apply("reClassChar")}));return this._applyWithArgs("exactly","]")}).call(this)},
+"reClassChar":function(){var $elf=this,_fromIdx=this.input.idx;return (function(){this._not((function(){return (function(){switch(this._apply('anything')){case "[":return "[";case "]":return "]";default: throw fail}}).call(this)}));return this._apply("reChar")}).call(this)},
+"reFlag":function(){var $elf=this,_fromIdx=this.input.idx;return this._apply("nameFirst")},
"formal":function(){var $elf=this,_fromIdx=this.input.idx;return (function(){this._apply("spaces");return this._applyWithArgs("token","name")}).call(this)},
"funcRest":function(){var $elf=this,_fromIdx=this.input.idx,fs,body;return (function(){this._applyWithArgs("token","(");fs=this._applyWithArgs("listOf","formal",",");this._applyWithArgs("token",")");this._applyWithArgs("token","{");body=this._apply("srcElems");this._applyWithArgs("token","}");return ["func",fs,body]}).call(this)},
"sc":function(){var $elf=this,_fromIdx=this.input.idx;return this._or((function(){return (function(){this._apply("spacesNoNl");return this._or((function(){return (function(){switch(this._apply('anything')){case "\n":return "\n";default: throw fail}}).call(this)}),(function(){return this._lookahead((function(){return this._applyWithArgs("exactly","}")}))}),(function(){return this._apply("end")}))}).call(this)}),(function(){return this._applyWithArgs("token",";")}))},
@@ -48,6 +56,7 @@
"continue":function(){var $elf=this,_fromIdx=this.input.idx;return "continue"},
"number":function(){var $elf=this,_fromIdx=this.input.idx,n;return (function(){n=this._apply("anything");return (("(" + n) + ")")}).call(this)},
"string":function(){var $elf=this,_fromIdx=this.input.idx,s;return (function(){s=this._apply("anything");return s.toProgramString()}).call(this)},
+"regExpr":function(){var $elf=this,_fromIdx=this.input.idx,x;return (function(){x=this._apply("anything");return x}).call(this)},
"arr":function(){var $elf=this,_fromIdx=this.input.idx,xs;return (function(){xs=this._many((function(){return this._apply("trans")}));return (("[" + xs.join(",")) + "]")}).call(this)},
"unop":function(){var $elf=this,_fromIdx=this.input.idx,op,x;return (function(){op=this._apply("anything");x=this._apply("trans");return (((("(" + op) + " ") + x) + ")")}).call(this)},
"getp":function(){var $elf=this,_fromIdx=this.input.idx,fd,x;return (function(){fd=this._apply("trans");x=this._apply("trans");return (((x + "[") + fd) + "]")}).call(this)},
View
31 bs-js-compiler.txt
@@ -22,23 +22,10 @@ ometa BSJSParser {
| '+' | ``--'' | ``-='' | '-' | ``*='' | '*' | ``/=''
| '/' | ``%='' | '%' | ``&&='' | ``&&'' | ``||='' | ``||''
| '.' | '!' ):s -> [s, s],
- tok = spaces (name | keyword | number | str | rexp | special),
+ tok = spaces (name | keyword | number | str | special),
toks = token*:ts spaces end -> ts,
token :tt = tok:t ?(t[0] == tt) -> t[1],
spacesNoNl = (~'\n' space)*,
-
- rexp = '/' rexpBody:x '/' rexpFlag*:y -> [#rexp, '/' + x + '/' + y.join('')],
- rexpFlag = 'g' | 'i' | 'm',
- rexpBody = rexpFChar:x rexpChar*:y -> (x + y.join('')),
- rexpFChar = ~'*' rexpChar,
- rexpChar = ~'\\' ~'/' ~'[' rexpNoTerm
- | escapeChar
- | rexpClass,
- rexpNoTerm = ~'\n' ~'\r' char,
- rexpClass = '[' rexpClChar*:x ']' -> ('[' + x.join('') + ']'),
- rexpClChar = ~']' ~'\\' rexpNoTerm
- | escapeChar,
-
expr = orExpr:e ( "?" expr:t ":" expr:f -> [#condExpr, e, t, f]
| "=" expr:rhs -> [#set, e, rhs]
| "+=" expr:rhs -> [#mset, e, "+", rhs]
@@ -101,10 +88,21 @@ ometa BSJSParser {
| "function" funcRest
| "new" "name":n "(" listOf(#expr, ','):as ")" -> [#new, n].concat(as)
| "[" listOf(#expr, ','):es "]" -> [#arr].concat(es)
- | json,
+ | json
+ | re,
json = "{" listOf(#jsonBinding, ','):bs "}" -> [#json].concat(bs),
jsonBinding = jsonPropName:n ":" expr:v -> [#binding, n, v],
jsonPropName = "name" | "number" | "string",
+ re = spaces <'/' reBody '/' reFlag*>:x -> [#regExp, x],
+ reBody = re1stChar reChar*,
+ re1stChar = ~('*' || '\\' || '/' || '[') reNonTerm
+ | escapeChar
+ | reClass,
+ reChar = re1stChar | '*',
+ reNonTerm = ~('\n' || '\r') char,
+ reClass = '[' reClassChar* ']',
+ reClassChar = ~('[' || ']') reChar,
+ reFlag = nameFirst,
formal = spaces "name",
funcRest = "(" listOf(#formal, ','):fs ")" "{" srcElems:body "}" -> [#func, fs, body],
sc = spacesNoNl ('\n' | &'}' | end)
@@ -113,7 +111,6 @@ ometa BSJSParser {
| empty -> [#get, 'undefined'] ):v -> [#var, n, v],
block = "{" srcElems:ss "}" -> ss,
stmt = block
- | rexp
| "var" listOf(#binding, ','):bs sc -> [#begin].concat(bs)
| "if" "(" expr:c ")" stmt:t ( "else" stmt
| empty -> [#get, 'undefined'] ):f -> [#if, c, t, f]
@@ -183,7 +180,7 @@ ometa BSJSTranslator {
continue -> 'continue',
number :n -> ('(' + n + ')'),
string :s -> s.toProgramString(),
- rexp :x -> x,
+ regExpr :x -> x,
arr trans*:xs -> ('[' + xs.join(',') + ']'),
unop :op trans:x -> ('(' + op + ' ' + x + ')'),
getp trans:fd trans:x -> (x + '[' + fd + ']'),

0 comments on commit 896ea65

Please sign in to comment.