Astranaut's core classes

Overview

Astranaut Core

Build and test Codecov License


Brief

This project contains classes describing a tree, primarily an abstract syntax tree (AST) of some programming language, and functionality for a tree conversion. We developed our AST model in order to be able to use common interfaces for analysis of ASTs, that can come from different sources, for example various third-party parsers. Moreover, this library provides base logic for processing of ASTs.

Astranaut uses this library to create and process syntax trees according to the rules described with our custom DSL. Also we use this model in the UAST research.

Requirements

  • Java 1.8
  • Maven 3.6.3+ (to build)

How to use

TODO after release

AST model

In the project, an abstract syntax tree (or just a syntax tree) is a directed graph (by the definition used in discrete mathematics) with the following additional properties:

  • Each node has a type (represented as a string);
  • Each node may optionally have data (also represented as a string);
  • The order of the successors is significant.

A type is a required property of a node, that allows you to assign the node to a certain class. For example, VariableDeclaration is a common name for all nodes that represent declarations of variables in source code.

A data is an optional property of a node that represents a string value. Most commonly, data is a property of terminal (leaf) nodes. For instance, string and numeric literals contain data.

Non-terminal nodes have children, that are stored in a list. The list is arranged in ascending order of children indexes. The order of the children is determined by a grammar of the programming language from which an AST is built. For example, if a grammar rule is <assignment> ::= <left expression> <operator> <assignment expression>, then the node assignment has 3 children. The child left expression has the index of 0, the child operator has the index of 1, and the assignment expression has the index of 2.

In our project, we construct an AST only of nodes and their relation. We do not use edge entities.

§1. Node

The base interface in the project is Node which represent each node in an AST. Every specific node in AST or UAST extends or implements Node.

