From 22d2ac8ac2c63be8f488d004b350342673669a54 Mon Sep 17 00:00:00 2001 From: David Majda Date: Sat, 20 Mar 2010 11:24:04 +0100 Subject: [PATCH] Rewrote implementation of classes to be regexp-based. --- lib/compiler.js | 25 + lib/metagrammar.js | 2059 +++++++------------------------------- lib/metagrammar.pegjs | 33 +- lib/runtime.js | 35 + test/metagrammar-test.js | 23 +- test/runtime-test.js | 13 + 6 files changed, 440 insertions(+), 1748 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index d987b11..5f32036 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -82,6 +82,8 @@ PEG.Grammar.Rule.prototype = { PEG.Grammar.Literal = function(value) { this._value = value; }; +PEG.Grammar.Class = function(characters) { this._characters = characters; }; + PEG.Grammar.Any = function() {}; PEG.Grammar.Sequence = function(elements) { this._elements = elements; }; @@ -111,6 +113,8 @@ PEG.Grammar.Rule.prototype.checkReferencedRulesExist = function(grammar) { PEG.Grammar.Literal.prototype.checkReferencedRulesExist = function(grammar) {}; +PEG.Grammar.Class.prototype.checkReferencedRulesExist = function(grammar) {}; + PEG.Grammar.Any.prototype.checkReferencedRulesExist = function(grammar) {}; PEG.Grammar.Sequence.prototype.checkReferencedRulesExist = function(grammar) { @@ -151,6 +155,8 @@ PEG.Grammar.Rule.prototype.checkNoLeftRecursion = function(grammar, appliedRules PEG.Grammar.Literal.prototype.checkNoLeftRecursion = function(grammar, appliedRules) {}; +PEG.Grammar.Class.prototype.checkNoLeftRecursion = function(grammar, appliedRules) {}; + PEG.Grammar.Any.prototype.checkNoLeftRecursion = function(grammar, appliedRules) {}; PEG.Grammar.Sequence.prototype.checkNoLeftRecursion = function(grammar, appliedRules) { @@ -418,6 +424,25 @@ PEG.Grammar.Literal.prototype.compile = function(resultVar) { ); }; +PEG.Grammar.Class.prototype.compile = function(resultVar) { + return PEG.Compiler.formatCode( + "if (this._input.substr(this._pos).match(${regexp}) !== null) {", + " var ${resultVar} = this._input[this._pos];", + " this._pos++;", + "} else {", + " var ${resultVar} = null;", + " if (context.reportMatchFailures) {", + " this._matchFailed(new PEG.Parser.ClassMatchFailure(${characters|string}));", + " }", + "}", + { + characters: this._characters, + regexp: "/^[" + this._characters + "]/", + resultVar: resultVar + } + ); +}; + PEG.Grammar.Any.prototype.compile = function(resultVar) { return PEG.Compiler.formatCode( "if (this._input.length > this._pos) {", diff --git a/lib/metagrammar.js b/lib/metagrammar.js index 490620b..ad63540 100644 --- a/lib/metagrammar.js +++ b/lib/metagrammar.js @@ -558,14 +558,7 @@ PEG.grammarParser = (function(){ } else { var result64 = this._parse_class(context); var result63 = result64 !== null - ? (function() { - return new PEG.Grammar.Choice( - PEG.ArrayUtils.map( - (arguments[0]).split(""), - function(character) { return new PEG.Grammar.Literal(character); } - ) - ); - }).call(this, result64) + ? (function() { return new PEG.Grammar.Class((arguments[0])); }).call(this, result64) : null; if (result63 !== null) { var result57 = result63; @@ -2083,19 +2076,19 @@ PEG.grammarParser = (function(){ } var result213 = result214 !== null ? (function() { - var beginCharCode = (arguments[0]).charCodeAt(0); - var endCharCode = (arguments[2]).charCodeAt(0); - if (beginCharCode > endCharCode) { + if ((arguments[0]).charCodeAt(0) > (arguments[2]).charCodeAt(0)) { throw new PEG.Parser.SyntaxError( - "Invalid character range: " + (arguments[0]) + "-" + (arguments[2]) + "." + "Invalid character range: " + + PEG.RegExpUtils.quoteForClass((arguments[0])) + + "-" + + PEG.RegExpUtils.quoteForClass((arguments[2])) + + "." ); } - var result = ""; - for (var charCode = beginCharCode; charCode <= endCharCode; charCode++) { - result += String.fromCharCode(charCode); - } - return result; + return PEG.RegExpUtils.quoteForClass((arguments[0])) + + "-" + + PEG.RegExpUtils.quoteForClass((arguments[2])); }).apply(this, result214) : null; @@ -2119,7 +2112,12 @@ PEG.grammarParser = (function(){ var pos = this._pos; - var result218 = this._parse_bracketDelimitedCharacter(context); + var result219 = this._parse_bracketDelimitedCharacter(context); + var result218 = result219 !== null + ? (function() { + return PEG.RegExpUtils.quoteForClass((arguments[0])); + }).call(this, result219) + : null; @@ -2141,31 +2139,31 @@ PEG.grammarParser = (function(){ var pos = this._pos; - var result225 = this._parse_simpleBracketDelimitedCharacter(context); - if (result225 !== null) { - var result219 = result225; + var result226 = this._parse_simpleBracketDelimitedCharacter(context); + if (result226 !== null) { + var result220 = result226; } else { - var result224 = this._parse_simpleEscapeSequence(context); - if (result224 !== null) { - var result219 = result224; + var result225 = this._parse_simpleEscapeSequence(context); + if (result225 !== null) { + var result220 = result225; } else { - var result223 = this._parse_zeroEscapeSequence(context); - if (result223 !== null) { - var result219 = result223; + var result224 = this._parse_zeroEscapeSequence(context); + if (result224 !== null) { + var result220 = result224; } else { - var result222 = this._parse_hexEscapeSequence(context); - if (result222 !== null) { - var result219 = result222; + var result223 = this._parse_hexEscapeSequence(context); + if (result223 !== null) { + var result220 = result223; } else { - var result221 = this._parse_unicodeEscapeSequence(context); - if (result221 !== null) { - var result219 = result221; + var result222 = this._parse_unicodeEscapeSequence(context); + if (result222 !== null) { + var result220 = result222; } else { - var result220 = this._parse_eolEscapeSequence(context); - if (result220 !== null) { - var result219 = result220; + var result221 = this._parse_eolEscapeSequence(context); + if (result221 !== null) { + var result220 = result221; } else { - var result219 = null;; + var result220 = null;; }; }; }; @@ -2177,9 +2175,9 @@ PEG.grammarParser = (function(){ this._cache["bracketDelimitedCharacter"][pos] = { nextPos: this._pos, - result: result219 + result: result220 }; - return result219; + return result220; }; result._parse_simpleBracketDelimitedCharacter = function(context) { @@ -2198,75 +2196,75 @@ PEG.grammarParser = (function(){ var savedReportMatchFailuresVar4 = context.reportMatchFailures; context.reportMatchFailures = false; if (this._input.substr(this._pos, 1) === "]") { - var result233 = "]"; + var result234 = "]"; this._pos += 1; } else { - var result233 = null; + var result234 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.LiteralMatchFailure("]")); } } - if (result233 !== null) { - var result230 = result233; + if (result234 !== null) { + var result231 = result234; } else { if (this._input.substr(this._pos, 1) === "\\") { - var result232 = "\\"; + var result233 = "\\"; this._pos += 1; } else { - var result232 = null; + var result233 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.LiteralMatchFailure("\\")); } } - if (result232 !== null) { - var result230 = result232; + if (result233 !== null) { + var result231 = result233; } else { - var result231 = this._parse_eolChar(context); - if (result231 !== null) { - var result230 = result231; + var result232 = this._parse_eolChar(context); + if (result232 !== null) { + var result231 = result232; } else { - var result230 = null;; + var result231 = null;; }; }; } context.reportMatchFailures = savedReportMatchFailuresVar4; - if (result230 === null) { - var result228 = ''; + if (result231 === null) { + var result229 = ''; } else { - var result228 = null; + var result229 = null; this._pos = savedPos41; } - if (result228 !== null) { + if (result229 !== null) { if (this._input.length > this._pos) { - var result229 = this._input[this._pos]; + var result230 = this._input[this._pos]; this._pos++; } else { - var result229 = null; + var result230 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.AnyMatchFailure()); } } - if (result229 !== null) { - var result227 = [result228, result229]; + if (result230 !== null) { + var result228 = [result229, result230]; } else { - var result227 = null; + var result228 = null; this._pos = savedPos40; } } else { - var result227 = null; + var result228 = null; this._pos = savedPos40; } - var result226 = result227 !== null - ? (function() { return (arguments[1]); }).apply(this, result227) + var result227 = result228 !== null + ? (function() { return (arguments[1]); }).apply(this, result228) : null; this._cache["simpleBracketDelimitedCharacter"][pos] = { nextPos: this._pos, - result: result226 + result: result227 }; - return result226; + return result227; }; result._parse_simpleEscapeSequence = function(context) { @@ -2282,87 +2280,87 @@ PEG.grammarParser = (function(){ var savedPos42 = this._pos; if (this._input.substr(this._pos, 1) === "\\") { - var result236 = "\\"; + var result237 = "\\"; this._pos += 1; } else { - var result236 = null; + var result237 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.LiteralMatchFailure("\\")); } } - if (result236 !== null) { + if (result237 !== null) { var savedPos43 = this._pos; var savedReportMatchFailuresVar5 = context.reportMatchFailures; context.reportMatchFailures = false; - var result243 = this._parse_digit(context); - if (result243 !== null) { - var result239 = result243; + var result244 = this._parse_digit(context); + if (result244 !== null) { + var result240 = result244; } else { if (this._input.substr(this._pos, 1) === "x") { - var result242 = "x"; + var result243 = "x"; this._pos += 1; } else { - var result242 = null; + var result243 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.LiteralMatchFailure("x")); } } - if (result242 !== null) { - var result239 = result242; + if (result243 !== null) { + var result240 = result243; } else { if (this._input.substr(this._pos, 1) === "u") { - var result241 = "u"; + var result242 = "u"; this._pos += 1; } else { - var result241 = null; + var result242 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.LiteralMatchFailure("u")); } } - if (result241 !== null) { - var result239 = result241; + if (result242 !== null) { + var result240 = result242; } else { - var result240 = this._parse_eolChar(context); - if (result240 !== null) { - var result239 = result240; + var result241 = this._parse_eolChar(context); + if (result241 !== null) { + var result240 = result241; } else { - var result239 = null;; + var result240 = null;; }; }; }; } context.reportMatchFailures = savedReportMatchFailuresVar5; - if (result239 === null) { - var result237 = ''; + if (result240 === null) { + var result238 = ''; } else { - var result237 = null; + var result238 = null; this._pos = savedPos43; } - if (result237 !== null) { + if (result238 !== null) { if (this._input.length > this._pos) { - var result238 = this._input[this._pos]; + var result239 = this._input[this._pos]; this._pos++; } else { - var result238 = null; + var result239 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.AnyMatchFailure()); } } - if (result238 !== null) { - var result235 = [result236, result237, result238]; + if (result239 !== null) { + var result236 = [result237, result238, result239]; } else { - var result235 = null; + var result236 = null; this._pos = savedPos42; } } else { - var result235 = null; + var result236 = null; this._pos = savedPos42; } } else { - var result235 = null; + var result236 = null; this._pos = savedPos42; } - var result234 = result235 !== null + var result235 = result236 !== null ? (function() { return (arguments[2]) .replace("b", "\b") @@ -2371,16 +2369,16 @@ PEG.grammarParser = (function(){ .replace("r", "\r") .replace("t", "\t") .replace("v", "\v") - }).apply(this, result235) + }).apply(this, result236) : null; this._cache["simpleEscapeSequence"][pos] = { nextPos: this._pos, - result: result234 + result: result235 }; - return result234; + return result235; }; result._parse_zeroEscapeSequence = function(context) { @@ -2396,47 +2394,47 @@ PEG.grammarParser = (function(){ var savedPos44 = this._pos; if (this._input.substr(this._pos, 2) === "\\0") { - var result246 = "\\0"; + var result247 = "\\0"; this._pos += 2; } else { - var result246 = null; + var result247 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.LiteralMatchFailure("\\0")); } } - if (result246 !== null) { + if (result247 !== null) { var savedPos45 = this._pos; var savedReportMatchFailuresVar6 = context.reportMatchFailures; context.reportMatchFailures = false; - var result248 = this._parse_digit(context); + var result249 = this._parse_digit(context); context.reportMatchFailures = savedReportMatchFailuresVar6; - if (result248 === null) { - var result247 = ''; + if (result249 === null) { + var result248 = ''; } else { - var result247 = null; + var result248 = null; this._pos = savedPos45; } - if (result247 !== null) { - var result245 = [result246, result247]; + if (result248 !== null) { + var result246 = [result247, result248]; } else { - var result245 = null; + var result246 = null; this._pos = savedPos44; } } else { - var result245 = null; + var result246 = null; this._pos = savedPos44; } - var result244 = result245 !== null - ? (function() { return "\0"; }).apply(this, result245) + var result245 = result246 !== null + ? (function() { return "\0"; }).apply(this, result246) : null; this._cache["zeroEscapeSequence"][pos] = { nextPos: this._pos, - result: result244 + result: result245 }; - return result244; + return result245; }; result._parse_hexEscapeSequence = function(context) { @@ -2452,45 +2450,45 @@ PEG.grammarParser = (function(){ var savedPos46 = this._pos; if (this._input.substr(this._pos, 2) === "\\x") { - var result251 = "\\x"; + var result252 = "\\x"; this._pos += 2; } else { - var result251 = null; + var result252 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.LiteralMatchFailure("\\x")); } } - if (result251 !== null) { - var result252 = this._parse_hexDigit(context); - if (result252 !== null) { - var result253 = this._parse_hexDigit(context); - if (result253 !== null) { - var result250 = [result251, result252, result253]; + if (result252 !== null) { + var result253 = this._parse_hexDigit(context); + if (result253 !== null) { + var result254 = this._parse_hexDigit(context); + if (result254 !== null) { + var result251 = [result252, result253, result254]; } else { - var result250 = null; + var result251 = null; this._pos = savedPos46; } } else { - var result250 = null; + var result251 = null; this._pos = savedPos46; } } else { - var result250 = null; + var result251 = null; this._pos = savedPos46; } - var result249 = result250 !== null + var result250 = result251 !== null ? (function() { return String.fromCharCode(parseInt("0x" + (arguments[1]) + (arguments[2]))); - }).apply(this, result250) + }).apply(this, result251) : null; this._cache["hexEscapeSequence"][pos] = { nextPos: this._pos, - result: result249 + result: result250 }; - return result249; + return result250; }; result._parse_unicodeEscapeSequence = function(context) { @@ -2506,57 +2504,57 @@ PEG.grammarParser = (function(){ var savedPos47 = this._pos; if (this._input.substr(this._pos, 2) === "\\u") { - var result256 = "\\u"; + var result257 = "\\u"; this._pos += 2; } else { - var result256 = null; + var result257 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.LiteralMatchFailure("\\u")); } } - if (result256 !== null) { - var result257 = this._parse_hexDigit(context); - if (result257 !== null) { - var result258 = this._parse_hexDigit(context); - if (result258 !== null) { - var result259 = this._parse_hexDigit(context); - if (result259 !== null) { - var result260 = this._parse_hexDigit(context); - if (result260 !== null) { - var result255 = [result256, result257, result258, result259, result260]; + if (result257 !== null) { + var result258 = this._parse_hexDigit(context); + if (result258 !== null) { + var result259 = this._parse_hexDigit(context); + if (result259 !== null) { + var result260 = this._parse_hexDigit(context); + if (result260 !== null) { + var result261 = this._parse_hexDigit(context); + if (result261 !== null) { + var result256 = [result257, result258, result259, result260, result261]; } else { - var result255 = null; + var result256 = null; this._pos = savedPos47; } } else { - var result255 = null; + var result256 = null; this._pos = savedPos47; } } else { - var result255 = null; + var result256 = null; this._pos = savedPos47; } } else { - var result255 = null; + var result256 = null; this._pos = savedPos47; } } else { - var result255 = null; + var result256 = null; this._pos = savedPos47; } - var result254 = result255 !== null + var result255 = result256 !== null ? (function() { return String.fromCharCode(parseInt("0x" + (arguments[1]) + (arguments[2]) + (arguments[3]) + (arguments[4]))); - }).apply(this, result255) + }).apply(this, result256) : null; this._cache["unicodeEscapeSequence"][pos] = { nextPos: this._pos, - result: result254 + result: result255 }; - return result254; + return result255; }; result._parse_eolEscapeSequence = function(context) { @@ -2572,37 +2570,37 @@ PEG.grammarParser = (function(){ var savedPos48 = this._pos; if (this._input.substr(this._pos, 1) === "\\") { - var result263 = "\\"; + var result264 = "\\"; this._pos += 1; } else { - var result263 = null; + var result264 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.LiteralMatchFailure("\\")); } } - if (result263 !== null) { - var result264 = this._parse_eol(context); - if (result264 !== null) { - var result262 = [result263, result264]; + if (result264 !== null) { + var result265 = this._parse_eol(context); + if (result265 !== null) { + var result263 = [result264, result265]; } else { - var result262 = null; + var result263 = null; this._pos = savedPos48; } } else { - var result262 = null; + var result263 = null; this._pos = savedPos48; } - var result261 = result262 !== null - ? (function() { return (arguments[1]); }).apply(this, result262) + var result262 = result263 !== null + ? (function() { return (arguments[1]); }).apply(this, result263) : null; this._cache["eolEscapeSequence"][pos] = { nextPos: this._pos, - result: result261 + result: result262 }; - return result261; + return result262; }; result._parse_digit = function(context) { @@ -2616,145 +2614,23 @@ PEG.grammarParser = (function(){ var pos = this._pos; - if (this._input.substr(this._pos, 1) === "0") { - var result275 = "0"; - this._pos += 1; + if (this._input.substr(this._pos).match(/^[0-9]/) !== null) { + var result266 = this._input[this._pos]; + this._pos++; } else { - var result275 = null; + var result266 = null; if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("0")); - } - } - if (result275 !== null) { - var result265 = result275; - } else { - if (this._input.substr(this._pos, 1) === "1") { - var result274 = "1"; - this._pos += 1; - } else { - var result274 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("1")); - } + this._matchFailed(new PEG.Parser.ClassMatchFailure("0-9")); } - if (result274 !== null) { - var result265 = result274; - } else { - if (this._input.substr(this._pos, 1) === "2") { - var result273 = "2"; - this._pos += 1; - } else { - var result273 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("2")); - } - } - if (result273 !== null) { - var result265 = result273; - } else { - if (this._input.substr(this._pos, 1) === "3") { - var result272 = "3"; - this._pos += 1; - } else { - var result272 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("3")); - } - } - if (result272 !== null) { - var result265 = result272; - } else { - if (this._input.substr(this._pos, 1) === "4") { - var result271 = "4"; - this._pos += 1; - } else { - var result271 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("4")); - } - } - if (result271 !== null) { - var result265 = result271; - } else { - if (this._input.substr(this._pos, 1) === "5") { - var result270 = "5"; - this._pos += 1; - } else { - var result270 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("5")); - } - } - if (result270 !== null) { - var result265 = result270; - } else { - if (this._input.substr(this._pos, 1) === "6") { - var result269 = "6"; - this._pos += 1; - } else { - var result269 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("6")); - } - } - if (result269 !== null) { - var result265 = result269; - } else { - if (this._input.substr(this._pos, 1) === "7") { - var result268 = "7"; - this._pos += 1; - } else { - var result268 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("7")); - } - } - if (result268 !== null) { - var result265 = result268; - } else { - if (this._input.substr(this._pos, 1) === "8") { - var result267 = "8"; - this._pos += 1; - } else { - var result267 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("8")); - } - } - if (result267 !== null) { - var result265 = result267; - } else { - if (this._input.substr(this._pos, 1) === "9") { - var result266 = "9"; - this._pos += 1; - } else { - var result266 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("9")); - } - } - if (result266 !== null) { - var result265 = result266; - } else { - var result265 = null;; - }; - }; - }; - }; - }; - }; - }; - }; - }; } this._cache["digit"][pos] = { nextPos: this._pos, - result: result265 + result: result266 }; - return result265; + return result266; }; result._parse_hexDigit = function(context) { @@ -2768,301 +2644,23 @@ PEG.grammarParser = (function(){ var pos = this._pos; - if (this._input.substr(this._pos, 1) === "0") { - var result298 = "0"; - this._pos += 1; + if (this._input.substr(this._pos).match(/^[0-9a-fA-F]/) !== null) { + var result267 = this._input[this._pos]; + this._pos++; } else { - var result298 = null; + var result267 = null; if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("0")); - } - } - if (result298 !== null) { - var result276 = result298; - } else { - if (this._input.substr(this._pos, 1) === "1") { - var result297 = "1"; - this._pos += 1; - } else { - var result297 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("1")); - } + this._matchFailed(new PEG.Parser.ClassMatchFailure("0-9a-fA-F")); } - if (result297 !== null) { - var result276 = result297; - } else { - if (this._input.substr(this._pos, 1) === "2") { - var result296 = "2"; - this._pos += 1; - } else { - var result296 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("2")); - } - } - if (result296 !== null) { - var result276 = result296; - } else { - if (this._input.substr(this._pos, 1) === "3") { - var result295 = "3"; - this._pos += 1; - } else { - var result295 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("3")); - } - } - if (result295 !== null) { - var result276 = result295; - } else { - if (this._input.substr(this._pos, 1) === "4") { - var result294 = "4"; - this._pos += 1; - } else { - var result294 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("4")); - } - } - if (result294 !== null) { - var result276 = result294; - } else { - if (this._input.substr(this._pos, 1) === "5") { - var result293 = "5"; - this._pos += 1; - } else { - var result293 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("5")); - } - } - if (result293 !== null) { - var result276 = result293; - } else { - if (this._input.substr(this._pos, 1) === "6") { - var result292 = "6"; - this._pos += 1; - } else { - var result292 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("6")); - } - } - if (result292 !== null) { - var result276 = result292; - } else { - if (this._input.substr(this._pos, 1) === "7") { - var result291 = "7"; - this._pos += 1; - } else { - var result291 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("7")); - } - } - if (result291 !== null) { - var result276 = result291; - } else { - if (this._input.substr(this._pos, 1) === "8") { - var result290 = "8"; - this._pos += 1; - } else { - var result290 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("8")); - } - } - if (result290 !== null) { - var result276 = result290; - } else { - if (this._input.substr(this._pos, 1) === "9") { - var result289 = "9"; - this._pos += 1; - } else { - var result289 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("9")); - } - } - if (result289 !== null) { - var result276 = result289; - } else { - if (this._input.substr(this._pos, 1) === "a") { - var result288 = "a"; - this._pos += 1; - } else { - var result288 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("a")); - } - } - if (result288 !== null) { - var result276 = result288; - } else { - if (this._input.substr(this._pos, 1) === "b") { - var result287 = "b"; - this._pos += 1; - } else { - var result287 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("b")); - } - } - if (result287 !== null) { - var result276 = result287; - } else { - if (this._input.substr(this._pos, 1) === "c") { - var result286 = "c"; - this._pos += 1; - } else { - var result286 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("c")); - } - } - if (result286 !== null) { - var result276 = result286; - } else { - if (this._input.substr(this._pos, 1) === "d") { - var result285 = "d"; - this._pos += 1; - } else { - var result285 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("d")); - } - } - if (result285 !== null) { - var result276 = result285; - } else { - if (this._input.substr(this._pos, 1) === "e") { - var result284 = "e"; - this._pos += 1; - } else { - var result284 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("e")); - } - } - if (result284 !== null) { - var result276 = result284; - } else { - if (this._input.substr(this._pos, 1) === "f") { - var result283 = "f"; - this._pos += 1; - } else { - var result283 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("f")); - } - } - if (result283 !== null) { - var result276 = result283; - } else { - if (this._input.substr(this._pos, 1) === "A") { - var result282 = "A"; - this._pos += 1; - } else { - var result282 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("A")); - } - } - if (result282 !== null) { - var result276 = result282; - } else { - if (this._input.substr(this._pos, 1) === "B") { - var result281 = "B"; - this._pos += 1; - } else { - var result281 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("B")); - } - } - if (result281 !== null) { - var result276 = result281; - } else { - if (this._input.substr(this._pos, 1) === "C") { - var result280 = "C"; - this._pos += 1; - } else { - var result280 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("C")); - } - } - if (result280 !== null) { - var result276 = result280; - } else { - if (this._input.substr(this._pos, 1) === "D") { - var result279 = "D"; - this._pos += 1; - } else { - var result279 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("D")); - } - } - if (result279 !== null) { - var result276 = result279; - } else { - if (this._input.substr(this._pos, 1) === "E") { - var result278 = "E"; - this._pos += 1; - } else { - var result278 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("E")); - } - } - if (result278 !== null) { - var result276 = result278; - } else { - if (this._input.substr(this._pos, 1) === "F") { - var result277 = "F"; - this._pos += 1; - } else { - var result277 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("F")); - } - } - if (result277 !== null) { - var result276 = result277; - } else { - var result276 = null;; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; } this._cache["hexDigit"][pos] = { nextPos: this._pos, - result: result276 + result: result267 }; - return result276; + return result267; }; result._parse_letter = function(context) { @@ -3076,15 +2674,15 @@ PEG.grammarParser = (function(){ var pos = this._pos; - var result301 = this._parse_lowerCaseLetter(context); - if (result301 !== null) { - var result299 = result301; + var result270 = this._parse_lowerCaseLetter(context); + if (result270 !== null) { + var result268 = result270; } else { - var result300 = this._parse_upperCaseLetter(context); - if (result300 !== null) { - var result299 = result300; + var result269 = this._parse_upperCaseLetter(context); + if (result269 !== null) { + var result268 = result269; } else { - var result299 = null;; + var result268 = null;; }; } @@ -3092,9 +2690,9 @@ PEG.grammarParser = (function(){ this._cache["letter"][pos] = { nextPos: this._pos, - result: result299 + result: result268 }; - return result299; + return result268; }; result._parse_lowerCaseLetter = function(context) { @@ -3108,353 +2706,23 @@ PEG.grammarParser = (function(){ var pos = this._pos; - if (this._input.substr(this._pos, 1) === "a") { - var result328 = "a"; - this._pos += 1; + if (this._input.substr(this._pos).match(/^[a-z]/) !== null) { + var result271 = this._input[this._pos]; + this._pos++; } else { - var result328 = null; + var result271 = null; if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("a")); + this._matchFailed(new PEG.Parser.ClassMatchFailure("a-z")); } } - if (result328 !== null) { - var result302 = result328; - } else { - if (this._input.substr(this._pos, 1) === "b") { - var result327 = "b"; - this._pos += 1; - } else { - var result327 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("b")); - } - } - if (result327 !== null) { - var result302 = result327; - } else { - if (this._input.substr(this._pos, 1) === "c") { - var result326 = "c"; - this._pos += 1; - } else { - var result326 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("c")); - } - } - if (result326 !== null) { - var result302 = result326; - } else { - if (this._input.substr(this._pos, 1) === "d") { - var result325 = "d"; - this._pos += 1; - } else { - var result325 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("d")); - } - } - if (result325 !== null) { - var result302 = result325; - } else { - if (this._input.substr(this._pos, 1) === "e") { - var result324 = "e"; - this._pos += 1; - } else { - var result324 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("e")); - } - } - if (result324 !== null) { - var result302 = result324; - } else { - if (this._input.substr(this._pos, 1) === "f") { - var result323 = "f"; - this._pos += 1; - } else { - var result323 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("f")); - } - } - if (result323 !== null) { - var result302 = result323; - } else { - if (this._input.substr(this._pos, 1) === "g") { - var result322 = "g"; - this._pos += 1; - } else { - var result322 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("g")); - } - } - if (result322 !== null) { - var result302 = result322; - } else { - if (this._input.substr(this._pos, 1) === "h") { - var result321 = "h"; - this._pos += 1; - } else { - var result321 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("h")); - } - } - if (result321 !== null) { - var result302 = result321; - } else { - if (this._input.substr(this._pos, 1) === "i") { - var result320 = "i"; - this._pos += 1; - } else { - var result320 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("i")); - } - } - if (result320 !== null) { - var result302 = result320; - } else { - if (this._input.substr(this._pos, 1) === "j") { - var result319 = "j"; - this._pos += 1; - } else { - var result319 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("j")); - } - } - if (result319 !== null) { - var result302 = result319; - } else { - if (this._input.substr(this._pos, 1) === "k") { - var result318 = "k"; - this._pos += 1; - } else { - var result318 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("k")); - } - } - if (result318 !== null) { - var result302 = result318; - } else { - if (this._input.substr(this._pos, 1) === "l") { - var result317 = "l"; - this._pos += 1; - } else { - var result317 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("l")); - } - } - if (result317 !== null) { - var result302 = result317; - } else { - if (this._input.substr(this._pos, 1) === "m") { - var result316 = "m"; - this._pos += 1; - } else { - var result316 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("m")); - } - } - if (result316 !== null) { - var result302 = result316; - } else { - if (this._input.substr(this._pos, 1) === "n") { - var result315 = "n"; - this._pos += 1; - } else { - var result315 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("n")); - } - } - if (result315 !== null) { - var result302 = result315; - } else { - if (this._input.substr(this._pos, 1) === "o") { - var result314 = "o"; - this._pos += 1; - } else { - var result314 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("o")); - } - } - if (result314 !== null) { - var result302 = result314; - } else { - if (this._input.substr(this._pos, 1) === "p") { - var result313 = "p"; - this._pos += 1; - } else { - var result313 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("p")); - } - } - if (result313 !== null) { - var result302 = result313; - } else { - if (this._input.substr(this._pos, 1) === "q") { - var result312 = "q"; - this._pos += 1; - } else { - var result312 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("q")); - } - } - if (result312 !== null) { - var result302 = result312; - } else { - if (this._input.substr(this._pos, 1) === "r") { - var result311 = "r"; - this._pos += 1; - } else { - var result311 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("r")); - } - } - if (result311 !== null) { - var result302 = result311; - } else { - if (this._input.substr(this._pos, 1) === "s") { - var result310 = "s"; - this._pos += 1; - } else { - var result310 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("s")); - } - } - if (result310 !== null) { - var result302 = result310; - } else { - if (this._input.substr(this._pos, 1) === "t") { - var result309 = "t"; - this._pos += 1; - } else { - var result309 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("t")); - } - } - if (result309 !== null) { - var result302 = result309; - } else { - if (this._input.substr(this._pos, 1) === "u") { - var result308 = "u"; - this._pos += 1; - } else { - var result308 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("u")); - } - } - if (result308 !== null) { - var result302 = result308; - } else { - if (this._input.substr(this._pos, 1) === "v") { - var result307 = "v"; - this._pos += 1; - } else { - var result307 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("v")); - } - } - if (result307 !== null) { - var result302 = result307; - } else { - if (this._input.substr(this._pos, 1) === "w") { - var result306 = "w"; - this._pos += 1; - } else { - var result306 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("w")); - } - } - if (result306 !== null) { - var result302 = result306; - } else { - if (this._input.substr(this._pos, 1) === "x") { - var result305 = "x"; - this._pos += 1; - } else { - var result305 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("x")); - } - } - if (result305 !== null) { - var result302 = result305; - } else { - if (this._input.substr(this._pos, 1) === "y") { - var result304 = "y"; - this._pos += 1; - } else { - var result304 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("y")); - } - } - if (result304 !== null) { - var result302 = result304; - } else { - if (this._input.substr(this._pos, 1) === "z") { - var result303 = "z"; - this._pos += 1; - } else { - var result303 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("z")); - } - } - if (result303 !== null) { - var result302 = result303; - } else { - var result302 = null;; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - } this._cache["lowerCaseLetter"][pos] = { nextPos: this._pos, - result: result302 + result: result271 }; - return result302; + return result271; }; result._parse_upperCaseLetter = function(context) { @@ -3468,353 +2736,23 @@ PEG.grammarParser = (function(){ var pos = this._pos; - if (this._input.substr(this._pos, 1) === "A") { - var result355 = "A"; - this._pos += 1; + if (this._input.substr(this._pos).match(/^[A-Z]/) !== null) { + var result272 = this._input[this._pos]; + this._pos++; } else { - var result355 = null; + var result272 = null; if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("A")); + this._matchFailed(new PEG.Parser.ClassMatchFailure("A-Z")); } } - if (result355 !== null) { - var result329 = result355; - } else { - if (this._input.substr(this._pos, 1) === "B") { - var result354 = "B"; - this._pos += 1; - } else { - var result354 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("B")); - } - } - if (result354 !== null) { - var result329 = result354; - } else { - if (this._input.substr(this._pos, 1) === "C") { - var result353 = "C"; - this._pos += 1; - } else { - var result353 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("C")); - } - } - if (result353 !== null) { - var result329 = result353; - } else { - if (this._input.substr(this._pos, 1) === "D") { - var result352 = "D"; - this._pos += 1; - } else { - var result352 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("D")); - } - } - if (result352 !== null) { - var result329 = result352; - } else { - if (this._input.substr(this._pos, 1) === "E") { - var result351 = "E"; - this._pos += 1; - } else { - var result351 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("E")); - } - } - if (result351 !== null) { - var result329 = result351; - } else { - if (this._input.substr(this._pos, 1) === "F") { - var result350 = "F"; - this._pos += 1; - } else { - var result350 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("F")); - } - } - if (result350 !== null) { - var result329 = result350; - } else { - if (this._input.substr(this._pos, 1) === "G") { - var result349 = "G"; - this._pos += 1; - } else { - var result349 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("G")); - } - } - if (result349 !== null) { - var result329 = result349; - } else { - if (this._input.substr(this._pos, 1) === "H") { - var result348 = "H"; - this._pos += 1; - } else { - var result348 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("H")); - } - } - if (result348 !== null) { - var result329 = result348; - } else { - if (this._input.substr(this._pos, 1) === "I") { - var result347 = "I"; - this._pos += 1; - } else { - var result347 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("I")); - } - } - if (result347 !== null) { - var result329 = result347; - } else { - if (this._input.substr(this._pos, 1) === "J") { - var result346 = "J"; - this._pos += 1; - } else { - var result346 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("J")); - } - } - if (result346 !== null) { - var result329 = result346; - } else { - if (this._input.substr(this._pos, 1) === "K") { - var result345 = "K"; - this._pos += 1; - } else { - var result345 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("K")); - } - } - if (result345 !== null) { - var result329 = result345; - } else { - if (this._input.substr(this._pos, 1) === "L") { - var result344 = "L"; - this._pos += 1; - } else { - var result344 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("L")); - } - } - if (result344 !== null) { - var result329 = result344; - } else { - if (this._input.substr(this._pos, 1) === "M") { - var result343 = "M"; - this._pos += 1; - } else { - var result343 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("M")); - } - } - if (result343 !== null) { - var result329 = result343; - } else { - if (this._input.substr(this._pos, 1) === "N") { - var result342 = "N"; - this._pos += 1; - } else { - var result342 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("N")); - } - } - if (result342 !== null) { - var result329 = result342; - } else { - if (this._input.substr(this._pos, 1) === "O") { - var result341 = "O"; - this._pos += 1; - } else { - var result341 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("O")); - } - } - if (result341 !== null) { - var result329 = result341; - } else { - if (this._input.substr(this._pos, 1) === "P") { - var result340 = "P"; - this._pos += 1; - } else { - var result340 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("P")); - } - } - if (result340 !== null) { - var result329 = result340; - } else { - if (this._input.substr(this._pos, 1) === "Q") { - var result339 = "Q"; - this._pos += 1; - } else { - var result339 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("Q")); - } - } - if (result339 !== null) { - var result329 = result339; - } else { - if (this._input.substr(this._pos, 1) === "R") { - var result338 = "R"; - this._pos += 1; - } else { - var result338 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("R")); - } - } - if (result338 !== null) { - var result329 = result338; - } else { - if (this._input.substr(this._pos, 1) === "S") { - var result337 = "S"; - this._pos += 1; - } else { - var result337 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("S")); - } - } - if (result337 !== null) { - var result329 = result337; - } else { - if (this._input.substr(this._pos, 1) === "T") { - var result336 = "T"; - this._pos += 1; - } else { - var result336 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("T")); - } - } - if (result336 !== null) { - var result329 = result336; - } else { - if (this._input.substr(this._pos, 1) === "U") { - var result335 = "U"; - this._pos += 1; - } else { - var result335 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("U")); - } - } - if (result335 !== null) { - var result329 = result335; - } else { - if (this._input.substr(this._pos, 1) === "V") { - var result334 = "V"; - this._pos += 1; - } else { - var result334 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("V")); - } - } - if (result334 !== null) { - var result329 = result334; - } else { - if (this._input.substr(this._pos, 1) === "W") { - var result333 = "W"; - this._pos += 1; - } else { - var result333 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("W")); - } - } - if (result333 !== null) { - var result329 = result333; - } else { - if (this._input.substr(this._pos, 1) === "X") { - var result332 = "X"; - this._pos += 1; - } else { - var result332 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("X")); - } - } - if (result332 !== null) { - var result329 = result332; - } else { - if (this._input.substr(this._pos, 1) === "Y") { - var result331 = "Y"; - this._pos += 1; - } else { - var result331 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("Y")); - } - } - if (result331 !== null) { - var result329 = result331; - } else { - if (this._input.substr(this._pos, 1) === "Z") { - var result330 = "Z"; - this._pos += 1; - } else { - var result330 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("Z")); - } - } - if (result330 !== null) { - var result329 = result330; - } else { - var result329 = null;; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - } this._cache["upperCaseLetter"][pos] = { nextPos: this._pos, - result: result329 + result: result272 }; - return result329; + return result272; }; result._parse___ = function(context) { @@ -3828,38 +2766,38 @@ PEG.grammarParser = (function(){ var pos = this._pos; - var result356 = []; - var result360 = this._parse_whitespace(context); - if (result360 !== null) { - var result357 = result360; + var result273 = []; + var result277 = this._parse_whitespace(context); + if (result277 !== null) { + var result274 = result277; } else { - var result359 = this._parse_eol(context); - if (result359 !== null) { - var result357 = result359; + var result276 = this._parse_eol(context); + if (result276 !== null) { + var result274 = result276; } else { - var result358 = this._parse_comment(context); - if (result358 !== null) { - var result357 = result358; + var result275 = this._parse_comment(context); + if (result275 !== null) { + var result274 = result275; } else { - var result357 = null;; + var result274 = null;; }; }; } - while (result357 !== null) { - result356.push(result357); - var result360 = this._parse_whitespace(context); - if (result360 !== null) { - var result357 = result360; + while (result274 !== null) { + result273.push(result274); + var result277 = this._parse_whitespace(context); + if (result277 !== null) { + var result274 = result277; } else { - var result359 = this._parse_eol(context); - if (result359 !== null) { - var result357 = result359; + var result276 = this._parse_eol(context); + if (result276 !== null) { + var result274 = result276; } else { - var result358 = this._parse_comment(context); - if (result358 !== null) { - var result357 = result358; + var result275 = this._parse_comment(context); + if (result275 !== null) { + var result274 = result275; } else { - var result357 = null;; + var result274 = null;; }; }; } @@ -3869,9 +2807,9 @@ PEG.grammarParser = (function(){ this._cache["__"][pos] = { nextPos: this._pos, - result: result356 + result: result273 }; - return result356; + return result273; }; result._parse_comment = function(context) { @@ -3886,27 +2824,27 @@ PEG.grammarParser = (function(){ var savedReportMatchFailures = context.reportMatchFailures; context.reportMatchFailures = false; - var result363 = this._parse_singleLineComment(context); - if (result363 !== null) { - var result361 = result363; + var result280 = this._parse_singleLineComment(context); + if (result280 !== null) { + var result278 = result280; } else { - var result362 = this._parse_multiLineComment(context); - if (result362 !== null) { - var result361 = result362; + var result279 = this._parse_multiLineComment(context); + if (result279 !== null) { + var result278 = result279; } else { - var result361 = null;; + var result278 = null;; }; } context.reportMatchFailures = savedReportMatchFailures; - if (context.reportMatchFailures && result361 === null) { + if (context.reportMatchFailures && result278 === null) { this._matchFailed(new PEG.Parser.NamedRuleMatchFailure("comment")); } this._cache["comment"][pos] = { nextPos: this._pos, - result: result361 + result: result278 }; - return result361; + return result278; }; result._parse_singleLineComment = function(context) { @@ -3922,91 +2860,91 @@ PEG.grammarParser = (function(){ var savedPos49 = this._pos; if (this._input.substr(this._pos, 2) === "//") { - var result365 = "//"; + var result282 = "//"; this._pos += 2; } else { - var result365 = null; + var result282 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.LiteralMatchFailure("//")); } } - if (result365 !== null) { - var result366 = []; + if (result282 !== null) { + var result283 = []; var savedPos50 = this._pos; var savedPos51 = this._pos; var savedReportMatchFailuresVar7 = context.reportMatchFailures; context.reportMatchFailures = false; - var result370 = this._parse_eolChar(context); + var result287 = this._parse_eolChar(context); context.reportMatchFailures = savedReportMatchFailuresVar7; - if (result370 === null) { - var result368 = ''; + if (result287 === null) { + var result285 = ''; } else { - var result368 = null; + var result285 = null; this._pos = savedPos51; } - if (result368 !== null) { + if (result285 !== null) { if (this._input.length > this._pos) { - var result369 = this._input[this._pos]; + var result286 = this._input[this._pos]; this._pos++; } else { - var result369 = null; + var result286 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.AnyMatchFailure()); } } - if (result369 !== null) { - var result367 = [result368, result369]; + if (result286 !== null) { + var result284 = [result285, result286]; } else { - var result367 = null; + var result284 = null; this._pos = savedPos50; } } else { - var result367 = null; + var result284 = null; this._pos = savedPos50; } - while (result367 !== null) { - result366.push(result367); + while (result284 !== null) { + result283.push(result284); var savedPos50 = this._pos; var savedPos51 = this._pos; var savedReportMatchFailuresVar7 = context.reportMatchFailures; context.reportMatchFailures = false; - var result370 = this._parse_eolChar(context); + var result287 = this._parse_eolChar(context); context.reportMatchFailures = savedReportMatchFailuresVar7; - if (result370 === null) { - var result368 = ''; + if (result287 === null) { + var result285 = ''; } else { - var result368 = null; + var result285 = null; this._pos = savedPos51; } - if (result368 !== null) { + if (result285 !== null) { if (this._input.length > this._pos) { - var result369 = this._input[this._pos]; + var result286 = this._input[this._pos]; this._pos++; } else { - var result369 = null; + var result286 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.AnyMatchFailure()); } } - if (result369 !== null) { - var result367 = [result368, result369]; + if (result286 !== null) { + var result284 = [result285, result286]; } else { - var result367 = null; + var result284 = null; this._pos = savedPos50; } } else { - var result367 = null; + var result284 = null; this._pos = savedPos50; } } - if (result366 !== null) { - var result364 = [result365, result366]; + if (result283 !== null) { + var result281 = [result282, result283]; } else { - var result364 = null; + var result281 = null; this._pos = savedPos49; } } else { - var result364 = null; + var result281 = null; this._pos = savedPos49; } @@ -4014,9 +2952,9 @@ PEG.grammarParser = (function(){ this._cache["singleLineComment"][pos] = { nextPos: this._pos, - result: result364 + result: result281 }; - return result364; + return result281; }; result._parse_multiLineComment = function(context) { @@ -4032,121 +2970,121 @@ PEG.grammarParser = (function(){ var savedPos52 = this._pos; if (this._input.substr(this._pos, 2) === "/*") { - var result372 = "/*"; + var result289 = "/*"; this._pos += 2; } else { - var result372 = null; + var result289 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.LiteralMatchFailure("/*")); } } - if (result372 !== null) { - var result373 = []; + if (result289 !== null) { + var result290 = []; var savedPos53 = this._pos; var savedPos54 = this._pos; var savedReportMatchFailuresVar8 = context.reportMatchFailures; context.reportMatchFailures = false; if (this._input.substr(this._pos, 2) === "*/") { - var result378 = "*/"; + var result295 = "*/"; this._pos += 2; } else { - var result378 = null; + var result295 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.LiteralMatchFailure("*/")); } } context.reportMatchFailures = savedReportMatchFailuresVar8; - if (result378 === null) { - var result376 = ''; + if (result295 === null) { + var result293 = ''; } else { - var result376 = null; + var result293 = null; this._pos = savedPos54; } - if (result376 !== null) { + if (result293 !== null) { if (this._input.length > this._pos) { - var result377 = this._input[this._pos]; + var result294 = this._input[this._pos]; this._pos++; } else { - var result377 = null; + var result294 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.AnyMatchFailure()); } } - if (result377 !== null) { - var result375 = [result376, result377]; + if (result294 !== null) { + var result292 = [result293, result294]; } else { - var result375 = null; + var result292 = null; this._pos = savedPos53; } } else { - var result375 = null; + var result292 = null; this._pos = savedPos53; } - while (result375 !== null) { - result373.push(result375); + while (result292 !== null) { + result290.push(result292); var savedPos53 = this._pos; var savedPos54 = this._pos; var savedReportMatchFailuresVar8 = context.reportMatchFailures; context.reportMatchFailures = false; if (this._input.substr(this._pos, 2) === "*/") { - var result378 = "*/"; + var result295 = "*/"; this._pos += 2; } else { - var result378 = null; + var result295 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.LiteralMatchFailure("*/")); } } context.reportMatchFailures = savedReportMatchFailuresVar8; - if (result378 === null) { - var result376 = ''; + if (result295 === null) { + var result293 = ''; } else { - var result376 = null; + var result293 = null; this._pos = savedPos54; } - if (result376 !== null) { + if (result293 !== null) { if (this._input.length > this._pos) { - var result377 = this._input[this._pos]; + var result294 = this._input[this._pos]; this._pos++; } else { - var result377 = null; + var result294 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.AnyMatchFailure()); } } - if (result377 !== null) { - var result375 = [result376, result377]; + if (result294 !== null) { + var result292 = [result293, result294]; } else { - var result375 = null; + var result292 = null; this._pos = savedPos53; } } else { - var result375 = null; + var result292 = null; this._pos = savedPos53; } } - if (result373 !== null) { + if (result290 !== null) { if (this._input.substr(this._pos, 2) === "*/") { - var result374 = "*/"; + var result291 = "*/"; this._pos += 2; } else { - var result374 = null; + var result291 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.LiteralMatchFailure("*/")); } } - if (result374 !== null) { - var result371 = [result372, result373, result374]; + if (result291 !== null) { + var result288 = [result289, result290, result291]; } else { - var result371 = null; + var result288 = null; this._pos = savedPos52; } } else { - var result371 = null; + var result288 = null; this._pos = savedPos52; } } else { - var result371 = null; + var result288 = null; this._pos = savedPos52; } @@ -4154,9 +3092,9 @@ PEG.grammarParser = (function(){ this._cache["multiLineComment"][pos] = { nextPos: this._pos, - result: result371 + result: result288 }; - return result371; + return result288; }; result._parse_eol = function(context) { @@ -4172,81 +3110,81 @@ PEG.grammarParser = (function(){ var savedReportMatchFailures = context.reportMatchFailures; context.reportMatchFailures = false; if (this._input.substr(this._pos, 1) === "\n") { - var result384 = "\n"; + var result301 = "\n"; this._pos += 1; } else { - var result384 = null; + var result301 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.LiteralMatchFailure("\n")); } } - if (result384 !== null) { - var result379 = result384; + if (result301 !== null) { + var result296 = result301; } else { if (this._input.substr(this._pos, 2) === "\r\n") { - var result383 = "\r\n"; + var result300 = "\r\n"; this._pos += 2; } else { - var result383 = null; + var result300 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.LiteralMatchFailure("\r\n")); } } - if (result383 !== null) { - var result379 = result383; + if (result300 !== null) { + var result296 = result300; } else { if (this._input.substr(this._pos, 1) === "\r") { - var result382 = "\r"; + var result299 = "\r"; this._pos += 1; } else { - var result382 = null; + var result299 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.LiteralMatchFailure("\r")); } } - if (result382 !== null) { - var result379 = result382; + if (result299 !== null) { + var result296 = result299; } else { if (this._input.substr(this._pos, 1) === "\u2028") { - var result381 = "\u2028"; + var result298 = "\u2028"; this._pos += 1; } else { - var result381 = null; + var result298 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.LiteralMatchFailure("\u2028")); } } - if (result381 !== null) { - var result379 = result381; + if (result298 !== null) { + var result296 = result298; } else { if (this._input.substr(this._pos, 1) === "\u2029") { - var result380 = "\u2029"; + var result297 = "\u2029"; this._pos += 1; } else { - var result380 = null; + var result297 = null; if (context.reportMatchFailures) { this._matchFailed(new PEG.Parser.LiteralMatchFailure("\u2029")); } } - if (result380 !== null) { - var result379 = result380; + if (result297 !== null) { + var result296 = result297; } else { - var result379 = null;; + var result296 = null;; }; }; }; }; } context.reportMatchFailures = savedReportMatchFailures; - if (context.reportMatchFailures && result379 === null) { + if (context.reportMatchFailures && result296 === null) { this._matchFailed(new PEG.Parser.NamedRuleMatchFailure("end of line")); } this._cache["eol"][pos] = { nextPos: this._pos, - result: result379 + result: result296 }; - return result379; + return result296; }; result._parse_eolChar = function(context) { @@ -4260,67 +3198,23 @@ PEG.grammarParser = (function(){ var pos = this._pos; - if (this._input.substr(this._pos, 1) === "\n") { - var result389 = "\n"; - this._pos += 1; + if (this._input.substr(this._pos).match(/^[\n\r\u2028\u2029]/) !== null) { + var result302 = this._input[this._pos]; + this._pos++; } else { - var result389 = null; + var result302 = null; if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("\n")); + this._matchFailed(new PEG.Parser.ClassMatchFailure("\\n\\r\\u2028\\u2029")); } } - if (result389 !== null) { - var result385 = result389; - } else { - if (this._input.substr(this._pos, 1) === "\r") { - var result388 = "\r"; - this._pos += 1; - } else { - var result388 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("\r")); - } - } - if (result388 !== null) { - var result385 = result388; - } else { - if (this._input.substr(this._pos, 1) === "\u2028") { - var result387 = "\u2028"; - this._pos += 1; - } else { - var result387 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("\u2028")); - } - } - if (result387 !== null) { - var result385 = result387; - } else { - if (this._input.substr(this._pos, 1) === "\u2029") { - var result386 = "\u2029"; - this._pos += 1; - } else { - var result386 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("\u2029")); - } - } - if (result386 !== null) { - var result385 = result386; - } else { - var result385 = null;; - }; - }; - }; - } this._cache["eolChar"][pos] = { nextPos: this._pos, - result: result385 + result: result302 }; - return result385; + return result302; }; result._parse_whitespace = function(context) { @@ -4335,290 +3229,25 @@ PEG.grammarParser = (function(){ var savedReportMatchFailures = context.reportMatchFailures; context.reportMatchFailures = false; - if (this._input.substr(this._pos, 1) === " ") { - var result411 = " "; - this._pos += 1; + if (this._input.substr(this._pos).match(/^[   ᠎ -    ]/) !== null) { + var result303 = this._input[this._pos]; + this._pos++; } else { - var result411 = null; + var result303 = null; if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } - } - if (result411 !== null) { - var result390 = result411; - } else { - if (this._input.substr(this._pos, 1) === " ") { - var result410 = " "; - this._pos += 1; - } else { - var result410 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } + this._matchFailed(new PEG.Parser.ClassMatchFailure("   ᠎ -    ")); } - if (result410 !== null) { - var result390 = result410; - } else { - if (this._input.substr(this._pos, 1) === " ") { - var result409 = " "; - this._pos += 1; - } else { - var result409 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } - } - if (result409 !== null) { - var result390 = result409; - } else { - if (this._input.substr(this._pos, 1) === " ") { - var result408 = " "; - this._pos += 1; - } else { - var result408 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } - } - if (result408 !== null) { - var result390 = result408; - } else { - if (this._input.substr(this._pos, 1) === " ") { - var result407 = " "; - this._pos += 1; - } else { - var result407 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } - } - if (result407 !== null) { - var result390 = result407; - } else { - if (this._input.substr(this._pos, 1) === " ") { - var result406 = " "; - this._pos += 1; - } else { - var result406 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } - } - if (result406 !== null) { - var result390 = result406; - } else { - if (this._input.substr(this._pos, 1) === "᠎") { - var result405 = "᠎"; - this._pos += 1; - } else { - var result405 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure("᠎")); - } - } - if (result405 !== null) { - var result390 = result405; - } else { - if (this._input.substr(this._pos, 1) === " ") { - var result404 = " "; - this._pos += 1; - } else { - var result404 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } - } - if (result404 !== null) { - var result390 = result404; - } else { - if (this._input.substr(this._pos, 1) === " ") { - var result403 = " "; - this._pos += 1; - } else { - var result403 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } - } - if (result403 !== null) { - var result390 = result403; - } else { - if (this._input.substr(this._pos, 1) === " ") { - var result402 = " "; - this._pos += 1; - } else { - var result402 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } - } - if (result402 !== null) { - var result390 = result402; - } else { - if (this._input.substr(this._pos, 1) === " ") { - var result401 = " "; - this._pos += 1; - } else { - var result401 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } - } - if (result401 !== null) { - var result390 = result401; - } else { - if (this._input.substr(this._pos, 1) === " ") { - var result400 = " "; - this._pos += 1; - } else { - var result400 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } - } - if (result400 !== null) { - var result390 = result400; - } else { - if (this._input.substr(this._pos, 1) === " ") { - var result399 = " "; - this._pos += 1; - } else { - var result399 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } - } - if (result399 !== null) { - var result390 = result399; - } else { - if (this._input.substr(this._pos, 1) === " ") { - var result398 = " "; - this._pos += 1; - } else { - var result398 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } - } - if (result398 !== null) { - var result390 = result398; - } else { - if (this._input.substr(this._pos, 1) === " ") { - var result397 = " "; - this._pos += 1; - } else { - var result397 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } - } - if (result397 !== null) { - var result390 = result397; - } else { - if (this._input.substr(this._pos, 1) === " ") { - var result396 = " "; - this._pos += 1; - } else { - var result396 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } - } - if (result396 !== null) { - var result390 = result396; - } else { - if (this._input.substr(this._pos, 1) === " ") { - var result395 = " "; - this._pos += 1; - } else { - var result395 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } - } - if (result395 !== null) { - var result390 = result395; - } else { - if (this._input.substr(this._pos, 1) === " ") { - var result394 = " "; - this._pos += 1; - } else { - var result394 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } - } - if (result394 !== null) { - var result390 = result394; - } else { - if (this._input.substr(this._pos, 1) === " ") { - var result393 = " "; - this._pos += 1; - } else { - var result393 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } - } - if (result393 !== null) { - var result390 = result393; - } else { - if (this._input.substr(this._pos, 1) === " ") { - var result392 = " "; - this._pos += 1; - } else { - var result392 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } - } - if (result392 !== null) { - var result390 = result392; - } else { - if (this._input.substr(this._pos, 1) === " ") { - var result391 = " "; - this._pos += 1; - } else { - var result391 = null; - if (context.reportMatchFailures) { - this._matchFailed(new PEG.Parser.LiteralMatchFailure(" ")); - } - } - if (result391 !== null) { - var result390 = result391; - } else { - var result390 = null;; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; } context.reportMatchFailures = savedReportMatchFailures; - if (context.reportMatchFailures && result390 === null) { + if (context.reportMatchFailures && result303 === null) { this._matchFailed(new PEG.Parser.NamedRuleMatchFailure("whitespace")); } this._cache["whitespace"][pos] = { nextPos: this._pos, - result: result390 + result: result303 }; - return result390; + return result303; }; return result; diff --git a/lib/metagrammar.pegjs b/lib/metagrammar.pegjs index 033f596..c39a388 100644 --- a/lib/metagrammar.pegjs +++ b/lib/metagrammar.pegjs @@ -52,15 +52,8 @@ primary : identifier !(( literal / "") colon) { return new PEG.Grammar.RuleRef($1); } / literal { return new PEG.Grammar.Literal($1); } / dot { return new PEG.Grammar.Any(); } - / class { - return new PEG.Grammar.Choice( - PEG.ArrayUtils.map( - $1.split(""), - function(character) { return new PEG.Grammar.Literal(character); } - ) - ); - } - / lparen expression rparen { return $2; } + / class { return new PEG.Grammar.Class($1); } + / lparen expression rparen { return $2; } /* "Lexical" elements */ @@ -137,22 +130,24 @@ class "character class": "[" (classCharacterRange / classCharacter)* "]" __ { } classCharacterRange: bracketDelimitedCharacter "-" bracketDelimitedCharacter { - var beginCharCode = $1.charCodeAt(0); - var endCharCode = $3.charCodeAt(0); - if (beginCharCode > endCharCode) { + if ($1.charCodeAt(0) > $3.charCodeAt(0)) { throw new PEG.Parser.SyntaxError( - "Invalid character range: " + $1 + "-" + $3 + "." + "Invalid character range: " + + PEG.RegExpUtils.quoteForClass($1) + + "-" + + PEG.RegExpUtils.quoteForClass($3) + + "." ); } - var result = ""; - for (var charCode = beginCharCode; charCode <= endCharCode; charCode++) { - result += String.fromCharCode(charCode); - } - return result; + return PEG.RegExpUtils.quoteForClass($1) + + "-" + + PEG.RegExpUtils.quoteForClass($3); } -classCharacter: bracketDelimitedCharacter +classCharacter: bracketDelimitedCharacter { + return PEG.RegExpUtils.quoteForClass($1); +} bracketDelimitedCharacter : simpleBracketDelimitedCharacter diff --git a/lib/runtime.js b/lib/runtime.js index 61c889d..39e9f58 100644 --- a/lib/runtime.js +++ b/lib/runtime.js @@ -58,6 +58,29 @@ PEG.StringUtils = { }; +/* ===== PEG.RegExpUtils ===== */ + +/* RegExp manipulation utility functions. */ + +PEG.RegExpUtils = { + /* + * Escapes characters inside the string so that it can be used as a list of + * characters in a character class of a regular expresion. + */ + quoteForClass: function(s) { + /* Based on ECMA-262, 5th ed., 7.8.5 & 15.10.1. */ + return s + .replace(/\\/g, '\\\\') // backslash + .replace(/\//g, '\\/') // closing slash + .replace(/]/g, '\\]') // closing bracket + .replace(/-/g, '\\-') // dash + .replace(/\r/g, '\\r') // carriage return + .replace(/\u2028/g, '\\u2028') // line separator + .replace(/\u2029/g, '\\u2029') // paragraph separator + .replace(/\n/g, '\\n') // line feed + } +}; + /* ===== PEG.Parser ===== */ /* Prototype of all parsers generated by PEG.js. */ @@ -211,6 +234,18 @@ PEG.Parser.LiteralMatchFailure.prototype = { toString: function() { return PEG.StringUtils.quote(this._value); } }; +/* ===== PEG.Parser.ClassMatchFailure ===== */ + +/* Stores information about a class match failure. */ + +PEG.Parser.ClassMatchFailure = function(characters) { + this._characters = characters; +}; + +PEG.Parser.ClassMatchFailure.prototype = { + toString: function() { return "[" + this._characters + "]"; } +}; + /* ===== PEG.Parser.AnyMatchFailure ===== */ /* Stores information about a failure to match a "." expression. */ diff --git a/test/metagrammar-test.js b/test/metagrammar-test.js index 629047c..077033b 100644 --- a/test/metagrammar-test.js +++ b/test/metagrammar-test.js @@ -47,12 +47,7 @@ with (PEG.Grammar) { } function classGrammar(chars) { - return oneRuleGrammar(new PEG.Grammar.Choice( - PEG.ArrayUtils.map( - chars.split(""), - function(char) { return new PEG.Grammar.Literal(char); } - ) - )); + return oneRuleGrammar(new PEG.Grammar.Class(chars)); } var anyGrammar = oneRuleGrammar(new Any()); @@ -169,7 +164,7 @@ with (PEG.Grammar) { grammarParserParses('start: a', identifierGrammar("a")); grammarParserParses('start: "abcd"', literalGrammar("abcd")); grammarParserParses('start: .', anyGrammar); - grammarParserParses('start: [a-d]', classGrammar("abcd")); + grammarParserParses('start: [a-d]', classGrammar("a-d")); grammarParserParses('start: ("abcd")', literalGrammar("abcd")); }); @@ -280,17 +275,17 @@ with (PEG.Grammar) { /* Canonical class is "[a-d]". */ test("parses classCharacterRange", function() { grammarParserParses("start: []", classGrammar("")); - grammarParserParses("start: [a-d]", classGrammar("abcd")); + grammarParserParses("start: [a-d]", classGrammar("a-d")); grammarParserParses("start: [a]", classGrammar("a")); - grammarParserParses("start: [a-de-hi-l]", classGrammar("abcdefghijkl")); + grammarParserParses("start: [a-de-hi-l]", classGrammar("a-de-hi-l")); - grammarParserParses("start: [a-d]\n", classGrammar("abcd")); + grammarParserParses("start: [a-d]\n", classGrammar("a-d")); }); /* Canonical classCharacterRange is "a-d". */ test("parses classCharacterRange", function() { - grammarParserParses("start: [a-d]", classGrammar("abcd")); - grammarParserParses("start: [a-a]", classGrammar("a")); + grammarParserParses("start: [a-d]", classGrammar("a-d")); + grammarParserParses("start: [a-a]", classGrammar("a-a")); grammarParserDoesNotParse("start: [b-a]"); }); @@ -302,11 +297,11 @@ with (PEG.Grammar) { /* Canonical bracketDelimitedCharacter is "a". */ test("parses bracketDelimitedCharacter", function() { grammarParserParses("start: [a]", classGrammar("a")); - grammarParserParses("start: [\\n]", classGrammar("\n")); + grammarParserParses("start: [\\n]", classGrammar("\\n")); grammarParserParses("start: [\\0]", classGrammar("\0")); grammarParserParses("start: [\\x00]", classGrammar("\x00")); grammarParserParses("start: [\\u0120]", classGrammar("\u0120")); - grammarParserParses("start: [\\\n]", classGrammar("\n")); + grammarParserParses("start: [\\\n]", classGrammar("\\n")); }); /* Canonical simpleBracketDelimiedCharacter is "a". */ diff --git a/test/runtime-test.js b/test/runtime-test.js index 9a7a469..90b3773 100644 --- a/test/runtime-test.js +++ b/test/runtime-test.js @@ -37,4 +37,17 @@ test("quote", function() { ); }); +/* ===== PEG.RegExpUtils ===== */ + +module("PEG.RegExpUtils"); + +test("quoteForClass", function() { + strictEqual(PEG.RegExpUtils.quoteForClass(""), ''); + strictEqual(PEG.RegExpUtils.quoteForClass("abcd"), 'abcd'); + strictEqual( + PEG.RegExpUtils.quoteForClass("\\/]-\r\u2028\u2029\n\\/]-\r\u2028\u2029\n"), + '\\\\\\/\\]\\-\\r\\u2028\\u2029\\n\\\\\\/\\]\\-\\r\\u2028\\u2029\\n' + ); +}); + })();