Using the TypeScript language service to parse source files

Topics: General
Mar 25, 2013 at 6:48 PM
Edited Mar 25, 2013 at 6:55 PM
I am looking for some direction on using the TypeScript language service to parse TypeScript source files. I have browsed the source, however I am hoping someone can point me in the right direction for what I am trying to accomplish...

Ideally what I would like to accomplish is the following: pass a TypeScript file to be parsed and have a data structure returned that allows me to access the body of a method, either as a string or as a collection of tokens I can use to reconstruct the source.

I have a C# to TypeScript code generator which compiles C# interfaces and classes to TypeScript, however it leaves the body of methods empty. If a user implements a method in an automatically generated output file I want to be able to preserve that implementation and put it back into the newly generated output file instead of the default empty implementation.

I would prefer to use the TypeScript language service to do this instead of generating my own parser with a tool like ANTLR however I am not finding much in terms of documentation or examples.
Mar 25, 2013 at 7:00 PM
I do not understand your use case (specifically the last sentence of the third paragraph. I have not used it from the language service, but I have programmatically parsed, type-checked, and emitted code using the TypeScript compiler. It's really easy, just dig into the source and see what TypeScript is doing. Basically construct a new TypeScript.TypeScriptCompiler (need to give error info and some settings), add source units (including lib.d.ts) which are parsed on add, then just call typeCheck (unless you don't want type checking). The compiler has a scripts property with all the scripts as a complete AST complete w/ type info.
Mar 25, 2013 at 7:18 PM
Edited Mar 25, 2013 at 7:20 PM
Prehaps that wasn't as clear as I could of made it, let me try and explain in more details. When my code generator converts a C# class to a TypeScript class it converts the following C#:
public class ExampleClass {
    public string ExampleMethod(int arg1) {
        return arg1.ToString()
    }
}
Into the following TypeScript:
class ExampleClass {
    ExampleMethod(arg1: number) : string {
       return "";
    }
}
When it generates the TypeScript it automatically adds it to the target project in a file called ExampleClass.ts. However, if I edit the C# ExampleClass it will cause the TypeScript class to be regenerated.

It is at this point that I want to parse the TypeScript file to see if the user has added any code, and if they have I want to read it out of the file, and put it back into the newly generated TypeScript file.

Basically I want to be able to take the following TypeScript
class ExampleClass {
    ExampleMethod(arg1: number) : string {
       return "Hello World"
    }
}
And extract the following for ExampleMethod:
return "Hello World"
Otherwise when the file is regenerated I will lose my changes to the TypeScript implementation.
Mar 28, 2013 at 3:20 PM
Edited Mar 29, 2013 at 9:07 PM
After researching the options I've decided to parse the TypeScript myself in C#. I plan to generate a parser with ANTLR, this will be more work upfront but should give me more flexibility in the future.

I am working on a open source ANTLR grammar definition for TypeScript at https://github.com/davemckeown/TypeScriptParser
Sep 24 at 12:07 AM
Edited Sep 24 at 12:07 AM
I've a problem very similar to your: parsing autogenerated typescript code to preserve methods implementation / additions during the regeneration phase.

Two questions: now that Roslyn is more mature than an year ago, have you finded something in its language services that can help on this kind of work?

And what about your work on an open source ANTRL grammar definition for Typescript? I've tryed the link in your last post but it doesn't works :(
Sep 25 at 7:34 PM
Hi Marcocenz,

I decided to abandon my effort to implement a standalone TypeScript parser in favor of using the TypeScript language service. This was for my C# to TypeScript generator, and I did not solve this problem, but I planned to use a tool like like typescript-tools to potentially solve it:

https://github.com/clausreinke/typescript-tools
Nov 12 at 12:31 PM
@dmck: How far have you got with your ANTRL grammar definition for TypeScript? There are efforts to build TypeScript support for NetBeans and now I want to connect the dots because people there are also looking for an ANTLR grammar definiton. Maybe we can work together?
Nov 12 at 5:59 PM
Hi bennyn

I followed dmck's excellent suggestion: use TypeScript parser features included in TypeScript compiler / language services.
It's certainly a well-written, full-featured parser :)
I then incorporated everything in my C # project using ChakraHost to execute the typescript/javascript typescript parser.
If u need details, don't hesitate to contact me.
Nov 12 at 9:29 PM
Hey marcocenz, thank you very much for your input and your offer! We want to bring TypeScript support to NetBeans IDE and the current concerns are the following: "Regarding reusing compiler from node.js package. The compiler is able to provide AST, but the problem is that you need to run parser and lexer almost after every keystroke" (quoting Petr Pisl). I myself have unfortunately never built a parser for a programming language and therefore try to connect people who know how things work / should work. You can follow our current discussion here: https://netbeans.org/bugzilla/show_bug.cgi?id=233197