From 4e968892be47c013ef660793b6b5a0d49ee55dd2 Mon Sep 17 00:00:00 2001 From: David Majda Date: Fri, 21 May 2010 20:50:29 +0200 Subject: [PATCH] Guard against redefinition of |undefined| In most cases, code pattern x === undefined was transformed to typeof(x) === "undefined" and similarly with |!==|. In the generated code, the condition was simply made less strict to avoid performance penalty of string comparison (I don't think JavaScript VMs optimize this specific pattern to avoid it). --- lib/compiler.js | 12 +++--- lib/metagrammar.js | 104 ++++++++++++++++++++++----------------------- 2 files changed, 58 insertions(+), 58 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index 0fff670..7823a9b 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -45,7 +45,7 @@ PEG.GrammarError.prototype = Error.prototype; PEG.ArrayUtils = { /* Like Python's |range|, but without |step|. */ range: function(start, stop) { - if (stop === undefined) { + if (typeof(stop) === "undefined") { stop = start; start = 0; } @@ -199,11 +199,11 @@ PEG.Compiler = { /\$\{([a-zA-Z_][a-zA-Z0-9_]*)(\|([a-zA-Z_][a-zA-Z0-9_]*))?\}/g, function(match, name, dummy, filter) { var value = vars[name]; - if (value === undefined) { + if (typeof(value) === "undefined") { throw new Error("Undefined variable: \"" + name + "\"."); } - if (filter !== undefined && filter != "") { // JavaScript engines differ here. + if (typeof(filter) !== "undefined" && filter != "") { // JavaScript engines differ here. if (filter === "string") { return PEG.StringUtils.quote(value); } else { @@ -284,7 +284,7 @@ PEG.Compiler = { rule_ref: function(node) { - if (ast[node.name] === undefined) { + if (typeof(ast[node.name]) === "undefined") { throw new PEG.GrammarError( "Referenced rule \"" + node.name + "\" does not exist." ); @@ -305,7 +305,7 @@ PEG.Compiler = { /* Checks that the start rule is defined. */ function(ast, startRule) { - if (ast[startRule] === undefined) { + if (typeof(ast[startRule]) === "undefined") { throw new PEG.GrammarError( "Missing \"" + startRule + "\" rule." ); @@ -403,7 +403,7 @@ PEG.Compiler = { "_parse_${name}: function(context) {", " var cacheKey = ${name|string} + '@' + this._pos;", " var cachedResult = this._cache[cacheKey];", - " if (cachedResult !== undefined) {", + " if (cachedResult) {", " this._pos = cachedResult.nextPos;", " return cachedResult.result;", " }", diff --git a/lib/metagrammar.js b/lib/metagrammar.js index ac02aa0..2a8520b 100644 --- a/lib/metagrammar.js +++ b/lib/metagrammar.js @@ -49,7 +49,7 @@ PEG.grammarParser = (function(){ _parse_grammar: function(context) { var cacheKey = "grammar" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -100,7 +100,7 @@ PEG.grammarParser = (function(){ _parse_rule: function(context) { var cacheKey = "rule" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -175,7 +175,7 @@ PEG.grammarParser = (function(){ _parse_expression: function(context) { var cacheKey = "expression" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -197,7 +197,7 @@ PEG.grammarParser = (function(){ _parse_choice: function(context) { var cacheKey = "choice" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -279,7 +279,7 @@ PEG.grammarParser = (function(){ _parse_sequence: function(context) { var cacheKey = "sequence" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -359,7 +359,7 @@ PEG.grammarParser = (function(){ _parse_prefixed: function(context) { var cacheKey = "prefixed" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -428,7 +428,7 @@ PEG.grammarParser = (function(){ _parse_suffixed: function(context) { var cacheKey = "suffixed" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -518,7 +518,7 @@ PEG.grammarParser = (function(){ _parse_primary: function(context) { var cacheKey = "primary" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -653,7 +653,7 @@ PEG.grammarParser = (function(){ _parse_action: function(context) { var cacheKey = "action" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -694,7 +694,7 @@ PEG.grammarParser = (function(){ _parse_braced: function(context) { var cacheKey = "braced" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -779,7 +779,7 @@ PEG.grammarParser = (function(){ _parse_nonBraceCharacters: function(context) { var cacheKey = "nonBraceCharacters" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -813,7 +813,7 @@ PEG.grammarParser = (function(){ _parse_nonBraceCharacter: function(context) { var cacheKey = "nonBraceCharacter" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -846,7 +846,7 @@ PEG.grammarParser = (function(){ _parse_colon: function(context) { var cacheKey = "colon" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -892,7 +892,7 @@ PEG.grammarParser = (function(){ _parse_slash: function(context) { var cacheKey = "slash" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -938,7 +938,7 @@ PEG.grammarParser = (function(){ _parse_and: function(context) { var cacheKey = "and" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -984,7 +984,7 @@ PEG.grammarParser = (function(){ _parse_not: function(context) { var cacheKey = "not" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -1030,7 +1030,7 @@ PEG.grammarParser = (function(){ _parse_question: function(context) { var cacheKey = "question" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -1076,7 +1076,7 @@ PEG.grammarParser = (function(){ _parse_star: function(context) { var cacheKey = "star" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -1122,7 +1122,7 @@ PEG.grammarParser = (function(){ _parse_plus: function(context) { var cacheKey = "plus" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -1168,7 +1168,7 @@ PEG.grammarParser = (function(){ _parse_lparen: function(context) { var cacheKey = "lparen" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -1214,7 +1214,7 @@ PEG.grammarParser = (function(){ _parse_rparen: function(context) { var cacheKey = "rparen" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -1260,7 +1260,7 @@ PEG.grammarParser = (function(){ _parse_dot: function(context) { var cacheKey = "dot" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -1306,7 +1306,7 @@ PEG.grammarParser = (function(){ _parse_identifier: function(context) { var cacheKey = "identifier" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -1463,7 +1463,7 @@ PEG.grammarParser = (function(){ _parse_literal: function(context) { var cacheKey = "literal" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -1514,7 +1514,7 @@ PEG.grammarParser = (function(){ _parse_doubleQuotedLiteral: function(context) { var cacheKey = "doubleQuotedLiteral" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -1579,7 +1579,7 @@ PEG.grammarParser = (function(){ _parse_doubleQuotedCharacter: function(context) { var cacheKey = "doubleQuotedCharacter" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -1631,7 +1631,7 @@ PEG.grammarParser = (function(){ _parse_simpleDoubleQuotedCharacter: function(context) { var cacheKey = "simpleDoubleQuotedCharacter" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -1718,7 +1718,7 @@ PEG.grammarParser = (function(){ _parse_singleQuotedLiteral: function(context) { var cacheKey = "singleQuotedLiteral" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -1783,7 +1783,7 @@ PEG.grammarParser = (function(){ _parse_singleQuotedCharacter: function(context) { var cacheKey = "singleQuotedCharacter" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -1835,7 +1835,7 @@ PEG.grammarParser = (function(){ _parse_simpleSingleQuotedCharacter: function(context) { var cacheKey = "simpleSingleQuotedCharacter" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -1922,7 +1922,7 @@ PEG.grammarParser = (function(){ _parse_class: function(context) { var cacheKey = "class" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -2033,7 +2033,7 @@ PEG.grammarParser = (function(){ _parse_classCharacterRange: function(context) { var cacheKey = "classCharacterRange" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -2099,7 +2099,7 @@ PEG.grammarParser = (function(){ _parse_classCharacter: function(context) { var cacheKey = "classCharacter" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -2126,7 +2126,7 @@ PEG.grammarParser = (function(){ _parse_bracketDelimitedCharacter: function(context) { var cacheKey = "bracketDelimitedCharacter" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -2178,7 +2178,7 @@ PEG.grammarParser = (function(){ _parse_simpleBracketDelimitedCharacter: function(context) { var cacheKey = "simpleBracketDelimitedCharacter" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -2265,7 +2265,7 @@ PEG.grammarParser = (function(){ _parse_simpleEscapeSequence: function(context) { var cacheKey = "simpleEscapeSequence" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -2379,7 +2379,7 @@ PEG.grammarParser = (function(){ _parse_zeroEscapeSequence: function(context) { var cacheKey = "zeroEscapeSequence" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -2435,7 +2435,7 @@ PEG.grammarParser = (function(){ _parse_hexEscapeSequence: function(context) { var cacheKey = "hexEscapeSequence" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -2489,7 +2489,7 @@ PEG.grammarParser = (function(){ _parse_unicodeEscapeSequence: function(context) { var cacheKey = "unicodeEscapeSequence" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -2555,7 +2555,7 @@ PEG.grammarParser = (function(){ _parse_eolEscapeSequence: function(context) { var cacheKey = "eolEscapeSequence" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -2601,7 +2601,7 @@ PEG.grammarParser = (function(){ _parse_digit: function(context) { var cacheKey = "digit" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -2631,7 +2631,7 @@ PEG.grammarParser = (function(){ _parse_hexDigit: function(context) { var cacheKey = "hexDigit" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -2661,7 +2661,7 @@ PEG.grammarParser = (function(){ _parse_letter: function(context) { var cacheKey = "letter" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -2693,7 +2693,7 @@ PEG.grammarParser = (function(){ _parse_lowerCaseLetter: function(context) { var cacheKey = "lowerCaseLetter" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -2723,7 +2723,7 @@ PEG.grammarParser = (function(){ _parse_upperCaseLetter: function(context) { var cacheKey = "upperCaseLetter" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -2753,7 +2753,7 @@ PEG.grammarParser = (function(){ _parse___: function(context) { var cacheKey = "__" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -2810,7 +2810,7 @@ PEG.grammarParser = (function(){ _parse_comment: function(context) { var cacheKey = "comment" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -2845,7 +2845,7 @@ PEG.grammarParser = (function(){ _parse_singleLineComment: function(context) { var cacheKey = "singleLineComment" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -2955,7 +2955,7 @@ PEG.grammarParser = (function(){ _parse_multiLineComment: function(context) { var cacheKey = "multiLineComment" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -3095,7 +3095,7 @@ PEG.grammarParser = (function(){ _parse_eol: function(context) { var cacheKey = "eol" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -3185,7 +3185,7 @@ PEG.grammarParser = (function(){ _parse_eolChar: function(context) { var cacheKey = "eolChar" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; } @@ -3215,7 +3215,7 @@ PEG.grammarParser = (function(){ _parse_whitespace: function(context) { var cacheKey = "whitespace" + '@' + this._pos; var cachedResult = this._cache[cacheKey]; - if (cachedResult !== undefined) { + if (cachedResult) { this._pos = cachedResult.nextPos; return cachedResult.result; }