A caudillo parsing API could treat the visitor interface from JsonMLAST spil a builder with a parametric type. Ter Java-like pseudo-types:
and take it spil an optional argument to the constructor of a parser object:
This would permit the parse method to use the visitor spil a way of injecting the parsed knots into whatever type A the client wants. Making the argument optional makes it effortless to default to the JsonMLAST format.
It’s a little messy (wrt types) to reuse the visitor interface for this, since the JsonMLAST visitor expects JsonMLAST values spil arguments, whereas this use expects the type A spil arguments. So it’s maybe better to have a parallel “Builder” interface that’s the same spil the visitor interface but with buildXXXX methods instead:
Should also implement a JsonMLAST processor that takes a visitor:
This could be implemented te zuivere JS, at least primarily. (It’ll be ugly without pattern matching, however.)
Several separate steps to make this work:
- Bug 548461 — refactor JSCompiler to JSSourceCompiler and JSAstCompiler (draft done, patch under review)
- Bug 533874 — implement marshalling to JsonMLAST (draft done, not fairly ready for review)
- implement source location information
- generalize to take visitor (not began)
- refactor to build on top of C++ API? (not began)
Presently jsparse.h defines a JSParseNode struct, a variant record documented by the major comment before its declaration, and a few JS_FRIEND_API entry points for parsing and compiling, but not executing, token rivulets.
There is rente, from the jseng newsgroup, ter adding a jsparseapi.[ch] module to SpiderMonkey that exposes thesis parse-tree internals ter a sustainable way. See my postbode ter the thread on this topic.
Te a followup postbode, I talk about various APIs that could be used to glean information from the rechtstreeks members of JSParseNode , but with a fresh jsparseapi.h interface, wij would do better to use functions to hide all gegevens types that are not already exposed via jspubtd.h and jsapi.h .
The parse-tree API should expose a JSParseTree opaque struct type. This type represents a tree parsed from a token stream, or a subtree (even just one leaf knot). Wij should not expose JSTokenStream , rather wij should make API entry points such spil
Related movie: Two способа купить биткоины за рубли с карты
that comebacks NULL on failure and a pointer to a valid ParseTree on success.
The API would consist of accessors only, no mutators. Wij might want an API modeled on the visitor pattern. So JSParseTree would be a Visitable and the API would introduce a Visitor interface (ter the C callbacks or poor-man’s vtable sense) for API clients to implement.
Please feel free to add your vormgeving notes below.
Work on this API originated from an email te the news group postbode by Jeremy. I volunteered to assist him and since then wij have worked together on this. After getting a feel of the task at forearm, wij embarked work on a note outlining ideas for this API. Jeremy has bot incapable to devote time owing to other committments, and so I am posting this even tho’ I have not had his comments on this note.
Work Done So Far
Based on the hints provided te Brendan’s postbode, the following progress has bot made :
1. Parsing a script using js_ParseTokenStream
Two. Walking of the ParseNodes tree using recursion
Trio. Implementation of a elementary callback set (HaveNewSubTree, HaveNodeInTree, EndSubTree) that is called during the walking of the knots. The structure used is :
This is not a zindelijk implementation of the Visitor pattern but used spil a plain commencing point.
Four. Implementation of two sets of callbacks – the very first that prints the contents of the knots (not all knot types have bot done spil yet) and the 2nd that builds a frequency distribution for ‘arity’ of knots, TOK_ constants and JS Opcodes.
Five. Implementation of a non-recursive walker making the same callbacks.
About the ParserAPI
Before implementing an API, it is essential to have the end-use ter perspective, spil well spil to delineate clearly the boundaries of capabilities.
Very first the boundaries: The ParserAPI will not evaluate the scripts. This will ensure that the API can be used on scripts for which object implementations are not available. For eg, debugger.js references an object jsd te its top level code. An attempt to evaluate the code will come back an error.
The idea of the ParserAPI emerged from an rente te a instrument, like JavaDoc, to aid ter documenting JS scripts and script collections. Some other potential uses for the API could be :
A GUI application that permits a user to drill down a collection of JS ie the knots of rente may differ depending on where the GUI is at that point te time, and secondly the capability to locate a parseNode and then proceed the API usage from there. Eg, te the very first pass, the GUI may display the source files and function names. When the user clicks a function, wij then have to display the variables, functions called etc. The application would hold the ParseNode tree – when the user clicks the funciton, the application would locate that ParseNode and then commence using our API from there.
An application that wants to build a Calls/Called By zuigeling of graph – they would use the API to parse the knots and then build their own gegevens structures.
An application is complicated and the user is reviewing the code – the API could be the foundation on which to build a plain search for usage zuigeling of interface.
Related movie: YoBit бот Two бесплатно, РОБОТ который зарабатывает
During the initial ‘getting-the-feel’ work, plain Token Type and OpCode counting functions have bot implemented spil users of the API. This could help to identify a group of scripts that use all tokens and op codes – this could serve spil a ‘testing set’ for JS engine builds.
1. A user of the API should be able to control the extent of recursion. For eg, a user who is interested only te top level code and function definitions should be able to comeback a value from the ‘visitor’ callback that will determine the next step for walking. So on receiving a FUNCTION during the walk, the callback could comeback a value that implies ‘SKIP THE FUNCTION Bod’. Likewise, a callback could terugwedstrijd a value telling ‘Zekering WALKING’ eg the user found what they were looking for or determined they werent interested any longer.
The callback should be able to inhibit the walking of the pn_next knot ter a given ParseNode.
Two. Related to the above, the API should be able to terugwedstrijd a ‘marker’ for each knot. The user of the API should be able to provide this marker at a subsequent point to determine the ‘walk begin’ knot. For implementations where GC is not an punt (eg a GUI application for documentation), this marker could be the ‘Knot’ pointer itself. Howevever, for other implementations the marker could be based on the BEGINPOS, ENDPOS available with each ParseNode.
Trio. A single walk of the parse tree should be able to cater to numerous ‘visitors’. This is an idea I bring from the strijdperk of photo processing – while walking the contents of an pic, different consumers are supported. At the end of walking, the application can query each consumer for information of rente. This has two advantages – the walking is done merienda, and consumer implementations can be simpler (and also lightly reused). An application sets up the consumers, walks the contents and then queries each consumer for information. This information is aggregated / evaluated to determine the application’s course of act.
Four. The Visitable/Visitor definition : I think the API should provide different types of thesis. At the ‘lowest’ level, the Visitor interface could include a callback for each type of ParseNode. At the next level, the Visitor interface could include some categorisation based on TOK_ types. This would seem to be like introducing a ‘guide’ interface ie the guide interface prepares the visitable which a vistor can visit.
Five. Reviewing the SpiderMonkey code, there are several places where this walker (spil a ‘js_friend’) could be used eg js_FoldConstants, js_EmitTree, CheckSideEffects and some others. Thesis functions presently use recursion and te some cases this recursion shows up expensive. I am not sure whether the ‘auhtors/maintainers’ of thesis functions would find the code lightly maintained if they were to switch to using this API.
1. The API should provide human readable descriptions for elements like JS OpCode and Parser Tokens. Presently, jsopcode.c is the only place ter the JS code where the ‘names’ of JS op codes are available. Thesis are defined ter the verkeersopstopping jsopcode.tbl. This verkeersopstopping is included ter jsopcode.h and jsopcode.c : ter the .h opstopping, only the numeric constants for the opcodes are included whereby the enumeration of the JS opcodes is exposed. Ter the .c verkeersopstopping, the accomplish table is included which is used during disassembly. For the ParserAPI, a function should be added to jsopcode.c that would terugwedstrijd a pointer to the name corresponding to a given JS Opcode. An option is that there is a public function exposed by the ParserAPI which te turn invokes a ‘friend’ function ter jsopcode.c.
Two. Memory and GC : The kicking off point for the API will be to parse a script using js_ParseTokenStream. Upon successful parsing, the core functions of the API will be used ie walking the tree of parse knots. During the use of the API, the memory allocated te the setting temp strijdperk pool voorwaarde not be liberated. This suggests when the API user has finished with the ParserAPI, they vereiste call a function that will release the strijdperk pool – this seems ‘reasonable’. However, the API will also depend on GC not taking place so that atoms referenced ter the different JSParseNodes will not get liberated. During js_ParseTokenStream, atoms are protected from GC by using JS_KEEP_ATOMS. Upon completion of the parsing, JS_UNKEEP_ATOMS is called. One option is that after parsing is finished, JS_KEEP_ATOMS is called again and JS_UNKEEP_ATOMS is called ter the same function when the strijdperk pool is released.
Trio. Folding of constants : When the parsing is finished successfully, js_FoldConstants is called. If I’ve understood this correctly, js_FoldConstants performs optimisation by resolving merienda expressions involving literals. A question that arises is whether, a user of the ParserAPI would choose that this optimisation is not performed. For eg, a user of the API may be attempting to identify the usage of a onveranderlijk value that has bot used spil a idéntico ter a collection of scripts – with the constants folded, the literals will not be visible via the ParserAPI. Similarly, a user of the API attempting to find examples of usage of different operators may get an empty result set on account of the folding of constants.
Four. When an error occurs during parsing, it is necessary to report the error to the ErrorHandler callback if set te the setting. The error is available te an Exception object. An punt to consider is whether the ParserAPI should aggregate all Warnings and make thesis available at the end of the parsing.
Five. Clarifications required on TOK_DEFSHARP and TOK_USESHARP. Also on