benjamn / ast-types

Esprima-compatible implementation of the Mozilla JS Parser API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom AST with recast

goodmind opened this issue · comments

Is it possible to define custom AST not based on any of types from ast-types/def (even core.js) and use it with recast? I already have parser to produce my custom AST

it looks like this

{ type: 'TLProgram',
  start: { offset: 0, line: 1, column: 1 },
  end: { offset: 67, line: 4, column: 1 },
  constructors: 
   { type: 'ConstructorDeclarations',
     start: { offset: 0, line: 1, column: 1 },
     end: { offset: 67, line: 4, column: 1 },
     declarations: 
      [ { type: 'BuiltinCombinatorDeclaration',
          start: { offset: 0, line: 1, column: 1 },
          end: { offset: 12, line: 1, column: 13 },
          id: 
           { type: 'FullCombinatorIdentifier',
             start: { offset: 0, line: 1, column: 1 },
             end: { offset: 3, line: 1, column: 4 },
             name: 'int' },
          result: 
           { type: 'BoxedTypeIdentifier',
             start: { offset: 8, line: 1, column: 9 },
             end: { offset: 11, line: 1, column: 12 },
             name: 'Int' } } ] },
...

ast-types already supports adding custom AST type definitions using the def syntax. (See [core.ts])(https://github.com/benjamn/ast-types/blob/master/def/core.ts) for an example of such syntax.)

However, to print custom ASTs, you would have to modify Recast’s code. Based on what I understand, if there are no modifications to the AST, Recast should be able to reprint your generated code if you include the tokens in the generated AST, but to reprint custom AST nodes will require further modifications to the Recast code.

tl;dr: this issue should probably be opened on the Recast side instead.