From f0a6bc92cc24b623689c7811bebc1ce2921442f0 Mon Sep 17 00:00:00 2001 From: David Majda Date: Sat, 1 Dec 2012 16:16:01 +0100 Subject: [PATCH] Text nodes: Use text nodes in PEG.js grammar --- lib/parser.js | 181 ++++++++++++++++++++++++++--------------------- src/parser.pegjs | 18 ++--- 2 files changed, 106 insertions(+), 93 deletions(-) diff --git a/lib/parser.js b/lib/parser.js index fb2ca94..26a0dce 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -913,38 +913,24 @@ module.exports = (function(){ pos = r2; } if (r0 !== null) { - reportedPos = r1; - r0 = (function(parts) { - return "{" + parts.join("") + "}"; - })(r4); - } - if (r0 === null) { - pos = r1; + r0 = input.substring(pos, r1); } return r0; } function parse_nonBraceCharacters() { - var r0, r1, r2; + var r0, r1; - r1 = pos; - r2 = parse_nonBraceCharacter(); - if (r2 !== null) { + r1 = parse_nonBraceCharacter(); + if (r1 !== null) { r0 = []; - while (r2 !== null) { - r0.push(r2); - r2 = parse_nonBraceCharacter(); + while (r1 !== null) { + r0.push(r1); + r1 = parse_nonBraceCharacter(); } } else { r0 = null; } - if (r0 !== null) { - reportedPos = r1; - r0 = (function(chars) { return chars.join(""); })(r0); - } - if (r0 === null) { - pos = r1; - } return r0; } @@ -1432,51 +1418,53 @@ module.exports = (function(){ } function parse_identifier() { - var r0, r1, r2, r3, r4, r5; + var r0, r1, r2, r3, r4, r5, r6, r7, r8; reportFailures++; r1 = pos; r2 = pos; - r3 = parse_letter(); - if (r3 === null) { + r4 = pos; + r5 = pos; + r6 = parse_letter(); + if (r6 === null) { if (input.charCodeAt(pos) === 95) { - r3 = "_"; + r6 = "_"; pos++; } else { - r3 = null; + r6 = null; if (reportFailures === 0) { matchFailed("\"_\""); } } } - if (r3 !== null) { - r4 = []; - r5 = parse_letter(); - if (r5 === null) { - r5 = parse_digit(); - if (r5 === null) { + if (r6 !== null) { + r7 = []; + r8 = parse_letter(); + if (r8 === null) { + r8 = parse_digit(); + if (r8 === null) { if (input.charCodeAt(pos) === 95) { - r5 = "_"; + r8 = "_"; pos++; } else { - r5 = null; + r8 = null; if (reportFailures === 0) { matchFailed("\"_\""); } } } } - while (r5 !== null) { - r4.push(r5); - r5 = parse_letter(); - if (r5 === null) { - r5 = parse_digit(); - if (r5 === null) { + while (r8 !== null) { + r7.push(r8); + r8 = parse_letter(); + if (r8 === null) { + r8 = parse_digit(); + if (r8 === null) { if (input.charCodeAt(pos) === 95) { - r5 = "_"; + r8 = "_"; pos++; } else { - r5 = null; + r8 = null; if (reportFailures === 0) { matchFailed("\"_\""); } @@ -1484,14 +1472,23 @@ module.exports = (function(){ } } } + if (r7 !== null) { + r3 = [r6, r7]; + } else { + r3 = null; + pos = r5; + } + } else { + r3 = null; + pos = r5; + } + if (r3 !== null) { + r3 = input.substring(pos, r4); + } + if (r3 !== null) { + r4 = parse___(); if (r4 !== null) { - r5 = parse___(); - if (r5 !== null) { - r0 = [r3, r4, r5]; - } else { - r0 = null; - pos = r2; - } + r0 = [r3, r4]; } else { r0 = null; pos = r2; @@ -1502,9 +1499,7 @@ module.exports = (function(){ } if (r0 !== null) { reportedPos = r1; - r0 = (function(head, tail) { - return head + tail.join(""); - })(r3, r4); + r0 = (function(chars) { return chars; })(r3); } if (r0 === null) { pos = r1; @@ -2317,7 +2312,7 @@ module.exports = (function(){ } function parse_hexEscapeSequence() { - var r0, r1, r2, r3, r4, r5; + var r0, r1, r2, r3, r4, r5, r6, r7, r8; r1 = pos; r2 = pos; @@ -2331,15 +2326,26 @@ module.exports = (function(){ } } if (r3 !== null) { - r4 = parse_hexDigit(); - if (r4 !== null) { - r5 = parse_hexDigit(); - if (r5 !== null) { - r0 = [r3, r4, r5]; + r5 = pos; + r6 = pos; + r7 = parse_hexDigit(); + if (r7 !== null) { + r8 = parse_hexDigit(); + if (r8 !== null) { + r4 = [r7, r8]; } else { - r0 = null; - pos = r2; + r4 = null; + pos = r6; } + } else { + r4 = null; + pos = r6; + } + if (r4 !== null) { + r4 = input.substring(pos, r5); + } + if (r4 !== null) { + r0 = [r3, r4]; } else { r0 = null; pos = r2; @@ -2350,9 +2356,9 @@ module.exports = (function(){ } if (r0 !== null) { reportedPos = r1; - r0 = (function(h1, h2) { - return String.fromCharCode(parseInt(h1 + h2, 16)); - })(r4, r5); + r0 = (function(digits) { + return String.fromCharCode(parseInt(digits, 16)); + })(r4); } if (r0 === null) { pos = r1; @@ -2361,7 +2367,7 @@ module.exports = (function(){ } function parse_unicodeEscapeSequence() { - var r0, r1, r2, r3, r4, r5, r6, r7; + var r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10; r1 = pos; r2 = pos; @@ -2375,27 +2381,38 @@ module.exports = (function(){ } } if (r3 !== null) { - r4 = parse_hexDigit(); - if (r4 !== null) { - r5 = parse_hexDigit(); - if (r5 !== null) { - r6 = parse_hexDigit(); - if (r6 !== null) { - r7 = parse_hexDigit(); - if (r7 !== null) { - r0 = [r3, r4, r5, r6, r7]; + r5 = pos; + r6 = pos; + r7 = parse_hexDigit(); + if (r7 !== null) { + r8 = parse_hexDigit(); + if (r8 !== null) { + r9 = parse_hexDigit(); + if (r9 !== null) { + r10 = parse_hexDigit(); + if (r10 !== null) { + r4 = [r7, r8, r9, r10]; } else { - r0 = null; - pos = r2; + r4 = null; + pos = r6; } } else { - r0 = null; - pos = r2; + r4 = null; + pos = r6; } } else { - r0 = null; - pos = r2; + r4 = null; + pos = r6; } + } else { + r4 = null; + pos = r6; + } + if (r4 !== null) { + r4 = input.substring(pos, r5); + } + if (r4 !== null) { + r0 = [r3, r4]; } else { r0 = null; pos = r2; @@ -2406,9 +2423,9 @@ module.exports = (function(){ } if (r0 !== null) { reportedPos = r1; - r0 = (function(h1, h2, h3, h4) { - return String.fromCharCode(parseInt(h1 + h2 + h3 + h4, 16)); - })(r4, r5, r6, r7); + r0 = (function(digits) { + return String.fromCharCode(parseInt(digits, 16)); + })(r4); } if (r0 === null) { pos = r1; diff --git a/src/parser.pegjs b/src/parser.pegjs index 91bac72..39ed65a 100644 --- a/src/parser.pegjs +++ b/src/parser.pegjs @@ -159,12 +159,10 @@ action "action" = braced:braced __ { return braced.substr(1, braced.length - 2); } braced - = "{" parts:(braced / nonBraceCharacters)* "}" { - return "{" + parts.join("") + "}"; - } + = $("{" (braced / nonBraceCharacters)* "}") nonBraceCharacters - = chars:nonBraceCharacter+ { return chars.join(""); } + = nonBraceCharacter+ nonBraceCharacter = [^{}] @@ -202,9 +200,7 @@ dot = "." __ { return "."; } * purpose in the grammar. */ identifier "identifier" - = head:(letter / "_") tail:(letter / digit / "_")* __ { - return head + tail.join(""); - } + = chars:$((letter / "_") (letter / digit / "_")*) __ { return chars; } /* * Modeled after ECMA-262, 5th ed., 7.8.4. (syntax & semantics, rules only @@ -319,13 +315,13 @@ zeroEscapeSequence = "\\0" !digit { return "\x00"; } hexEscapeSequence - = "\\x" h1:hexDigit h2:hexDigit { - return String.fromCharCode(parseInt(h1 + h2, 16)); + = "\\x" digits:$(hexDigit hexDigit) { + return String.fromCharCode(parseInt(digits, 16)); } unicodeEscapeSequence - = "\\u" h1:hexDigit h2:hexDigit h3:hexDigit h4:hexDigit { - return String.fromCharCode(parseInt(h1 + h2 + h3 + h4, 16)); + = "\\u" digits:$(hexDigit hexDigit hexDigit hexDigit) { + return String.fromCharCode(parseInt(digits, 16)); } eolEscapeSequence