Skip to content

Commit

Permalink
made regular expression support a little more ECMAScript-262 compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwarth committed Jun 30, 2011
1 parent 43c6a74 commit 896ea65
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
11 changes: 10 additions & 1 deletion bs-js-compiler.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 14 additions & 17 deletions bs-js-compiler.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand All @@ -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]
Expand Down Expand Up @@ -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 + ']'),
Expand Down

3 comments on commit 896ea65

@veged
Copy link

@veged veged commented on 896ea65 Jul 1, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems a typo: "re" produce [#regExp, x], but BSJSTranslator has "regExpr :x"

@alexwarth
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, you're right. Nice catch :)

Fixed.

@veged
Copy link

@veged veged commented on 896ea65 Jul 2, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing supernatural, i'm just try run code ;-)

Please sign in to comment.