1 /* 2 * Copyright (C) 2010 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'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #ifndef JSParser_h 27 #define JSParser_h 28 29 namespace JSC { 30 31 class ExecState; 32 class FunctionParameters; 33 class Identifier; 34 class JSGlobalData; 35 class SourceCode; 36 37 enum { 38 UnaryOpTokenFlag = 64, 39 KeywordTokenFlag = 128, 40 BinaryOpTokenPrecedenceShift = 8, 41 BinaryOpTokenAllowsInPrecedenceAdditionalShift = 4, 42 BinaryOpTokenPrecedenceMask = 15 << BinaryOpTokenPrecedenceShift, 43 }; 44 45 #define BINARY_OP_PRECEDENCE(prec) (((prec) << BinaryOpTokenPrecedenceShift) | ((prec) << (BinaryOpTokenPrecedenceShift + BinaryOpTokenAllowsInPrecedenceAdditionalShift))) 46 #define IN_OP_PRECEDENCE(prec) ((prec) << (BinaryOpTokenPrecedenceShift + BinaryOpTokenAllowsInPrecedenceAdditionalShift)) 47 48 enum JSTokenType { 49 NULLTOKEN = KeywordTokenFlag, 50 TRUETOKEN, 51 FALSETOKEN, 52 BREAK, 53 CASE, 54 DEFAULT, 55 FOR, 56 NEW, 57 VAR, 58 CONSTTOKEN, 59 CONTINUE, 60 FUNCTION, 61 RETURN, 62 IF, 63 THISTOKEN, 64 DO, 65 WHILE, 66 SWITCH, 67 WITH, 68 RESERVED, 69 THROW, 70 TRY, 71 CATCH, 72 FINALLY, 73 DEBUGGER, 74 ELSE, 75 OPENBRACE = 0, 76 CLOSEBRACE, 77 OPENPAREN, 78 CLOSEPAREN, 79 OPENBRACKET, 80 CLOSEBRACKET, 81 COMMA, 82 QUESTION, 83 NUMBER, 84 IDENT, 85 STRING, 86 SEMICOLON, 87 COLON, 88 DOT, 89 ERRORTOK, 90 EOFTOK, 91 EQUAL, 92 PLUSEQUAL, 93 MINUSEQUAL, 94 MULTEQUAL, 95 DIVEQUAL, 96 LSHIFTEQUAL, 97 RSHIFTEQUAL, 98 URSHIFTEQUAL, 99 ANDEQUAL, 100 MODEQUAL, 101 XOREQUAL, 102 OREQUAL, 103 LastUntaggedToken, 104 105 // Begin tagged tokens 106 PLUSPLUS = 0 | UnaryOpTokenFlag, 107 MINUSMINUS = 1 | UnaryOpTokenFlag, 108 EXCLAMATION = 2 | UnaryOpTokenFlag, 109 TILDE = 3 | UnaryOpTokenFlag, 110 AUTOPLUSPLUS = 4 | UnaryOpTokenFlag, 111 AUTOMINUSMINUS = 5 | UnaryOpTokenFlag, 112 TYPEOF = 6 | UnaryOpTokenFlag | KeywordTokenFlag, 113 VOIDTOKEN = 7 | UnaryOpTokenFlag | KeywordTokenFlag, 114 DELETETOKEN = 8 | UnaryOpTokenFlag | KeywordTokenFlag, 115 OR = 0 | BINARY_OP_PRECEDENCE(1), 116 AND = 1 | BINARY_OP_PRECEDENCE(2), 117 BITOR = 2 | BINARY_OP_PRECEDENCE(3), 118 BITXOR = 3 | BINARY_OP_PRECEDENCE(4), 119 BITAND = 4 | BINARY_OP_PRECEDENCE(5), 120 EQEQ = 5 | BINARY_OP_PRECEDENCE(6), 121 NE = 6 | BINARY_OP_PRECEDENCE(6), 122 STREQ = 7 | BINARY_OP_PRECEDENCE(6), 123 STRNEQ = 8 | BINARY_OP_PRECEDENCE(6), 124 LT = 9 | BINARY_OP_PRECEDENCE(7), 125 GT = 10 | BINARY_OP_PRECEDENCE(7), 126 LE = 11 | BINARY_OP_PRECEDENCE(7), 127 GE = 12 | BINARY_OP_PRECEDENCE(7), 128 INSTANCEOF = 13 | BINARY_OP_PRECEDENCE(7) | KeywordTokenFlag, 129 INTOKEN = 14 | IN_OP_PRECEDENCE(7) | KeywordTokenFlag, 130 LSHIFT = 15 | BINARY_OP_PRECEDENCE(8), 131 RSHIFT = 16 | BINARY_OP_PRECEDENCE(8), 132 URSHIFT = 17 | BINARY_OP_PRECEDENCE(8), 133 PLUS = 18 | BINARY_OP_PRECEDENCE(9) | UnaryOpTokenFlag, 134 MINUS = 19 | BINARY_OP_PRECEDENCE(9) | UnaryOpTokenFlag, 135 TIMES = 20 | BINARY_OP_PRECEDENCE(10), 136 DIVIDE = 21 | BINARY_OP_PRECEDENCE(10), 137 MOD = 22 | BINARY_OP_PRECEDENCE(10) 138 }; 139 140 union JSTokenData { 141 int intValue; 142 double doubleValue; 143 const Identifier* ident; 144 }; 145 146 struct JSTokenInfo { 147 JSTokenInfo() : line(0) {} 148 int line; 149 int startOffset; 150 int endOffset; 151 }; 152 153 struct JSToken { 154 JSTokenType m_type; 155 JSTokenData m_data; 156 JSTokenInfo m_info; 157 }; 158 159 enum JSParserStrictness { JSParseNormal, JSParseStrict }; 160 enum JSParserMode { JSParseProgramCode, JSParseFunctionCode }; 161 162 const char* jsParse(JSGlobalData*, FunctionParameters*, JSParserStrictness, JSParserMode, const SourceCode*); 163 } 164 #endif // JSParser_h 165