Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 | |
| 24 | description( |
| 25 | "This test verifies that keywords and reserved words match those specified in ES5 section 7.6." |
| 26 | ); |
| 27 | |
| 28 | function isKeyword(x) |
| 29 | { |
| 30 | try { |
| 31 | eval("var "+x+";"); |
| 32 | } catch(e) { |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | function isStrictKeyword(x) |
| 40 | { |
| 41 | try { |
| 42 | eval("'use strict'; var "+x+";"); |
| 43 | } catch(e) { |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | function 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. |
| 67 | shouldBe('classifyIdentifier("x")', '"identifier"'); |
| 68 | shouldBe('classifyIdentifier("id")', '"identifier"'); |
| 69 | shouldBe('classifyIdentifier("identifier")', '"identifier"'); |
| 70 | shouldBe('classifyIdentifier("keyword")', '"identifier"'); |
| 71 | shouldBe('classifyIdentifier("strict")', '"identifier"'); |
| 72 | shouldBe('classifyIdentifier("use")', '"identifier"'); |
| 73 | // These are identifiers that we used to treat as keywords! |
| 74 | shouldBe('classifyIdentifier("abstract")', '"identifier"'); |
| 75 | shouldBe('classifyIdentifier("boolean")', '"identifier"'); |
| 76 | shouldBe('classifyIdentifier("byte")', '"identifier"'); |
| 77 | shouldBe('classifyIdentifier("char")', '"identifier"'); |
| 78 | shouldBe('classifyIdentifier("double")', '"identifier"'); |
| 79 | shouldBe('classifyIdentifier("final")', '"identifier"'); |
| 80 | shouldBe('classifyIdentifier("float")', '"identifier"'); |
| 81 | shouldBe('classifyIdentifier("goto")', '"identifier"'); |
| 82 | shouldBe('classifyIdentifier("int")', '"identifier"'); |
| 83 | shouldBe('classifyIdentifier("long")', '"identifier"'); |
| 84 | shouldBe('classifyIdentifier("native")', '"identifier"'); |
| 85 | shouldBe('classifyIdentifier("short")', '"identifier"'); |
| 86 | shouldBe('classifyIdentifier("synchronized")', '"identifier"'); |
| 87 | shouldBe('classifyIdentifier("throws")', '"identifier"'); |
| 88 | shouldBe('classifyIdentifier("transient")', '"identifier"'); |
| 89 | shouldBe('classifyIdentifier("volatile")', '"identifier"'); |
| 90 | |
| 91 | // Keywords. |
| 92 | shouldBe('classifyIdentifier("break")', '"keyword"'); |
| 93 | shouldBe('classifyIdentifier("case")', '"keyword"'); |
| 94 | shouldBe('classifyIdentifier("catch")', '"keyword"'); |
| 95 | shouldBe('classifyIdentifier("continue")', '"keyword"'); |
| 96 | shouldBe('classifyIdentifier("debugger")', '"keyword"'); |
| 97 | shouldBe('classifyIdentifier("default")', '"keyword"'); |
| 98 | shouldBe('classifyIdentifier("delete")', '"keyword"'); |
| 99 | shouldBe('classifyIdentifier("do")', '"keyword"'); |
| 100 | shouldBe('classifyIdentifier("else")', '"keyword"'); |
| 101 | shouldBe('classifyIdentifier("finally")', '"keyword"'); |
| 102 | shouldBe('classifyIdentifier("for")', '"keyword"'); |
| 103 | shouldBe('classifyIdentifier("function")', '"keyword"'); |
| 104 | shouldBe('classifyIdentifier("if")', '"keyword"'); |
| 105 | shouldBe('classifyIdentifier("in")', '"keyword"'); |
| 106 | shouldBe('classifyIdentifier("instanceof")', '"keyword"'); |
| 107 | shouldBe('classifyIdentifier("new")', '"keyword"'); |
| 108 | shouldBe('classifyIdentifier("return")', '"keyword"'); |
| 109 | shouldBe('classifyIdentifier("switch")', '"keyword"'); |
| 110 | shouldBe('classifyIdentifier("this")', '"keyword"'); |
| 111 | shouldBe('classifyIdentifier("throw")', '"keyword"'); |
| 112 | shouldBe('classifyIdentifier("try")', '"keyword"'); |
| 113 | shouldBe('classifyIdentifier("typeof")', '"keyword"'); |
| 114 | shouldBe('classifyIdentifier("var")', '"keyword"'); |
| 115 | shouldBe('classifyIdentifier("void")', '"keyword"'); |
| 116 | shouldBe('classifyIdentifier("while")', '"keyword"'); |
| 117 | shouldBe('classifyIdentifier("with")', '"keyword"'); |
| 118 | // Technically these are "Future Reserved Words"! |
| 119 | shouldBe('classifyIdentifier("class")', '"keyword"'); |
| 120 | shouldBe('classifyIdentifier("const")', '"keyword"'); |
| 121 | shouldBe('classifyIdentifier("enum")', '"keyword"'); |
| 122 | shouldBe('classifyIdentifier("export")', '"keyword"'); |
| 123 | shouldBe('classifyIdentifier("extends")', '"keyword"'); |
| 124 | shouldBe('classifyIdentifier("import")', '"keyword"'); |
| 125 | shouldBe('classifyIdentifier("super")', '"keyword"'); |
| 126 | |
| 127 | // Future Reserved Words, in strict mode only. |
| 128 | shouldBe('classifyIdentifier("implements")', '"strict"'); |
| 129 | shouldBe('classifyIdentifier("interface")', '"strict"'); |
| 130 | shouldBe('classifyIdentifier("let")', '"strict"'); |
| 131 | shouldBe('classifyIdentifier("package")', '"strict"'); |
| 132 | shouldBe('classifyIdentifier("private")', '"strict"'); |
| 133 | shouldBe('classifyIdentifier("protected")', '"strict"'); |
| 134 | shouldBe('classifyIdentifier("public")', '"strict"'); |
| 135 | shouldBe('classifyIdentifier("static")', '"strict"'); |
| 136 | shouldBe('classifyIdentifier("yield")', '"strict"'); |