A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.pmd-code.org/pmd-doc-7.0.0/pmd_devdocs_major_adding_new_language_javacc.html below:

Adding PMD support for a new JavaCC grammar based language

How to add a new language to PMD using JavaCC grammar.

Table of Contents Before you start…

This is really a big contribution and can’t be done with a drive by contribution. It requires dedicated passion and long commitment to implement support for a new language.

This step-by-step guide is just a small intro to get the basics started, and it’s also not necessarily up-to-date or complete. You have to be able to fill in the blanks.

After the basic support for a language is there, there are lots of missing features left. Typical features that can greatly improve rule writing are: symbol table, type resolution, call/data flow analysis.

Symbol table keeps track of variables and their usages. Type resolution tries to find the actual class type of each used type, following along method calls (including overloaded and overwritten methods), allowing to query subtypes and type hierarchy. This requires additional configuration of an auxiliary classpath. Call and data flow analysis keep track of the data as it is moving through different execution paths a program has.

These features are out of scope of this guide. Type resolution and data flow are features that definitely don’t come for free. It is much effort and requires perseverance to implement.

Steps 1. Start with a new sub-module 2. Implement an AST parser for your language 3. Create AST node classes 4. Generate your parser (using JJT) 5. Create a PMD parser “adapter” 6. Create a language version handler 7. Create a base visitor 8. Make PMD recognize your language 9. Add AST regression tests

For languages, that use an external library for parsing, the AST can easily change when upgrading the library. Also for languages, where we have the grammar under our control, it is useful to have such tests.

The tests parse one or more source files and generate a textual representation of the AST. This text is compared against a previously recorded version. If there are differences, the test fails.

This helps to detect anything in the AST structure that changed, maybe unexpectedly.

A complete example can be seen in the JavaScript module: net.sourceforge.pmd.lang.ecmascript.ast.JsTreeDumpTest. The test resources are in the subpackage “testdata”: pmd-javascript/src/test/resources/net/sourceforge/pmd/lang/ecmascript/ast/testdata/.

The Scala module also has a test, written in Kotlin instead of Java: net.sourceforge.pmd.lang.scala.ast.ScalaParserTests.

10. Create an abstract rule class for the language 11. Create rules 12. Test the rules 13. Create documentation page

Finishing up your new language module by adding a page in the documentation. Create a new markdown file <langId>.md in docs/pages/pmd/languages/. This file should have the following frontmatter:

---
title: <Language Name>
permalink: pmd_languages_<langId>.html
last_updated: <Month> <Year> (<PMD Version>)
tags: [languages, PmdCapableLanguage, CpdCapableLanguage]
---

On this page, language specifics can be documented, e.g. when the language was first supported by PMD. There is also the following Jekyll Include, that creates summary box for the language:


{% include language_info.html name='<Language Name>' id='<langId>' implementation='<langId>::lang.<langId>.<langId>LanguageModule' supports_cpd=true supports_pmd=true %}

XPath integration

PMD exposes the AST nodes for use by XPath based rules (see DOM representation of ASTs). Most Java getters in the AST classes are made available by default. These getters constitute the API of the language. If a getter method is renamed, then every XPath rule that uses this getter also needs to be adjusted. In order to have more control over this, there are two annotations that can be used for AST classes and their methods:

Note: Not all getters are available for XPath rules. It depends on the result type. Especially Lists or Collections in general are not supported.

Only the following Java result types are supported:

Debugging with Rule Designer

When implementing your grammar it may be very useful to see how PMD parses your example files. This can be achieved with Rule Designer:

Optional features Metrics

If you want to add support for computing metrics:

See JavaMetrics for an example.

Symbol table

A symbol table keeps track of variables and their usages. It is part of semantic analysis and would be executed in your parser adapter as an additional pass after you got the initial AST.

There is no general language independent API in PMD core. For now, each language will need to implement its own solution. The symbol information that has been resolved in the additional parser pass can be made available on the AST nodes via extra methods, e.g. getSymbolTable(), getSymbol(), or getUsages().

Currently only Java provides an implementation for symbol table, see Java-specific features and guidance.

Note:

With PMD 7.0.0 the symbol table and type resolution implementation has been rewritten from scratch. There is still an old API for symbol table support, that is used by PLSQL, see

net.sourceforge.pmd.lang.symboltable

. This will be deprecated and should not be used.

Type resolution

For typed languages like Java type information can be useful for writing rules, that trigger only on specific types. Resolving types of expressions and variables would be done after in your parser adapter as yet another additional pass, potentially after resolving the symbol table.

Type resolution tries to find the actual class type of each used type, following along method calls (including overloaded and overwritten methods), allowing to query subtypes and type hierarchy. This might require additional configuration for the language, e.g. in Java you need to configure an auxiliary classpath.

There is no general language independent API in PMD core. For now, each language will need to implement its own solution. The type information can be made available on the AST nodes via extra methods, e.g. getType().

Currently only Java provides an implementation for type resolution, see Java-specific features and guidance.

Call and data flow analysis

Call and data flow analysis keep track of the data as it is moving through different execution paths a program has. This would be yet another analysis pass.

There is no general language independent API in PMD core. For now, each language will need to implement its own solution.

Currently Java has some limited support for data flow analysis, see Java-specific features and guidance.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4