1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=78:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
17 * The Original Code is Mozilla Communicator client code, released
20 * The Initial Developer of the Original Code is
21 * Netscape Communications Corporation.
22 * Portions created by the Initial Developer are Copyright (C) 1998
23 * the Initial Developer. All Rights Reserved.
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
44 * JS parser definitions.
54 * Parsing builds a tree of nodes that directs code generation. This tree is
55 * not a concrete syntax tree in all respects (for example, || and && are left
56 * associative, but (A && B && C) translates into the right-associated tree
57 * <A && <B && C>> so that code generation can emit a left-associative branch
58 * around <B && C> when A is false). Nodes are labeled by token type, with a
59 * JSOp secondary label when needed:
61 * Label Variant Members
62 * ----- ------- -------
64 * TOK_FUNCTION func pn_funpob: JSParsedObjectBox holding function
65 * object containing arg and var properties. We
66 * create the function object at parse (not emit)
67 * time to specialize arg and var bytecodes early.
68 * pn_body: TOK_LC node for function body statements
69 * pn_flags: TCF_FUN_* flags (see jsemit.h) collected
70 * while parsing the function's body
73 * TOK_LC list pn_head: list of pn_count statements
74 * TOK_EXPORT list pn_head: list of pn_count TOK_NAMEs or one TOK_STAR
75 * (which is not a multiply node)
76 * TOK_IMPORT list pn_head: list of pn_count sub-trees of the form
77 * a.b.*, a[b].*, a.*, a.b, or a[b] -- but never a.
78 * Each member is expressed with TOK_DOT or TOK_LB.
79 * Each sub-tree's root node has a pn_op in the set
80 * JSOP_IMPORT{ALL,PROP,ELEM}
81 * TOK_IF ternary pn_kid1: cond, pn_kid2: then, pn_kid3: else or null
82 * TOK_SWITCH binary pn_left: discriminant
83 * pn_right: list of TOK_CASE nodes, with at most one
84 * TOK_DEFAULT node, or if there are let bindings
85 * in the top level of the switch body's cases, a
86 * TOK_LEXICALSCOPE node that contains the list of
88 * TOK_CASE, binary pn_left: case expr or null if TOK_DEFAULT
89 * TOK_DEFAULT pn_right: TOK_LC node for this case's statements
90 * pn_val: constant value if lookup or table switch
91 * TOK_WHILE binary pn_left: cond, pn_right: body
92 * TOK_DO binary pn_left: body, pn_right: cond
93 * TOK_FOR binary pn_left: either
94 * for/in loop: a binary TOK_IN node with
95 * pn_left: TOK_VAR or TOK_NAME to left of 'in'
96 * if TOK_VAR, its pn_extra may have PNX_POPVAR
97 * and PNX_FORINVAR bits set
98 * pn_right: object expr to right of 'in'
99 * for(;;) loop: a ternary TOK_RESERVED node with
100 * pn_kid1: init expr before first ';'
101 * pn_kid2: cond expr before second ';'
102 * pn_kid3: update expr after second ';'
103 * any kid may be null
105 * TOK_THROW unary pn_op: JSOP_THROW, pn_kid: exception
106 * TOK_TRY ternary pn_kid1: try block
107 * pn_kid2: null or TOK_RESERVED list of
108 * TOK_LEXICALSCOPE nodes, each with pn_expr pointing
109 * to a TOK_CATCH node
110 * pn_kid3: null or finally block
111 * TOK_CATCH ternary pn_kid1: TOK_NAME, TOK_RB, or TOK_RC catch var node
112 * (TOK_RB or TOK_RC if destructuring)
113 * pn_kid2: null or the catch guard expression
114 * pn_kid3: catch block statements
115 * TOK_BREAK name pn_atom: label or null
116 * TOK_CONTINUE name pn_atom: label or null
117 * TOK_WITH binary pn_left: head expr, pn_right: body
118 * TOK_VAR list pn_head: list of pn_count TOK_NAME nodes
120 * pn_atom: variable name
121 * pn_expr: initializer or null
122 * TOK_RETURN unary pn_kid: return expr or null
123 * TOK_SEMI unary pn_kid: expr or null statement
124 * TOK_COLON name pn_atom: label, pn_expr: labeled statement
127 * All left-associated binary trees of the same type are optimized into lists
128 * to avoid recursion when processing expression chains.
129 * TOK_COMMA list pn_head: list of pn_count comma-separated exprs
130 * TOK_ASSIGN binary pn_left: lvalue, pn_right: rvalue
131 * pn_op: JSOP_ADD for +=, etc.
132 * TOK_HOOK ternary pn_kid1: cond, pn_kid2: then, pn_kid3: else
133 * TOK_OR binary pn_left: first in || chain, pn_right: rest of chain
134 * TOK_AND binary pn_left: first in && chain, pn_right: rest of chain
135 * TOK_BITOR binary pn_left: left-assoc | expr, pn_right: ^ expr
136 * TOK_BITXOR binary pn_left: left-assoc ^ expr, pn_right: & expr
137 * TOK_BITAND binary pn_left: left-assoc & expr, pn_right: EQ expr
138 * TOK_EQOP binary pn_left: left-assoc EQ expr, pn_right: REL expr
139 * pn_op: JSOP_EQ, JSOP_NE,
140 * JSOP_STRICTEQ, JSOP_STRICTNE
141 * TOK_RELOP binary pn_left: left-assoc REL expr, pn_right: SH expr
142 * pn_op: JSOP_LT, JSOP_LE, JSOP_GT, JSOP_GE
143 * TOK_SHOP binary pn_left: left-assoc SH expr, pn_right: ADD expr
144 * pn_op: JSOP_LSH, JSOP_RSH, JSOP_URSH
145 * TOK_PLUS, binary pn_left: left-assoc ADD expr, pn_right: MUL expr
146 * pn_extra: if a left-associated binary TOK_PLUS
147 * tree has been flattened into a list (see above
148 * under <Expressions>), pn_extra will contain
149 * PNX_STRCAT if at least one list element is a
150 * string literal (TOK_STRING); if such a list has
151 * any non-string, non-number term, pn_extra will
152 * contain PNX_CANTFOLD.
154 * TOK_MINUS pn_op: JSOP_ADD, JSOP_SUB
155 * TOK_STAR, binary pn_left: left-assoc MUL expr, pn_right: UNARY expr
156 * TOK_DIVOP pn_op: JSOP_MUL, JSOP_DIV, JSOP_MOD
157 * TOK_UNARYOP unary pn_kid: UNARY expr, pn_op: JSOP_NEG, JSOP_POS,
158 * JSOP_NOT, JSOP_BITNOT, JSOP_TYPEOF, JSOP_VOID
159 * TOK_INC, unary pn_kid: MEMBER expr
161 * TOK_NEW list pn_head: list of ctor, arg1, arg2, ... argN
162 * pn_count: 1 + N (where N is number of args)
163 * ctor is a MEMBER expr
164 * TOK_DELETE unary pn_kid: MEMBER expr
165 * TOK_DOT, name pn_expr: MEMBER expr to left of .
166 * TOK_DBLDOT pn_atom: name to right of .
167 * TOK_LB binary pn_left: MEMBER expr to left of [
168 * pn_right: expr between [ and ]
169 * TOK_LP list pn_head: list of call, arg1, arg2, ... argN
170 * pn_count: 1 + N (where N is number of args)
171 * call is a MEMBER expr naming a callable object
172 * TOK_RB list pn_head: list of pn_count array element exprs
173 * [,,] holes are represented by TOK_COMMA nodes
174 * #n=[...] produces TOK_DEFSHARP at head of list
175 * pn_extra: PN_ENDCOMMA if extra comma at end
176 * TOK_RC list pn_head: list of pn_count TOK_COLON nodes where
177 * each has pn_left: property id, pn_right: value
178 * #n={...} produces TOK_DEFSHARP at head of list
179 * TOK_DEFSHARP unary pn_num: jsint value of n in #n=
180 * pn_kid: null for #n=[...] and #n={...}, primary
181 * if #n=primary for function, paren, name, object
182 * literal expressions
183 * TOK_USESHARP nullary pn_num: jsint value of n in #n#
184 * TOK_RP unary pn_kid: parenthesized expression
185 * TOK_NAME, name pn_atom: name, string, or object atom
186 * TOK_STRING, pn_op: JSOP_NAME, JSOP_STRING, or JSOP_OBJECT, or
188 * TOK_REGEXP If JSOP_NAME, pn_op may be JSOP_*ARG or JSOP_*VAR
189 * with pn_slot >= 0 and pn_attrs telling const-ness
190 * TOK_NUMBER dval pn_dval: double value of numeric literal
191 * TOK_PRIMARY nullary pn_op: JSOp bytecode
193 * <E4X node descriptions>
194 * TOK_ANYNAME nullary pn_op: JSOP_ANYNAME
195 * pn_atom: cx->runtime->atomState.starAtom
196 * TOK_AT unary pn_op: JSOP_TOATTRNAME; pn_kid attribute id/expr
197 * TOK_DBLCOLON binary pn_op: JSOP_QNAME
198 * pn_left: TOK_ANYNAME or TOK_NAME node
199 * pn_right: TOK_STRING "*" node, or expr within []
200 * name pn_op: JSOP_QNAMECONST
201 * pn_expr: TOK_ANYNAME or TOK_NAME left operand
202 * pn_atom: name on right of ::
203 * TOK_XMLELEM list XML element node
204 * pn_head: start tag, content1, ... contentN, end tag
205 * pn_count: 2 + N where N is number of content nodes
206 * N may be > x.length() if {expr} embedded
207 * TOK_XMLLIST list XML list node
208 * pn_head: content1, ... contentN
209 * TOK_XMLSTAGO, list XML start, end, and point tag contents
210 * TOK_XMLETAGC, pn_head: tag name or {expr}, ... XML attrs ...
212 * TOK_XMLNAME nullary pn_atom: XML name, with no {expr} embedded
213 * TOK_XMLNAME list pn_head: tag name or {expr}, ... name or {expr}
214 * TOK_XMLATTR, nullary pn_atom: attribute value string; pn_op: JSOP_STRING
217 * TOK_XMLPI nullary pn_atom: XML processing instruction target
218 * pn_atom2: XML PI content, or null if no content
219 * TOK_XMLTEXT nullary pn_atom: marked-up text, or null if empty string
220 * TOK_LC unary {expr} in XML tag or content; pn_kid is expr
222 * So an XML tag with no {expr} and three attributes is a list with the form:
224 * (tagname attrname1 attrvalue1 attrname2 attrvalue2 attrname2 attrvalue3)
226 * An XML tag with embedded expressions like so:
228 * <name1{expr1} name2{expr2}name3={expr3}>
230 * would have the form:
232 * ((name1 {expr1}) (name2 {expr2} name3) {expr3})
234 * where () bracket a list with elements separated by spaces, and {expr} is a
235 * TOK_LC unary node with expr as its kid.
237 * Thus, the attribute name/value pairs occupy successive odd and even list
238 * locations, where pn_head is the TOK_XMLNAME node at list location 0. The
239 * parser builds the same sort of structures for elements:
241 * <a x={x}>Hi there!<b y={y}>How are you?</b><answer>{x + y}</answer></a>
245 * ((a x {x}) 'Hi there!' ((b y {y}) 'How are you?') ((answer) {x + y}))
247 * <Non-E4X node descriptions, continued>
249 * Label Variant Members
250 * ----- ------- -------
251 * TOK_LEXICALSCOPE name pn_op: JSOP_LEAVEBLOCK or JSOP_LEAVEBLOCKEXPR
252 * pn_pob: block object
253 * pn_expr: block body
254 * TOK_ARRAYCOMP list pn_head: list of pn_count (1 or 2) elements
255 * if pn_count is 2, first element is #n=[...]
256 * last element is block enclosing for loop(s)
257 * and optionally if-guarded TOK_ARRAYPUSH
258 * pn_extra: stack slot, used during code gen
259 * TOK_ARRAYPUSH unary pn_op: JSOP_ARRAYCOMP
260 * pn_kid: array comprehension expression
262 typedef enum JSParseNodeArity
{
277 ptrdiff_t pn_offset
; /* first generated bytecode offset */
279 struct { /* TOK_FUNCTION node */
280 JSParsedObjectBox
*funpob
; /* function object */
281 JSParseNode
*body
; /* TOK_LC list of statements */
282 uint32 flags
; /* accumulated tree context flags */
284 struct { /* list of next-linked nodes */
285 JSParseNode
*head
; /* first node in list */
286 JSParseNode
**tail
; /* ptr to ptr to last node in list */
287 uint32 count
; /* number of nodes in list */
288 uint32 extra
; /* extra flags, see below */
290 struct { /* ternary: if, for(;;), ?: */
291 JSParseNode
*kid1
; /* condition, discriminant, etc. */
292 JSParseNode
*kid2
; /* then-part, case list, etc. */
293 JSParseNode
*kid3
; /* else-part, default case, etc. */
295 struct { /* two kids if binary */
298 jsval val
; /* switch case value */
300 struct { /* one kid if unary */
302 jsint num
; /* -1 or sharp variable number */
303 JSBool hidden
; /* hidden genexp-induced JSOP_YIELD */
305 struct { /* name, labeled statement, etc. */
306 JSAtom
*atom
; /* name or label atom, null if slot */
307 JSParseNode
*expr
; /* object or initializer */
308 jsint slot
; /* -1 or arg or local var slot */
309 uintN attrs
; /* attributes if local var or const */
311 struct { /* lexical scope. */
312 JSParsedObjectBox
*pob
; /* block object */
313 JSParseNode
*expr
; /* object or initializer */
314 jsint slot
; /* -1 or arg or local var slot */
317 JSAtom
*atom
; /* first atom in pair */
318 JSAtom
*atom2
; /* second atom in pair or null */
320 struct { /* object literal */
321 JSParsedObjectBox
*pob
;
323 jsdouble dval
; /* aligned numeric literal value */
325 JSParseNode
*pn_next
; /* to align dval and pn_u on RISCs */
326 JSTokenStream
*pn_ts
; /* token stream for error reports */
329 #define pn_funpob pn_u.func.funpob
330 #define pn_body pn_u.func.body
331 #define pn_flags pn_u.func.flags
332 #define pn_head pn_u.list.head
333 #define pn_tail pn_u.list.tail
334 #define pn_count pn_u.list.count
335 #define pn_extra pn_u.list.extra
336 #define pn_kid1 pn_u.ternary.kid1
337 #define pn_kid2 pn_u.ternary.kid2
338 #define pn_kid3 pn_u.ternary.kid3
339 #define pn_left pn_u.binary.left
340 #define pn_right pn_u.binary.right
341 #define pn_val pn_u.binary.val
342 #define pn_kid pn_u.unary.kid
343 #define pn_num pn_u.unary.num
344 #define pn_hidden pn_u.unary.hidden
345 #define pn_atom pn_u.name.atom
346 #define pn_expr pn_u.name.expr
347 #define pn_slot pn_u.name.slot
348 #define pn_attrs pn_u.name.attrs
349 #define pn_dval pn_u.dval
350 #define pn_atom2 pn_u.apair.atom2
351 #define pn_pob pn_u.object.pob
353 /* PN_LIST pn_extra flags. */
354 #define PNX_STRCAT 0x01 /* TOK_PLUS list has string term */
355 #define PNX_CANTFOLD 0x02 /* TOK_PLUS list has unfoldable term */
356 #define PNX_POPVAR 0x04 /* TOK_VAR last result needs popping */
357 #define PNX_FORINVAR 0x08 /* TOK_VAR is left kid of TOK_IN node,
358 which is left kid of TOK_FOR */
359 #define PNX_ENDCOMMA 0x10 /* array literal has comma at end */
360 #define PNX_XMLROOT 0x20 /* top-most node in XML literal tree */
361 #define PNX_GROUPINIT 0x40 /* var [a, b] = [c, d]; unit list */
362 #define PNX_NEEDBRACES 0x80 /* braces necessary due to closure */
365 * Move pn2 into pn, preserving pn->pn_pos and pn->pn_offset and handing off
366 * any kids in pn2->pn_u, by clearing pn2.
368 #define PN_MOVE_NODE(pn, pn2) \
370 (pn)->pn_type = (pn2)->pn_type; \
371 (pn)->pn_op = (pn2)->pn_op; \
372 (pn)->pn_arity = (pn2)->pn_arity; \
373 (pn)->pn_u = (pn2)->pn_u; \
374 PN_CLEAR_NODE(pn2); \
377 #define PN_CLEAR_NODE(pn) \
379 (pn)->pn_type = TOK_EOF; \
380 (pn)->pn_op = JSOP_NOP; \
381 (pn)->pn_arity = PN_NULLARY; \
384 /* True if pn is a parsenode representing a literal constant. */
385 #define PN_IS_CONSTANT(pn) \
386 ((pn)->pn_type == TOK_NUMBER || \
387 (pn)->pn_type == TOK_STRING || \
388 ((pn)->pn_type == TOK_PRIMARY && (pn)->pn_op != JSOP_THIS))
390 #define PN_OP(pn) ((JSOp)(pn)->pn_op)
391 #define PN_TYPE(pn) ((JSTokenType)(pn)->pn_type)
394 * Compute a pointer to the last JSParseNode element in a singly-linked list.
395 * NB: list must be non-empty for correct PN_LAST usage!
397 #define PN_LAST(list) \
398 ((JSParseNode *)((char *)(list)->pn_tail - offsetof(JSParseNode, pn_next)))
400 #define PN_INIT_LIST(list) \
402 (list)->pn_head = NULL; \
403 (list)->pn_tail = &(list)->pn_head; \
404 (list)->pn_count = (list)->pn_extra = 0; \
407 #define PN_INIT_LIST_1(list, pn) \
409 (list)->pn_head = (pn); \
410 (list)->pn_tail = &(pn)->pn_next; \
411 (list)->pn_count = 1; \
412 (list)->pn_extra = 0; \
415 #define PN_APPEND(list, pn) \
417 *(list)->pn_tail = (pn); \
418 (list)->pn_tail = &(pn)->pn_next; \
419 (list)->pn_count++; \
422 struct JSParsedObjectBox
{
423 JSParsedObjectBox
*traceLink
;
424 JSParsedObjectBox
*emitLink
;
428 struct JSParseContext
{
429 JSTokenStream tokenStream
;
430 void *tempPoolMark
; /* initial JSContext.tempPool mark */
431 JSPrincipals
*principals
; /* principals associated with source */
432 JSParseNode
*nodeList
; /* list of recyclable parse-node
434 JSParsedObjectBox
*traceListHead
; /* list of parsed object for GC
436 JSTempValueRooter tempRoot
; /* root to trace traceListHead */
439 * JSContext.tempPool mark after the last allocation of JSParseNode or
440 * JSParsedObjectBox to assist with asserting that we do not release the
441 * parsed structures until they are no longer used.
448 * Convenience macro to access JSParseContext.tokenStream as a pointer.
450 #define TS(pc) (&(pc)->tokenStream)
453 * Parse a top-level JS script.
455 extern JS_FRIEND_API(JSParseNode
*)
456 js_ParseScript(JSContext
*cx
, JSObject
*chain
, JSParseContext
*pc
);
458 extern JS_FRIEND_API(JSScript
*)
459 js_CompileScript(JSContext
*cx
, JSObject
*chain
, JSParseContext
*pc
);
462 js_CompileFunctionBody(JSContext
*cx
, JSParseContext
*pc
, JSFunction
*fun
);
465 js_FoldConstants(JSContext
*cx
, JSParseNode
*pn
, JSTreeContext
*tc
);
467 #if JS_HAS_XML_SUPPORT
468 JS_FRIEND_API(JSParseNode
*)
469 js_ParseXMLText(JSContext
*cx
, JSObject
*chain
, JSParseContext
*pc
,
474 * Initialize a parse context. All parameters after pc are passed to
475 * js_InitTokenStream.
477 * The parse context owns the arena pool "tops-of-stack" space above the
478 * current JSContext.tempPool mark. This means you cannot allocate from
479 * tempPool and save the pointer beyond the next js_FinishParseContext.
481 extern JS_FRIEND_API(JSBool
)
482 js_InitParseContext(JSContext
*cx
, JSParseContext
*pc
,
483 const jschar
*base
, size_t length
, FILE *fp
,
484 const char *filename
, uintN lineno
);
486 extern JS_FRIEND_API(void)
487 js_FinishParseContext(JSContext
*cx
, JSParseContext
*pc
);
490 js_InitCompilePrincipals(JSContext
*cx
, JSParseContext
*pc
,
491 JSPrincipals
*principals
);
494 * Allocate a new parseed object node from cx->tempPool.
496 extern JSParsedObjectBox
*
497 js_NewParsedObjectBox(JSContext
*cx
, JSParseContext
*pc
, JSObject
*obj
);
500 js_TraceParseContext(JSTracer
*trc
, JSParseContext
*pc
);
504 #endif /* jsparse_h___ */