Skip to content

barrettotte/Pogo2Ts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pogo2Ts

Use Antlr grammars to generate TypeScript models from POGOs (Plain Old Groovy Objects). With this side project I wanted to learn a little bit more about Antlr grammars and code generation.

This is kind of just a toy and would need a pretty good amount of work to be considered usable. I just wanted have some fun with this and get a simple case working.

General Idea

  • Lex and parse POGOs using Antlr4
  • Walk through each generated AST
  • Generate TypeScript source for each POGO

Groovy Grammars

There are official Antlr4 grammars located in https://github.com/apache/groovy/blob/master/src/antlr/ . However, I found out the Parrot parser for Groovy uses a fork of Antlr4 (https://issues.apache.org/jira/browse/GROOVY-9232). This means that the official Antlr4 grammars don't work out of the box with the official Antlr4 jar.

After a lot of screwing around with trying to build the fork from https://github.com/tunnelvisionlabs/antlr4 , I decided I would just modify the existing Antlr4 Java8 grammars at https://github.com/antlr/grammars-v4/tree/master/java/java8 since Groovy is just a superset of Java.

Since I'm really just trying to convert POGOs, I will be skipping over the following items:

  • Closures
  • Metaprogramming
  • All the cool Groovy string literal stuff - (interpolation, GStrings, etc)
  • Probably much more...

My EBNF/Antlr is not strong enough to tackle these yet. If coerced enough I'd probably dive back in.

Setup (Windows)

  • Download Antlr4 jar - https://www.antlr.org/download/antlr-4.8-complete.jar
  • Add Antlr4 jar to CLASSPATH system variable
    • System Properties > Environment Variables > CLASSPATH (create if it doesn't exist)
    • Example: CLASSPATH = C:\Antlr4\antlr-4.8-complete.jar
  • Create C:\Antlr4\antlr4.bat with the content java org.antlr.v4.Tool %* and add to PATH
  • Generate lexer and parser using generate.bat
  • Run program with dotnet run <directory_path>

Usage

  • Run example files - dotnet run ../model/groovy ../model/ts com.github.barrettotte.model

References