Skip to content

Mapper of XPath expressions onto AST nodes #4369

@MEZk

Description

@MEZk

We need to investigate how to map XPath expressions onto AST nodes. To begin with, it is worth designing a model of mapper which will allow to map XPath expressions onto AST nodes. The main idea of the mapper is to connect XPath expressions which will be based on the values of TokenTypes enum onto the nodes of DetailAST.

For example, if we have the following code fragment:

public class TestClass {
    void method() {
    }
}

the following tree structure:

CLASS_DEF -> CLASS_DEF [1:0]
|--MODIFIERS -> MODIFIERS [1:0]
|  `--LITERAL_PUBLIC -> public [1:0]
|--LITERAL_CLASS -> class [1:7]
|--IDENT -> TestClass [1:13]
`--OBJBLOCK -> OBJBLOCK [1:23]
    |--LCURLY -> { [1:23]
    |--METHOD_DEF -> METHOD_DEF [2:4]
    |  |--MODIFIERS -> MODIFIERS [2:4]
    |  |--TYPE -> TYPE [2:4]
    |  |  `--LITERAL_VOID -> void [2:4]
    |  |--IDENT -> method [2:9]
    |  |--LPAREN -> ( [2:15]
    |  |--PARAMETERS -> PARAMETERS [2:16]
    |  |--RPAREN -> ) [2:16]
    |  `--SLIST -> { [2:18]
    |      `--RCURLY -> } [3:4]
    `--RCURLY -> } [4:0]

and the following XPath expression (example, format should be discussed):

//ClassDef[@Text='TestClass']

than the parser should connect the XPath expression elements with the AST nodes in this way:

DetailAST (type: CLASS_DEF, text: "TestClass") <- XpathExpression(ClassDef, text: "TestClass")

The base idea can be taken from PMD. They implemented both Saxon and Jaxen mappers. From my point of view, Saxon mapper is easier to reimplement for our needs. Saxon XPath rule mapper maps each XPath node onto AST node. Their algorithm is based on the tree traversal algorithm. They map each node of the AST starting with root onto XPath element.

Thus, what should be done in the scope of this issue:

  1. Design XPath expression format for the new Checkstyle Suppression Model;
  2. Investigate what library (Saxon, Jaxen, or native Java) should be used to implement mapper;
  3. Provide base details of mapper model implementation (UML diagram of classes can be a good example).

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions