kamicane / nodes Goto Github PK
View Code? Open in Web Editor NEWdocument object model for ast nodes, with parent references, queries and validation
document object model for ast nodes, with parent references, queries and validation
I've gotten a lot of mileage out of the def
syntax I developed for my ast-types
library: https://github.com/benjamn/ast-types/blob/master/def/core.js. It's definitely similar in spirit to your describe
function, but I believe it has some advantages:
FunctionDeclaration
can count as both a Function
(a subtype of Node
that serves as the common supertype of FunctionDeclaration
, FunctionExpression
, and ArrowFunctionExpression
) and a Declaration
(a subtype of Statement
). This means instanceof
no longer works, but it also means we can check subtype relationships in constant time, rather than having to traverse the prototype chain.def("Identifier")
is used many times before it is finally defined. Types are referred to by their string names using the def
function, e.g. def("ObjectExpression")
, and those types are cached by name, so you don't have to worry about passing around references to types, or accidentally creating multiple types with the same name.def("Function")
type has only id
, params
, and body
fields when first defined, but it incrementally acquires additional fields if you include the lib/es6.js or lib/es7.js files.TryStatement
can have either a handler
or handlers
field, since there was some confusion between the Mozilla Parser API and Esprima about which was correct, and the other field will be automatically generated if need be.require("ast-types").builders
), type checking functions (require("ast-types").namedTypes
), and (hopefully soon) documentation. The sky is the limit here, once you have a good declarative syntax for defining the grammar.The reason I think it would be worthwhile to settle on a standard syntax we can both use is that we have the opportunity to provide the only truly machine-executable specification of the ES6+ grammar. Esprima certainly doesn't capture all the necessary type requirements, the Mozilla Parser API is pretty far out of date at this point, and the ES6 grammar is scattered all over the specification document, is not directly useful for programming purposes, and doesn't really match the Esprima AST type system.
If we can settle on a common syntax (I'm happy to call it describe
instead of def
, if you prefer), and perhaps move the type descriptions into a standalone project, I'm confident our specification will become the most complete and useful reference for the grammar as it continues evolving.
What do you think?
I need to have a list with all nodes or, alternatively, be able to query by position in the code, for example all the nodes which start between line 10 and 25.
It's possible to have any of these things?
Anyway thank you for this library!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.