A Node has the following methods:

  • getType() - returns a Type of the node as an object;
  • getTypeName() - returns a type of the node as a string;
  • getData() - returns node`s data if it exists;
  • getChildCount() - returns an amount of node`s children;
  • getChild(int index) - returns a specific child node by its index;
  • getChildrenList() - returns a list of node`s children;
  • belongsToGroup(String type) - checks if the node type belongs to the specific hierarchy of nodes.

§2. Type hierarchy

A type hierarchy is a list of node types, starting from the current node with some type and going upwards by abstraction level.

Example:

In Java Binary Expression is a variation of Expression construct. Also, Binary Expression is a common name for relational and arithmetic expressions. Moreover, arithmetic expressions include a variety of operands, like addition, multiplication and others. Then the hierarchy for the node that performs addition will be the sequence:

Expression <- Binary Expression <- Arithmetic Expression <- Addition

§3. Abstract node

An abstract node is a node that extend the Node class or its descendant.

We use an abstract node to describe a generalized name of several language constructs.

Example:

The Arithmetic Expression construct can be implemented in source code by Addition, Subtraction, Multiplication and other binary expressions. In our AST Arithmetic Expression will be an abstract node.

If the full hierarchy of language constructs is

Expression <- Binary Expression <- Arithmetic Expression <- Addition

then Expression and BinaryExpression will also be abstract.

§4. Final node

A final (non-terminal) node is a node that implements the Node class or its descendant.

We use a final node to describe the last node in the hierarchy of language constructs.

Such nodes either contain a list of child nodes, or are independent units in the language, or represent a literal.

Example:

If the full hierarchy of language constructs is

Expression <- Binary Expression <- Arithmetic Expression <- Addition

then in our AST the Addition will be a final node.

§5. Type

To describe the properties of nodes we also use the interface Type. The TypeImpl class within each Node class implements this interface. Each final node has a Type. This project uses Type objects to store and collect additional data about nodes and their inheritance hierarchy.

A Type has the following methods:

  • getName() - returns a type of the node as a string;
  • getChildTypes() - returns a list of child node descriptions (ChildDescriptor) with additional information;
  • getHierarchy() - returns the hierarchy of type names to which the current type belongs;
  • belongsToGroup(String type) - checks if the node type belongs to a specific hierarchy;
  • createBuilder() - returns a constructor class that creates nodes if the given type;
  • getProperty(String name) - returns additional properties describing the features of the node type.

A Type has properties which we use as part of the unification task. Properties is a dictionary, in which a key is the name of the type characteristic, and a value is one of the possible options of this characteristic.

For now, we use the following properties:

  • color - a color of the node type which can be:
    • green, if all the languages under consideration have constructs of this type;
    • red, if a current type is language-specific, i.e. only a specific language has a construct of this type.
  • language - a name of a programming language under processing, may be:
    • java, if a red node belongs to Java;
    • python, if a red node belongs to Python;
    • js, if a red node belongs to JavaScript;
    • common, if a node is green.

§6. Node tags

The DSL that we use for nodes generation has syntax that allows to add tags to node's children.

Knowing a tag you can get some node's child directly by the name of its tag. The name of such a getter is get + capitalized <tag>.

In some cases using a tag is more convenient than referring to a child by its index.

Example:

Suppose you need to analyze a FunctionDeclaration node which is obtained as a result of source code parsing and may have a variable number of children.

If such a node is created by the following rule:

FunctionDeclaration <- [ModifierBlock], [TypeName], Identifier, ParameterBlock, StatementBlock;

then to get the name of the function, which is of Identifier type you will need to, firstly, get the amount of children. Secondly, you will iterate over them and check their types in order to find the expected one.

However, if a node is created with the usage of tags:

FunctionDeclaration <- [modifiers@ModifierBlock], [restype@TypeName], name@Identifier, parameters@ParameterBlock, body@StatementBlock;

you can get a function name with the only one method getName().

AST processing

The core includes the following classes for a tree processing.

For creation of ASTs using DSL:

  • Converter - applies a single transformation rule. The conversion consists of two steps. Firstly, Matcher the subtree to the template. Secondly, if the subtree satisfies the comparison conditions, the converter builds a new subtree using Factory, that is a collection of possible (supported) nodes. If the transformation rule cannot be applied, it returns an empty subtree.
  • Matcher - compares the subtree to the template of the tree to be modified.
  • Adapter - replaces the original tree with a new one. Adapter sequentially traverses all nodes of a tree, applying all converters to each node. The order in which a tree is traversed is determined by topological sorting. Thus, leaf nodes are analyzed first, followed by nodes that contain leaf nodes, and so on up to the root node, being analyzed last.

For post-processing of previously created ASTs (described with Node classes):

  • NodeReplacer - takes the initial tree and replaces the specified subtree with a new one.

Contributors

  • Ivan Kniazkov, @kniazkov
  • Polina Volkhontseva, @pollyvolk

See our Contributing policy.

You might also like...

Astranaut's core classes

Astranaut Core Brief This project contains classes describing a tree, primarily an abstract syntax tree (AST) of some programming language, and functi

Sep 13, 2022

Provides additional date-time classes that complement those in JDK 8

ThreeTen-Extra ThreeTen-Extra provides additional date-time classes that complement those in JDK 8. Not every piece of date/time logic is destined for

Jan 8, 2023

Java unlimited redefinition of classes at runtime.

Hotswap Agent This is an overview page, please visit hotswapagent.org for more information. Overview Java unlimited runtime class and resource redefin

Dec 30, 2022

Joda-Time is the widely used replacement for the Java date and time classes prior to Java SE 8.

Joda-Time Joda-Time provides a quality replacement for the Java date and time classes. The design allows for multiple calendar systems, while still pr

Dec 27, 2022

Lightweight analysis tool for detecting mutability in Java classes

What is Mutability Detector? Mutability Detector is designed to analyse Java classes and report on whether instances of a given class are immutable. I

Dec 29, 2022

jetbrick utility classes

jetbrick-commons This is an utility classes project for jetbrick framework. Documentation http://subchen.github.io/ Dependencies dependency gro

Nov 15, 2022

Some utility classes around java records

record-util Some utility classes around java records On the menu MapTrait Transform any record to a java.util.Map just by implementing the interface M

Apr 6, 2022

Exercico com Junit 5, UML, Interação de classes e Exception

AcessoClube Exercico com Junit 5, UML, Interação de classes e Exception Exercicio: Projete e desenvolva um sistema de controle de acesso a um clube co

May 3, 2021

Este é um projeto Maven que contém vários métodos e classes criados, além de vários testes unitários. Os métodos desse projeto não contém uma implementação de fato, sendo assim você desenvolvedor(a) deverá escreve-lo.

Complete o código em Java O projeto tem como objetivo auxiliar aqueles que estão iniciando sua jornada em programação, mais precisamente, em Java. Est

Nov 3, 2022

🎒 💻 Material for Computer Club Classes

🎒 💻 Material for Computer Club Classes

MNNIT Computer Coding Club This repository contains the codes, support links and other relevant materials for every class under Computer Coding Club,

Dec 18, 2022

Base classes and utilities for Java Carbyne Stack service clients

Carbyne Stack Java HTTP Client This project provides common functionality for the Java-based HTTP clients for the Carbyne Stack microservices. License

Oct 15, 2022

A series of classes with respective junit tests.

Contact-Service-Files A series of classes with respective junit tests. This project has certainly shown me the importance of testing in the SDLC. Ther

Dec 20, 2021

JHook - A tool that can dynamically modify Java classes at runtime.

JHook A tool that can dynamically modify Java classes at runtime. Demo Tested on Java 1.8 - Java 17, just support JDK package com.binklac.jhook.test;

Dec 23, 2022

Create Queries using only POJO classes.

Fluent Query Create Queries, Inserts, Updates e Deletes using only POJO classes. Features Configuration over code: independence business code of the i

Sep 1, 2022

Quick and dirty framework for on-the-fly patching of classes via the java attach api and transformers.

Nuclear Quick and easy framework for on the fly patching of running java applications. Inspiration from an idea of an injectable minecraft cheat. What

Dec 28, 2021

Dalek’s Game is a Java project for Object-Oriented Technologies classes

Dalek’s Game is a Java project for Object-Oriented Technologies classes

Dalek’s Game is a Java project for Object-Oriented Technologies classes. The goal of the game is to survive and eliminate Daleks (who are the enemies) from the board. Each time the main character (Doctor) moves, every Dalek follows him. When a Dalek bumps into another Dalek or junk, it becomes junk. When there is no Dalek left on the map, Doctor wins.

Feb 7, 2022

A plugin for the ja-netfilter, it can dump the transformed classes.

A plugin for the ja-netfilter, it can dump the transformed classes.

Apr 17, 2022

The Java collections framework provides a set of interfaces and classes to implement various data structures and algorithms.

The Java collections framework provides a set of interfaces and classes to implement various data structures and algorithms.

Homework #14 Table of Contents General Info Technologies Used Project Status Contact General Information Homework contains topics: Sorting an ArrayLis

Feb 12, 2022

KC4Streams - a simple Java library that provides utility classes and standard implementations for most of the Kafka Streams pluggable interfaces

KC4Streams (which stands for Kafka Commons for Streams) is a simple Java library that provides utility classes and standard implementations for most of the Kafka Streams pluggable interfaces.

Mar 2, 2022
Comments
  • Bug in PositionSet.findSuitableBaseType

    Bug in PositionSet.findSuitableBaseType

    In UAST I wrote some rules for unification of JavaScript tree.

    I tried to unify a tree from this snippet:

    class Car {
        constructor(name, year) {
            this.name = name;
            this.year = year;
        }
    }
    

    However the PropertyAccess node cannot be created.

    In my opinion, the problem is here

    opened by pollyvolk 3
Releases(1.0.5)
  • 1.0.5(Oct 11, 2022)

    See #8, release log:

    • feb0685767535b2f39f7c2aeaecc35d919b5b29a by @kniazkov: Merge pull request #9 from kni...
    • 1b5216427adb8d5b2497b49f963035c72e7a5370 by @kniazkov: Fix #8

    Released by Rultor 2.0-SNAPSHOT, see build log

    Source code(tar.gz)
    Source code(zip)
  • 1.0.4(Oct 10, 2022)

    See #7, release log:

    • 563bab1c4736c27d6fc49bcae7ef377ea72b5ecd by @kniazkov: Merge pull request #7 from kni...
    • dd0325e6aff630530e0718f4998e6dff52932d8c by @kniazkov: Fix adapter to be able perform...
    • be68b3fb21fab5a6589c6233521856f9045bfe6e by @kniazkov: Merge pull request #1 from cqf...

    Released by Rultor 2.0-SNAPSHOT, see build log

    Source code(tar.gz)
    Source code(zip)
  • 1.0.3(Sep 13, 2022)

    See #5, release log:

    • 2615a53f0ab79fc53430c1e7bdc39253eb5bfcd6 by @kniazkov: Merge pull request #6 from pol...
    • 8825c0a016109c916eb11ac89463de6e37a76212: Fixes in exceptions

    Released by Rultor 2.0-SNAPSHOT, see build log

    Source code(tar.gz)
    Source code(zip)
  • 1.0.2(Sep 13, 2022)

    See #5, release log:

    • f4fb61ef4fc129cc34847644d7c13f038f6a8004 by @kniazkov: Merge pull request #4 from pol...
    • fdda7d338fa715aa5ad80bda7a2147b0807eeb9e: Changes related to the update ...
    • 221bcd9ebbcc14fc6f2ef9982fe0d90b5bedb6a5 by @kniazkov: Merge pull request #3 from pol...
    • 8ba197806c73055806e9841ab93effb146801444: Added utility classes and test...

    Released by Rultor 2.0-SNAPSHOT, see build log

    Source code(tar.gz)
    Source code(zip)
  • 1.0.1(Sep 8, 2022)

    See #1, release log:

    • 8d8d4b90eab752fae77882f0e1b70f76d59b8a0f by @kniazkov: Merge pull request #2 from pol...
    • e5dfa4a5bc3220a6d8860f6d6c27c3cfa8966fe2: Updates in Adapter
    • e5a545ed6c8588b75dc98973c5a82a3d5484f535 by @pollyvolk: Updates in Javadoc and readme
    • 5565c9e8aabfea74699742d48f259165044d656d by @pollyvolk: Removed unused dependencies
    • 86c836b0be601eef3701477a6e625f0864c333dd by @pollyvolk: Changed version and package
    • 189b9b6b911bd23f409d56aa6a9b9e0513b64303 by @pollyvolk: Added rultor configuration
    • e7f22c1aac0389367ccd2a88cdc81694cc17623e by @pollyvolk: Merge branch 'master' of https...
    • efeec444f76858408ff0eb281cfd55a8bed7faaf by @pollyvolk: Fix in codecov badge
    • 8cb84a176cff6b03fc4b726d071fb353e4754ef8 by @pollyvolk: Set up build and test workflow
    • 71e0ec4cc5aa63fa58f49d8c1caaa76e744f2061 by @pollyvolk: Delete target directory
    • c300d976c16011ac93b70606edfd5b920e605e79 by @pollyvolk: Delete .idea directory
    • 007294813491dbbf4cf16d75671a0bb26cab1316: first commit

    Released by Rultor 2.0-SNAPSHOT, see build log

    Source code(tar.gz)
    Source code(zip)
Owner
CQFN
Code Quality Foundation
CQFN
Lightweight analysis tool for detecting mutability in Java classes

What is Mutability Detector? Mutability Detector is designed to analyse Java classes and report on whether instances of a given class are immutable. I

Mutability Detector 234 Dec 29, 2022
Exercico com Junit 5, UML, Interação de classes e Exception

AcessoClube Exercico com Junit 5, UML, Interação de classes e Exception Exercicio: Projete e desenvolva um sistema de controle de acesso a um clube co

Mateus Samartini 3 May 3, 2021
Este é um projeto Maven que contém vários métodos e classes criados, além de vários testes unitários. Os métodos desse projeto não contém uma implementação de fato, sendo assim você desenvolvedor(a) deverá escreve-lo.

Complete o código em Java O projeto tem como objetivo auxiliar aqueles que estão iniciando sua jornada em programação, mais precisamente, em Java. Est

Felix Gilioli 44 Nov 3, 2022
Randomized Testing (Core JUnit Runner, ANT, Maven)

RANDOMIZED TESTING ================== JUnit test runner and plugins for running JUnit tests with pseudo-randomness. See the following for more infor

null 167 Dec 26, 2022
Graphstream core

GraphStream The GraphStream project is a java library that provides an API to model, analyze and visualize graphs and dynamic graphs. Check out the We

GraphStream 382 Jan 6, 2023
Core System of LANSERVER

LanEssential Core System of LANSERVER This is a minecraft plugin for paper 1.17.1 server. This plugin has many features, but many bugs too. It's on al

lanthanide 0 Aug 11, 2022
Core library of SinoCraft Mods

Core library of SinoCraft Mods

SinoCraft Project Team 4 Aug 8, 2022
The ultimate KitPvP Core with a ton of features! Fully configurable & Open source.

KitPvP-Core The ultimate KitPvP Core with a ton of features! Fully configurable & Open source. Placeholders My core plugin offers several features wit

ImGqbbo 1 Nov 14, 2022
Core classes for Skija, JWM and HumbleUI

Core types for Skija, JWM and HumbleUI Dependency Key Value groupId io.github.humbleui artifactId types version Building from source Prerequisites: Sh

Humble UI 5 Apr 24, 2022
Core for open source libraries, included some important Classes for those libs.

OpenSource Core You could also read the CHINESE version of README This is a very useful Java class library. In this update, we have merged the origina

Theodore Hills 10 Nov 16, 2022