Find your content:

Search form

You are here

Grammar for creating an Apex parser


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).

Attribution to: Daniel Ballinger

Possible Suggestion/Solution #1

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.

Attribution to: Peter Wisnovsky

Possible Suggestion/Solution #2

Although I already answered years ago I want to add some news. There is a wonderful Apex parser implementation as part of the 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 interal parser. It works like a charm.

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

Attribution to: Robert Sösemann

Possible Suggestion/Solution #3

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

Attribution to: Daniel Ballinger

Possible Suggestion/Solution #4

The 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)

Attribution to: NSjonas

Possible Suggestion/Solution #5

Long before the 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( , its Javascript bridge and the free 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:

Attribution to: Robert Sösemann

Possible Suggestion/Solution #6

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

Attribution to: Abhinav Gupta
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content