Subj : Re: The root of the syntax tree To : joao From : Brendan Eich Date : Tue Jul 06 2004 08:03 pm joao wrote: > Then to have this code called, I looked into js_CompileTokenStream in > jsparse.c, and added my call after the line that says > > pn = Statements(cx, ts, &cg->treeContext); > > It turns out that this node only has a TOK_EOF... I was hoping to find > a list of top-level statements. So I went one step further, inside the > Statements call, and inserted my call inside the 'while' loop, after > the line > > pn2 = Statement(cx, ts, tc); > > and sure enough, I can output each of the toplevel statements. But they > are not connected, I do not have a root for this syntax tree. Does this > root exist ? Not when you are generating code. To conserve and recycle JSParseNode space, each top-level statement is compiled in turn: parsed and then traversed by js_EmitTree to generate bytecode. If you want the entire tree, don't use a JSCodeGenerator and don't call js_CompileTokenStream -- call js_ParseTokenStream instead. Notice the terminology, which is used consistently: Compile means generate bytecode, Parse means generate only a parse tree (which could then be traversed to generate bytecode, but not using any friend or public API). > are all the top-level statements collected somewhere, > before the EOF is reached ? or does JS emit the bytecode for each > statement and then forget about it ? Precisely -- but only when compiling, not when parsing. Look for TCF_COMPILING, which means the tc can be downcast to a cg. Hope that makes sense! > Related question : for a string literal, I get a TOK_STRING token with > a JSOP_STRING operator, but I can't see how to get at the value of the > string ; it has arity PN_NULLARY so there's nothing more in the > JSParseNode. How can I reach this value ? pn->pn_atom is the string's atom. Looks like jsparse.h's big comment does not say this clearly enough. But if you read very carefully, noting the commas in the first column, you can see it. Look for "pn_atom: name, string, or object atom" in the third column. /be .