|
|
1.1 ! root 1: /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- ! 2: * ! 3: * The contents of this file are subject to the Netscape Public ! 4: * License Version 1.1 (the "License"); you may not use this file ! 5: * except in compliance with the License. You may obtain a copy of ! 6: * the License at http://www.mozilla.org/NPL/ ! 7: * ! 8: * Software distributed under the License is distributed on an "AS ! 9: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or ! 10: * implied. See the License for the specific language governing ! 11: * rights and limitations under the License. ! 12: * ! 13: * The Original Code is Mozilla Communicator client code, released ! 14: * March 31, 1998. ! 15: * ! 16: * The Initial Developer of the Original Code is Netscape ! 17: * Communications Corporation. Portions created by Netscape are ! 18: * Copyright (C) 1998 Netscape Communications Corporation. All ! 19: * Rights Reserved. ! 20: * ! 21: * Contributor(s): ! 22: * ! 23: * Alternatively, the contents of this file may be used under the ! 24: * terms of the GNU Public License (the "GPL"), in which case the ! 25: * provisions of the GPL are applicable instead of those above. ! 26: * If you wish to allow use of your version of this file only ! 27: * under the terms of the GPL and not to allow others to use your ! 28: * version of this file under the NPL, indicate your decision by ! 29: * deleting the provisions above and replace them with the notice ! 30: * and other provisions required by the GPL. If you do not delete ! 31: * the provisions above, a recipient may use your version of this ! 32: * file under either the NPL or the GPL. ! 33: */ ! 34: ! 35: #ifndef jsparse_h___ ! 36: #define jsparse_h___ ! 37: /* ! 38: * JS parser definitions. ! 39: */ ! 40: #include "jsprvtd.h" ! 41: #include "jspubtd.h" ! 42: #include "jsscan.h" ! 43: ! 44: JS_BEGIN_EXTERN_C ! 45: ! 46: /* ! 47: * Parsing builds a tree of nodes that directs code generation. This tree is ! 48: * not a concrete syntax tree in all respects (for example, || and && are left ! 49: * associative, but (A && B && C) translates into the right-associated tree ! 50: * <A && <B && C>> so that code generation can emit a left-associative branch ! 51: * around <B && C> when A is false). Nodes are labeled by token type, with a ! 52: * JSOp secondary label when needed: ! 53: * ! 54: * Label Variant Members ! 55: * ----- ------- ------- ! 56: * <Definitions> ! 57: * TOK_FUNCTION func pn_funAtom: atom holding function object containing ! 58: * arg and var properties. We create the function ! 59: * object at parse (not emit) time to specialize arg ! 60: * and var bytecodes early. ! 61: * pn_body: TOK_LC node for function body statements ! 62: * pn_flags: TCF_FUN_* flags (see jsemit.h) collected ! 63: * while parsing the function's body ! 64: * pn_tryCount: of try statements in function ! 65: * ! 66: * <Statements> ! 67: * TOK_LC list pn_head: list of pn_count statements ! 68: * TOK_EXPORT list pn_head: list of pn_count TOK_NAMEs or one TOK_STAR ! 69: * (which is not a multiply node) ! 70: * TOK_IMPORT list pn_head: list of pn_count sub-trees of the form ! 71: * a.b.*, a[b].*, a.*, a.b, or a[b] -- but never a. ! 72: * Each member is expressed with TOK_DOT or TOK_LB. ! 73: * Each sub-tree's root node has a pn_op in the set ! 74: * JSOP_IMPORT{ALL,PROP,ELEM} ! 75: * TOK_IF ternary pn_kid1: cond, pn_kid2: then, pn_kid3: else or null ! 76: * TOK_SWITCH binary pn_left: discriminant ! 77: * pn_right: list of TOK_CASE nodes, with at most one ! 78: * TOK_DEFAULT node ! 79: * TOK_CASE, binary pn_left: case expr or null if TOK_DEFAULT ! 80: * TOK_DEFAULT pn_right: TOK_LC node for this case's statements ! 81: * TOK_WHILE binary pn_left: cond, pn_right: body ! 82: * TOK_DO binary pn_left: body, pn_right: cond ! 83: * TOK_FOR binary pn_left: either ! 84: * for/in loop: a binary TOK_IN node with ! 85: * pn_left: TOK_VAR or TOK_NAME to left of 'in' ! 86: * pn_right: object expr to right of 'in' ! 87: * for(;;) loop: a ternary TOK_RESERVED node with ! 88: * pn_kid1: init expr before first ';' ! 89: * pn_kid2: cond expr before second ';' ! 90: * pn_kid3: update expr after second ';' ! 91: * any kid may be null ! 92: * pn_right: body ! 93: * TOK_THROW unary pn_op: JSOP_THROW, pn_kid: exception ! 94: * TOK_TRY ternary pn_kid1: try block ! 95: * pn_kid2: catch blocks or null ! 96: * pn_kid3: finally block or null ! 97: * TOK_CATCH ternary pn_kid1: PN_NAME node for catch var (with pn_expr ! 98: * null or the catch guard expression) ! 99: * pn_kid2: more catch blocks or null ! 100: * pn_kid3: catch block statements ! 101: * TOK_BREAK name pn_atom: label or null ! 102: * TOK_CONTINUE name pn_atom: label or null ! 103: * TOK_WITH binary pn_left: head expr, pn_right: body ! 104: * TOK_VAR list pn_head: list of pn_count TOK_NAME nodes ! 105: * each name node has ! 106: * pn_atom: variable name ! 107: * pn_expr: initializer or null ! 108: * TOK_RETURN unary pn_kid: return expr or null ! 109: * TOK_SEMI unary pn_kid: expr or null statement ! 110: * TOK_COLON name pn_atom: label, pn_expr: labeled statement ! 111: * ! 112: * <Expressions> ! 113: * All left-associated binary trees of the same type are optimized into lists ! 114: * to avoid recursion when processing expression chains. ! 115: * TOK_COMMA list pn_head: list of pn_count comma-separated exprs ! 116: * TOK_ASSIGN binary pn_left: lvalue, pn_right: rvalue ! 117: * pn_op: JSOP_ADD for +=, etc. ! 118: * TOK_HOOK ternary pn_kid1: cond, pn_kid2: then, pn_kid3: else ! 119: * TOK_OR binary pn_left: first in || chain, pn_right: rest of chain ! 120: * TOK_AND binary pn_left: first in && chain, pn_right: rest of chain ! 121: * TOK_BITOR binary pn_left: left-assoc | expr, pn_right: ^ expr ! 122: * TOK_BITXOR binary pn_left: left-assoc ^ expr, pn_right: & expr ! 123: * TOK_BITAND binary pn_left: left-assoc & expr, pn_right: EQ expr ! 124: * TOK_EQOP binary pn_left: left-assoc EQ expr, pn_right: REL expr ! 125: * pn_op: JSOP_EQ, JSOP_NE, JSOP_NEW_EQ, JSOP_NEW_NE ! 126: * TOK_RELOP binary pn_left: left-assoc REL expr, pn_right: SH expr ! 127: * pn_op: JSOP_LT, JSOP_LE, JSOP_GT, JSOP_GE ! 128: * TOK_SHOP binary pn_left: left-assoc SH expr, pn_right: ADD expr ! 129: * pn_op: JSOP_LSH, JSOP_RSH, JSOP_URSH ! 130: * TOK_PLUS, binary pn_left: left-assoc ADD expr, pn_right: MUL expr ! 131: * pn_extra: if a left-associated binary TOK_PLUS ! 132: * tree has been flattened into a list (see above ! 133: * under <Expressions>), pn_extra will contain ! 134: * PNX_STRCAT if at least one list element is a ! 135: * string literal (TOK_STRING); if such a list has ! 136: * any non-string, non-number term, pn_extra will ! 137: * contain PNX_CANTFOLD. ! 138: * pn_ ! 139: * TOK_MINUS pn_op: JSOP_ADD, JSOP_SUB ! 140: * TOK_STAR, binary pn_left: left-assoc MUL expr, pn_right: UNARY expr ! 141: * TOK_DIVOP pn_op: JSOP_MUL, JSOP_DIV, JSOP_MOD ! 142: * TOK_UNARYOP unary pn_kid: UNARY expr, pn_op: JSOP_NEG, JSOP_POS, ! 143: * JSOP_NOT, JSOP_BITNOT, JSOP_TYPEOF, JSOP_VOID ! 144: * TOK_INC, unary pn_kid: MEMBER expr ! 145: * TOK_DEC ! 146: * TOK_NEW list pn_head: list of ctor, arg1, arg2, ... argN ! 147: * pn_count: 1 + N (where N is number of args) ! 148: * ctor is a MEMBER expr ! 149: * TOK_DELETE unary pn_kid: MEMBER expr ! 150: * TOK_DOT name pn_expr: MEMBER expr to left of . ! 151: * pn_atom: name to right of . ! 152: * TOK_LB binary pn_left: MEMBER expr to left of [ ! 153: * pn_right: expr between [ and ] ! 154: * TOK_LP list pn_head: list of call, arg1, arg2, ... argN ! 155: * pn_count: 1 + N (where N is number of args) ! 156: * call is a MEMBER expr naming a callable object ! 157: * TOK_RB list pn_head: list of pn_count array element exprs ! 158: * [,,] holes are represented by TOK_COMMA nodes ! 159: * #n=[...] produces TOK_DEFSHARP at head of list ! 160: * pn_extra: true if extra comma at end ! 161: * TOK_RC list pn_head: list of pn_count TOK_COLON nodes where ! 162: * each has pn_left: property id, pn_right: value ! 163: * #n={...} produces TOK_DEFSHARP at head of list ! 164: * TOK_DEFSHARP unary pn_num: jsint value of n in #n= ! 165: * pn_kid: null for #n=[...] and #n={...}, primary ! 166: * if #n=primary for function, paren, name, object ! 167: * literal expressions ! 168: * TOK_USESHARP nullary pn_num: jsint value of n in #n# ! 169: * TOK_RP unary pn_kid: parenthesized expression ! 170: * TOK_NAME, name pn_atom: name, string, or object atom ! 171: * TOK_STRING, pn_op: JSOP_NAME, JSOP_STRING, or JSOP_OBJECT ! 172: * TOK_OBJECT If JSOP_NAME, pn_op may be JSOP_*ARG or JSOP_*VAR ! 173: * with pn_slot >= 0 and pn_attrs telling const-ness ! 174: * TOK_NUMBER dval pn_dval: double value of numeric literal ! 175: * TOK_PRIMARY nullary pn_op: JSOp bytecode ! 176: */ ! 177: typedef enum JSParseNodeArity { ! 178: PN_FUNC = -3, ! 179: PN_LIST = -2, ! 180: PN_TERNARY = 3, ! 181: PN_BINARY = 2, ! 182: PN_UNARY = 1, ! 183: PN_NAME = -1, ! 184: PN_NULLARY = 0 ! 185: } JSParseNodeArity; ! 186: ! 187: struct JSParseNode { ! 188: JSTokenType pn_type; ! 189: JSTokenPos pn_pos; ! 190: JSOp pn_op; ! 191: ptrdiff_t pn_offset; /* first generated bytecode offset */ ! 192: JSParseNodeArity pn_arity; ! 193: union { ! 194: struct { /* TOK_FUNCTION node */ ! 195: JSAtom *funAtom; /* atomized function object */ ! 196: JSParseNode *body; /* TOK_LC list of statements */ ! 197: uint32 flags; /* accumulated tree context flags */ ! 198: uint32 tryCount; /* count of try statements in body */ ! 199: } func; ! 200: struct { /* list of next-linked nodes */ ! 201: JSParseNode *head; /* first node in list */ ! 202: JSParseNode **tail; /* ptr to ptr to last node in list */ ! 203: uint32 count; /* number of nodes in list */ ! 204: uint32 extra; /* extra comma flag for [1,2,,] */ ! 205: } list; ! 206: struct { /* ternary: if, for(;;), ?: */ ! 207: JSParseNode *kid1; /* condition, discriminant, etc. */ ! 208: JSParseNode *kid2; /* then-part, case list, etc. */ ! 209: JSParseNode *kid3; /* else-part, default case, etc. */ ! 210: } ternary; ! 211: struct { /* two kids if binary */ ! 212: JSParseNode *left; ! 213: JSParseNode *right; ! 214: jsval val; /* switch case value */ ! 215: } binary; ! 216: struct { /* one kid if unary */ ! 217: JSParseNode *kid; ! 218: jsint num; /* -1 or sharp variable number */ ! 219: } unary; ! 220: struct { /* name, labeled statement, etc. */ ! 221: JSAtom *atom; /* name or label atom, null if slot */ ! 222: JSParseNode *expr; /* object or initializer */ ! 223: jsint slot; /* -1 or arg or local var slot */ ! 224: uintN attrs; /* attributes if local var or const */ ! 225: } name; ! 226: jsdouble dval; /* aligned numeric literal value */ ! 227: } pn_u; ! 228: JSParseNode *pn_next; /* to align dval and pn_u on RISCs */ ! 229: }; ! 230: ! 231: #define pn_funAtom pn_u.func.funAtom ! 232: #define pn_body pn_u.func.body ! 233: #define pn_flags pn_u.func.flags ! 234: #define pn_tryCount pn_u.func.tryCount ! 235: #define pn_head pn_u.list.head ! 236: #define pn_tail pn_u.list.tail ! 237: #define pn_count pn_u.list.count ! 238: #define pn_extra pn_u.list.extra ! 239: #define pn_kid1 pn_u.ternary.kid1 ! 240: #define pn_kid2 pn_u.ternary.kid2 ! 241: #define pn_kid3 pn_u.ternary.kid3 ! 242: #define pn_left pn_u.binary.left ! 243: #define pn_right pn_u.binary.right ! 244: #define pn_val pn_u.binary.val ! 245: #define pn_kid pn_u.unary.kid ! 246: #define pn_num pn_u.unary.num ! 247: #define pn_atom pn_u.name.atom ! 248: #define pn_expr pn_u.name.expr ! 249: #define pn_slot pn_u.name.slot ! 250: #define pn_attrs pn_u.name.attrs ! 251: #define pn_dval pn_u.dval ! 252: ! 253: /* PN_LIST pn_extra flags. */ ! 254: #define PNX_STRCAT 0x1 /* TOK_PLUS list has string term */ ! 255: #define PNX_CANTFOLD 0x2 /* TOK_PLUS list has unfoldable term */ ! 256: ! 257: /* ! 258: * Move pn2 into pn, preserving pn->pn_pos and pn->pn_offset and handing off ! 259: * any kids in pn2->pn_u, by clearing pn2. ! 260: */ ! 261: #define PN_MOVE_NODE(pn, pn2) \ ! 262: JS_BEGIN_MACRO \ ! 263: (pn)->pn_type = (pn2)->pn_type; \ ! 264: (pn)->pn_op = (pn2)->pn_op; \ ! 265: (pn)->pn_arity = (pn2)->pn_arity; \ ! 266: (pn)->pn_u = (pn2)->pn_u; \ ! 267: PN_CLEAR_NODE(pn2); \ ! 268: JS_END_MACRO ! 269: ! 270: #define PN_CLEAR_NODE(pn) \ ! 271: JS_BEGIN_MACRO \ ! 272: (pn)->pn_type = TOK_EOF; \ ! 273: (pn)->pn_op = JSOP_NOP; \ ! 274: (pn)->pn_arity = PN_NULLARY; \ ! 275: JS_END_MACRO ! 276: ! 277: /* True if pn is a parsenode representing a literal constant. */ ! 278: #define PN_IS_CONSTANT(pn) \ ! 279: ((pn)->pn_type == TOK_NUMBER || \ ! 280: (pn)->pn_type == TOK_STRING || \ ! 281: ((pn)->pn_type == TOK_PRIMARY && (pn)->pn_op != JSOP_THIS)) ! 282: ! 283: /* ! 284: * Compute a pointer to the last JSParseNode element in a singly-linked list. ! 285: * NB: list must be non-empty for correct PN_LAST usage! ! 286: */ ! 287: #define PN_LAST(list) \ ! 288: ((JSParseNode *)((char *)(list)->pn_tail - offsetof(JSParseNode, pn_next))) ! 289: ! 290: #define PN_INIT_LIST(list) \ ! 291: JS_BEGIN_MACRO \ ! 292: (list)->pn_head = NULL; \ ! 293: (list)->pn_tail = &(list)->pn_head; \ ! 294: (list)->pn_count = 0; \ ! 295: JS_END_MACRO ! 296: ! 297: #define PN_INIT_LIST_1(list, pn) \ ! 298: JS_BEGIN_MACRO \ ! 299: (list)->pn_head = (pn); \ ! 300: (list)->pn_tail = &(pn)->pn_next; \ ! 301: (list)->pn_count = 1; \ ! 302: JS_END_MACRO ! 303: ! 304: #define PN_APPEND(list, pn) \ ! 305: JS_BEGIN_MACRO \ ! 306: *(list)->pn_tail = (pn); \ ! 307: (list)->pn_tail = &(pn)->pn_next; \ ! 308: (list)->pn_count++; \ ! 309: JS_END_MACRO ! 310: ! 311: /* ! 312: * Parse a top-level JS script. ! 313: * ! 314: * The caller must prevent the GC from running while this function is active, ! 315: * because atoms and function newborns are not rooted yet. ! 316: */ ! 317: extern JS_FRIEND_API(JSParseNode *) ! 318: js_ParseTokenStream(JSContext *cx, JSObject *chain, JSTokenStream *ts); ! 319: ! 320: extern JS_FRIEND_API(JSBool) ! 321: js_CompileTokenStream(JSContext *cx, JSObject *chain, JSTokenStream *ts, ! 322: JSCodeGenerator *cg); ! 323: ! 324: extern JSBool ! 325: js_CompileFunctionBody(JSContext *cx, JSTokenStream *ts, JSFunction *fun); ! 326: ! 327: extern JSBool ! 328: js_FoldConstants(JSContext *cx, JSParseNode *pn, JSTreeContext *tc); ! 329: ! 330: JS_END_EXTERN_C ! 331: ! 332: #endif /* jsparse_h___ */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.