Changeset 13153 in webkit for trunk/JavaScriptCore/kjs/Parser.h
- Timestamp:
- Mar 5, 2006, 9:29:48 PM (19 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/Parser.h
r13127 r13153 1 // -*- c-basic-offset: 2-*-1 // -*- c-basic-offset: 4 -*- 2 2 /* 3 3 * This file is part of the KDE libraries 4 4 * Copyright (C) 1999-2001 Harri Porten ([email protected]) 5 5 * Copyright (C) 2001 Peter Kelly ([email protected]) 6 * Copyright (C) 2003 Apple Computer, Inc.6 * Copyright (C) 2003, 2006 Apple Computer, Inc. 7 7 * 8 8 * This library is free software; you can redistribute it and/or … … 23 23 */ 24 24 25 #ifndef INTERNAL_H26 #define INTERNAL_H25 #ifndef Parser_h 26 #define Parser_h 27 27 28 #include "JSType.h" 29 #include "interpreter.h" 30 #include "object.h" 31 #include "protect.h" 32 #include "scope_chain.h" 33 #include "types.h" 34 #include "ustring.h" 35 36 #include <kxmlcore/Noncopyable.h> 37 #include <kxmlcore/RefPtr.h> 38 39 #define I18N_NOOP(s) s 28 #include <kxmlcore/PassRefPtr.h> 40 29 41 30 namespace KJS { 42 31 43 class Node; 44 class ProgramNode; 45 class FunctionBodyNode; 46 class FunctionPrototype; 47 class FunctionImp; 48 class Debugger; 49 50 // --------------------------------------------------------------------------- 51 // Primitive impls 52 // --------------------------------------------------------------------------- 53 54 class StringImp : public JSCell { 55 public: 56 StringImp(const UString& v) : val(v) { } 57 UString value() const { return val; } 58 59 JSType type() const { return StringType; } 60 61 JSValue *toPrimitive(ExecState *exec, JSType preferred = UnspecifiedType) const; 62 bool toBoolean(ExecState *exec) const; 63 double toNumber(ExecState *exec) const; 64 UString toString(ExecState *exec) const; 65 JSObject *toObject(ExecState *exec) const; 66 67 private: 68 UString val; 69 }; 70 71 class NumberImp : public JSCell { 72 friend class ConstantValues; 73 friend class InterpreterImp; 74 friend JSValue *jsNumberCell(double); 75 public: 76 double value() const { return val; } 77 78 JSType type() const { return NumberType; } 79 80 JSValue *toPrimitive(ExecState *exec, JSType preferred = UnspecifiedType) const; 81 bool toBoolean(ExecState *exec) const; 82 double toNumber(ExecState *exec) const; 83 UString toString(ExecState *exec) const; 84 JSObject *toObject(ExecState *exec) const; 85 86 private: 87 NumberImp(double v) : val(v) { } 88 89 virtual bool getUInt32(uint32_t&) const; 90 91 double val; 92 }; 93 94 95 /** 96 * @short The "label set" in Ecma-262 spec 97 */ 98 class LabelStack : Noncopyable { 99 public: 100 LabelStack(): tos(0), iterationDepth(0), switchDepth(0) {} 101 ~LabelStack(); 32 class Node; 33 class ProgramNode; 34 class UChar; 35 class UString; 102 36 103 37 /** 104 * If id is not empty and is not in the stack already, puts it on top of 105 * the stack and returns true, otherwise returns false 38 * @internal 39 * 40 * Parses ECMAScript source code and converts into ProgramNode objects, which 41 * represent the root of a parse tree. This class provides a convenient workaround 42 * for the problem of the bison parser working in a static context. 106 43 */ 107 bool push(const Identifier &id); 108 /** 109 * Is the id in the stack? 110 */ 111 bool contains(const Identifier &id) const; 112 /** 113 * Removes from the stack the last pushed id (what else?) 114 */ 115 void pop(); 116 117 void pushIteration() { iterationDepth++; } 118 void popIteration() { iterationDepth--; } 119 bool inIteration() const { return (iterationDepth > 0); } 120 121 void pushSwitch() { switchDepth++; } 122 void popSwitch() { switchDepth--; } 123 bool inSwitch() const { return (switchDepth > 0); } 124 125 private: 126 struct StackElem { 127 Identifier id; 128 StackElem *prev; 44 class Parser { 45 public: 46 static PassRefPtr<ProgramNode> parse(const UString& sourceURL, int startingLineNumber, 47 const UChar* code, unsigned length, 48 int* sourceId = 0, int* errLine = 0, UString* errMsg = 0); 49 50 static void accept(PassRefPtr<ProgramNode>); 51 52 static void saveNewNode(Node*); 53 static void noteNodeCycle(Node*); 54 static void removeNodeCycle(Node*); 55 56 static int sid; 129 57 }; 130 131 StackElem *tos;132 int iterationDepth;133 int switchDepth;134 };135 136 137 // ---------------------------------------------------------------------------138 // Parsing & evaluation139 // ---------------------------------------------------------------------------140 141 enum CodeType { GlobalCode,142 EvalCode,143 FunctionCode,144 AnonymousCode };145 146 /**147 * @internal148 *149 * Parses ECMAScript source code and converts into ProgramNode objects, which150 * represent the root of a parse tree. This class provides a conveniant workaround151 * for the problem of the bison parser working in a static context.152 */153 class Parser {154 public:155 static RefPtr<ProgramNode> parse(const UString &sourceURL, int startingLineNumber,156 const UChar *code, unsigned int length, int *sourceId = 0,157 int *errLine = 0, UString *errMsg = 0);158 static void accept(ProgramNode *prog);159 160 static void saveNewNode(Node *node);161 static void noteNodeCycle(Node *node);162 static void removeNodeCycle(Node *node);163 164 static int sid;165 };166 167 class SavedBuiltinsInternal {168 friend class InterpreterImp;169 private:170 ProtectedPtr<JSObject> b_Object;171 ProtectedPtr<JSObject> b_Function;172 ProtectedPtr<JSObject> b_Array;173 ProtectedPtr<JSObject> b_Boolean;174 ProtectedPtr<JSObject> b_String;175 ProtectedPtr<JSObject> b_Number;176 ProtectedPtr<JSObject> b_Date;177 ProtectedPtr<JSObject> b_RegExp;178 ProtectedPtr<JSObject> b_Error;179 180 ProtectedPtr<JSObject> b_ObjectPrototype;181 ProtectedPtr<JSObject> b_FunctionPrototype;182 ProtectedPtr<JSObject> b_ArrayPrototype;183 ProtectedPtr<JSObject> b_BooleanPrototype;184 ProtectedPtr<JSObject> b_StringPrototype;185 ProtectedPtr<JSObject> b_NumberPrototype;186 ProtectedPtr<JSObject> b_DatePrototype;187 ProtectedPtr<JSObject> b_RegExpPrototype;188 ProtectedPtr<JSObject> b_ErrorPrototype;189 190 ProtectedPtr<JSObject> b_evalError;191 ProtectedPtr<JSObject> b_rangeError;192 ProtectedPtr<JSObject> b_referenceError;193 ProtectedPtr<JSObject> b_syntaxError;194 ProtectedPtr<JSObject> b_typeError;195 ProtectedPtr<JSObject> b_uriError;196 197 ProtectedPtr<JSObject> b_evalErrorPrototype;198 ProtectedPtr<JSObject> b_rangeErrorPrototype;199 ProtectedPtr<JSObject> b_referenceErrorPrototype;200 ProtectedPtr<JSObject> b_syntaxErrorPrototype;201 ProtectedPtr<JSObject> b_typeErrorPrototype;202 ProtectedPtr<JSObject> b_uriErrorPrototype;203 };204 205 class InterpreterImp {206 friend class Collector;207 public:208 InterpreterImp(Interpreter *interp, JSObject *glob);209 ~InterpreterImp();210 211 JSObject *globalObject() { return global; }212 Interpreter *interpreter() const { return m_interpreter; }213 214 void initGlobalObject();215 216 void mark();217 218 ExecState *globalExec() { return &globExec; }219 bool checkSyntax(const UString &code);220 Completion evaluate(const UChar* code, int codeLength, JSValue* thisV, const UString& sourceURL, int startingLineNumber);221 Debugger *debugger() const { return dbg; }222 void setDebugger(Debugger *d) { dbg = d; }223 224 JSObject *builtinObject() const { return b_Object; }225 JSObject *builtinFunction() const { return b_Function; }226 JSObject *builtinArray() const { return b_Array; }227 JSObject *builtinBoolean() const { return b_Boolean; }228 JSObject *builtinString() const { return b_String; }229 JSObject *builtinNumber() const { return b_Number; }230 JSObject *builtinDate() const { return b_Date; }231 JSObject *builtinRegExp() const { return b_RegExp; }232 JSObject *builtinError() const { return b_Error; }233 234 JSObject *builtinObjectPrototype() const { return b_ObjectPrototype; }235 JSObject *builtinFunctionPrototype() const { return b_FunctionPrototype; }236 JSObject *builtinArrayPrototype() const { return b_ArrayPrototype; }237 JSObject *builtinBooleanPrototype() const { return b_BooleanPrototype; }238 JSObject *builtinStringPrototype() const { return b_StringPrototype; }239 JSObject *builtinNumberPrototype() const { return b_NumberPrototype; }240 JSObject *builtinDatePrototype() const { return b_DatePrototype; }241 JSObject *builtinRegExpPrototype() const { return b_RegExpPrototype; }242 JSObject *builtinErrorPrototype() const { return b_ErrorPrototype; }243 244 JSObject *builtinEvalError() const { return b_evalError; }245 JSObject *builtinRangeError() const { return b_rangeError; }246 JSObject *builtinReferenceError() const { return b_referenceError; }247 JSObject *builtinSyntaxError() const { return b_syntaxError; }248 JSObject *builtinTypeError() const { return b_typeError; }249 JSObject *builtinURIError() const { return b_uriError; }250 251 JSObject *builtinEvalErrorPrototype() const { return b_evalErrorPrototype; }252 JSObject *builtinRangeErrorPrototype() const { return b_rangeErrorPrototype; }253 JSObject *builtinReferenceErrorPrototype() const { return b_referenceErrorPrototype; }254 JSObject *builtinSyntaxErrorPrototype() const { return b_syntaxErrorPrototype; }255 JSObject *builtinTypeErrorPrototype() const { return b_typeErrorPrototype; }256 JSObject *builtinURIErrorPrototype() const { return b_uriErrorPrototype; }257 258 void setCompatMode(Interpreter::CompatMode mode) { m_compatMode = mode; }259 Interpreter::CompatMode compatMode() const { return m_compatMode; }260 261 // Chained list of interpreters (ring)262 static InterpreterImp* firstInterpreter() { return s_hook; }263 InterpreterImp *nextInterpreter() const { return next; }264 InterpreterImp *prevInterpreter() const { return prev; }265 266 static InterpreterImp *interpreterWithGlobalObject(JSObject *);267 268 void setContext(ContextImp *c) { _context = c; }269 ContextImp *context() const { return _context; }270 271 void saveBuiltins (SavedBuiltins &builtins) const;272 void restoreBuiltins (const SavedBuiltins &builtins);273 274 private:275 void clear();276 Interpreter *m_interpreter;277 JSObject *global;278 Debugger *dbg;279 280 // Built-in properties of the object prototype. These are accessible281 // from here even if they are replaced by js code (e.g. assigning to282 // Array.prototype)283 284 ProtectedPtr<JSObject> b_Object;285 ProtectedPtr<JSObject> b_Function;286 ProtectedPtr<JSObject> b_Array;287 ProtectedPtr<JSObject> b_Boolean;288 ProtectedPtr<JSObject> b_String;289 ProtectedPtr<JSObject> b_Number;290 ProtectedPtr<JSObject> b_Date;291 ProtectedPtr<JSObject> b_RegExp;292 ProtectedPtr<JSObject> b_Error;293 294 ProtectedPtr<JSObject> b_ObjectPrototype;295 ProtectedPtr<JSObject> b_FunctionPrototype;296 ProtectedPtr<JSObject> b_ArrayPrototype;297 ProtectedPtr<JSObject> b_BooleanPrototype;298 ProtectedPtr<JSObject> b_StringPrototype;299 ProtectedPtr<JSObject> b_NumberPrototype;300 ProtectedPtr<JSObject> b_DatePrototype;301 ProtectedPtr<JSObject> b_RegExpPrototype;302 ProtectedPtr<JSObject> b_ErrorPrototype;303 304 ProtectedPtr<JSObject> b_evalError;305 ProtectedPtr<JSObject> b_rangeError;306 ProtectedPtr<JSObject> b_referenceError;307 ProtectedPtr<JSObject> b_syntaxError;308 ProtectedPtr<JSObject> b_typeError;309 ProtectedPtr<JSObject> b_uriError;310 311 ProtectedPtr<JSObject> b_evalErrorPrototype;312 ProtectedPtr<JSObject> b_rangeErrorPrototype;313 ProtectedPtr<JSObject> b_referenceErrorPrototype;314 ProtectedPtr<JSObject> b_syntaxErrorPrototype;315 ProtectedPtr<JSObject> b_typeErrorPrototype;316 ProtectedPtr<JSObject> b_uriErrorPrototype;317 318 ExecState globExec;319 Interpreter::CompatMode m_compatMode;320 321 // Chained list of interpreters (ring) - for collector322 static InterpreterImp* s_hook;323 InterpreterImp *next, *prev;324 325 ContextImp *_context;326 327 int recursion;328 };329 330 class AttachedInterpreter;331 class DebuggerImp {332 public:333 334 DebuggerImp() {335 interps = 0;336 isAborted = false;337 }338 339 void abort() { isAborted = true; }340 bool aborted() const { return isAborted; }341 342 AttachedInterpreter *interps;343 bool isAborted;344 };345 346 class InternalFunctionImp : public JSObject {347 public:348 InternalFunctionImp();349 InternalFunctionImp(FunctionPrototype*);350 InternalFunctionImp(FunctionPrototype*, const Identifier&);351 352 virtual bool implementsCall() const;353 virtual JSValue* callAsFunction(ExecState*, JSObject* thisObjec, const List& args) = 0;354 virtual bool implementsHasInstance() const;355 virtual bool hasInstance(ExecState*, JSValue*);356 357 virtual const ClassInfo* classInfo() const { return &info; }358 static const ClassInfo info;359 const Identifier& functionName() const { return m_name; }360 361 private:362 Identifier m_name;363 };364 365 // helper function for toInteger, toInt32, toUInt32 and toUInt16366 double roundValue(ExecState *, JSValue *);367 368 #ifndef NDEBUG369 void printInfo(ExecState *exec, const char *s, JSValue *, int lineno = -1);370 #endif371 372 inline LabelStack::~LabelStack()373 {374 StackElem *prev;375 for (StackElem *e = tos; e; e = prev) {376 prev = e->prev;377 delete e;378 }379 }380 381 inline void LabelStack::pop()382 {383 if (StackElem *e = tos) {384 tos = e->prev;385 delete e;386 }387 }388 58 389 59 } // namespace 390 60 391 #endif // INTERNAL_H61 #endif
Note:
See TracChangeset
for help on using the changeset viewer.