Hi, I’ve been trying to build a library to represent Javascript ASTs to provide a strongly typed interface to e.g. @babel/parser etc, and it seems like it’s well beyond my understanding of Purescript.
https://github.com/sharkbrainguy/purescript-estree/blob/master/src/Estree.purs
Because there are so many “versions” of javascript that I want to be able to manipulate.
- ES5
- ES201x
- JSX
- Various babel extensions
I tried to model the “core” with an open variant
-- x is additions to Expression
-- s is additions to Statement
-- p is additions to Pattern
newtype Statement (x :: # Type) (s :: # Type) (p :: # Type)
= Statement (Variant
( "ExpressionStatement" :: ExpressionStatement x s p
, "BlockStatement" :: BlockStatement x s p
, "EmptyStatement" :: Node ()
, "DebuggerStatement" :: Node ()
, "WithStatement" :: WithStatement x s p
, "ReturnStatement" :: ReturnStatement x s p
, "LabeledStatement" :: LabeledStatement x s p
, "BreakStatement" :: BreakStatement
, "ContinueStatement" :: ContinueStatement
, "IfStatement" :: IfStatement x s p
, "ThrowStatement" :: ThrowStatement x s p
, "WhileStatement" :: WhileStatement x s p
, "DoWhileStatement" :: DoWhileStatement x s p
, "ForStatement" :: ForStatement x s p
, "FunctionDeclaration" :: FunctionDeclaration x s p
, "VariableDeclaration" :: VariableDeclaration x s p
| s
))
but then I literally don’t know how to populate those variables with anything other than ()
.
Is this a bad approach? Is this a solved problem?
Any help is appreciated.