diff --git a/.gitignore b/.gitignore index eaa96d8e..5703c891 100755 --- a/.gitignore +++ b/.gitignore @@ -2,9 +2,6 @@ node_modules package-lock.json -# Noise Parser -noiseParser.js - # macOS .DS_Store diff --git a/src/parser/noiseParser.js b/src/parser/noiseParser.js new file mode 100644 index 00000000..784dbe0f --- /dev/null +++ b/src/parser/noiseParser.js @@ -0,0 +1,1235 @@ +/* + * Generated by PEG.js 0.10.0. + * + * http://pegjs.org/ + */ + +"use strict"; + +function peg$subclass(child, parent) { + function ctor() { this.constructor = child; } + ctor.prototype = parent.prototype; + child.prototype = new ctor(); +} + +function peg$SyntaxError(message, expected, found, location) { + this.message = message; + this.expected = expected; + this.found = found; + this.location = location; + this.name = "SyntaxError"; + + if (typeof Error.captureStackTrace === "function") { + Error.captureStackTrace(this, peg$SyntaxError); + } +} + +peg$subclass(peg$SyntaxError, Error); + +peg$SyntaxError.buildMessage = function(expected, found) { + var DESCRIBE_EXPECTATION_FNS = { + literal: function(expectation) { + return "\"" + literalEscape(expectation.text) + "\""; + }, + + "class": function(expectation) { + var escapedParts = "", + i; + + for (i = 0; i < expectation.parts.length; i++) { + escapedParts += expectation.parts[i] instanceof Array + ? classEscape(expectation.parts[i][0]) + "-" + classEscape(expectation.parts[i][1]) + : classEscape(expectation.parts[i]); + } + + return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]"; + }, + + any: function(expectation) { + return "any character"; + }, + + end: function(expectation) { + 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 = new Array(expected.length), + i, j; + + for (i = 0; i < expected.length; i++) { + descriptions[i] = describeExpectation(expected[i]); + } + + 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 !== void 0 ? options : {}; + + var peg$FAILED = {}, + + peg$startRuleFunctions = { Pattern: peg$parsePattern }, + peg$startRuleFunction = peg$parsePattern, + + peg$c0 = ":", + peg$c1 = peg$literalExpectation(":", false), + peg$c2 = function(Name, PreMessages, Messages) { + let pattern = { + name: Name, + preMessages: [], + messages: Messages + }; + pattern.preMessages = PreMessages? PreMessages : []; + check.preMessages(pattern); + check.messages(pattern); + check.psk(pattern); + check.transportMessages(pattern); + return pattern; + }, + peg$c3 = /^[a-zA-Z0-9]/, + peg$c4 = peg$classExpectation([["a", "z"], ["A", "Z"], ["0", "9"]], false, false), + peg$c5 = function() { + if (text().length > 16) { + error(errMsg.tooLongName); + } else { + return text(); + } + }, + peg$c6 = /^[ \t\n\r]/, + peg$c7 = peg$classExpectation([" ", "\t", "\n", "\r"], false, false), + peg$c8 = function() { + return text(); + }, + peg$c9 = "...", + peg$c10 = peg$literalExpectation("...", false), + peg$c11 = function() { + return (text().length > 0) + }, + peg$c12 = "->", + peg$c13 = peg$literalExpectation("->", false), + peg$c14 = function() { + return 'send'; + }, + peg$c15 = "<-", + peg$c16 = peg$literalExpectation("<-", false), + peg$c17 = function() { + return 'recv'; + }, + peg$c18 = "psk", + peg$c19 = peg$literalExpectation("psk", false), + peg$c20 = "ss", + peg$c21 = peg$literalExpectation("ss", false), + peg$c22 = "se", + peg$c23 = peg$literalExpectation("se", false), + peg$c24 = "es", + peg$c25 = peg$literalExpectation("es", false), + peg$c26 = "ee", + peg$c27 = peg$literalExpectation("ee", false), + peg$c28 = "s", + peg$c29 = peg$literalExpectation("s", false), + peg$c30 = "e", + peg$c31 = peg$literalExpectation("e", false), + peg$c32 = "e, s", + peg$c33 = peg$literalExpectation("e, s", false), + peg$c34 = ", ", + peg$c35 = peg$literalExpectation(", ", false), + peg$c36 = ",", + peg$c37 = peg$literalExpectation(",", false), + peg$c38 = function() { + let normalized = text().replace(/\,\s/g, ','); + return normalized.split(','); + }, + peg$c39 = function(Dir, Token) { + return { + type: 'PreMessage', + dir: Dir, + tokens: Token + }; + }, + peg$c40 = function(PreMessages) { + let pMsg = [PreMessages[0][0]]; + if (!Array.isArray(PreMessages[1])) { + // Do nothing. + } else if (PreMessages[1][0].dir === 'recv') { + pMsg.push(PreMessages[1][0]); + } else if (PreMessages[1][0].dir === 'send') { + pMsg.unshift(PreMessages[1][0]); + } + return pMsg; + }, + peg$c41 = function(Dir, Tokens) { + return { + type: 'Message', + dir: Dir, + tokens: Tokens? Tokens : [] + }; + }, + peg$c42 = function(Messages) { + let msgs = []; + Messages.forEach((msg, i) => { + msgs.push(msg[0]); + }); + return msgs; + }, + + peg$currPos = 0, + peg$savedPos = 0, + peg$posDetailsCache = [{ line: 1, column: 1 }], + peg$maxFailPos = 0, + peg$maxFailExpected = [], + peg$silentFails = 0, + + 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 location() { + return peg$computeLocation(peg$savedPos, peg$currPos); + } + + function expected(description, location) { + location = location !== void 0 ? 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 !== void 0 ? 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], 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), + endPosDetails = peg$computePosDetails(endPos); + + return { + 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$parsePattern() { + var s0, s1, s2, s3, s4, s5, s6; + + s0 = peg$currPos; + s1 = peg$parseIdentifier(); + if (s1 !== peg$FAILED) { + if (input.charCodeAt(peg$currPos) === 58) { + s2 = peg$c0; + peg$currPos++; + } else { + s2 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c1); } + } + if (s2 !== peg$FAILED) { + s3 = peg$parse_(); + if (s3 !== peg$FAILED) { + s4 = peg$parsePreMessages(); + if (s4 === peg$FAILED) { + s4 = null; + } + if (s4 !== peg$FAILED) { + s5 = peg$parse_(); + if (s5 !== peg$FAILED) { + s6 = peg$parseMessages(); + if (s6 !== peg$FAILED) { + peg$savedPos = s0; + s1 = peg$c2(s1, s4, s6); + s0 = s1; + } 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; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parseIdentifier() { + var s0, s1, s2; + + s0 = peg$currPos; + s1 = []; + if (peg$c3.test(input.charAt(peg$currPos))) { + s2 = input.charAt(peg$currPos); + peg$currPos++; + } else { + s2 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c4); } + } + if (s2 !== peg$FAILED) { + while (s2 !== peg$FAILED) { + s1.push(s2); + if (peg$c3.test(input.charAt(peg$currPos))) { + s2 = input.charAt(peg$currPos); + peg$currPos++; + } else { + s2 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c4); } + } + } + } else { + s1 = peg$FAILED; + } + if (s1 !== peg$FAILED) { + peg$savedPos = s0; + s1 = peg$c5(); + } + s0 = s1; + + return s0; + } + + function peg$parse_() { + var s0, s1, s2; + + s0 = peg$currPos; + s1 = []; + if (peg$c6.test(input.charAt(peg$currPos))) { + s2 = input.charAt(peg$currPos); + peg$currPos++; + } else { + s2 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c7); } + } + while (s2 !== peg$FAILED) { + s1.push(s2); + if (peg$c6.test(input.charAt(peg$currPos))) { + s2 = input.charAt(peg$currPos); + peg$currPos++; + } else { + s2 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c7); } + } + } + if (s1 !== peg$FAILED) { + peg$savedPos = s0; + s1 = peg$c8(); + } + s0 = s1; + + return s0; + } + + function peg$parseEllipsis() { + var s0, s1, s2, s3; + + s0 = peg$currPos; + s1 = peg$parse_(); + if (s1 !== peg$FAILED) { + if (input.substr(peg$currPos, 3) === peg$c9) { + s2 = peg$c9; + peg$currPos += 3; + } else { + s2 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c10); } + } + if (s2 !== peg$FAILED) { + s3 = peg$parse_(); + if (s3 !== peg$FAILED) { + peg$savedPos = s0; + s1 = peg$c11(); + s0 = s1; + } 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$parseArrow() { + var s0, s1; + + s0 = peg$currPos; + if (input.substr(peg$currPos, 2) === peg$c12) { + s1 = peg$c12; + peg$currPos += 2; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c13); } + } + if (s1 !== peg$FAILED) { + peg$savedPos = s0; + s1 = peg$c14(); + } + s0 = s1; + if (s0 === peg$FAILED) { + s0 = peg$currPos; + if (input.substr(peg$currPos, 2) === peg$c15) { + s1 = peg$c15; + peg$currPos += 2; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c16); } + } + if (s1 !== peg$FAILED) { + peg$savedPos = s0; + s1 = peg$c17(); + } + s0 = s1; + } + + return s0; + } + + function peg$parseToken() { + var s0, s1; + + s0 = peg$currPos; + if (input.substr(peg$currPos, 3) === peg$c18) { + s1 = peg$c18; + peg$currPos += 3; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c19); } + } + if (s1 === peg$FAILED) { + if (input.substr(peg$currPos, 2) === peg$c20) { + s1 = peg$c20; + peg$currPos += 2; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c21); } + } + if (s1 === peg$FAILED) { + if (input.substr(peg$currPos, 2) === peg$c22) { + s1 = peg$c22; + peg$currPos += 2; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c23); } + } + if (s1 === peg$FAILED) { + if (input.substr(peg$currPos, 2) === peg$c24) { + s1 = peg$c24; + peg$currPos += 2; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c25); } + } + if (s1 === peg$FAILED) { + 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$c27); } + } + if (s1 === peg$FAILED) { + if (input.charCodeAt(peg$currPos) === 115) { + s1 = peg$c28; + peg$currPos++; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c29); } + } + if (s1 === peg$FAILED) { + if (input.charCodeAt(peg$currPos) === 101) { + s1 = peg$c30; + peg$currPos++; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c31); } + } + } + } + } + } + } + } + if (s1 !== peg$FAILED) { + peg$savedPos = s0; + s1 = peg$c8(); + } + s0 = s1; + + return s0; + } + + function peg$parsePreMessageToken() { + var s0, s1; + + s0 = peg$currPos; + if (input.substr(peg$currPos, 4) === peg$c32) { + s1 = peg$c32; + peg$currPos += 4; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c33); } + } + if (s1 === peg$FAILED) { + if (input.charCodeAt(peg$currPos) === 101) { + s1 = peg$c30; + peg$currPos++; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c31); } + } + if (s1 === peg$FAILED) { + if (input.charCodeAt(peg$currPos) === 115) { + s1 = peg$c28; + peg$currPos++; + } else { + s1 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c29); } + } + } + } + if (s1 !== peg$FAILED) { + peg$savedPos = s0; + s1 = peg$c8(); + } + s0 = s1; + + return s0; + } + + function peg$parseTokens() { + var s0, s1, s2, s3, s4; + + s0 = peg$currPos; + s1 = []; + s2 = peg$currPos; + s3 = peg$parseToken(); + if (s3 !== peg$FAILED) { + if (input.substr(peg$currPos, 2) === peg$c34) { + s4 = peg$c34; + peg$currPos += 2; + } else { + s4 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c35); } + } + if (s4 === peg$FAILED) { + if (input.charCodeAt(peg$currPos) === 44) { + s4 = peg$c36; + peg$currPos++; + } else { + s4 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c37); } + } + } + if (s4 !== peg$FAILED) { + s3 = [s3, s4]; + s2 = s3; + } else { + peg$currPos = s2; + s2 = peg$FAILED; + } + } else { + peg$currPos = s2; + s2 = peg$FAILED; + } + while (s2 !== peg$FAILED) { + s1.push(s2); + s2 = peg$currPos; + s3 = peg$parseToken(); + if (s3 !== peg$FAILED) { + if (input.substr(peg$currPos, 2) === peg$c34) { + s4 = peg$c34; + peg$currPos += 2; + } else { + s4 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c35); } + } + if (s4 === peg$FAILED) { + if (input.charCodeAt(peg$currPos) === 44) { + s4 = peg$c36; + peg$currPos++; + } else { + s4 = peg$FAILED; + if (peg$silentFails === 0) { peg$fail(peg$c37); } + } + } + if (s4 !== peg$FAILED) { + s3 = [s3, s4]; + s2 = s3; + } else { + peg$currPos = s2; + s2 = peg$FAILED; + } + } else { + peg$currPos = s2; + s2 = peg$FAILED; + } + } + if (s1 !== peg$FAILED) { + s2 = peg$parseToken(); + if (s2 !== peg$FAILED) { + peg$savedPos = s0; + s1 = peg$c38(); + s0 = s1; + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + } else { + peg$currPos = s0; + s0 = peg$FAILED; + } + + return s0; + } + + function peg$parsePreMessage() { + var s0, s1, s2, s3, s4; + + s0 = peg$currPos; + s1 = peg$parse_(); + if (s1 !== peg$FAILED) { + s2 = peg$parseArrow(); + if (s2 !== peg$FAILED) { + s3 = peg$parse_(); + if (s3 !== peg$FAILED) { + s4 = peg$parsePreMessageToken(); + if (s4 !== peg$FAILED) { + peg$savedPos = s0; + s1 = peg$c39(s2, s4); + s0 = s1; + } 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$parsePreMessages() { + var s0, s1, s2, s3, s4, s5; + + s0 = peg$currPos; + s1 = peg$currPos; + s2 = peg$currPos; + s3 = peg$parsePreMessage(); + if (s3 !== peg$FAILED) { + s4 = peg$parse_(); + if (s4 !== peg$FAILED) { + s3 = [s3, s4]; + s2 = s3; + } else { + peg$currPos = s2; + s2 = peg$FAILED; + } + } else { + peg$currPos = s2; + s2 = peg$FAILED; + } + if (s2 !== peg$FAILED) { + s3 = peg$currPos; + s4 = peg$parsePreMessage(); + if (s4 !== peg$FAILED) { + s5 = peg$parse_(); + if (s5 !== peg$FAILED) { + s4 = [s4, s5]; + s3 = s4; + } else { + peg$currPos = s3; + s3 = peg$FAILED; + } + } else { + peg$currPos = s3; + s3 = peg$FAILED; + } + if (s3 === peg$FAILED) { + s3 = null; + } + if (s3 !== peg$FAILED) { + s4 = peg$parseEllipsis(); + if (s4 !== peg$FAILED) { + s2 = [s2, s3, s4]; + s1 = s2; + } else { + peg$currPos = s1; + s1 = peg$FAILED; + } + } else { + peg$currPos = s1; + s1 = peg$FAILED; + } + } else { + peg$currPos = s1; + s1 = peg$FAILED; + } + if (s1 !== peg$FAILED) { + peg$savedPos = s0; + s1 = peg$c40(s1); + } + s0 = s1; + + return s0; + } + + function peg$parseMessage() { + var s0, s1, s2, s3, s4; + + s0 = peg$currPos; + s1 = peg$parse_(); + if (s1 !== peg$FAILED) { + s2 = peg$parseArrow(); + if (s2 !== peg$FAILED) { + s3 = peg$parse_(); + if (s3 !== peg$FAILED) { + s4 = peg$parseTokens(); + if (s4 === peg$FAILED) { + s4 = null; + } + if (s4 !== peg$FAILED) { + peg$savedPos = s0; + s1 = peg$c41(s2, s4); + s0 = s1; + } 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$parseMessages() { + var s0, s1, s2, s3, s4; + + s0 = peg$currPos; + s1 = []; + s2 = peg$currPos; + s3 = peg$parseMessage(); + if (s3 !== peg$FAILED) { + s4 = peg$parse_(); + if (s4 !== peg$FAILED) { + s3 = [s3, s4]; + s2 = s3; + } else { + peg$currPos = s2; + s2 = peg$FAILED; + } + } else { + peg$currPos = s2; + s2 = peg$FAILED; + } + if (s2 !== peg$FAILED) { + while (s2 !== peg$FAILED) { + s1.push(s2); + s2 = peg$currPos; + s3 = peg$parseMessage(); + if (s3 !== peg$FAILED) { + s4 = peg$parse_(); + if (s4 !== peg$FAILED) { + s3 = [s3, s4]; + s2 = s3; + } else { + peg$currPos = s2; + s2 = peg$FAILED; + } + } else { + peg$currPos = s2; + s2 = peg$FAILED; + } + } + } else { + s1 = peg$FAILED; + } + if (s1 !== peg$FAILED) { + peg$savedPos = s0; + s1 = peg$c42(s1); + } + s0 = s1; + + return s0; + } + + + const g = { + s: 0, + e: 0, + rs: 0, + re: 0, + ss: 0, + se: 0, + es: 0, + ee: 0 + }; + + const util = { + hasDuplicates: (arr) => { + let vo = {}; + for (let i = 0; i < arr.length; ++i) { + let v = arr[i]; + if (v in vo) { return true; } + vo[v] = true; + } + return false; + } + }; + + const errMsg = { + // Below are validity rules specific to Noise Explorer and not to the Noise Protocol Framework. + tooLongName: 'Noise Handshake Pattern names with a maximum length of 16 characters are currently supported.', + tooManyTokens: 'Noise Message Patterns with a maximum of 8 tokens are currently supported.', + tooManyMessages: 'Noise Handshake Patterns with a maximum of 8 message patterns are currently supported.', + moreThanOnePsk: 'Noise Handshake Patterns with a maximum of one PSK are currently supported.', + // Below are validity rules which we are not exactly sure are shared by the Noise Protocol Framework (but likely are.) + tokenOrderIncorrect: 'Public key tokens must be ordered in a message to appear before Diffie-Hellman operations that implicate them.', + transportNotLast: 'Noise Handshake Patterns can only contain transport handshake messages at the very bottom of the pattern.', + transportOnly: 'Noise Handshake Patterns cannot consist purely of transport messages.', + unusedKeySent: 'Noise Handshake Patterns should not contain key shares that are not subsequently used in any Diffie-Hellman operation.', + // Below are validity rules shared by the Noise Protocol Framework. + dupTokens: 'Noise Handshake Patterns must not contain duplicate tokens in the same message flight.', + keySentMoreThanOnce: 'Principals must not send their static public key or ephemeral public key more than once per handshake.', + dhSentMoreThanOnce: 'Principals must not perform the same Diffie-Hellman key agreement more than once per handshake.', + wrongPskModifier: 'PSK modifiers must correctly indicate the position of PSK tokens.', + wrongPskLocation: 'PSK tokens must appear at the beginning or end of the first handshake message or at the end of any other handshake message.', + pskNotAtEndOfName: 'PSK modifiers must appear at the end of the Noise Handshake Pattern name.', + wrongMessageDir: 'Message direction within a Noise Handshake Pattern must alternate (initiator -> responder, initiator <- responder), with the first message being sent by the initiator.', + dhWithUnknownKey: 'Principals cannot perform a Diffie-Hellman operation with a key share that does not exist.', + seEeRule: 'After an se token, the initiator must not send a handshake payload or transport payload unless there has also been an ee token.', + ssEsRule: 'After an ss token, the initiator must not send a handshake payload or transport payload unless there has also been an es token.', + esEeRule: 'After an es token, the responder must not send a handshake payload or transport payload unless there has also been an ee token.', + ssSeRule: 'After an ss token, the responder must not send a handshake payload or transport payload unless there has also been an se token.' + }; + + const check = { + preMessages: (pattern) => { + pattern.preMessages.forEach((preMessage) => { + if (preMessage.tokens.indexOf('s') >= 0) { + (preMessage.dir === 'send')? g.s++ : g.rs++; + } + if (preMessage.tokens.indexOf('e') >= 0) { + (preMessage.dir === 'send')? g.e++ : g.re++; + } + }); + }, + messages: (pattern) => { + if (pattern.messages.length > 8) { + error(errMsg.tooManyMessages); + } + pattern.messages.forEach((message, i) => { + if ( + ((i % 2) && (message.dir === 'send')) || + (!(i % 2) && (message.dir === 'recv')) + ) { + error(errMsg.wrongMessageDir); + } + if (util.hasDuplicates(message.tokens)) { + error(errMsg.dupTokens); + } + if (message.tokens.length > 8) { + error(errMsg.tooManyTokens); + } + if (message.tokens.indexOf('s') >= 0) { + if (((message.dir === 'send') && ( + ( + (message.tokens.indexOf('se') >= 0) && + (message.tokens.indexOf('se') < message.tokens.indexOf('s')) + ) || ( + (message.tokens.indexOf('ss') >= 0) && + (message.tokens.indexOf('ss') < message.tokens.indexOf('s')) + ) + )) || ((message.dir === 'recv') && ( + ( + (message.tokens.indexOf('es') >= 0) && + (message.tokens.indexOf('es') < message.tokens.indexOf('s')) + ) || ( + (message.tokens.indexOf('ss') >= 0) && + (message.tokens.indexOf('ss') < message.tokens.indexOf('s')) + ) + ))) { + error(errMsg.tokenOrderIncorrect); + } + (message.dir === 'send')? g.s++ : g.rs++; + } + if (message.tokens.indexOf('e') >= 0) { + if (((message.dir === 'send') && ( + ( + (message.tokens.indexOf('es') >= 0) && + (message.tokens.indexOf('es') < message.tokens.indexOf('e')) + ) || ( + (message.tokens.indexOf('ee') >= 0) && + (message.tokens.indexOf('ee') < message.tokens.indexOf('e')) + ) + )) || ((message.dir === 'recv') && ( + ( + (message.tokens.indexOf('se') >= 0) && + (message.tokens.indexOf('se') < message.tokens.indexOf('e')) + ) || ( + (message.tokens.indexOf('ee') >= 0) && + (message.tokens.indexOf('ee') < message.tokens.indexOf('e')) + ) + ))) { + error(errMsg.tokenOrderIncorrect); + } + (message.dir === 'send')? g.e++ : g.re++; + } + if (message.tokens.indexOf('ss') >= 0) { + g.ss++; + if ( + (message.dir === 'send') && + (g.es === 0) && + (message.tokens.indexOf('es') < 0) + ) { + error(errMsg.ssEsRule); + } + if ( + (message.dir === 'recv') && + (g.se === 0) && + (message.tokens.indexOf('se') < 0) + ) { + error(errMsg.ssSeRule); + } + } + if (message.tokens.indexOf('se') >= 0) { + g.se++; + if ( + (message.dir === 'send') && + (g.ee === 0) && + (message.tokens.indexOf('ee') < 0) + ) { + error(errMsg.seEeRule); + } + } + if (message.tokens.indexOf('es') >= 0) { + g.es++; + if ( + (message.dir === 'recv') && + (g.ee === 0) && + (message.tokens.indexOf('ee') < 0) + ) { + error(errMsg.esEeRule); + } + } + if (message.tokens.indexOf('ee') >= 0) { + g.ee++; + } + if ( + (g.ss && (!g.s || !g.rs)) || + (g.se && (!g.s || !g.re)) || + (g.es && (!g.e || !g.rs)) || + (g.ee && (!g.e || !g.re)) + ) { + error(errMsg.dhWithUnknownKey); + } + }); + if ( + (g.s && (!g.ss && !g.se)) || + (g.e && (!g.es && !g.ee)) || + (g.rs && (!g.ss && !g.es)) || + (g.re && (!g.se && !g.ee)) + ) { + error(errMsg.unusedKeySent); + } + if ( + (g.s > 1) || (g.e > 1) || + (g.rs > 1) || (g.re > 1) + ) { + error(errMsg.keySentMoreThanOnce); + } + if ( + (g.ee > 1) || (g.es > 1) || + (g.se > 1) || (g.ss > 1) + ) { + error(errMsg.dhSentMoreThanOnce); + } + }, + psk: (pattern) => { + let pskMods = pattern.name.match(/psk\d/g); + if (!pskMods) { + pattern.messages.forEach((message) => { + if (message.tokens.indexOf('psk') >= 0) { + error(errMsg.wrongPskModifier); + } + }); + return false; + } + if (pskMods.length > 1) { + error(errMsg.moreThanOnePsk); + } + if (!/psk\d$/.test(pattern.name)) { + error(errMsg.pskNotAtEndOfName); + } + pskMods.forEach((pskMod) => { + pskMod = parseInt(pskMod.charAt(3), 10); + if (pskMod > pattern.messages.length) { + error(errMsg.wrongPskModifier); + } else if (pskMod === 0) { + let tokens = pattern.messages[pskMod].tokens; + if (tokens.indexOf('psk') < 0) { + error(errMsg.wrongPskModifier); + } else if (tokens.indexOf('psk') > 0) { + error(errMsg.wrongPskLocation); + } + } else { + let tokens = pattern.messages[pskMod - 1].tokens; + if (tokens.indexOf('psk') < 0) { + error(errMsg.wrongPskModifier); + } else if (tokens.indexOf('psk') !== (tokens.length - 1)) { + (pskMod === 1)? error(errMsg.wrongPskModifier) : error(errMsg.wrongPskLocation); + } + } + }); + }, + transportMessages: (pattern) => { + let transportMessage = -1; + pattern.messages.forEach((message, i) => { + if ( + (message.tokens.length === 0) && + (transportMessage === -1) + ) { + transportMessage = i; + } + if ( + (message.tokens.length > 0) && + (transportMessage >= 0) && + (i > transportMessage) + ) { + error(errMsg.transportNotLast); + } + }); + if (pattern.messages[0].tokens.length === 0) { + error(errMsg.transportOnly); + } + } + }; + + + 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) + ); + } +} + +module.exports = { + SyntaxError: peg$SyntaxError, + parse: peg$parse +};