Is there a grammar available that could be used for creating an Apex parser?

Ideally something like the SOQL BNF Notation published by Salesforce or Syntax Rules for SOQL by Jeff Douglas that I've used in the past to create a recursive descent parser for SOQL.

I did find Simon Fell's ApexCoder TextMate bundle that had the start of a grammar. There is also Joe Ferraro's MavensMate which at first glance appears to be a more up to date version of the same thing (parsing wise).

7 Answers 7


Keep an eye on Apex tooling api, which is used in Developer console. This is supposed to be released to public access soon.

  • 2
    Can't wait to see what sort of awesome tools we'll gain if that is the case!
    – Matt Lacey
    Aug 30, 2012 at 22:38
  • True, this will add new dynamics to force.com tools(IDEs), as of now all of them are based on Apex/Metadata WSDL, which is not meant to support or work well for dev tools. Sep 2, 2012 at 15:47

Although I already answered years ago I want to add some news. There is a wonderful Apex parser implementation as part of the Force.com IDE on GitHub. It's just a jar without source code but you can use it for whatever you want. And the developers behind it are really supportive and helpful.

We are currently building an Apex port of the famous Java static code analyzer PMD. And we use Salesforce.com interal parser. It works like a charm.

BTW: Daniel if you are interested to share your knowledge as an Salesforce.com expert and MVP I would love to show you how to contribute to our effort to bring Apex code metrics to the community.

  • 1
    Interesting, if it is a JAR file then we could potentially reverse engineer it. Your project sounds interesting. I'll look into it when I get a spare moment. Mar 25, 2016 at 4:58
  • 1
    @DanielBallinger Great, just in case you want to jump in I gave you write access to the repo. Mar 25, 2016 at 10:47
  • I can attest, using the jar in your own project is not too hard, just a matter of guess-and-check to gather up the right dependencies. It wasn't right for my usage but it was only an hour or two to get it working. Jul 13, 2016 at 12:55

There is now an official Apex Language syntax grammar released on GitHub.

Salesforce Apex Language syntax grammar used for colorization

The token structure is based off of Textmate's Language Grammar guidelines


The tooling API will give access to the server side parser for information relevant for tooling, such as navigation and highlighting. It will provide this information about the global entry points of installed managed packages as well, which is handy because a parser is only useful for the classes you can access the source for. Since the source for installed managed code can't be accessed, this would be a problem for tools relying on source parsing.


The tooling-force.com library (by Andrey Gavrikov) has a fairly comprehensive g4 apex definition file (used to generate ANTLR4 classes).

I'm not sure how complete the definition is, but I use this library in my vscode autocomplete extension and it seems to do a very good job (95% or better success rate on sytanx errors and defintions)


Long before the Force.com Tooling API came out I tried to buid a native tool in Salesforce to render UML class diagrams for Apex classes and SObjects.

For the rendering of the diagrams I use PlantUML(http://plantuml.sourceforge.net/) , its Javascript bridge and the free plantuml.com server.

The extraction of the class and object information is solely done in Apex. I really had to use very ugly RegExp parsing of classes to get out relationships, fields and object.

This definitly runs into limits or errors if you classes are too complex. So regard this as a proof of concept and...

Feel free to check (fork and collaborate) it on GitHub: https://github.com/rsoesemann/plantuml4force


There is an antlr grammar available at https://github.com/apex-dev-tools/apex-parser/tree/main/antlr.

pmd and apexfmt use parsers generated from the antlr grammar.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .