diff --git a/repl/krill-parser.js b/repl/krill-parser.js new file mode 100644 index 00000000..db17b13f --- /dev/null +++ b/repl/krill-parser.js @@ -0,0 +1,1916 @@ +// Generated by Peggy 1.2.0. +// +// https://peggyjs.org/ + +function peg$subclass(child, parent) { + function C() { this.constructor = child; } + C.prototype = parent.prototype; + child.prototype = new C(); +} + +function peg$SyntaxError(message, expected, found, location) { + var self = Error.call(this, message); + if (Object.setPrototypeOf) { + Object.setPrototypeOf(self, peg$SyntaxError.prototype); + } + self.expected = expected; + self.found = found; + self.location = location; + self.name = "SyntaxError"; + return self; +} + +peg$subclass(peg$SyntaxError, Error); + +function peg$padEnd(str, targetLength, padString) { + padString = padString || " "; + if (str.length > targetLength) { return str; } + targetLength -= str.length; + padString += padString.repeat(targetLength); + return str + padString.slice(0, targetLength); +} + +peg$SyntaxError.prototype.format = function(sources) { + var str = "Error: " + this.message; + if (this.location) { + var src = null; + var k; + for (k = 0; k < sources.length; k++) { + if (sources[k].source === this.location.source) { + src = sources[k].text.split(/\r\n|\n|\r/g); + break; + } + } + var s = this.location.start; + var loc = this.location.source + ":" + s.line + ":" + s.column; + if (src) { + var e = this.location.end; + var filler = peg$padEnd("", s.line.toString().length); + var line = src[s.line - 1]; + var last = s.line === e.line ? e.column : line.length + 1; + str += "\n --> " + loc + "\n" + + filler + " |\n" + + s.line + " | " + line + "\n" + + filler + " | " + peg$padEnd("", s.column - 1) + + peg$padEnd("", last - s.column, "^"); + } else { + str += "\n at " + loc; + } + } + return str; +}; + +peg$SyntaxError.buildMessage = function(expected, found) { + var DESCRIBE_EXPECTATION_FNS = { + literal: function(expectation) { + return "\"" + literalEscape(expectation.text) + "\""; + }, + + class: function(expectation) { + var escapedParts = expectation.parts.map(function(part) { + return Array.isArray(part) + ? classEscape(part[0]) + "-" + classEscape(part[1]) + : classEscape(part); + }); + + return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]"; + }, + + any: function() { + return "any character"; + }, + + end: function() { + return "end of input"; + }, + + other: function(expectation) { + return expectation.description; + } + }; + + function hex(ch) { + return ch.charCodeAt(0).toString(16).toUpperCase(); + } + + function literalEscape(s) { + return s + .replace(/\\/g, "\\\\") + .replace(/"/g, "\\\"") + .replace(/\0/g, "\\0") + .replace(/\t/g, "\\t") + .replace(/\n/g, "\\n") + .replace(/\r/g, "\\r") + .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0" + hex(ch); }) + .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x" + hex(ch); }); + } + + function classEscape(s) { + return s + .replace(/\\/g, "\\\\") + .replace(/\]/g, "\\]") + .replace(/\^/g, "\\^") + .replace(/-/g, "\\-") + .replace(/\0/g, "\\0") + .replace(/\t/g, "\\t") + .replace(/\n/g, "\\n") + .replace(/\r/g, "\\r") + .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0" + hex(ch); }) + .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x" + hex(ch); }); + } + + function describeExpectation(expectation) { + return DESCRIBE_EXPECTATION_FNS[expectation.type](expectation); + } + + function describeExpected(expected) { + var descriptions = expected.map(describeExpectation); + var i, j; + + descriptions.sort(); + + if (descriptions.length > 0) { + for (i = 1, j = 1; i < descriptions.length; i++) { + if (descriptions[i - 1] !== descriptions[i]) { + descriptions[j] = descriptions[i]; + j++; + } + } + descriptions.length = j; + } + + switch (descriptions.length) { + case 1: + return descriptions[0]; + + case 2: + return descriptions[0] + " or " + descriptions[1]; + + default: + return descriptions.slice(0, -1).join(", ") + + ", or " + + descriptions[descriptions.length - 1]; + } + } + + function describeFound(found) { + return found ? "\"" + literalEscape(found) + "\"" : "end of input"; + } + + return "Expected " + describeExpected(expected) + " but " + describeFound(found) + " found."; +}; + +function peg$parse(input, options) { + options = options !== undefined ? options : {}; + + var peg$FAILED = {}; + var peg$source = options.grammarSource; + + var peg$startRuleFunctions = { start: peg$parsestart }; + var peg$startRuleFunction = peg$parsestart; + + var peg$c0 = "."; + var peg$c1 = "-"; + var peg$c2 = "+"; + var peg$c3 = "0"; + var peg$c4 = ","; + var peg$c5 = "\""; + var peg$c6 = "'"; + var peg$c7 = "#"; + var peg$c8 = "["; + var peg$c9 = "]"; + var peg$c10 = "<"; + var peg$c11 = ">"; + var peg$c12 = "@"; + var peg$c13 = "("; + var peg$c14 = ")"; + var peg$c15 = "/"; + var peg$c16 = "*"; + var peg$c17 = "%"; + var peg$c18 = "struct"; + var peg$c19 = "target"; + var peg$c20 = "euclid"; + var peg$c21 = "slow"; + var peg$c22 = "rotL"; + var peg$c23 = "rotR"; + var peg$c24 = "fast"; + var peg$c25 = "scale"; + var peg$c26 = "//"; + var peg$c27 = "cat"; + var peg$c28 = "$"; + var peg$c29 = "setcps"; + var peg$c30 = "setbpm"; + var peg$c31 = "hush"; + + var peg$r0 = /^[1-9]/; + var peg$r1 = /^[eE]/; + var peg$r2 = /^[0-9]/; + var peg$r3 = /^[ \n\r\t]/; + var peg$r4 = /^[0-9a-zA-Z~]/; + var peg$r5 = /^[^\n]/; + + var peg$e0 = peg$otherExpectation("number"); + var peg$e1 = peg$literalExpectation(".", false); + var peg$e2 = peg$classExpectation([["1", "9"]], false, false); + var peg$e3 = peg$classExpectation(["e", "E"], false, false); + var peg$e4 = peg$literalExpectation("-", false); + var peg$e5 = peg$literalExpectation("+", false); + var peg$e6 = peg$literalExpectation("0", false); + var peg$e7 = peg$classExpectation([["0", "9"]], false, false); + var peg$e8 = peg$otherExpectation("whitespace"); + var peg$e9 = peg$classExpectation([" ", "\n", "\r", "\t"], false, false); + var peg$e10 = peg$literalExpectation(",", false); + var peg$e11 = peg$literalExpectation("\"", false); + var peg$e12 = peg$literalExpectation("'", false); + var peg$e13 = peg$classExpectation([["0", "9"], ["a", "z"], ["A", "Z"], "~"], false, false); + var peg$e14 = peg$literalExpectation("#", false); + var peg$e15 = peg$literalExpectation("[", false); + var peg$e16 = peg$literalExpectation("]", false); + var peg$e17 = peg$literalExpectation("<", false); + var peg$e18 = peg$literalExpectation(">", false); + var peg$e19 = peg$literalExpectation("@", false); + var peg$e20 = peg$literalExpectation("(", false); + var peg$e21 = peg$literalExpectation(")", false); + var peg$e22 = peg$literalExpectation("/", false); + var peg$e23 = peg$literalExpectation("*", false); + var peg$e24 = peg$literalExpectation("%", false); + var peg$e25 = peg$literalExpectation("struct", false); + var peg$e26 = peg$literalExpectation("target", false); + var peg$e27 = peg$literalExpectation("euclid", false); + var peg$e28 = peg$literalExpectation("slow", false); + var peg$e29 = peg$literalExpectation("rotL", false); + var peg$e30 = peg$literalExpectation("rotR", false); + var peg$e31 = peg$literalExpectation("fast", false); + var peg$e32 = peg$literalExpectation("scale", false); + var peg$e33 = peg$literalExpectation("//", false); + var peg$e34 = peg$classExpectation(["\n"], true, false); + var peg$e35 = peg$literalExpectation("cat", false); + var peg$e36 = peg$literalExpectation("$", false); + var peg$e37 = peg$literalExpectation("setcps", false); + var peg$e38 = peg$literalExpectation("setbpm", false); + var peg$e39 = peg$literalExpectation("hush", false); + + var peg$f0 = function() { return parseFloat(text()); }; + var peg$f1 = function(chars) { return chars.join("") }; + var peg$f2 = function(s) { return s}; + var peg$f3 = function(sc) { sc.arguments_.alignment = "t"; return sc;}; + var peg$f4 = function(a) { return { weight: a} }; + var peg$f5 = function(p, s) { return { operator : { type_: "bjorklund", arguments_ :{ pulse: p, step:s } } } }; + var peg$f6 = function(a) { return { operator : { type_: "stretch", arguments_ :{ amount:a } } } }; + var peg$f7 = function(a) { return { operator : { type_: "stretch", arguments_ :{ amount:"1/"+a } } } }; + var peg$f8 = function(a) { return { operator : { type_: "fixed-step", arguments_ :{ amount:a } } } }; + var peg$f9 = function(s, o) { return new ElementStub(s, o);}; + var peg$f10 = function(s) { return new PatternStub(s,"h"); }; + var peg$f11 = function(c, v) { return v}; + var peg$f12 = function(c, cs) { if (cs.length == 0 && c instanceof Object) { return c;} else { cs.unshift(c); return new PatternStub(cs,"v");} }; + var peg$f13 = function(s) { return s; }; + var peg$f14 = function(s) { return { name: "struct", args: { sequence:s }}}; + var peg$f15 = function(s) { return { name: "target", args : { name:s}}}; + var peg$f16 = function(p, s) { return { name: "bjorklund", args :{ pulse: parseInt(p), step:parseInt(s) }}}; + var peg$f17 = function(a) { return { name: "stretch", args :{ amount: a}}}; + var peg$f18 = function(a) { return { name: "shift", args :{ amount: "-"+a}}}; + var peg$f19 = function(a) { return { name: "shift", args :{ amount: a}}}; + var peg$f20 = function(a) { return { name: "stretch", args :{ amount: "1/"+a}}}; + var peg$f21 = function(s) { return { name: "scale", args :{ scale: s.join("")}}}; + var peg$f22 = function(s, v) { return v}; + var peg$f23 = function(s, ss) { ss.unshift(s); return new PatternStub(ss,"t"); }; + var peg$f24 = function(sg) {return sg}; + var peg$f25 = function(o, soc) { return new OperatorStub(o.name,o.args,soc)}; + var peg$f26 = function(sc) { return sc }; + var peg$f27 = function(c) { return c }; + var peg$f28 = function(v) { return new CommandStub("setcps", { value: v})}; + var peg$f29 = function(v) { return new CommandStub("setcps", { value: (v/120/2)})}; + var peg$f30 = function() { return new CommandStub("hush")}; + + var peg$currPos = 0; + var peg$savedPos = 0; + var peg$posDetailsCache = [{ line: 1, column: 1 }]; + var peg$maxFailPos = 0; + var peg$maxFailExpected = []; + var peg$silentFails = 0; + + var peg$result; + + if ("startRule" in options) { + if (!(options.startRule in peg$startRuleFunctions)) { + throw new Error("Can't start parsing from rule \"" + options.startRule + "\"."); + } + + peg$startRuleFunction = peg$startRuleFunctions[options.startRule]; + } + + function text() { + return input.substring(peg$savedPos, peg$currPos); + } + + function offset() { + return peg$savedPos; + } + + function range() { + return { + source: peg$source, + start: peg$savedPos, + end: peg$currPos + }; + } + + function location() { + return peg$computeLocation(peg$savedPos, peg$currPos); + } + + function expected(description, location) { + location = location !== undefined + ? location + : peg$computeLocation(peg$savedPos, peg$currPos); + + throw peg$buildStructuredError( + [peg$otherExpectation(description)], + input.substring(peg$savedPos, peg$currPos), + location + ); + } + + function error(message, location) { + location = location !== undefined + ? location + : peg$computeLocation(peg$savedPos, peg$currPos); + + throw peg$buildSimpleError(message, location); + } + + function peg$literalExpectation(text, ignoreCase) { + return { type: "literal", text: text, ignoreCase: ignoreCase }; + } + + function peg$classExpectation(parts, inverted, ignoreCase) { + return { type: "class", parts: parts, inverted: inverted, ignoreCase: ignoreCase }; + } + + function peg$anyExpectation() { + return { type: "any" }; + } + + function peg$endExpectation() { + return { type: "end" }; + } + + function peg$otherExpectation(description) { + return { type: "other", description: description }; + } + + function peg$computePosDetails(pos) { + var details = peg$posDetailsCache[pos]; + var p; + + if (details) { + return details; + } else { + p = pos - 1; + while (!peg$posDetailsCache[p]) { + p--; + } + + details = peg$posDetailsCache[p]; + details = { + line: details.line, + column: details.column + }; + + while (p < pos) { + if (input.charCodeAt(p) === 10) { + details.line++; + details.column = 1; + } else { + details.column++; + } + + p++; + } + + peg$posDetailsCache[pos] = details; + + return details; + } + } + + function peg$computeLocation(startPos, endPos) { + var startPosDetails = peg$computePosDetails(startPos); + var endPosDetails = peg$computePosDetails(endPos); + + return { + source: peg$source, + start: { + offset: startPos, + line: startPosDetails.line, + column: startPosDetails.column + }, + end: { + offset: endPos, + line: endPosDetails.line, + column: endPosDetails.column + } + }; + } + + function peg$fail(expected) { + if (peg$currPos < peg$maxFailPos) { return; } + + if (peg$currPos > peg$maxFailPos) { + peg$maxFailPos = peg$currPos; + peg$maxFailExpected = []; + } + + peg$maxFailExpected.push(expected); + } + + function peg$buildSimpleError(message, location) { + return new peg$SyntaxError(message, null, null, location); + } + + function peg$buildStructuredError(expected, found, location) { + return new peg$SyntaxError( + peg$SyntaxError.buildMessage(expected, found), + expected, + found, + location + ); + } + + function peg$parsestart() { + var s0; + + s0 = peg$parsestatement(); + + return s0; + } + + function peg$parsenumber() { + var s0, s1, s2, s3, s4; + + peg$silentFails++; + s0 = peg$currPos; + s1 = peg$parseminus(); + if (s1 === peg$FAILED) { + s1 = null; + } + s2 = peg$parseint(); + if (s2 !== peg$FAILED) { + s3 = peg$parsefrac(); + if (s3 === peg$FAILED) { + s3 = null; + } + s4 = peg$parseexp(); + if (s4 === peg$FAILED) { + s4 = null; + } + peg$savedPos = s0; + s0 = peg$f0(); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + peg$silentFails--; + if (s0 === peg$FAILED) { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e0); } + } + + return s0; + } + + function peg$parsedecimal_point() { + var s0; + + if (input.charCodeAt(peg$currPos) === 46) { + s0 = peg$c0; + peg$currPos++; + } else { + s0 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e1); } + } + + return s0; + } + + function peg$parsedigit1_9() { + var s0; + + if (peg$r0.test(input.charAt(peg$currPos))) { + s0 = input.charAt(peg$currPos); + peg$currPos++; + } else { + s0 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e2); } + } + + return s0; + } + + function peg$parsee() { + var s0; + + if (peg$r1.test(input.charAt(peg$currPos))) { + s0 = input.charAt(peg$currPos); + peg$currPos++; + } else { + s0 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e3); } + } + + return s0; + } + + function peg$parseexp() { + var s0, s1, s2, s3, s4; + + s0 = peg$currPos; + s1 = peg$parsee(); + if (s1 !== peg$FAILED) { + s2 = peg$parseminus(); + if (s2 === peg$FAILED) { + s2 = peg$parseplus(); + } + if (s2 === peg$FAILED) { + s2 = null; + } + s3 = []; + s4 = peg$parseDIGIT(); + if (s4 !== peg$FAILED) { + while (s4 !== peg$FAILED) { + s3.push(s4); + s4 = peg$parseDIGIT(); + } + } else { + s3 = peg$FAILED; + } + if (s3 !== peg$FAILED) { + s1 = [s1, s2, s3]; + s0 = s1; + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parsefrac() { + var s0, s1, s2, s3; + + s0 = peg$currPos; + s1 = peg$parsedecimal_point(); + if (s1 !== peg$FAILED) { + s2 = []; + s3 = peg$parseDIGIT(); + if (s3 !== peg$FAILED) { + while (s3 !== peg$FAILED) { + s2.push(s3); + s3 = peg$parseDIGIT(); + } + } else { + s2 = peg$FAILED; + } + if (s2 !== peg$FAILED) { + s1 = [s1, s2]; + s0 = s1; + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parseint() { + var s0, s1, s2, s3; + + s0 = peg$parsezero(); + if (s0 === peg$FAILED) { + s0 = peg$currPos; + s1 = peg$parsedigit1_9(); + if (s1 !== peg$FAILED) { + s2 = []; + s3 = peg$parseDIGIT(); + while (s3 !== peg$FAILED) { + s2.push(s3); + s3 = peg$parseDIGIT(); + } + s1 = [s1, s2]; + s0 = s1; + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } + + return s0; + } + + function peg$parseminus() { + var s0; + + if (input.charCodeAt(peg$currPos) === 45) { + s0 = peg$c1; + peg$currPos++; + } else { + s0 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e4); } + } + + return s0; + } + + function peg$parseplus() { + var s0; + + if (input.charCodeAt(peg$currPos) === 43) { + s0 = peg$c2; + peg$currPos++; + } else { + s0 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e5); } + } + + return s0; + } + + function peg$parsezero() { + var s0; + + if (input.charCodeAt(peg$currPos) === 48) { + s0 = peg$c3; + peg$currPos++; + } else { + s0 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e6); } + } + + return s0; + } + + function peg$parseDIGIT() { + var s0; + + if (peg$r2.test(input.charAt(peg$currPos))) { + s0 = input.charAt(peg$currPos); + peg$currPos++; + } else { + s0 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e7); } + } + + return s0; + } + + function peg$parsews() { + var s0, s1; + + peg$silentFails++; + s0 = []; + if (peg$r3.test(input.charAt(peg$currPos))) { + s1 = input.charAt(peg$currPos); + peg$currPos++; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e9); } + } + while (s1 !== peg$FAILED) { + s0.push(s1); + if (peg$r3.test(input.charAt(peg$currPos))) { + s1 = input.charAt(peg$currPos); + peg$currPos++; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e9); } + } + } + peg$silentFails--; + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e8); } + + return s0; + } + + function peg$parsecomma() { + var s0, s1, s2, s3; + + s0 = peg$currPos; + s1 = peg$parsews(); + if (input.charCodeAt(peg$currPos) === 44) { + s2 = peg$c4; + peg$currPos++; + } else { + s2 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e10); } + } + if (s2 !== peg$FAILED) { + s3 = peg$parsews(); + s1 = [s1, s2, s3]; + s0 = s1; + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parsequote() { + var s0; + + if (input.charCodeAt(peg$currPos) === 34) { + s0 = peg$c5; + peg$currPos++; + } else { + s0 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e11); } + } + if (s0 === peg$FAILED) { + if (input.charCodeAt(peg$currPos) === 39) { + s0 = peg$c6; + peg$currPos++; + } else { + s0 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e12); } + } + } + + return s0; + } + + function peg$parsestep_char() { + var s0; + + if (peg$r4.test(input.charAt(peg$currPos))) { + s0 = input.charAt(peg$currPos); + peg$currPos++; + } else { + s0 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e13); } + } + if (s0 === peg$FAILED) { + if (input.charCodeAt(peg$currPos) === 45) { + s0 = peg$c1; + peg$currPos++; + } else { + s0 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e4); } + } + if (s0 === peg$FAILED) { + if (input.charCodeAt(peg$currPos) === 35) { + s0 = peg$c7; + peg$currPos++; + } else { + s0 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e14); } + } + if (s0 === peg$FAILED) { + if (input.charCodeAt(peg$currPos) === 46) { + s0 = peg$c0; + peg$currPos++; + } else { + s0 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e1); } + } + } + } + } + + return s0; + } + + function peg$parsestep() { + var s0, s1, s2, s3; + + s0 = peg$currPos; + s1 = peg$parsews(); + s2 = []; + s3 = peg$parsestep_char(); + if (s3 !== peg$FAILED) { + while (s3 !== peg$FAILED) { + s2.push(s3); + s3 = peg$parsestep_char(); + } + } else { + s2 = peg$FAILED; + } + if (s2 !== peg$FAILED) { + s3 = peg$parsews(); + peg$savedPos = s0; + s0 = peg$f1(s2); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parsesub_cycle() { + var s0, s1, s2, s3, s4, s5, s6, s7; + + s0 = peg$currPos; + s1 = peg$parsews(); + if (input.charCodeAt(peg$currPos) === 91) { + s2 = peg$c8; + peg$currPos++; + } else { + s2 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e15); } + } + if (s2 !== peg$FAILED) { + s3 = peg$parsews(); + s4 = peg$parsestack(); + if (s4 !== peg$FAILED) { + s5 = peg$parsews(); + if (input.charCodeAt(peg$currPos) === 93) { + s6 = peg$c9; + peg$currPos++; + } else { + s6 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e16); } + } + if (s6 !== peg$FAILED) { + s7 = peg$parsews(); + peg$savedPos = s0; + s0 = peg$f2(s4); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parsetimeline() { + var s0, s1, s2, s3, s4, s5, s6, s7; + + s0 = peg$currPos; + s1 = peg$parsews(); + if (input.charCodeAt(peg$currPos) === 60) { + s2 = peg$c10; + peg$currPos++; + } else { + s2 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e17); } + } + if (s2 !== peg$FAILED) { + s3 = peg$parsews(); + s4 = peg$parsesingle_cycle(); + if (s4 !== peg$FAILED) { + s5 = peg$parsews(); + if (input.charCodeAt(peg$currPos) === 62) { + s6 = peg$c11; + peg$currPos++; + } else { + s6 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e18); } + } + if (s6 !== peg$FAILED) { + s7 = peg$parsews(); + peg$savedPos = s0; + s0 = peg$f3(s4); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parseslice() { + var s0; + + s0 = peg$parsestep(); + if (s0 === peg$FAILED) { + s0 = peg$parsesub_cycle(); + if (s0 === peg$FAILED) { + s0 = peg$parsetimeline(); + } + } + + return s0; + } + + function peg$parseslice_modifier() { + var s0; + + s0 = peg$parseslice_weight(); + if (s0 === peg$FAILED) { + s0 = peg$parseslice_bjorklund(); + if (s0 === peg$FAILED) { + s0 = peg$parseslice_slow(); + if (s0 === peg$FAILED) { + s0 = peg$parseslice_fast(); + if (s0 === peg$FAILED) { + s0 = peg$parseslice_fixed_step(); + } + } + } + } + + return s0; + } + + function peg$parseslice_weight() { + var s0, s1, s2; + + s0 = peg$currPos; + if (input.charCodeAt(peg$currPos) === 64) { + s1 = peg$c12; + peg$currPos++; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e19); } + } + if (s1 !== peg$FAILED) { + s2 = peg$parsenumber(); + if (s2 !== peg$FAILED) { + peg$savedPos = s0; + s0 = peg$f4(s2); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parseslice_bjorklund() { + var s0, s1, s2, s3, s4, s5, s6, s7, s8, s9; + + s0 = peg$currPos; + if (input.charCodeAt(peg$currPos) === 40) { + s1 = peg$c13; + peg$currPos++; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e20); } + } + if (s1 !== peg$FAILED) { + s2 = peg$parsews(); + s3 = peg$parsenumber(); + if (s3 !== peg$FAILED) { + s4 = peg$parsews(); + s5 = peg$parsecomma(); + if (s5 !== peg$FAILED) { + s6 = peg$parsews(); + s7 = peg$parsenumber(); + if (s7 !== peg$FAILED) { + s8 = peg$parsews(); + if (input.charCodeAt(peg$currPos) === 41) { + s9 = peg$c14; + peg$currPos++; + } else { + s9 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e21); } + } + if (s9 !== peg$FAILED) { + peg$savedPos = s0; + s0 = peg$f5(s3, s7); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parseslice_slow() { + var s0, s1, s2; + + s0 = peg$currPos; + if (input.charCodeAt(peg$currPos) === 47) { + s1 = peg$c15; + peg$currPos++; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e22); } + } + if (s1 !== peg$FAILED) { + s2 = peg$parsenumber(); + if (s2 !== peg$FAILED) { + peg$savedPos = s0; + s0 = peg$f6(s2); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parseslice_fast() { + var s0, s1, s2; + + s0 = peg$currPos; + if (input.charCodeAt(peg$currPos) === 42) { + s1 = peg$c16; + peg$currPos++; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e23); } + } + if (s1 !== peg$FAILED) { + s2 = peg$parsenumber(); + if (s2 !== peg$FAILED) { + peg$savedPos = s0; + s0 = peg$f7(s2); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parseslice_fixed_step() { + var s0, s1, s2; + + s0 = peg$currPos; + if (input.charCodeAt(peg$currPos) === 37) { + s1 = peg$c17; + peg$currPos++; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e24); } + } + if (s1 !== peg$FAILED) { + s2 = peg$parsenumber(); + if (s2 !== peg$FAILED) { + peg$savedPos = s0; + s0 = peg$f8(s2); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parseslice_with_modifier() { + var s0, s1, s2; + + s0 = peg$currPos; + s1 = peg$parseslice(); + if (s1 !== peg$FAILED) { + s2 = peg$parseslice_modifier(); + if (s2 === peg$FAILED) { + s2 = null; + } + peg$savedPos = s0; + s0 = peg$f9(s1, s2); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parsesingle_cycle() { + var s0, s1, s2; + + s0 = peg$currPos; + s1 = []; + s2 = peg$parseslice_with_modifier(); + if (s2 !== peg$FAILED) { + while (s2 !== peg$FAILED) { + s1.push(s2); + s2 = peg$parseslice_with_modifier(); + } + } else { + s1 = peg$FAILED; + } + if (s1 !== peg$FAILED) { + peg$savedPos = s0; + s1 = peg$f10(s1); + } + s0 = s1; + + return s0; + } + + function peg$parsestack() { + var s0, s1, s2, s3, s4, s5; + + s0 = peg$currPos; + s1 = peg$parsesingle_cycle(); + if (s1 !== peg$FAILED) { + s2 = []; + s3 = peg$currPos; + s4 = peg$parsecomma(); + if (s4 !== peg$FAILED) { + s5 = peg$parsesingle_cycle(); + if (s5 !== peg$FAILED) { + peg$savedPos = s3; + s3 = peg$f11(s1, s5); + } else { + peg$currPos = s3; + s3 = peg$FAILED; + } + } else { + peg$currPos = s3; + s3 = peg$FAILED; + } + while (s3 !== peg$FAILED) { + s2.push(s3); + s3 = peg$currPos; + s4 = peg$parsecomma(); + if (s4 !== peg$FAILED) { + s5 = peg$parsesingle_cycle(); + if (s5 !== peg$FAILED) { + peg$savedPos = s3; + s3 = peg$f11(s1, s5); + } else { + peg$currPos = s3; + s3 = peg$FAILED; + } + } else { + peg$currPos = s3; + s3 = peg$FAILED; + } + } + peg$savedPos = s0; + s0 = peg$f12(s1, s2); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parsesequence() { + var s0, s1, s2, s3, s4; + + s0 = peg$currPos; + s1 = peg$parsews(); + s2 = peg$parsequote(); + if (s2 !== peg$FAILED) { + s3 = peg$parsestack(); + if (s3 !== peg$FAILED) { + s4 = peg$parsequote(); + if (s4 !== peg$FAILED) { + peg$savedPos = s0; + s0 = peg$f13(s3); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parseoperator() { + var s0; + + s0 = peg$parsescale(); + if (s0 === peg$FAILED) { + s0 = peg$parseslow(); + if (s0 === peg$FAILED) { + s0 = peg$parsefast(); + if (s0 === peg$FAILED) { + s0 = peg$parsetarget(); + if (s0 === peg$FAILED) { + s0 = peg$parsebjorklund(); + if (s0 === peg$FAILED) { + s0 = peg$parsestruct(); + if (s0 === peg$FAILED) { + s0 = peg$parserotR(); + if (s0 === peg$FAILED) { + s0 = peg$parserotL(); + } + } + } + } + } + } + } + + return s0; + } + + function peg$parsestruct() { + var s0, s1, s2, s3; + + s0 = peg$currPos; + if (input.substr(peg$currPos, 6) === peg$c18) { + s1 = peg$c18; + peg$currPos += 6; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e25); } + } + if (s1 !== peg$FAILED) { + s2 = peg$parsews(); + s3 = peg$parsesequence_or_operator(); + if (s3 !== peg$FAILED) { + peg$savedPos = s0; + s0 = peg$f14(s3); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parsetarget() { + var s0, s1, s2, s3, s4, s5; + + s0 = peg$currPos; + if (input.substr(peg$currPos, 6) === peg$c19) { + s1 = peg$c19; + peg$currPos += 6; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e26); } + } + if (s1 !== peg$FAILED) { + s2 = peg$parsews(); + s3 = peg$parsequote(); + if (s3 !== peg$FAILED) { + s4 = peg$parsestep(); + if (s4 !== peg$FAILED) { + s5 = peg$parsequote(); + if (s5 !== peg$FAILED) { + peg$savedPos = s0; + s0 = peg$f15(s4); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parsebjorklund() { + var s0, s1, s2, s3, s4, s5; + + s0 = peg$currPos; + if (input.substr(peg$currPos, 6) === peg$c20) { + s1 = peg$c20; + peg$currPos += 6; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e27); } + } + if (s1 !== peg$FAILED) { + s2 = peg$parsews(); + s3 = peg$parseint(); + if (s3 !== peg$FAILED) { + s4 = peg$parsews(); + s5 = peg$parseint(); + if (s5 !== peg$FAILED) { + peg$savedPos = s0; + s0 = peg$f16(s3, s5); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parseslow() { + var s0, s1, s2, s3; + + s0 = peg$currPos; + if (input.substr(peg$currPos, 4) === peg$c21) { + s1 = peg$c21; + peg$currPos += 4; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e28); } + } + if (s1 !== peg$FAILED) { + s2 = peg$parsews(); + s3 = peg$parsenumber(); + if (s3 !== peg$FAILED) { + peg$savedPos = s0; + s0 = peg$f17(s3); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parserotL() { + var s0, s1, s2, s3; + + s0 = peg$currPos; + if (input.substr(peg$currPos, 4) === peg$c22) { + s1 = peg$c22; + peg$currPos += 4; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e29); } + } + if (s1 !== peg$FAILED) { + s2 = peg$parsews(); + s3 = peg$parsenumber(); + if (s3 !== peg$FAILED) { + peg$savedPos = s0; + s0 = peg$f18(s3); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parserotR() { + var s0, s1, s2, s3; + + s0 = peg$currPos; + if (input.substr(peg$currPos, 4) === peg$c23) { + s1 = peg$c23; + peg$currPos += 4; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e30); } + } + if (s1 !== peg$FAILED) { + s2 = peg$parsews(); + s3 = peg$parsenumber(); + if (s3 !== peg$FAILED) { + peg$savedPos = s0; + s0 = peg$f19(s3); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parsefast() { + var s0, s1, s2, s3; + + s0 = peg$currPos; + if (input.substr(peg$currPos, 4) === peg$c24) { + s1 = peg$c24; + peg$currPos += 4; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e31); } + } + if (s1 !== peg$FAILED) { + s2 = peg$parsews(); + s3 = peg$parsenumber(); + if (s3 !== peg$FAILED) { + peg$savedPos = s0; + s0 = peg$f20(s3); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parsescale() { + var s0, s1, s2, s3, s4, s5; + + s0 = peg$currPos; + if (input.substr(peg$currPos, 5) === peg$c25) { + s1 = peg$c25; + peg$currPos += 5; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e32); } + } + if (s1 !== peg$FAILED) { + s2 = peg$parsews(); + s3 = peg$parsequote(); + if (s3 !== peg$FAILED) { + s4 = []; + s5 = peg$parsestep_char(); + if (s5 !== peg$FAILED) { + while (s5 !== peg$FAILED) { + s4.push(s5); + s5 = peg$parsestep_char(); + } + } else { + s4 = peg$FAILED; + } + if (s4 !== peg$FAILED) { + s5 = peg$parsequote(); + if (s5 !== peg$FAILED) { + peg$savedPos = s0; + s0 = peg$f21(s4); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parsecomment() { + var s0, s1, s2, s3; + + s0 = peg$currPos; + if (input.substr(peg$currPos, 2) === peg$c26) { + s1 = peg$c26; + peg$currPos += 2; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e33); } + } + if (s1 !== peg$FAILED) { + s2 = []; + if (peg$r5.test(input.charAt(peg$currPos))) { + s3 = input.charAt(peg$currPos); + peg$currPos++; + } else { + s3 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e34); } + } + while (s3 !== peg$FAILED) { + s2.push(s3); + if (peg$r5.test(input.charAt(peg$currPos))) { + s3 = input.charAt(peg$currPos); + peg$currPos++; + } else { + s3 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e34); } + } + } + s1 = [s1, s2]; + s0 = s1; + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parsecat() { + var s0, s1, s2, s3, s4, s5, s6, s7, s8, s9; + + s0 = peg$currPos; + if (input.substr(peg$currPos, 3) === peg$c27) { + s1 = peg$c27; + peg$currPos += 3; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e35); } + } + if (s1 !== peg$FAILED) { + s2 = peg$parsews(); + if (input.charCodeAt(peg$currPos) === 91) { + s3 = peg$c8; + peg$currPos++; + } else { + s3 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e15); } + } + if (s3 !== peg$FAILED) { + s4 = peg$parsews(); + s5 = peg$parsesequence_or_operator(); + if (s5 !== peg$FAILED) { + s6 = []; + s7 = peg$currPos; + s8 = peg$parsecomma(); + if (s8 !== peg$FAILED) { + s9 = peg$parsesequence_or_operator(); + if (s9 !== peg$FAILED) { + peg$savedPos = s7; + s7 = peg$f22(s5, s9); + } else { + peg$currPos = s7; + s7 = peg$FAILED; + } + } else { + peg$currPos = s7; + s7 = peg$FAILED; + } + while (s7 !== peg$FAILED) { + s6.push(s7); + s7 = peg$currPos; + s8 = peg$parsecomma(); + if (s8 !== peg$FAILED) { + s9 = peg$parsesequence_or_operator(); + if (s9 !== peg$FAILED) { + peg$savedPos = s7; + s7 = peg$f22(s5, s9); + } else { + peg$currPos = s7; + s7 = peg$FAILED; + } + } else { + peg$currPos = s7; + s7 = peg$FAILED; + } + } + s7 = peg$parsews(); + if (input.charCodeAt(peg$currPos) === 93) { + s8 = peg$c9; + peg$currPos++; + } else { + s8 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e16); } + } + if (s8 !== peg$FAILED) { + peg$savedPos = s0; + s0 = peg$f23(s5, s6); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parsesequence_or_group() { + var s0; + + s0 = peg$parsecat(); + if (s0 === peg$FAILED) { + s0 = peg$parsesequence(); + } + + return s0; + } + + function peg$parsesequence_or_operator() { + var s0, s1, s2, s3, s4, s5; + + s0 = peg$currPos; + s1 = peg$parsesequence_or_group(); + if (s1 !== peg$FAILED) { + s2 = peg$parsews(); + s3 = []; + s4 = peg$parsecomment(); + while (s4 !== peg$FAILED) { + s3.push(s4); + s4 = peg$parsecomment(); + } + peg$savedPos = s0; + s0 = peg$f24(s1); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + if (s0 === peg$FAILED) { + s0 = peg$currPos; + s1 = peg$parseoperator(); + if (s1 !== peg$FAILED) { + s2 = peg$parsews(); + if (input.charCodeAt(peg$currPos) === 36) { + s3 = peg$c28; + peg$currPos++; + } else { + s3 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e36); } + } + if (s3 !== peg$FAILED) { + s4 = peg$parsews(); + s5 = peg$parsesequence_or_operator(); + if (s5 !== peg$FAILED) { + peg$savedPos = s0; + s0 = peg$f25(s1, s5); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } + + return s0; + } + + function peg$parsesequ_or_operator_or_comment() { + var s0, s1; + + s0 = peg$currPos; + s1 = peg$parsesequence_or_operator(); + if (s1 !== peg$FAILED) { + peg$savedPos = s0; + s1 = peg$f26(s1); + } + s0 = s1; + if (s0 === peg$FAILED) { + s0 = peg$parsecomment(); + } + + return s0; + } + + function peg$parsesequence_definition() { + var s0; + + s0 = peg$parsesequ_or_operator_or_comment(); + + return s0; + } + + function peg$parsecommand() { + var s0, s1, s2, s3; + + s0 = peg$currPos; + s1 = peg$parsews(); + s2 = peg$parsesetcps(); + if (s2 === peg$FAILED) { + s2 = peg$parsesetbpm(); + if (s2 === peg$FAILED) { + s2 = peg$parsehush(); + } + } + if (s2 !== peg$FAILED) { + s3 = peg$parsews(); + peg$savedPos = s0; + s0 = peg$f27(s2); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parsesetcps() { + var s0, s1, s2, s3; + + s0 = peg$currPos; + if (input.substr(peg$currPos, 6) === peg$c29) { + s1 = peg$c29; + peg$currPos += 6; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e37); } + } + if (s1 !== peg$FAILED) { + s2 = peg$parsews(); + s3 = peg$parsenumber(); + if (s3 !== peg$FAILED) { + peg$savedPos = s0; + s0 = peg$f28(s3); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parsesetbpm() { + var s0, s1, s2, s3; + + s0 = peg$currPos; + if (input.substr(peg$currPos, 6) === peg$c30) { + s1 = peg$c30; + peg$currPos += 6; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e38); } + } + if (s1 !== peg$FAILED) { + s2 = peg$parsews(); + s3 = peg$parsenumber(); + if (s3 !== peg$FAILED) { + peg$savedPos = s0; + s0 = peg$f29(s3); + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parsehush() { + var s0, s1; + + s0 = peg$currPos; + if (input.substr(peg$currPos, 4) === peg$c31) { + s1 = peg$c31; + peg$currPos += 4; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$e39); } + } + if (s1 !== peg$FAILED) { + peg$savedPos = s0; + s1 = peg$f30(); + } + s0 = s1; + + return s0; + } + + function peg$parsestatement() { + var s0; + + s0 = peg$parsesequence_definition(); + if (s0 === peg$FAILED) { + s0 = peg$parsecommand(); + } + + return s0; + } + + + var PatternStub = function(source, alignment) + { + this.type_ = "pattern"; + this.arguments_ = { alignment : alignment}; + this.source_ = source; + } + + var OperatorStub = function(name, args, source) + { + this.type_ = name; + this.arguments_ = args; + this.source_ = source; + } + + var ElementStub = function(source, options) + { + this.type_ = "element"; + this.source_ = source; + this.options_ = options; + } + + var CommandStub = function(name, options) + { + this.type_ = "command"; + this.name_ = name; + this.options_ = options; + } + + + + peg$result = peg$startRuleFunction(); + + if (peg$result !== peg$FAILED && peg$currPos === input.length) { + return peg$result; + } else { + if (peg$result !== peg$FAILED && peg$currPos < input.length) { + peg$fail(peg$endExpectation()); + } + + throw peg$buildStructuredError( + peg$maxFailExpected, + peg$maxFailPos < input.length ? input.charAt(peg$maxFailPos) : null, + peg$maxFailPos < input.length + ? peg$computeLocation(peg$maxFailPos, peg$maxFailPos + 1) + : peg$computeLocation(peg$maxFailPos, peg$maxFailPos) + ); + } +} + +export { + peg$SyntaxError as SyntaxError, + peg$parse as parse +}; diff --git a/repl/krill.pegjs b/repl/krill.pegjs new file mode 100644 index 00000000..e32a9763 --- /dev/null +++ b/repl/krill.pegjs @@ -0,0 +1,207 @@ +// Some terminology: +// a sequence = a serie of elements placed between quotes +// a stack = a serie of vertically aligned slices sharing the same overall length +// a slice = a serie of horizontally aligned elements + + +{ + var PatternStub = function(source, alignment) + { + this.type_ = "pattern"; + this.arguments_ = { alignment : alignment}; + this.source_ = source; + } + + var OperatorStub = function(name, args, source) + { + this.type_ = name; + this.arguments_ = args; + this.source_ = source; + } + + var ElementStub = function(source, options) + { + this.type_ = "element"; + this.source_ = source; + this.options_ = options; + } + + var CommandStub = function(name, options) + { + this.type_ = "command"; + this.name_ = name; + this.options_ = options; + } + +} + +start = statement + +// ----- Numbers ----- + +number "number" + = minus? int frac? exp? { return parseFloat(text()); } + +decimal_point + = "." + +digit1_9 + = [1-9] + +e + = [eE] + +exp + = e (minus / plus)? DIGIT+ + +frac + = decimal_point DIGIT+ + +int + = zero / (digit1_9 DIGIT*) + +minus + = "-" + +plus + = "+" + +zero + = "0" + +DIGIT = [0-9] + +// ------------------ delimiters --------------------------- + +ws "whitespace" = [ \n\r\t]* +comma = ws "," ws; +quote = '"' / "'" + +// ------------------ steps and cycles --------------------------- + +// single step definition (e.g bd) +step_char = [0-9a-zA-Z~] / "-" / "#" / "." +step = ws chars:step_char+ ws { return chars.join("") } + +// define a sub cycle e.g. [1 2, 3 [4]] +sub_cycle = ws "[" ws s:stack ws "]" ws { return s} + +// define a timeline e.g <1 3 [3 5]>. We simply defer to a stack and change the alignement +timeline = ws "<" ws sc:single_cycle ws ">" ws + { sc.arguments_.alignment = "t"; return sc;} + +// a slice is either a single step or a sub cycle +slice = step / sub_cycle / timeline + +// slice modifier affects the timing/size of a slice (e.g. [a b c]@3) +// at this point, we assume we can represent them as regular sequence operators +slice_modifier = slice_weight / slice_bjorklund / slice_slow / slice_fast / slice_fixed_step + +slice_weight = "@" a:number + { return { weight: a} } + +slice_bjorklund = "(" ws p:number ws comma ws s:number ws")" + { return { operator : { type_: "bjorklund", arguments_ :{ pulse: p, step:s } } } } + +slice_slow = "/"a:number + { return { operator : { type_: "stretch", arguments_ :{ amount:a } } } } + +slice_fast = "*"a:number + { return { operator : { type_: "stretch", arguments_ :{ amount:"1/"+a } } } } + +slice_fixed_step = "%"a:number + { return { operator : { type_: "fixed-step", arguments_ :{ amount:a } } } } + +// a slice with an modifier applied i.e [bd@4 sd@3]@2 hh] +slice_with_modifier = s:slice o:slice_modifier? + { return new ElementStub(s, o);} + +// a single cycle is a combination of one or more successive slices (as an array). If we +// have only one element, we skip the array and return the element itself +single_cycle = s:(slice_with_modifier)+ + { return new PatternStub(s,"h"); } + +// a stack is a serie of vertically aligned single cycles, separated by a comma +// if the stack contains only one element, we don't create a stack but return the +// underlying element +stack = c:single_cycle cs:(comma v:single_cycle { return v})* + { if (cs.length == 0 && c instanceof Object) { return c;} else { cs.unshift(c); return new PatternStub(cs,"v");} } + +// a sequence is a quoted stack +sequence = ws quote s:stack quote + { return s; } + +// ------------------ operators --------------------------- + +operator = scale / slow / fast / target / bjorklund / struct / rotR / rotL + +struct = "struct" ws s:sequence_or_operator + { return { name: "struct", args: { sequence:s }}} + +target = "target" ws quote s:step quote + { return { name: "target", args : { name:s}}} + +bjorklund = "euclid" ws p:int ws s:int + { return { name: "bjorklund", args :{ pulse: parseInt(p), step:parseInt(s) }}} + +slow = "slow" ws a:number + { return { name: "stretch", args :{ amount: a}}} + +rotL = "rotL" ws a:number + { return { name: "shift", args :{ amount: "-"+a}}} + +rotR = "rotR" ws a:number + { return { name: "shift", args :{ amount: a}}} + +fast = "fast" ws a:number + { return { name: "stretch", args :{ amount: "1/"+a}}} + +scale = "scale" ws quote s:(step_char)+ quote +{ return { name: "scale", args :{ scale: s.join("")}}} + +comment = '//' p:([^\n]*) + +// ---------------- grouping -------------------------------- + +group_operator = cat + +// cat is another form of timeline +cat = "cat" ws "[" ws s:sequence_or_operator ss:(comma v:sequence_or_operator { return v})* ws "]" + { ss.unshift(s); return new PatternStub(ss,"t"); } + +// ------------------ high level sequence --------------------------- + +sequence_or_group = + group_operator / + sequence + +sequence_or_operator = + sg:sequence_or_group ws (comment)* + {return sg} + / o:operator ws "$" ws soc:sequence_or_operator + { return new OperatorStub(o.name,o.args,soc)} + +sequ_or_operator_or_comment = + sc: sequence_or_operator + { return sc } + / comment + +sequence_definition = s:sequ_or_operator_or_comment + +// ---------------------- statements ---------------------------- + +command = ws c:(setcps / setbpm / hush) ws + { return c } + +setcps = "setcps" ws v:number + { return new CommandStub("setcps", { value: v})} + +setbpm = "setbpm" ws v:number + { return new CommandStub("setcps", { value: (v/120/2)})} + +hush = "hush" + { return new CommandStub("hush")} + +// ---------------------- statements ---------------------------- + +statement = sequence_definition / command diff --git a/repl/package-lock.json b/repl/package-lock.json index 6ffdd90a..a08c11c7 100644 --- a/repl/package-lock.json +++ b/repl/package-lock.json @@ -25,6 +25,8 @@ "@web/test-runner": "^0.13.3", "autoprefixer": "^10.4.2", "chai": "^4.3.4", + "peggy": "^1.2.0", + "pegjs": "^0.10.0", "postcss": "^8.4.6", "prettier": "^2.2.1", "snowpack": "^3.3.7", @@ -6067,6 +6069,30 @@ "node": "*" } }, + "node_modules/peggy": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/peggy/-/peggy-1.2.0.tgz", + "integrity": "sha512-PQ+NKpAobImfMprYQtc4Egmyi29bidRGEX0kKjCU5uuW09s0Cthwqhfy7mLkwcB4VcgacE5L/ZjruD/kOPCUUw==", + "dev": true, + "bin": { + "peggy": "bin/peggy" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/pegjs": { + "version": "0.10.0", + "resolved": "https://registry.npmjs.org/pegjs/-/pegjs-0.10.0.tgz", + "integrity": "sha1-z4uvrm7d/0tafvsYUmnqr0YQ3b0=", + "dev": true, + "bin": { + "pegjs": "bin/pegjs" + }, + "engines": { + "node": ">=0.10" + } + }, "node_modules/pend": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz", @@ -13113,6 +13139,18 @@ "integrity": "sha512-Dp6zGqpTdETdR63lehJYPeIOqpiNBNtc7BpWSLrOje7UaIsE5aY92r/AunQA7rsXvet3lrJ3JnZX29UPTKXyKQ==", "dev": true }, + "peggy": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/peggy/-/peggy-1.2.0.tgz", + "integrity": "sha512-PQ+NKpAobImfMprYQtc4Egmyi29bidRGEX0kKjCU5uuW09s0Cthwqhfy7mLkwcB4VcgacE5L/ZjruD/kOPCUUw==", + "dev": true + }, + "pegjs": { + "version": "0.10.0", + "resolved": "https://registry.npmjs.org/pegjs/-/pegjs-0.10.0.tgz", + "integrity": "sha1-z4uvrm7d/0tafvsYUmnqr0YQ3b0=", + "dev": true + }, "pend": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz", diff --git a/repl/package.json b/repl/package.json index eabaec9d..e6c8dc39 100644 --- a/repl/package.json +++ b/repl/package.json @@ -4,7 +4,8 @@ "build": "snowpack build && cp ./public/.nojekyll ../docs", "test": "web-test-runner \"src/**/*.test.tsx\"", "format": "prettier --write \"src/**/*.{js,jsx,ts,tsx}\"", - "lint": "prettier --check \"src/**/*.{js,jsx,ts,tsx}\"" + "lint": "prettier --check \"src/**/*.{js,jsx,ts,tsx}\"", + "peggy": "peggy -o krill-parser.js --format es ./krill.pegjs" }, "dependencies": { "react": "^17.0.2", @@ -27,6 +28,7 @@ "@web/test-runner": "^0.13.3", "autoprefixer": "^10.4.2", "chai": "^4.3.4", + "peggy": "^1.2.0", "postcss": "^8.4.6", "prettier": "^2.2.1", "snowpack": "^3.3.7", diff --git a/repl/src/App.tsx b/repl/src/App.tsx index c2770173..18d9e797 100644 --- a/repl/src/App.tsx +++ b/repl/src/App.tsx @@ -6,15 +6,11 @@ import * as Tone from 'tone'; import useCycle from './useCycle'; import type { Hap, Pattern } from './types'; import { tetris } from './tunes'; +import _mini from './mini'; -const { Fraction, TimeSpan } = strudel; - -const fr = (v: number) => new Fraction(v); -const ts = (start: number, end: number) => new TimeSpan(fr(start), fr(end)); -const parse = (code: string): Pattern => { - const { sequence, pure, reify, slowcat, fastcat, cat, stack, silence } = strudel; // make available to eval - return eval(code); -}; +const { sequence, pure, reify, slowcat, fastcat, cat, stack, silence } = strudel; // make available to eval +const mini = _mini; // for eval (direct import wont work somehow) +const parse = (code: string): Pattern => eval(code); const synth = new Tone.PolySynth().toDestination(); synth.set({ diff --git a/repl/src/mini.ts b/repl/src/mini.ts new file mode 100644 index 00000000..b56baf8c --- /dev/null +++ b/repl/src/mini.ts @@ -0,0 +1,34 @@ +import * as krill from '../krill-parser'; +import * as strudel from '../../strudel.mjs'; + +export function patternifyAST(ast: any): any { + switch (ast.type_) { + case 'pattern': + return strudel.sequence(...ast.source_.map(patternifyAST)); + case 'element': + if (typeof ast.source_ !== 'object') { + return ast.source_; + } + return patternifyAST(ast.source_); + } +} +export default (str: string) => patternifyAST(krill.parse(`"${str}"`)); + +/* +TODO: +export interface Arguments { + alignment: string; +} + +export interface ElementStub { + type_: string; + source_: string; + options_?: any; +} + +export interface PatternStub { + type_: string; // pattern + arguments_: Arguments; + source_: ElementStub[]; +} + */