Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first commit

  • Loading branch information...
commit 59a3634bf128831ecd56d85fdde92524c934c8be 1 parent ab1b470
@veged authored
Showing with 1,047 additions and 6,370 deletions.
  1. +26 −0 GNUmakefile
  2. +0 −624 OMeta_WJS.wjs
  3. +0 −110 OMeta_WJS_Mods.js
  4. +0 −112 Worlds2.js
  5. +0 −311 Worlds2_Library.js
  6. +0 −35 arrays.wjs
  7. +3 −0  bin/ometajs2js
  8. +6 −0 bla.js
  9. +13 −0 bla.txt
  10. +0 −3  bs-project-list-parser.js
  11. +0 −5 bs-project-list-parser.txt
  12. 0  { → doc}/Not_Quite_JS.txt
  13. 0  { → doc}/OMeta_Tutorial.txt
  14. 0  { → doc}/README.txt
  15. 0  { → doc}/Things_You_Should_Know.txt
  16. +0 −166 index.html
  17. +914 −0 lib/ometajs.js
  18. +58 −0 lib/ometajs/2js.js
  19. +0 −19 ometa-rhino.js
  20. +0 −21 ometa-script-tag.js
  21. +0 −2  oshell
  22. +19 −0 package.json
  23. +0 −91 prototype-fix.js
  24. +0 −4,222 prototype.js
  25. +0 −10 readme-rhino.txt
  26. 0  src/*.ometa
  27. 0  bs-js-compiler.txt → src/bs-js-compiler.ometajs
  28. 0  bs-js-compiler.js → src/bs-js-compiler.ometajs.js
  29. 0  bs-ometa-compiler.txt → src/bs-ometa-compiler.ometajs
  30. 0  bs-ometa-compiler.js → src/bs-ometa-compiler.ometajs.js
  31. 0  bs-ometa-js-compiler.txt → src/bs-ometa-js-compiler.ometajs
  32. 0  bs-ometa-js-compiler.js → src/bs-ometa-js-compiler.ometajs.js
  33. 0  bs-ometa-optimizer.txt → src/bs-ometa-optimizer.ometajs
  34. 0  bs-ometa-optimizer.js → src/bs-ometa-optimizer.ometajs.js
  35. 0  { → src}/lib.js
  36. 0  { → src}/ometa-base.js
  37. +8 −0 src/ometajs.js
  38. 0  { → src}/parser.js
  39. +0 −21 standalone.html
  40. +0 −320 v8-shell.cc
  41. +0 −2  w2shell
  42. +0 −73 wiki.js
  43. +0 −223 workspace.js
View
26 GNUmakefile
@@ -0,0 +1,26 @@
+
+all: lib
+ echo all
+
+install:
+ echo $@
+
+src: $(patsubst %.ometajs,%.ometajs.js,$(wildcard src/*.ometajs))
+
+%.ometajs.js: %.ometajs
+ bin/ometajs2js -i $< -o $@
+
+lib: lib/ometajs.js
+
+lib/ometajs.js: src
+ -rm $@
+ cat $</lib.js >> $@
+ cat $</ometa-base.js >> $@
+ cat $</parser.js >> $@
+ cat $</bs-js-compiler.ometajs.js >> $@
+ cat $</bs-ometa-compiler.ometajs.js >> $@
+ cat $</bs-ometa-optimizer.ometajs.js >> $@
+ cat $</bs-ometa-js-compiler.ometajs.js >> $@
+ cat $</ometajs.js >> $@
+
+.PHONY: all install
View
624 OMeta_WJS.wjs
@@ -1,624 +0,0 @@
-// TODO: rewrite _apply to use worlds
-
-
-//////// lib.js
-
-/*
- Copyright (c) 2007, 2008 Alessandro Warth <awarth@cs.ucla.edu>
-
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
-
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-// try to use StringBuffer instead of string concatenation to improve performance
-
-function StringBuffer() {
- this.strings = []
- for (var idx = 0; idx < arguments.length; idx++)
- this.nextPutAll(arguments[idx])
-}
-StringBuffer.prototype.nextPutAll = function(s) { this.strings.push(s) }
-StringBuffer.prototype.contents = function() { return this.strings.join("") }
-String.prototype.writeStream = function() { return new StringBuffer(this) }
-
-// make Arrays print themselves sensibly
-
-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(", ")
- this[idx].printOn(ws)
- }
- ws.nextPutAll("]")
-}
-
-// delegation
-
-Object.prototype.delegated = function(props) {
- var f = function() { }
- f.prototype = this
- var r = new f()
- for (var p in props)
- if (props.hasOwn(p))
- r[p] = props[p]
- return r
-}
-
-// some reflective stuff
-
-Object.prototype.ownPropertyNames = function() {
- var r = []
- for (name in this)
- if (this.hasOwn(name))
- r.push(name)
- return r
-}
-
-Object.prototype.hasProperty = function(p) { return this[p] != undefined }
-
-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 }
-
-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
-
-// some functional programming stuff
-
-Array.prototype.map = function(f) {
- var r = []
- for (var idx = 0; idx < this.length; idx++)
- r[idx] = f(this[idx])
- return r
-}
-
-Array.prototype.reduce = function(f, z) {
- var r = z
- for (var idx = 0; idx < this.length; idx++)
- r = f(r, this[idx])
- return r
-}
-
-Array.prototype.delimWith = function(d) {
- return this.reduce(
- function(xs, x) {
- if (xs.length > 0)
- xs.push(d)
- xs.push(x)
- return xs
- },
- [])
-}
-
-// Squeak's ReadStream, kind of
-
-function ReadStream(anArrayOrString) {
- this.src = anArrayOrString
- this.pos = 0
-}
-ReadStream.prototype.atEnd = function() { return this.pos >= this.src.length }
-ReadStream.prototype.next = function() { return this.src[this.pos++] }
-
-// escape characters
-
-escapeStringFor = new Object()
-for (var c = 0; c < 256; c++)
- escapeStringFor[c] = String.fromCharCode(c)
-escapeStringFor["\\".charCodeAt(0)] = "\\\\"
-escapeStringFor['"'.charCodeAt(0)] = '\\"'
-escapeStringFor["'".charCodeAt(0)] = "\\'"
-escapeStringFor["\r".charCodeAt(0)] = "\\r"
-escapeStringFor["\n".charCodeAt(0)] = "\\n"
-escapeStringFor["\t".charCodeAt(0)] = "\\t"
-escapeChar = function(c) {
- var charCode = c.charCodeAt(0)
- return charCode > 255 ? String.fromCharCode(charCode) : escapeStringFor[charCode]
-}
-
-function unescape(s) {
- if (s[0] == '\\')
- switch (s[1]) {
- case '\\': return '\\'
- case 'r': return '\r'
- case 'n': return '\n'
- case 't': return '\t'
- default: return s[1]
- }
- else
- return s
-}
-
-String.prototype.toProgramString = function() {
- var ws = "\"".writeStream()
- for (var idx = 0; idx < this.length; idx++)
- ws.nextPutAll(escapeChar(this[idx]))
- ws.nextPutAll("\"")
- return ws.contents()
-}
-
-// C-style tempnam function
-
-function tempnam(s) { return (s ? s : "_tmpnam_") + tempnam.n++ }
-tempnam.n = 0
-
-
-//////// ometa-base.js
-
-/*
- Copyright (c) 2007, 2008 Alessandro Warth <awarth@cs.ucla.edu>
-
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
-
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-/*
- new syntax:
- #foo match the string object 'foo' (should also be accepted in JS)
- 'abc' match the string object 'abc'
- 'c' match the string object 'c'
- ``abc'' match the sequence of string objects 'a', 'b', 'c'
- "abc" token('abc')
- [1 2 3] match the array object [1, 2, 3]
- foo(bar) apply rule foo with argument bar
- -> ... do semantic actions in JS (no more ST). allow multiple statements, but no declarations
- probably don't even need {}s, because newlines can terminate it!
-*/
-
-/*
-// ometa M {
-// number = number:n digit:d -> { n * 10 + d.digitValue() }
-// | digit:d -> { d.digitValue() }.
-// }
-
-try {
- M = OMeta.delegated({
- number: function() {
- var $elf = this
- return $elf._or(
- function() {
- var n, d
- n = $elf._apply("number")
- d = $elf._apply("digit")
- return n * 10 + d.digitValue()
- },
- function() {
- var d
- d = $elf._apply("digit")
- return d.digitValue()
- }
- )
- }
- })
- M.matchAll("123456789", "number")
-} catch (f) { alert(f) }
-*/
-
-// the failure exception
-
-fail = { toString: function() { return "match failed" } }
-
-// streams and memoization
-
-function OMInputStream(hd, tl) {
- this.memo = { }
- this.hd = hd
- this.tl = tl
-}
-OMInputStream.prototype.head = function() { return this.hd }
-OMInputStream.prototype.tail = function() { return this.tl }
-
-function OMInputStreamEnd() { this.memo = { } }
-OMInputStreamEnd.prototype.head = function() { throw fail }
-OMInputStreamEnd.prototype.tail = function() { throw fail }
-
-Array.prototype.toOMInputStream = function() { return makeArrayOMInputStream(this, 0) }
-String.prototype.toOMInputStream = Array.prototype.toOMInputStream
-
-function makeArrayOMInputStream(arr, idx) { return idx < arr.length ? new ArrayOMInputStream(arr, idx) : new OMInputStreamEnd() }
-
-function ArrayOMInputStream(arr, idx) {
- this.memo = { }
- this.arr = arr
- this.idx = idx
- this.hd = arr[idx]
-}
-ArrayOMInputStream.prototype.head = function() { return this.hd }
-ArrayOMInputStream.prototype.tail = function() {
- if (this.tl == undefined)
- this.tl = makeArrayOMInputStream(this.arr, this.idx + 1)
- return this.tl
-}
-
-function makeOMInputStreamProxy(target) {
- return target.delegated({
- memo: { },
- target: target,
- tail: function() { return makeOMInputStreamProxy(target.tail()) }
- })
-}
-
-// Failer (i.e., that which makes things fail) is used to detect (direct) left recursion and memoize failures
-
-function Failer() { }
-Failer.prototype.used = false
-
-// the OMeta "class" and basic functionality
-
-OMeta = {
- _apply: function(rule) {
- var memoRec = this.input.memo[rule]
- if (memoRec == undefined) {
- var origInput = this.input,
- failer = new Failer()
- this.input.memo[rule] = failer
- this.input.memo[rule] = memoRec = {ans: this[rule].apply(this), nextInput: this.input}
- if (failer.used) {
- var sentinel = this.input
- while (true) {
- try {
- this.input = origInput
- var ans = this[rule].apply(this)
- if (this.input == sentinel)
- throw fail
- memoRec.ans = ans
- memoRec.nextInput = this.input
- }
- catch (f) {
- if (f != fail)
- throw f
- break
- }
- }
- }
- }
- else if (memoRec instanceof Failer) {
- memoRec.used = true
- throw fail
- }
- this.input = memoRec.nextInput
- return memoRec.ans
- },
-
- // note: _applyWithArgs and _superApplyWithArgs are not memoized, so they can't be left-recursive
- _applyWithArgs: function(rule) {
- for (var idx = arguments.length - 1; idx > 0; idx--)
- this.input = new OMInputStream(arguments[idx], this.input)
- return this[rule].apply(this)
- },
- _superApplyWithArgs: function($elf, rule) {
- for (var idx = arguments.length - 1; idx > 1; idx--)
- $elf.input = new OMInputStream(arguments[idx], $elf.input)
- return this[rule].apply($elf)
- },
- _pred: function(b) {
- if (b)
- return true
- throw fail
- },
- _not: function(x) {
- var origInput = this.input
- try { x() }
- catch (f) {
- if (f != fail)
- throw f
- this.input = origInput
- return true
- }
- throw fail
- },
- _lookahead: function(x) {
- var origInput = this.input,
- r = x()
- this.input = origInput
- return r
- },
- _or: function() {
- var origInput = this.input
- for (var idx = 0; idx < arguments.length; idx++)
- try { this.input = origInput; return arguments[idx]() }
- catch (f) {
- if (f != fail)
- throw f
- }
- throw fail
- },
- _many: function(x) {
- var ans = arguments[1] != undefined ? [arguments[1]] : []
- while (true) {
- var origInput = this.input
- try { ans.push(x()) }
- catch (f) {
- if (f != fail)
- throw f
- this.input = origInput
- break
- }
- }
- return ans
- },
- _many1: function(x) { return this._many(x, x()) },
- _form: function(x) {
- var v = this._apply("anything")
- if (!v.isSequenceable)
- throw fail
- var origInput = this.input
- this.input = makeArrayOMInputStream(v, 0)
- var r = x()
- this._apply("end")
- this.input = origInput
- return v
- },
-
- // some basic rules
- anything: function() {
- var r = this.input.head()
- this.input = this.input.tail()
- return r
- },
- end: function() {
- var $elf = this
- return this._not(function() { return $elf._apply("anything") })
- },
- pos: function() {
- return this.input.idx
- },
- empty: function() { return true },
- apply: function() {
- var r = this._apply("anything")
- return this._apply(r)
- },
- foreign: function() {
- var g = this._apply("anything"),
- r = this._apply("anything"),
- gi = g.delegated({input: makeOMInputStreamProxy(this.input)})
- var ans = gi._apply(r)
- this.input = gi.input.target
- return ans
- },
-
- // some useful "derived" rules
- exactly: function() {
- var wanted = this._apply("anything")
- if (wanted === this._apply("anything"))
- return wanted
- throw fail
- },
- "true": function() {
- var r = this._apply("anything")
- this._pred(r == true)
- return r
- },
- "false": function() {
- var r = this._apply("anything")
- this._pred(r == false)
- return r
- },
- "undefined": function() {
- var r = this._apply("anything")
- this._pred(r == undefined)
- return r
- },
- number: function() {
- var r = this._apply("anything")
- this._pred(r.isNumber())
- return r
- },
- string: function() {
- var r = this._apply("anything")
- this._pred(r.isString())
- return r
- },
- "char": function() {
- var r = this._apply("anything")
- this._pred(r.isCharacter())
- return r
- },
- space: function() {
- var r = this._apply("char")
- this._pred(r.charCodeAt(0) <= 32)
- return r
- },
- spaces: function() {
- var $elf = this
- return this._many(function() { return $elf._apply("space") })
- },
- digit: function() {
- var r = this._apply("char")
- this._pred(r.isDigit())
- return r
- },
- lower: function() {
- var r = this._apply("char")
- this._pred(r.isLower())
- return r
- },
- upper: function() {
- var r = this._apply("char")
- this._pred(r.isUpper())
- return r
- },
- letter: function() {
- var $elf = this
- return this._or(function() { return $elf._apply("lower") },
- function() { return $elf._apply("upper") })
- },
- letterOrDigit: function() {
- var $elf = this
- return this._or(function() { return $elf._apply("letter") },
- function() { return $elf._apply("digit") })
- },
- firstAndRest: function() {
- var $elf = this,
- first = this._apply("anything"),
- rest = this._apply("anything")
- return this._many(function() { return $elf._apply(rest) }, this._apply(first))
- },
- seq: function() {
- var xs = this._apply("anything")
- for (var idx = 0; idx < xs.length; idx++)
- this._applyWithArgs("exactly", xs[idx])
- return xs
- },
- notLast: function() {
- var $elf = this,
- rule = this._apply("anything"),
- r = this._apply(rule)
- this._lookahead(function() { return $elf._apply(rule) })
- return r
- },
-
- initialize: function() { },
- // match and matchAll are a grammar's "public interface"
- _genericMatch: function(input, rule, args, matchFailed) {
- if (args == undefined)
- args = []
- var realArgs = [rule]
- for (var idx = 0; idx < args.length; idx++)
- realArgs.push(args[idx])
- var m = this.delegated({input: input})
- m.initialize()
- try { return realArgs.length == 1 ? m._apply.call(m, realArgs[0]) : m._applyWithArgs.apply(m, realArgs) }
- catch (f) {
- if (f == fail && matchFailed != undefined) {
- var input = m.input
- if (input.idx != undefined) {
- while (input.tl != undefined && input.tl.idx != undefined)
- input = input.tl
- input.idx--
- }
- return matchFailed(m, input.idx)
- }
- throw f
- }
- },
- match: function(obj, rule, args, matchFailed) {
- return this._genericMatch([obj].toOMInputStream(), rule, args, matchFailed)
- },
- matchAll: function(listyObj, rule, args, matchFailed) {
- return this._genericMatch(listyObj.toOMInputStream(), rule, args, matchFailed)
- }
-}
-
-
-//////// parser.js
-
-/*
- Copyright (c) 2007, 2008 Alessandro Warth <awarth@cs.ucla.edu>
-
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
-
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-Parser = OMeta.delegated({
- listOf: function() {
- var $elf = this,
- rule = this._apply("anything"),
- delim = this._apply("anything")
- return this._or(function() {
- var r = $elf._apply(rule)
- return $elf._many(function() {
- $elf._applyWithArgs("token", delim)
- return $elf._apply(rule)
- },
- r)
- },
- function() { return [] })
- },
- token: function() {
- var cs = this._apply("anything")
- this._apply("spaces")
- return this._applyWithArgs("seq", cs)
- }
-})
-
-
-//////// bs-js-compiler.js
-
-{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("unary");return ["binop","*",x,y]})()}),(function(){return (function(){x=$elf._apply("mulExpr");$elf._applyWithArgs("token","/");y=$elf._apply("unary");return ["binop","/",x,y]})()}),(function(){return (function(){x=$elf._apply("mulExpr");$elf._applyWithArgs("token","%");y=$elf._apply("unary");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})()}),(function(){return (function(){$elf._applyWithArgs("token",";");return ["get","undefined"]})()}))},"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)})()},"default":function(){var $elf=this,y;return (function(){y=$elf._apply("trans");return ("default: " + y)})()}})}
-
-
-//////// bs-ometa-compiler.js
-
-{BSOMetaParser=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._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("exactly","(");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("")})()}})}
-
-
-//////// bs-ometa-optimizer.js
-
-{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;}
-
-
-//////// bs-ometa-js-compiler.js
-
-{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;}
-
View
110 OMeta_WJS_Mods.js
@@ -1,110 +0,0 @@
-OMeta._or = function() {
- for (var idx = 0; idx < arguments.length; idx++) {
- var ok = true
- in thisWorld.sprout() {
- try { return arguments[idx]() }
- catch (f) {
- ok = false
- if (f != fail)
- throw f
- }
- finally { if (ok) thisWorld.commit() }
- }
- }
- throw fail
-}
-
-OMeta._many = function(x) {
- var ans = arguments[1] != undefined ? [arguments[1]] : []
- while (true) {
- in thisWorld.sprout() {
- try {
- ans.push(x())
- //print("committing " + ans.toString())
- thisWorld.commit()
- }
- catch (f) {
- if (f != fail)
- throw f
- break
- }
- }
- }
- return ans
-}
-
-OMeta._not = function(x) {
- in thisWorld.sprout() {
- try { x() }
- catch (f) {
- if (f != fail)
- throw f
- return true
- }
- }
- throw fail
-}
-
-/*
-OMeta._lookahead = function(x) {
- in thisWorld.sprout() {
- var r = x()
- //print("la = " + r.toString())
- return x
- }
-}
-*/
-
-/*
-OMeta._apply = function(rule) {
- var memoRec = this.input.memo[rule]
- if (memoRec == undefined) {
- var origInput = this.input,
- failer = new Failer()
- this.input.memo[rule] = failer
- this.input.memo[rule] = memoRec = {ans: this[rule].apply(this), nextInput: this.input}
- if (failer.used) {
- var sentinel = this.input
- while (true) {
- try {
- this.input = origInput
- var ans = this[rule].apply(this)
- if (this.input == sentinel)
- throw fail
- memoRec.ans = ans
- memoRec.nextInput = this.input
- }
- catch (f) {
- if (f != fail)
- throw f
- break
- }
- }
- }
- }
- else if (memoRec instanceof Failer) {
- memoRec.used = true
- throw fail
- }
- this.input = memoRec.nextInput
- return memoRec.ans
-}
-*/
-
-
-print("defining example 1")
-eval(BSOMetaJSTranslator.match(BSOMetaJSParser.matchAll("ometa M { ones = (1 -> 2)* }", "srcElem"), "trans"))
-print("running example 1")
-print(M.matchAll([1, 1, 1, 1], "ones"))
-
-print("defining example 2")
-eval(BSOMetaJSTranslator.match(BSOMetaJSParser.matchAll("ometa M { foo = &(:x) anything*:ys -> [x, ys] }", "srcElem"), "trans"))
-print("running example 2")
-print(M.matchAll([1, 2, 3, 4], "foo"))
-
-print("defining example 3")
-eval(BSOMetaJSTranslator.match(BSOMetaJSParser.matchAll("ometa M { ones = {count=0} ({count++} 1 -> 2)* }", "srcElem"), "trans"))
-print("running example 3")
-print(M.matchAll([1, 1, 1, 1], "ones"))
-print("count = " + count)
-
View
112 Worlds2.js
@@ -1,112 +0,0 @@
-/*
- Copyright (c) 2008, 2010 Alessandro Warth <awarth@cs.ucla.edu>
-
- Limitations:
- * assignments into the properties of "arguments" (e.g., arguments[5] = 1234) don't modify their respective variables
- * for-in doesn't work when the loop variable is a property access (e.g., for (x.y in ys) ...)
-*/
-
-Array.prototype.each = function(f) {
- for (var idx = 0; idx < this.length; idx++)
- f(this[idx], idx)
-}
-
-ometa WJSParser <: BSJSParser {
- isKeyword :x = ?(x == 'thisWorld')
- | super('isKeyword', x),
- primExprHd = "thisWorld" -> ['thisWorld']
- | super('primExprHd'),
- stmt = "in" expr:w block:b -> ['in', w, b]
- | super('stmt')
-}
-
-makeFunction = function(fs, body) {
- return '(function() { var staticScope = thisScope;' +
- ' var r = function() {' +
- ' var oldScope = thisScope;' +
- ' thisScope = staticScope.makeChild();' +
- ' thisScope.set("arguments", arguments);' +
- ' thisWorld.set(arguments, "length", arguments.length);' +
- ' for (var i = 0; i < arguments.length; i++) thisWorld.set(arguments, i, arguments[i]);' +
- ' try { ' + fs + body + '}' +
- ' finally { thisScope = oldScope }};' +
- ' baseWorld.set(r, "prototype", {parent: Object.prototype});' +
- ' return r })()' }
-
-makeIn = function(w, body) {
- return '{ try { worldStack.push(thisWorld); thisWorld = ' + w + '; ' + body + '} ' +
- 'finally { thisWorld = worldStack.pop() }' +
- 'undefined }'
-}
-
-makeForIn = function(v, e, s, decl) {
- var p = tempnam(), ps = tempnam()
- var r = 'for (var ' + p + ' in ' + ps + ' = thisWorld.props(' + e + ', {})) {' +
- 'if (!' + ps + '.hasOwnProperty(' + p + ')) continue; ' +
- 'thisScope.set("' + v + '", ' + p + '); ' + s +
- '}'
- if (decl)
- r = 'thisScope.decl("' + v + '", undefined); ' + r
- r = '{ var ' + ps + ' = undefined; ' + r + '}'
- return r
-}
-
-ometa WJSTranslator <: BSJSTranslator {
- initialize = { self.level = 0 },
- fargs = [anything*:fs] -> { var ss = []
- fs.each(function(v, i) { ss.push('thisScope.decl("' + v +
- '", arguments[' + i + ']);') })
- ss.join('') },
- thisWorld -> 'thisWorld',
- var :n trans:v -> ('thisScope.decl("' + n + '", ' + v + ')'),
- get :n -> ('thisScope.get("' + n + '")'),
- getp trans:p ['get' 'arguments'] -> ('arguments[' + p + ']'),
- getp trans:p trans:x -> ('thisWorld.get(' + x + ', ' + p + ')'),
- set ['get' :n] trans:v -> ('thisScope.set("' + n + '", ' + v + ')'),
- set ['getp' trans:p ['get' 'arguments']] trans:v -> 'UNSUPPORTED OPERATION',
- set ['getp' trans:p trans:x] trans:v -> ('thisWorld.set(' + x + ', ' + p + ', ' + v + ')'),
- mset ['get' :n] :op trans:rhs -> ('thisScope.set("' + n + '", thisScope.get("' + n + '")' + op + rhs + ')'),
- mset ['getp' trans:p trans:x] :op trans:rhs -> ('(function(r, p) { return thisWorld.set(r, p, thisWorld.get(r, p) ' +
- op + ' ' + rhs + ') })(' + x + ', ' + p + ')'),
- preop :op ['get' :n] -> ('thisScope.set("' + n + '", thisScope.get("' + n + '")' + op[0] + '1)'),
- preop :op ['getp' trans:p trans:x] -> ('(function(r, p) { return thisWorld.set(r, p, thisWorld.get(r, p) ' +
- op[0] + ' 1) })(' + x + ', ' + p + ')'),
- postop :op ['get' :n] -> ('(function(n) { var ans = thisScope.get(n); ' +
- 'thisScope.set(n, ans ' + op[0] + ' 1); ' +
- 'return ans })("' + n + '")'),
- postop :op ['getp' trans:p trans:x] -> ('(function(r, p) { var ans = thisWorld.get(r, p); ' +
- 'thisWorld.set(r, p, ans ' + op[0] + ' 1); ' +
- 'return ans })(' + x + ', ' + p + ')'),
- binop 'instanceof' trans:x trans:y -> ('instanceOf(' + x + ', ' + y + ')'),
- binop :op trans:x trans:y -> ('(' + x + ' ' + op + ' ' + y + ')'),
- call trans:f trans*:as -> ('(' + f + ')(' + as.join(',') + ')'),
- send :m trans:r trans*:as -> ('send("' + m + '", ' + r + ', [' + as.join(',') + '])'),
- new :x trans*:as -> ('thisScope.get("' + x + '").worldsNew(' + as.join(',') + ')'),
- func fargs:fs {self.level++} trans:body
- {self.level--} -> makeFunction(fs, body),
- in trans:w trans:b -> makeIn(w, b),
- arr trans*:xs -> ('[' + xs.join(',') + '].toWJSArray()'),
- json trans*:xs -> ('({' + xs.join(',') + '}).toWJSObject()'),
- try curlyTrans:x :name curlyTrans:c curlyTrans:f -> { var e = tempnam()
- 'try ' + x +
- 'catch(' + e + ') {thisScope.decl("' + name + '", ' + e + '); ' + c + '}' +
- 'finally' + f },
- forIn ['get' :v ] trans:e trans:s -> makeForIn(v, e, s, false),
- forIn ['var' :v :init] trans:e trans:s -> makeForIn(v, e, s, true)
-}
-
-compileWJS = function(code) {
- var tree = WJSParser.matchAll(code, "topLevel", undefined, function(m, i) { throw fail.delegated({errorPos: i}) })
- //print("parsed: " + tree)
- var code = WJSTranslator.match(tree, 'trans')
- //print("compiled: " + code)
- return code
-}
-
-thisScope.decl("eval", function(s) { return eval(compileWJS(s)) })
-
-oldPrint = print
-print = function(x) { oldPrint(x == undefined || x == null ? x : send("toString", x)) }
-
-translateCode = compileWJS
-
View
311 Worlds2_Library.js
@@ -1,311 +0,0 @@
-// Copyright (c) 2008, 2010 Alessandro Warth <awarth@cs.ucla.edu>
-
-DEBUG = false;
-
-// getTag: object -> unique id
-// tagToRef: unique id -> object
-
-// Note: this hashing scheme causes a huge memory leak (all b/c JS doesn't support weak references)
-
-(function() {
- var numIdx = 0, tagToRef = {}
- var _getTag = function(r) {
- if (r === null || r === undefined)
- return r
- switch (typeof r) {
- case "boolean": return r == true ? "Btrue" : "Bfalse"
- case "string": return "S" + r
- case "number": return "N" + r
- default: return r.hasOwnProperty("_id_") ? r._id_ : r._id_ = "R" + numIdx++
- }
- }
- getTag = function(r) {
- var tag = _getTag(r)
- tagToRef[tag] = r
- return tag
- }
- getRef = function(t) {
- return tagToRef[t]
- }
-})()
-
-// implementation of possible worlds
-
-worldProto = {}
-
-baseWorld = thisWorld = (function() {
- var writes = {}
- return {
- parent: worldProto,
- writes: writes,
- hasOwn: function(r, p) {
- var id = getTag(r)
- return writes.hasOwnProperty(id) && writes[id].hasOwnProperty(p)
- },
- has: function(r, p) {
- return this.hasOwn(r, p) ||
- r !== Object.prototype && this.has(r === null || r === undefined ? Object.prototype : r.parent, p)
- },
- props: function(r, ps) {
- var id = getTag(r)
- if (writes.hasOwnProperty(id))
- for (var p in writes[id])
- if (writes[id].hasOwnProperty(p))
- ps[p] = true
- if (r !== Object.prototype)
- this.props(r === null || r === undefined ? Object.prototype : r.parent, ps)
- return ps
- },
- _get: function(r, p) {
- var id = getTag(r)
- if (DEBUG) console.log("? top-level world looking up " + id + "." + p)
- if (writes.hasOwnProperty(id) && writes[id].hasOwnProperty(p))
- return writes[id][p]
- else if (r !== Object.prototype)
- return thisWorld._get(r === null || r === undefined ? Object.prototype : r.parent, p)
- else
- return undefined
- },
- get: function(r, p) {
- // the top-level world's commit operation is a no-op, so reads don't have to be recorded
- if (typeof r === "string" && (typeof p === "number" || p === "length"))
- return r[p]
- else
- return this._get(r, p)
- },
- set: function(r, p, v) {
- if (typeof r === "string" && (typeof p === "number" || p === "length"))
- throw "the indices and length of a string are immutable, and you tried to change them!"
- var id = getTag(r)
- if (DEBUG) console.log("! top-level world assigning to " + id + "." + p)
- if (!writes.hasOwnProperty(id))
- writes[id] = {}
- writes[id][p] = v
- return v
- },
- commit: function() { },
- sprout: function() {
- var parentWorld = this, writes = {}, reads = {}
- return {
- parent: worldProto,
- writes: writes,
- reads: reads,
- hasOwn: function(r, p) {
- var id = getTag(r)
- return writes.hasOwnProperty(id) && writes[id].hasOwnProperty(p) ||
- reads.hasOwnProperty(id) && reads[id].hasOwnProperty(p) ||
- parentWorld.hasOwn(r, p)
- },
- has: function(r, p) {
- return this.hasOwn(r, p) ||
- r !== Object.prototype && this.has(r === null || r === undefined ? Object.prototype : r.parent, p)
- },
- props: function(r, ps) {
- var id = getTag(r)
- if (writes.hasOwnProperty(id))
- for (var p in writes[id])
- if (writes[id].hasOwnProperty(p))
- ps[p] = true
- if (reads.hasOwnProperty(id))
- for (var p in reads[id])
- if (reads[id].hasOwnProperty(p))
- ps[p] = true
- if (r !== Object.prototype)
- this.props(r === null || r === undefined ? Object.prototype : r.parent, ps)
- parentWorld.props(r, ps)
- return ps
- },
- _get: function(r, p) {
- var id = getTag(r)
- if (DEBUG) console.log("? child world looking up " + id + "." + p)
- if (writes.hasOwnProperty(id) && writes[id].hasOwnProperty(p))
- return writes[id][p]
- else if (reads.hasOwnProperty(id) && reads[id].hasOwnProperty(p))
- return reads[id][p]
- else
- return parentWorld._get(r, p)
- },
- get: function(r, p) {
- if (typeof r === "string" && (typeof p === "number" || p === "length"))
- return r[p]
- var id = getTag(r), ans = this._get(r, p)
- if (!reads.hasOwnProperty(id)) {
- reads[id] = {}
- if (!reads[id].hasOwnProperty(p))
- reads[id][p] = ans
- }
- return ans
- },
- set: function(r, p, v) {
- if (typeof r === "string" && (typeof p === "number" || p === "length"))
- throw "the indices and length of a string are immutable, and you tried to change them!"
- var id = getTag(r)
- if (DEBUG) console.log("! child world assigning to " + id + "." + p)
- if (!writes.hasOwnProperty(id))
- writes[id] = {}
- writes[id][p] = v
- return v
- },
- commit: function() {
- // serializability check
- for (var id in reads) {
- if (!reads.hasOwnProperty(id))
- continue
- for (var p in reads[id]) {
- if (!reads[id].hasOwnProperty(p))
- continue
- else if (reads[id][p] !== parentWorld._get(getRef(id), p))
- throw "commit failed"
- }
- }
- // propagation of side effects
- for (var id in writes) {
- if (!writes.hasOwnProperty(id))
- continue
- for (var p in writes[id]) {
- if (!writes[id].hasOwnProperty(p))
- continue
- if (!parentWorld.writes.hasOwnProperty(id))
- parentWorld.writes[id] = {}
- if (DEBUG) console.log("committing " + id + "." + p)
- parentWorld.writes[id][p] = writes[id][p]
- }
- }
- writes = {}
- reads = {}
- },
- sprout: parentWorld.sprout
- }
- }
- }
-})()
-worldStack = [thisWorld]
-
-// Lexical scopes
-
-GlobalScope = function() { }
-GlobalScope.prototype = {
- parent: Object.prototype,
- hasOwn: function(n) { return thisWorld.hasOwn(this, n) },
- has: function(n) { return thisWorld.has(this, n) },
- get: function(n) { return thisWorld.get(this, n) },
- set: function(n, v) { return thisWorld.set(this, n, v) },
- decl: function(n, v) { return baseWorld.set(this, n, v) },
- makeChild: function() { return new ActivationRecord(this) }
-}
-
-ActivationRecord = function(parent) { this.parent = parent }
-ActivationRecord.prototype = new GlobalScope()
-ActivationRecord.prototype.get = function(n) { return thisWorld.has(this, n) ?
- thisWorld.get(this, n) :
- this.parent.get(n) }
-ActivationRecord.prototype.set = function(n, v) { return thisWorld.has(this, n) ?
- thisWorld.set(this, n, v) :
- this.parent.set(n, v) }
-
-thisScope = new GlobalScope()
-
-// Sends
-
-send = function(sel, recv, args) {
- //alert("doing a send, sel=" + sel + ", recv=" + recv + ", args=" + args)
- return thisWorld.get(recv, sel).apply(recv, args)
-}
-
-// New
-
-Function.prototype.worldsNew = function() {
- var r = {parent: thisWorld.get(this, "prototype")}
- this.apply(r, arguments)
- return r
-}
-
-// instanceof
-
-instanceOf = function(x, C) {
- var p = x.parent, Cp = thisWorld.get(C, "prototype")
- while (p != undefined) {
- if (p == Cp)
- return true
- p = p.parent
- }
- return false
-}
-
-// Some globals, etc.
-
-wObject = function() { }
-thisScope.decl("Object", wObject)
-thisWorld.set(wObject, "prototype", Object.prototype)
-thisWorld.set(Object.prototype, "hasOwn", function(p) { return thisWorld.has(this, p) })
-thisWorld.set(Object.prototype, "toString", function() { return "" + this })
-
-thisWorld.set(worldProto, "sprout", function() { return this.sprout() })
-thisWorld.set(worldProto, "commit", function() { return this.commit() })
-thisWorld.set(worldProto, "toString", function() { return "[World " + this.getTag() + "]" })
-
-wWorld = function() { }; thisScope.decl("World", wWorld); thisWorld.set(wWorld, "prototype", worldProto)
-wBoolean = function() { }; thisScope.decl("Boolean", wBoolean); thisWorld.set(wBoolean, "prototype", {parent: Object.prototype})
-wNumber = function() { }; thisScope.decl("Number", wNumber); thisWorld.set(wNumber, "prototype", {parent: Object.prototype})
-wString = function() { }; thisScope.decl("String", wString); thisWorld.set(wString, "prototype", {parent: Object.prototype})
-wArray = function() { }; thisScope.decl("Array", wArray); thisWorld.set(wArray, "prototype", {parent: Object.prototype})
-wFunction = function() { }; thisScope.decl("Function", wFunction); thisWorld.set(wFunction, "prototype", {parent: Object.prototype})
-
-Boolean.prototype.parent = thisWorld.get(wBoolean, "prototype")
-Number.prototype.parent = thisWorld.get(wNumber, "prototype")
-String.prototype.parent = thisWorld.get(wString, "prototype")
-Function.prototype.parent = thisWorld.get(wFunction, "prototype")
-// Don't need to do this for Array because Worlds/JS arrays are not JS arrays
-
-thisWorld.set(wString, "fromCharCode", function(x) { return String.fromCharCode(x) })
-thisWorld.set(String.prototype.parent, "charCodeAt", function(x) { return this.charCodeAt(x) })
-
-thisWorld.set(Function.prototype.parent, "apply", function(recv, args) {
- var jsArgs
- if (args && thisWorld.get(args, "length") > 0) {
- jsArgs = []
- for (var idx = 0; idx < thisWorld.get(args, "length"); idx++)
- jsArgs.push(thisWorld.get(args, idx))
- }
- return this.apply(recv, jsArgs)
-})
-thisWorld.set(Function.prototype.parent, "call", function(recv) {
- var jsArgs = []
- for (var idx = 1; idx < arguments.length; idx++)
- jsArgs.push(arguments[idx])
- return this.apply(recv, jsArgs)
-})
-
-thisScope.decl("null", null)
-thisScope.decl("undefined", undefined)
-thisScope.decl("true", true)
-thisScope.decl("false", false)
-
-thisScope.decl("jsEval", function(s) { return eval(thisWorld.get(s, "toString").call(s)) })
-thisScope.decl("print", function(s) { print(thisWorld.get(s, "toString").call(s)) })
-thisScope.decl("alert", function(s) { alert(thisWorld.get(s, "toString").call(s)) })
-thisScope.decl("prompt", function(s) { return prompt(thisWorld.get(s, "toString").call(s)) })
-thisScope.decl("confirm", function(s) { return confirm(thisWorld.get(s, "toString").call(s)) })
-
-thisScope.decl("parseInt", function(s) { return parseInt(s) })
-thisScope.decl("parseFloat", function(s) { return parseFloat(s) })
-
-WorldsConsole = {}
-thisScope.decl("console", WorldsConsole)
-thisWorld.set(WorldsConsole, "log", function(s) { Transcript.show(thisWorld.get(s, "toString").apply(s)) })
-
-Array.prototype.toWJSArray = function() {
- var r = wArray.worldsNew()
- for (var idx = 0; idx < this.length; idx++)
- thisWorld.set(r, idx, this[idx])
- thisWorld.set(r, "length", this.length)
- return r
-}
-Object.prototype.toWJSObject = function() {
- var r = wObject.worldsNew()
- for (var p in this)
- if (this.hasOwnProperty(p))
- thisWorld.set(r, p, this[p])
- return r
-}
-
View
35 arrays.wjs
@@ -1,35 +0,0 @@
-Array.prototype.toString = function() {
- var r = "["
- for (var idx = 0; idx < this.length; idx = idx + 1) {
- if (idx > 0)
- r = r + ", "
- r = r + this[idx].toString()
- }
- return r + "]"
-}
-Array.prototype.push = function(x) {
- this[this.length] = x
- this.length = this.length + 1
- return x
-}
-Array.prototype.pop = function() {
- var r = this[this.length - 1]
- this[this.length - 1] = undefined
- this.length = this.length - 1
- return r
-}
-Array.prototype.join = function(delim) {
- var r = ""
- for (var idx = 0; idx < this.length; idx++) {
- if (idx > 0)
- r += delim
- r += this[idx]
- }
- return r
-}
-Array.prototype.concat = function(xs) {
- for (var idx = 0; idx < xs.length; idx++)
- this.push(xs[idx])
- return this
-}
-
View
3  bin/ometajs2js
@@ -0,0 +1,3 @@
+#!/usr/bin/env node
+
+require('/Users/veged/Documents/ometa-js/lib/ometajs/2js').main();
View
6 bla.js
@@ -0,0 +1,6 @@
+L=objectThatDelegatesTo(OMeta,{
+"number":function(){var $elf=this,_fromIdx=this.input.idx;return this._many1((function(){return this._apply("digit")}))},
+"addExpr":function(){var $elf=this,_fromIdx=this.input.idx;return this._or((function(){return (function(){this._apply("addExpr");this._applyWithArgs("exactly","+");return this._apply("mulExpr")}).call(this)}),(function(){return (function(){this._apply("addExpr");this._applyWithArgs("exactly","-");return this._apply("mulExpr")}).call(this)}),(function(){return this._apply("mulExpr")}))},
+"mulExpr":function(){var $elf=this,_fromIdx=this.input.idx;return this._or((function(){return (function(){this._apply("mulExpr");this._applyWithArgs("exactly","*");return this._apply("primExpr")}).call(this)}),(function(){return (function(){this._apply("mulExpr");this._applyWithArgs("exactly","/");return this._apply("primExpr")}).call(this)}),(function(){return this._apply("primExpr")}))},
+"primExpr":function(){var $elf=this,_fromIdx=this.input.idx;return this._or((function(){return (function(){switch(this._apply('anything')){case "(":return (function(){this._apply("expr");return this._applyWithArgs("exactly",")")}).call(this);default: throw fail}}).call(this)}),(function(){return this._apply("number")}))},
+"expr":function(){var $elf=this,_fromIdx=this.input.idx;return this._apply("addExpr")}})
View
13 bla.txt
@@ -0,0 +1,13 @@
+ometa L {
+ number = digit+,
+ addExpr = addExpr '+' mulExpr
+ | addExpr '-' mulExpr
+ | mulExpr,
+ mulExpr = mulExpr '*' primExpr
+ | mulExpr '/' primExpr
+ | primExpr,
+ primExpr = '(' expr ')'
+ | number,
+ expr = addExpr
+}
+
View
3  bs-project-list-parser.js
@@ -1,3 +0,0 @@
-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
5 bs-project-list-parser.txt
@@ -1,5 +0,0 @@
-ometa ProjectListParser <: BSJSParser {
- proj = spaces iName,
- projs = proj:x (spaces ',' proj)*:xs -> [x].concat(xs)
-}
-
View
0  Not_Quite_JS.txt → doc/Not_Quite_JS.txt
File renamed without changes
View
0  OMeta_Tutorial.txt → doc/OMeta_Tutorial.txt
File renamed without changes
View
0  README.txt → doc/README.txt
File renamed without changes
View
0  Things_You_Should_Know.txt → doc/Things_You_Should_Know.txt
File renamed without changes
View
166 index.html
@@ -1,166 +0,0 @@
-<!--
- Copyright (c) 2007-2010 Alessandro Warth <awarth@cs.ucla.edu>
-
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
-
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
-!-->
-
-<html>
- <head>
- <title>OMeta/JS 2.0 Workspace</title>
- <script src="prototype.js"></script>
- <script src="prototype-fix.js"></script>
- <script src="lib.js"></script>
- <script src="ometa-base.js"></script>
- <script src="parser.js"></script>
- <script src="bs-js-compiler.js"></script>
- <script src="bs-ometa-compiler.js"></script>
- <script src="bs-ometa-optimizer.js"></script>
- <script src="bs-ometa-js-compiler.js"></script>
- <script src="bs-project-list-parser.js"></script>
- <script src="workspace.js"></script>
- <script src="wiki.js"></script>
- <script>
-
-function toggleVisible(id) {
- var style = document.getElementById(id).style
- style.visibility = style.visibility == "hidden" ? "visible" : "hidden"
- style.display = style.display == "block" ? "none" : "block"
-}
-
-function initializeThisPage() {
- $('workspaceForm').source.onkeydown = onShortCutKey
- var makeProjectSelector = function(id) {
- $(id + "Home").innerHTML =
- "<select id=" + id +
- " onChange='loadProject(this.options[this.selectedIndex].value)'><option value=''>(nevermind)</option></select>"
- }
- var addOption = function(toId, x) {
- var option = document.createElement("option")
- option.innerHTML = x.replace(/_/g, " ")
- option.value = x
- $(toId).appendChild(option)
- }
- makeProjectSelector('infoSelector')
- addOption('infoSelector', 'Sample_Project')
- addOption('infoSelector', 'Not_Quite_JS')
- addOption('infoSelector', 'Things_You_Should_Know')
- addOption('infoSelector', 'OMeta_Tutorial')
- addOption('infoSelector', 'Memoizing_Parameterized_Rules')
- addOption('infoSelector', 'Project_Listing')
- makeProjectSelector('projectSelector')
- var ps = $('projectSelector')
- try {
- var projects = ProjectListParser.matchAll(readFile("Project_Listing"), "projs")
- for (var idx = 0; idx < projects.length; idx++)
- addOption('projectSelector', projects[idx])
- }
- catch (e) { }
- $('workspaceForm').source.value = $('workspaceForm').source.origValue = ''
- $('workspaceForm').translation.value = ''
- $('workspaceForm').transcript.value = ''
- loadProject()
-}
-
-saveIt = saveProject
-
-if (!document.location.hash)
- document.location.hash = "#Sample_Project"
-titleRest = " - <nobr><span title='In case you are wondering, this is\n" +
- "(OMeta/JS) 2.0, not OMeta/(JS 2.0).\n" +
- "In other words, it has nothing to do\n" +
- "with JS 2 / EcmaScript 2.'>OMeta/JS 2.0</span> Workspace</nobr>"
-
-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 objectThatDelegatesTo(fail, {errorPos: i}) })
- return BSOMetaJSTranslator.match(tree, "trans", undefined, translationError)
-}
-
-function include(fileName) {
- return eval(readFile(fileName))
-}
-
- </script>
- </head>
- <body onLoad="initializeThisPage()">
- <table border=0 align=right>
- <tr><td align=right><b>important info: </b></td><td><span id=infoSelectorHome></span></td></tr>
- <tr><td align=right><b>go to project: </b></td><td><span id=projectSelectorHome></span></td></tr>
- <tr><td colspan=2 align=right><input type=button value="previous versions of this project" onClick="document.location = 'http://www.tinlizzie.org/ometa-js/websvn/log.php?repname=ometa-js-projects&path=%2F' + document.location.hash.substring(1) + '.txt&rev=0&sc=0&isdir=0'"><br>
- </td></tr>
- </table>
- <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>
-
-
- <b>Instructions</b>
- <input type=button value="+/-" onClick="toggleVisible('instructions')">
- <br>
- <div id=instructions style="visibility: visible; display: block">
- <table bgcolor=#f9f9f9 cellpadding=4
- style="border: 1px solid #333333; padding: .2em .2em .2em .2em; margin-bottom: .4em; margin-top: .1em">
- <tr><td>
- The text area below (<b>source</b>) works like a Smalltalk workspace:
- <bl><li>
- To evaluate some code, just select it and press the <input type=button value="do it" onClick="doIt()"> button.
- </li><li>
- Pressing <input type=button value="do it" onClick="doIt()"> without a selection evaluates the line that the cursor is on.
- </li><li>
- <input type=button value="print it" onClick="printIt()"> is like <input type=button value="do it" onClick="doIt()">,
- but it also prints the result.
- </li></bl>
- Also, you can press <input type=button value="save it" onClick="saveIt()"> to save the current project.
- </td></tr></table>
- </div>
- <b><span title="A useful place to add new HTMLElements - see Canvas project for an example">Play Area</span></b>
- <input type=button value="+/-" onClick="toggleVisible('playArea')">
- <br>
- <div id=playArea style="visibility: visible; display: block"></div>
- <form id=workspaceForm>
- <b>Source</b>
- <input type=button value="+/-" onClick="toggleVisible('source')">
- <br>
- <div id=source style="visibility: visible; display: block">
- <textarea cols=132 rows=24 name=source style="font-family:monaco, monospace; font-size: 10pt"></textarea><br>
- <input type=button value="print it (ctrl+p)" onClick="printIt()" style="margin-bottom: .4em">
- <input type=button value="do it (ctrl+d)" onClick="doIt()" style="margin-bottom: .4em">
- <input type=button value="save it (ctrl + s)" onClick="saveProject()" style="margin-bottom: .4em"><br>
- </div>
- <b><span title="Shows the JavaScript translation of the last thing that was evaluated above">Translation</span></b>
- <input type=button value="+/-" onClick="toggleVisible('translation')">
- <br>
- <div id=translation style="visibility: hidden; display: none">
- <textarea cols=132 rows=4 name=translation style="font-family:monaco, monospace; font-size: 10pt; margin-bottom: .4em">
- </textArea>
- </div>
-
- <b>Transcript</b>
- <input type=button value="+/-" onClick="toggleVisible('transcript')">
- <br>
- <div id=transcript style="visibility: hidden; display: none">
- <textarea cols=132 rows=4 name=transcript
- style="font-family:monaco, monospace; font-size: 10pt; margin-bottom: .4em"></textArea>
- </div>
- </form>
- To learn more about OMeta, click <a href=http://www.tinlizzie.org/ometa/>here</a>.<br>
- </body>
-</html>
-
View
914 lib/ometajs.js
@@ -0,0 +1,914 @@
+/*
+ Copyright (c) 2007-2010 Alessandro Warth <awarth@cs.ucla.edu>
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation
+ files (the "Software"), to deal in the Software without
+ restriction, including without limitation the rights to use,
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following
+ conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+// try to use StringBuffer instead of string concatenation to improve performance
+
+function StringBuffer() {
+ this.strings = []
+ for (var idx = 0; idx < arguments.length; idx++)
+ this.nextPutAll(arguments[idx])
+}
+StringBuffer.prototype.nextPutAll = function(s) { this.strings.push(s) }
+StringBuffer.prototype.contents = function() { return this.strings.join("") }
+String.prototype.writeStream = function() { return new StringBuffer(this) }
+
+// make Arrays print themselves sensibly
+
+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
+ ws.nextPutAll(x.toString())
+}
+
+Array.prototype.toString = function() { var ws = "".writeStream(); printOn(this, ws); return ws.contents() }
+
+// delegation
+
+objectThatDelegatesTo = function(x, props) {
+ var f = function() { }
+ f.prototype = x
+ var r = new f()
+ for (var p in props)
+ if (props.hasOwnProperty(p))
+ r[p] = props[p]
+ return r
+}
+
+// some reflective stuff
+
+ownPropertyNames = function(x) {
+ var r = []
+ for (name in x)
+ if (x.hasOwnProperty(name))
+ r.push(name)
+ return r
+}
+
+isImmutable = function(x) {
+ return x === null || x === undefined || typeof x === "boolean" || typeof x === "number" || typeof x === "string"
+}
+
+String.prototype.digitValue = function() { return this.charCodeAt(0) - "0".charCodeAt(0) }
+
+isSequenceable = function(x) { return typeof x == "string" || x.constructor === Array }
+
+// some functional programming stuff
+
+Array.prototype.map = function(f) {
+ var r = []
+ for (var idx = 0; idx < this.length; idx++)
+ r[idx] = f(this[idx])
+ return r
+}
+
+Array.prototype.reduce = function(f, z) {
+ var r = z
+ for (var idx = 0; idx < this.length; idx++)
+ r = f(r, this[idx])
+ return r
+}
+
+Array.prototype.delimWith = function(d) {
+ return this.reduce(
+ function(xs, x) {
+ if (xs.length > 0)
+ xs.push(d)
+ xs.push(x)
+ return xs
+ },
+ [])
+}
+
+// Squeak's ReadStream, kind of
+
+function ReadStream(anArrayOrString) {
+ this.src = anArrayOrString
+ this.pos = 0
+}
+ReadStream.prototype.atEnd = function() { return this.pos >= this.src.length }
+ReadStream.prototype.next = function() { return this.src.at(this.pos++) }
+
+// escape characters
+
+escapeStringFor = new Object()
+for (var c = 0; c < 256; c++)
+ escapeStringFor[c] = String.fromCharCode(c)
+escapeStringFor["\\".charCodeAt(0)] = "\\\\"
+escapeStringFor['"'.charCodeAt(0)] = '\\"'
+escapeStringFor["'".charCodeAt(0)] = "\\'"
+escapeStringFor["\r".charCodeAt(0)] = "\\r"
+escapeStringFor["\n".charCodeAt(0)] = "\\n"
+escapeStringFor["\t".charCodeAt(0)] = "\\t"
+escapeChar = function(c) {
+ var charCode = c.charCodeAt(0)
+ return charCode > 255 ? String.fromCharCode(charCode) : escapeStringFor[charCode]
+}
+
+function unescape(s) {
+ if (s.charAt(0) == '\\')
+ switch (s.charAt(1)) {
+ case '\\': return '\\'
+ case 'r': return '\r'
+ case 'n': return '\n'
+ case 't': return '\t'
+ default: return s.charAt(1)
+ }
+ else
+ return s
+}
+
+String.prototype.toProgramString = function() {
+ var ws = "\"".writeStream()
+ for (var idx = 0; idx < this.length; idx++)
+ ws.nextPutAll(escapeChar(this.charAt(idx)))
+ ws.nextPutAll("\"")
+ return ws.contents()
+}
+
+// C-style tempnam function
+
+function tempnam(s) { return (s ? s : "_tmpnam_") + tempnam.n++ }
+tempnam.n = 0
+
+// unique tags for objects (useful for making "hash tables")
+
+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_") ? x._id_ : x._id_ = "R" + numIdx++
+ }
+ }
+})()
+
+/*
+ Copyright (c) 2007-2010 Alessandro Warth <awarth@cs.ucla.edu>
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation
+ files (the "Software"), to deal in the Software without
+ restriction, including without limitation the rights to use,
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following
+ conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+/*
+ new syntax:
+ #foo and `foo match the string object 'foo' (it's also accepted in my JS)
+ 'abc' match the string object 'abc'
+ 'c' match the string object 'c'
+ ``abc'' match the sequence of string objects 'a', 'b', 'c'
+ "abc" token('abc')
+ [1 2 3] match the array object [1, 2, 3]
+ foo(bar) apply rule foo with argument bar
+ -> ... semantic actions written in JS (see OMetaParser's atomicHostExpr rule)
+*/
+
+/*
+ometa M {
+ number = number:n digit:d -> { n * 10 + d.digitValue() }
+ | digit:d -> { d.digitValue() }
+}
+
+translates to...
+
+M = objectThatDelegatesTo(OMeta, {
+ number: function() {
+ return this._or(function() {
+ var n = this._apply("number"),
+ d = this._apply("digit")
+ return n * 10 + d.digitValue()
+ },
+ function() {
+ var d = this._apply("digit")
+ return d.digitValue()
+ }
+ )
+ }
+})
+M.matchAll("123456789", "number")
+*/
+
+// the failure exception
+
+fail = { toString: function() { return "match failed" } }
+
+// streams and memoization
+
+function OMInputStream(hd, tl) {
+ this.memo = { }
+ this.lst = tl.lst
+ this.idx = tl.idx
+ this.hd = hd
+ this.tl = tl
+}
+OMInputStream.prototype.head = function() { return this.hd }
+OMInputStream.prototype.tail = function() { return this.tl }
+OMInputStream.prototype.type = function() { return this.lst.constructor }
+OMInputStream.prototype.upTo = function(that) {
+ var r = [], curr = this
+ while (curr != that) {
+ r.push(curr.head())
+ curr = curr.tail()
+ }
+ return this.type() == String ? r.join('') : r
+}
+
+function OMInputStreamEnd(lst, idx) {
+ this.memo = { }
+ this.lst = lst
+ this.idx = idx
+}
+OMInputStreamEnd.prototype = objectThatDelegatesTo(OMInputStream.prototype)
+OMInputStreamEnd.prototype.head = function() { throw fail }
+OMInputStreamEnd.prototype.tail = function() { throw fail }
+
+// This is necessary b/c in IE, you can't say "foo"[idx]
+Array.prototype.at = function(idx) { return this[idx] }
+String.prototype.at = String.prototype.charAt
+
+function ListOMInputStream(lst, idx) {
+ this.memo = { }
+ this.lst = lst
+ this.idx = idx
+ this.hd = lst.at(idx)
+}
+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)) }
+
+function makeListOMInputStream(lst, idx) { return new (idx < lst.length ? ListOMInputStream : OMInputStreamEnd)(lst, idx) }
+
+Array.prototype.toOMInputStream = function() { return makeListOMInputStream(this, 0) }
+String.prototype.toOMInputStream = function() { return makeListOMInputStream(this, 0) }
+
+function makeOMInputStreamProxy(target) {
+ return objectThatDelegatesTo(target, {
+ memo: { },
+ target: target,
+ tail: function() { return makeOMInputStreamProxy(target.tail()) }
+ })
+}
+
+// Failer (i.e., that which makes things fail) is used to detect (direct) left recursion and memoize failures
+
+function Failer() { }
+Failer.prototype.used = false
+
+// the OMeta "class" and basic functionality
+
+OMeta = {
+ _apply: function(rule) {
+ var memoRec = this.input.memo[rule]
+ if (memoRec == undefined) {
+ var origInput = this.input,
+ failer = new Failer()
+ if (this[rule] === undefined)
+ throw 'tried to apply undefined rule "' + rule + '"'
+ this.input.memo[rule] = failer
+ this.input.memo[rule] = memoRec = {ans: this[rule].call(this), nextInput: this.input}
+ if (failer.used) {
+ var sentinel = this.input
+ while (true) {
+ try {
+ this.input = origInput
+ var ans = this[rule].call(this)
+ if (this.input == sentinel)
+ throw fail
+ memoRec.ans = ans
+ memoRec.nextInput = this.input
+ }
+ catch (f) {
+ if (f != fail)
+ throw f
+ break
+ }
+ }
+ }
+ }
+ else if (memoRec instanceof Failer) {
+ memoRec.used = true
+ throw fail
+ }
+ this.input = memoRec.nextInput
+ return memoRec.ans
+ },
+
+ // note: _applyWithArgs and _superApplyWithArgs are not memoized, so they can't be left-recursive
+ _applyWithArgs: function(rule) {
+ for (var idx = arguments.length - 1; idx > 0; idx--)
+ this._prependInput(arguments[idx])
+ return this[rule].call(this)
+ },
+ _superApplyWithArgs: function(recv, rule) {
+ for (var idx = arguments.length - 1; idx > 1; idx--)
+ recv._prependInput(arguments[idx])
+ return this[rule].call(recv)
+ },
+ _prependInput: function(v) {
+ this.input = new OMInputStream(v, this.input)
+ },
+
+ // if you want your grammar (and its subgrammars) to memoize parameterized rules, invoke this method on it:
+ memoizeParameterizedRules: function() {
+ this._prependInput = function(v) {
+ var newInput
+ if (isImmutable(v)) {
+ newInput = this.input[getTag(v)]
+ if (!newInput) {
+ newInput = new OMInputStream(v, this.input)
+ this.input[getTag(v)] = newInput
+ }
+ }
+ else newInput = new OMInputStream(v, this.input)
+ this.input = newInput
+ }
+ this._applyWithArgs = function(rule) {
+ for (var idx = arguments.length - 1; idx > 0; idx--)
+ this._prependInput(arguments[idx])
+ return this._apply(rule)
+ }
+ },
+
+ _pred: function(b) {
+ if (b)
+ return true
+ throw fail
+ },
+ _not: function(x) {
+ var origInput = this.input
+ try { x.call(this) }
+ catch (f) {
+ if (f != fail)
+ throw f
+ this.input = origInput
+ return true
+ }
+ throw fail
+ },
+ _lookahead: function(x) {
+ var origInput = this.input,
+ r = x.call(this)
+ this.input = origInput
+ return r
+ },
+ _or: function() {
+ var origInput = this.input
+ for (var idx = 0; idx < arguments.length; idx++)
+ try { this.input = origInput; return arguments[idx].call(this) }
+ catch (f) {
+ if (f != fail)
+ throw f
+ }
+ throw fail
+ },
+ _xor: function(ruleName) {
+ var origInput = this.input, idx = 1, newInput, ans
+ while (idx < arguments.length) {
+ try {
+ this.input = origInput
+ ans = arguments[idx].call(this)
+ if (newInput)
+ throw 'more than one choice matched by "exclusive-OR" in ' + ruleName
+ newInput = this.input
+ }
+ catch (f) {
+ if (f != fail)
+ throw f
+ }
+ idx++
+ }
+ if (newInput) {
+ this.input = newInput
+ return ans
+ }
+ else
+ throw fail
+ },
+ disableXORs: function() {
+ this._xor = function(ruleName) {
+ var origInput = this.input
+ for (var idx = 1; idx < arguments.length; idx++)
+ try { this.input = origInput; return arguments[idx].call(this) }
+ catch (f) {
+ if (f != fail)
+ throw f
+ }
+ throw fail
+ }
+ },
+ _opt: function(x) {
+ var origInput = this.input, ans
+ try { ans = x.call(this) }
+ catch (f) {
+ if (f != fail)
+ throw f
+ this.input = origInput
+ }
+ return ans
+ },
+ _many: function(x) {
+ var ans = arguments[1] != undefined ? [arguments[1]] : []
+ while (true) {
+ var origInput = this.input
+ try { ans.push(x.call(this)) }
+ catch (f) {
+ if (f != fail)
+ throw f
+ this.input = origInput
+ break
+ }
+ }
+ return ans
+ },
+ _many1: function(x) { return this._many(x, x.call(this)) },
+ _form: function(x) {
+ var v = this._apply("anything")
+ if (!isSequenceable(v))
+ throw fail
+ var origInput = this.input
+ this.input = v.toOMInputStream()
+ var r = x.call(this)
+ this._apply("end")
+ this.input = origInput
+ return v
+ },
+ _consumedBy: function(x) {
+ var origInput = this.input
+ x.call(this)
+ return origInput.upTo(this.input)
+ },
+ _idxConsumedBy: function(x) {
+ var origInput = this.input
+ x.call(this)
+ return {fromIdx: origInput.idx, toIdx: this.input.idx}
+ },
+ _interleave: function(mode1, part1, mode2, part2 /* ..., moden, partn */) {
+ var currInput = this.input, ans = []
+ for (var idx = 0; idx < arguments.length; idx += 2)
+ ans[idx / 2] = (arguments[idx] == "*" || arguments[idx] == "+") ? [] : undefined
+ while (true) {
+ var idx = 0, allDone = true
+ while (idx < arguments.length) {
+ if (arguments[idx] != "0")
+ try {
+ this.input = currInput
+ switch (arguments[idx]) {
+ case "*": ans[idx / 2].push(arguments[idx + 1].call(this)); break
+ case "+": ans[idx / 2].push(arguments[idx + 1].call(this)); arguments[idx] = "*"; break
+ case "?": ans[idx / 2] = arguments[idx + 1].call(this); arguments[idx] = "0"; break
+ case "1": ans[idx / 2] = arguments[idx + 1].call(this); arguments[idx] = "0"; break
+ default: throw "invalid mode '" + arguments[idx] + "' in OMeta._interleave"
+ }
+ currInput = this.input
+ break
+ }
+ catch (f) {
+ if (f != fail)
+ throw f
+ // if this (failed) part's mode is "1" or "+", we're not done yet
+ allDone = allDone && (arguments[idx] == "*" || arguments[idx] == "?")
+ }
+ idx += 2
+ }
+ if (idx == arguments.length) {
+ if (allDone)
+ return ans
+ else
+ throw fail
+ }
+ }
+ },
+ _currIdx: function() { return this.input.idx },
+
+ // some basic rules
+ anything: function() {
+ var r = this.input.head()
+ this.input = this.input.tail()
+ return r
+ },
+ end: function() {
+ return this._not(function() { return this._apply("anything") })
+ },
+ pos: function() {
+ return this.input.idx
+ },
+ empty: function() { return true },
+ apply: function() {
+ var r = this._apply("anything")
+ return this._apply(r)
+ },
+ foreign: function() {
+ var g = this._apply("anything"),
+ r = this._apply("anything"),
+ gi = objectThatDelegatesTo(g, {input: makeOMInputStreamProxy(this.input)})
+ var ans = gi._apply(r)
+ this.input = gi.input.target
+ return ans
+ },
+
+ // some useful "derived" rules
+ exactly: function() {
+ var wanted = this._apply("anything")
+ if (wanted === this._apply("anything"))
+ return wanted
+ throw fail
+ },
+ "true": function() {
+ var r = this._apply("anything")
+ this._pred(r === true)
+ return r
+ },
+ "false": function() {
+ var r = this._apply("anything")
+ this._pred(r === false)
+ return r
+ },
+ "undefined": function() {
+ var r = this._apply("anything")
+ this._pred(r === undefined)
+ return r
+ },
+ number: function() {
+ var r = this._apply("anything")
+ this._pred(typeof r === "number")
+ return r
+ },
+ string: function() {
+ var r = this._apply("anything")
+ this._pred(typeof r === "string")
+ return r
+ },
+ "char": function() {
+ var r = this._apply("anything")
+ this._pred(typeof r === "string" && r.length == 1)
+ return r
+ },
+ space: function() {
+ var r = this._apply("char")
+ this._pred(r.charCodeAt(0) <= 32)
+ return r
+ },
+ spaces: function() {
+ return this._many(function() { return this._apply("space") })
+ },
+ digit: function() {
+ var r = this._apply("char")
+ this._pred(r >= "0" && r <= "9")
+ return r
+ },
+ lower: function() {
+ var r = this._apply("char")
+ this._pred(r >= "a" && r <= "z")
+ return r
+ },
+ upper: function() {
+ var r = this._apply("char")
+ this._pred(r >= "A" && r <= "Z")
+ return r
+ },
+ letter: function() {
+ return this._or(function() { return this._apply("lower") },
+ function() { return this._apply("upper") })
+ },
+ letterOrDigit: function() {
+ return this._or(function() { return this._apply("letter") },
+ function() { return this._apply("digit") })
+ },
+ firstAndRest: function() {
+ var first = this._apply("anything"),
+ rest = this._apply("anything")
+ return this._many(function() { return this._apply(rest) }, this._apply(first))
+ },
+ seq: function() {
+ var xs = this._apply("anything")
+ for (var idx = 0; idx < xs.length; idx++)
+ this._applyWithArgs("exactly", xs.at(idx))
+ return xs
+ },
+ notLast: function() {
+ var rule = this._apply("anything"),
+ r = this._apply(rule)
+ this._lookahead(function() { return this._apply(rule) })
+ return r
+ },
+
+ initialize: function() { },
+ // match and matchAll are a grammar's "public interface"
+ _genericMatch: function(input, rule, args, matchFailed) {
+ if (args == undefined)
+ args = []
+ var realArgs = [rule]
+ for (var idx = 0; idx < args.length; idx++)
+ realArgs.push(args[idx])
+ 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) {
+ if (f == fail && matchFailed != undefined) {
+ var input = m.input
+ if (input.idx != undefined) {
+ while (input.tl != undefined && input.tl.idx != undefined)
+ input = input.tl
+ input.idx--
+ }
+ return matchFailed(m, input.idx)
+ }
+ throw f
+ }
+ },
+ match: function(obj, rule, args, matchFailed) {
+ return this._genericMatch([obj].toOMInputStream(), rule, args, matchFailed)
+ },
+ matchAll: function(listyObj, rule, args, matchFailed) {
+ return this._genericMatch(listyObj.toOMInputStream(), rule, args, matchFailed)
+ }
+}
+
+/*
+ Copyright (c) 2007, 2008 Alessandro Warth <awarth@cs.ucla.edu>
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation
+ files (the "Software"), to deal in the Software without
+ restriction, including without limitation the rights to use,
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following
+ conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ OTHER DEALINGS IN THE SOFTWARE.
+*/