2 Copyright (C) 2013 Yusuke Suzuki <utatane.tea@gmail.com>
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
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.
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.
28 var code = require('./code');
30 function isStrictModeReservedWordES6(id) {
46 function isKeywordES5(id, strict) {
47 // yield should not be treated as keyword under non-strict mode.
48 if (!strict && id === 'yield') {
51 return isKeywordES6(id, strict);
54 function isKeywordES6(id, strict) {
55 if (strict && isStrictModeReservedWordES6(id)) {
61 return (id === 'if') || (id === 'in') || (id === 'do');
63 return (id === 'var') || (id === 'for') || (id === 'new') || (id === 'try');
65 return (id === 'this') || (id === 'else') || (id === 'case') ||
66 (id === 'void') || (id === 'with') || (id === 'enum');
68 return (id === 'while') || (id === 'break') || (id === 'catch') ||
69 (id === 'throw') || (id === 'const') || (id === 'yield') ||
70 (id === 'class') || (id === 'super');
72 return (id === 'return') || (id === 'typeof') || (id === 'delete') ||
73 (id === 'switch') || (id === 'export') || (id === 'import');
75 return (id === 'default') || (id === 'finally') || (id === 'extends');
77 return (id === 'function') || (id === 'continue') || (id === 'debugger');
79 return (id === 'instanceof');
85 function isReservedWordES5(id, strict) {
86 return id === 'null' || id === 'true' || id === 'false' || isKeywordES5(id, strict);
89 function isReservedWordES6(id, strict) {
90 return id === 'null' || id === 'true' || id === 'false' || isKeywordES6(id, strict);
93 function isRestrictedWord(id) {
94 return id === 'eval' || id === 'arguments';
97 function isIdentifierNameES5(id) {
100 if (id.length === 0) { return false; }
102 ch = id.charCodeAt(0);
103 if (!code.isIdentifierStartES5(ch)) {
107 for (i = 1, iz = id.length; i < iz; ++i) {
108 ch = id.charCodeAt(i);
109 if (!code.isIdentifierPartES5(ch)) {
116 function decodeUtf16(lead, trail) {
117 return (lead - 0xD800) * 0x400 + (trail - 0xDC00) + 0x10000;
120 function isIdentifierNameES6(id) {
121 var i, iz, ch, lowCh, check;
123 if (id.length === 0) { return false; }
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) {
130 if (i >= iz) { return false; }
131 lowCh = id.charCodeAt(i);
132 if (!(0xDC00 <= lowCh && lowCh <= 0xDFFF)) {
135 ch = decodeUtf16(ch, lowCh);
140 check = code.isIdentifierPartES6;
145 function isIdentifierES5(id, strict) {
146 return isIdentifierNameES5(id) && !isReservedWordES5(id, strict);
149 function isIdentifierES6(id, strict) {
150 return isIdentifierNameES6(id) && !isReservedWordES6(id, strict);
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
165 /* vim: set sw=4 ts=4 et tw=80 : */