1// Copyright 2013 the V8 project authors. All rights reserved. 2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. 3// 4// Redistribution and use in source and binary forms, with or without 5// modification, are permitted provided that the following conditions 6// are met: 7// 1. Redistributions of source code must retain the above copyright 8// notice, this list of conditions and the following disclaimer. 9// 2. Redistributions in binary form must reproduce the above copyright 10// notice, this list of conditions and the following disclaimer in the 11// documentation and/or other materials provided with the distribution. 12// 13// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 24description( 25"This test verifies that keywords and reserved words match those specified in ES5 section 7.6." 26); 27 28function isKeyword(x) 29{ 30 try { 31 eval("var "+x+";"); 32 } catch(e) { 33 return true; 34 } 35 36 return false; 37} 38 39function isStrictKeyword(x) 40{ 41 try { 42 eval("'use strict'; var "+x+";"); 43 } catch(e) { 44 return true; 45 } 46 47 return false; 48} 49 50function classifyIdentifier(x) 51{ 52 if (isKeyword(x)) { 53 // All non-strict keywords are also keywords in strict code. 54 if (!isStrictKeyword(x)) 55 return "ERROR"; 56 return "keyword"; 57 } 58 59 // Check for strict mode future reserved words. 60 if (isStrictKeyword(x)) 61 return "strict"; 62 63 return "identifier"; 64} 65 66// Not keywords - these are all just identifiers. 67shouldBe('classifyIdentifier("x")', '"identifier"'); 68shouldBe('classifyIdentifier("id")', '"identifier"'); 69shouldBe('classifyIdentifier("identifier")', '"identifier"'); 70shouldBe('classifyIdentifier("keyword")', '"identifier"'); 71shouldBe('classifyIdentifier("strict")', '"identifier"'); 72shouldBe('classifyIdentifier("use")', '"identifier"'); 73// These are identifiers that we used to treat as keywords! 74shouldBe('classifyIdentifier("abstract")', '"identifier"'); 75shouldBe('classifyIdentifier("boolean")', '"identifier"'); 76shouldBe('classifyIdentifier("byte")', '"identifier"'); 77shouldBe('classifyIdentifier("char")', '"identifier"'); 78shouldBe('classifyIdentifier("double")', '"identifier"'); 79shouldBe('classifyIdentifier("final")', '"identifier"'); 80shouldBe('classifyIdentifier("float")', '"identifier"'); 81shouldBe('classifyIdentifier("goto")', '"identifier"'); 82shouldBe('classifyIdentifier("int")', '"identifier"'); 83shouldBe('classifyIdentifier("long")', '"identifier"'); 84shouldBe('classifyIdentifier("native")', '"identifier"'); 85shouldBe('classifyIdentifier("short")', '"identifier"'); 86shouldBe('classifyIdentifier("synchronized")', '"identifier"'); 87shouldBe('classifyIdentifier("throws")', '"identifier"'); 88shouldBe('classifyIdentifier("transient")', '"identifier"'); 89shouldBe('classifyIdentifier("volatile")', '"identifier"'); 90 91// Keywords. 92shouldBe('classifyIdentifier("break")', '"keyword"'); 93shouldBe('classifyIdentifier("case")', '"keyword"'); 94shouldBe('classifyIdentifier("catch")', '"keyword"'); 95shouldBe('classifyIdentifier("continue")', '"keyword"'); 96shouldBe('classifyIdentifier("debugger")', '"keyword"'); 97shouldBe('classifyIdentifier("default")', '"keyword"'); 98shouldBe('classifyIdentifier("delete")', '"keyword"'); 99shouldBe('classifyIdentifier("do")', '"keyword"'); 100shouldBe('classifyIdentifier("else")', '"keyword"'); 101shouldBe('classifyIdentifier("finally")', '"keyword"'); 102shouldBe('classifyIdentifier("for")', '"keyword"'); 103shouldBe('classifyIdentifier("function")', '"keyword"'); 104shouldBe('classifyIdentifier("if")', '"keyword"'); 105shouldBe('classifyIdentifier("in")', '"keyword"'); 106shouldBe('classifyIdentifier("instanceof")', '"keyword"'); 107shouldBe('classifyIdentifier("new")', '"keyword"'); 108shouldBe('classifyIdentifier("return")', '"keyword"'); 109shouldBe('classifyIdentifier("switch")', '"keyword"'); 110shouldBe('classifyIdentifier("this")', '"keyword"'); 111shouldBe('classifyIdentifier("throw")', '"keyword"'); 112shouldBe('classifyIdentifier("try")', '"keyword"'); 113shouldBe('classifyIdentifier("typeof")', '"keyword"'); 114shouldBe('classifyIdentifier("var")', '"keyword"'); 115shouldBe('classifyIdentifier("void")', '"keyword"'); 116shouldBe('classifyIdentifier("while")', '"keyword"'); 117shouldBe('classifyIdentifier("with")', '"keyword"'); 118// Technically these are "Future Reserved Words"! 119shouldBe('classifyIdentifier("class")', '"keyword"'); 120shouldBe('classifyIdentifier("const")', '"keyword"'); 121shouldBe('classifyIdentifier("enum")', '"keyword"'); 122shouldBe('classifyIdentifier("export")', '"keyword"'); 123shouldBe('classifyIdentifier("extends")', '"keyword"'); 124shouldBe('classifyIdentifier("import")', '"keyword"'); 125shouldBe('classifyIdentifier("super")', '"keyword"'); 126 127// Future Reserved Words, in strict mode only. 128shouldBe('classifyIdentifier("implements")', '"strict"'); 129shouldBe('classifyIdentifier("interface")', '"strict"'); 130shouldBe('classifyIdentifier("let")', '"strict"'); 131shouldBe('classifyIdentifier("package")', '"strict"'); 132shouldBe('classifyIdentifier("private")', '"strict"'); 133shouldBe('classifyIdentifier("protected")', '"strict"'); 134shouldBe('classifyIdentifier("public")', '"strict"'); 135shouldBe('classifyIdentifier("static")', '"strict"'); 136shouldBe('classifyIdentifier("yield")', '"strict"'); 137