title: Extending the TypeScript compiler for a DSL
body: <p>Before I start: <strong>yes</strong> I do need(/want) a DSL (even if it's just for the experience), there's no other format that expresses what I'm trying to do in a not-terribly-verbose, type-safe way.</p>
  
  <hr>
  
  <h3>Some background information, actual question at the bottom</h3>
  
  <hr>
  
  <p>I want to create a DSL-compiler for my project (TypeScript), and since the language shares a lot of syntax and concepts with TypeScript itself (I designed it that way) I thought of re-using parts of the TypeScript compiler to avoid re-inventing the wheel. I'm mostly interested in <code>scanner</code>, <code>parser</code> and <code>binder</code>. The other semantics and the emitting parts of my DLS differ so much from TypeScript that I'm pretty sure it'll be more efficient to write it from scratch.</p>
  
  <p>I found <a href="https://basarat.gitbooks.io/typescript/content/docs/compiler/overview.html" rel="noreferrer">this</a> site which explains how the compiler actually works (although I'm pretty sure it's a little out of date), and so far I can understand everything pretty well. As far as I can see there are no reasons why I should reinvent the wheel if I can extend the TypeScript compiler.</p>
  
  <p>The first part is of course the <code>scanner</code>.
  I tried to <a href="https://basarat.gitbooks.io/typescript/content/docs/compiler/scanner.html" rel="noreferrer">scan</a> a file in my language to see what happens, turns out that everything tsc doesn't know about gets classified (<code>SyntaxKind</code>) as an <code>Identifier</code> (which makes sense). To implement my DLS I would have to add keywords and syntax types to this scanner to be sent to the <code>parser</code> for building an AST. I'll have to extend the <code>parser</code> as well of course, but that's not my concern right now.</p>
  
  <p>My first instinct was to just clone the <a href="https://github.com/Microsoft/TypeScript" rel="noreferrer">TypeScript respository</a> and add the required logic. That way I could use the same API as on the aforementioned site, but with my added keywords/syntax/AST-nodes. I would then write the semantic checks and actually emitting the generated output from scratch, and all would be good.</p>
  
  <p>Unfortunately that didn't really work (hence this question). The first roadblock was just being overwhelmed by the size and structure of the repository, I just didn't where I should look.
  I tried to create a little program that used the local source instead of the built one, but that didn't work either (probably due to some complicated pipeline tsc goes through before it's in any usable state).
  I did find the <code>compiler.ts</code>, <code>parser.ts</code>, etc. files, but without a way to use my custom version they weren't of much use.
  I tried to copy only the files I thought I'd need into my own project, but that caused all kinds of typing errors and missing functions and I have no idea how (and if) I should fix them. To make it even worse: certain files had actual compilation errors when I tried to compile them in my own environment (relaxing <code>tsconfig.json</code> settings didn't help).</p>
  
  <hr>
  
  <h3>Actual question</h3>
  
  <p>I have three questions:</p>
  
  <ul>
  <li>Is it a good idea to try extend the parts of the TypeScript compiler which are useful, or should I just write from scratch? (I'm mainly talking about the <code>scanner</code>, <code>parser</code> and <code>binder</code> components.)</li>
  <li>If I should just write from scratch, are there any tools for TypeScript/JavaScript that can help me with this (like <a href="https://www.antlr.org/" rel="noreferrer">ANTLR</a> or <a href="https://javacc.org/" rel="noreferrer">javacc</a> for Java)?</li>
  <li>Are there any resources on how to implement a compiler in TypeScript/JavaScript except for the TypeScript compiler itself?</li>
  </ul>