[d565449] | 1 | /*
|
---|
| 2 | Copyright (C) 2013 Yusuke Suzuki <utatane.tea@gmail.com>
|
---|
| 3 |
|
---|
| 4 | Redistribution and use in source and binary forms, with or without
|
---|
| 5 | modification, are permitted provided that the following conditions are met:
|
---|
| 6 |
|
---|
| 7 | * Redistributions of source code must retain the above copyright
|
---|
| 8 | notice, this list of conditions and the following disclaimer.
|
---|
| 9 | * 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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
---|
| 14 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 15 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
| 16 | ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> 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
|
---|
| 20 | ON 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
|
---|
| 22 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 23 | */
|
---|
| 24 |
|
---|
| 25 | (function () {
|
---|
| 26 | 'use strict';
|
---|
| 27 |
|
---|
| 28 | var code = require('./code');
|
---|
| 29 |
|
---|
| 30 | function isStrictModeReservedWordES6(id) {
|
---|
| 31 | switch (id) {
|
---|
| 32 | case 'implements':
|
---|
| 33 | case 'interface':
|
---|
| 34 | case 'package':
|
---|
| 35 | case 'private':
|
---|
| 36 | case 'protected':
|
---|
| 37 | case 'public':
|
---|
| 38 | case 'static':
|
---|
| 39 | case 'let':
|
---|
| 40 | return true;
|
---|
| 41 | default:
|
---|
| 42 | return false;
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | function isKeywordES5(id, strict) {
|
---|
| 47 | // yield should not be treated as keyword under non-strict mode.
|
---|
| 48 | if (!strict && id === 'yield') {
|
---|
| 49 | return false;
|
---|
| 50 | }
|
---|
| 51 | return isKeywordES6(id, strict);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | function isKeywordES6(id, strict) {
|
---|
| 55 | if (strict && isStrictModeReservedWordES6(id)) {
|
---|
| 56 | return true;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | switch (id.length) {
|
---|
| 60 | case 2:
|
---|
| 61 | return (id === 'if') || (id === 'in') || (id === 'do');
|
---|
| 62 | case 3:
|
---|
| 63 | return (id === 'var') || (id === 'for') || (id === 'new') || (id === 'try');
|
---|
| 64 | case 4:
|
---|
| 65 | return (id === 'this') || (id === 'else') || (id === 'case') ||
|
---|
| 66 | (id === 'void') || (id === 'with') || (id === 'enum');
|
---|
| 67 | case 5:
|
---|
| 68 | return (id === 'while') || (id === 'break') || (id === 'catch') ||
|
---|
| 69 | (id === 'throw') || (id === 'const') || (id === 'yield') ||
|
---|
| 70 | (id === 'class') || (id === 'super');
|
---|
| 71 | case 6:
|
---|
| 72 | return (id === 'return') || (id === 'typeof') || (id === 'delete') ||
|
---|
| 73 | (id === 'switch') || (id === 'export') || (id === 'import');
|
---|
| 74 | case 7:
|
---|
| 75 | return (id === 'default') || (id === 'finally') || (id === 'extends');
|
---|
| 76 | case 8:
|
---|
| 77 | return (id === 'function') || (id === 'continue') || (id === 'debugger');
|
---|
| 78 | case 10:
|
---|
| 79 | return (id === 'instanceof');
|
---|
| 80 | default:
|
---|
| 81 | return false;
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | function isReservedWordES5(id, strict) {
|
---|
| 86 | return id === 'null' || id === 'true' || id === 'false' || isKeywordES5(id, strict);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | function isReservedWordES6(id, strict) {
|
---|
| 90 | return id === 'null' || id === 'true' || id === 'false' || isKeywordES6(id, strict);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | function isRestrictedWord(id) {
|
---|
| 94 | return id === 'eval' || id === 'arguments';
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | function isIdentifierNameES5(id) {
|
---|
| 98 | var i, iz, ch;
|
---|
| 99 |
|
---|
| 100 | if (id.length === 0) { return false; }
|
---|
| 101 |
|
---|
| 102 | ch = id.charCodeAt(0);
|
---|
| 103 | if (!code.isIdentifierStartES5(ch)) {
|
---|
| 104 | return false;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | for (i = 1, iz = id.length; i < iz; ++i) {
|
---|
| 108 | ch = id.charCodeAt(i);
|
---|
| 109 | if (!code.isIdentifierPartES5(ch)) {
|
---|
| 110 | return false;
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | return true;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | function decodeUtf16(lead, trail) {
|
---|
| 117 | return (lead - 0xD800) * 0x400 + (trail - 0xDC00) + 0x10000;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | function isIdentifierNameES6(id) {
|
---|
| 121 | var i, iz, ch, lowCh, check;
|
---|
| 122 |
|
---|
| 123 | if (id.length === 0) { return false; }
|
---|
| 124 |
|
---|
| 125 | check = code.isIdentifierStartES6;
|
---|
| 126 | for (i = 0, iz = id.length; i < iz; ++i) {
|
---|
| 127 | ch = id.charCodeAt(i);
|
---|
| 128 | if (0xD800 <= ch && ch <= 0xDBFF) {
|
---|
| 129 | ++i;
|
---|
| 130 | if (i >= iz) { return false; }
|
---|
| 131 | lowCh = id.charCodeAt(i);
|
---|
| 132 | if (!(0xDC00 <= lowCh && lowCh <= 0xDFFF)) {
|
---|
| 133 | return false;
|
---|
| 134 | }
|
---|
| 135 | ch = decodeUtf16(ch, lowCh);
|
---|
| 136 | }
|
---|
| 137 | if (!check(ch)) {
|
---|
| 138 | return false;
|
---|
| 139 | }
|
---|
| 140 | check = code.isIdentifierPartES6;
|
---|
| 141 | }
|
---|
| 142 | return true;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | function isIdentifierES5(id, strict) {
|
---|
| 146 | return isIdentifierNameES5(id) && !isReservedWordES5(id, strict);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | function isIdentifierES6(id, strict) {
|
---|
| 150 | return isIdentifierNameES6(id) && !isReservedWordES6(id, strict);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | module.exports = {
|
---|
| 154 | isKeywordES5: isKeywordES5,
|
---|
| 155 | isKeywordES6: isKeywordES6,
|
---|
| 156 | isReservedWordES5: isReservedWordES5,
|
---|
| 157 | isReservedWordES6: isReservedWordES6,
|
---|
| 158 | isRestrictedWord: isRestrictedWord,
|
---|
| 159 | isIdentifierNameES5: isIdentifierNameES5,
|
---|
| 160 | isIdentifierNameES6: isIdentifierNameES6,
|
---|
| 161 | isIdentifierES5: isIdentifierES5,
|
---|
| 162 | isIdentifierES6: isIdentifierES6
|
---|
| 163 | };
|
---|
| 164 | }());
|
---|
| 165 | /* vim: set sw=4 ts=4 et tw=80 : */
|
---|