Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A saucerful of secrets #54

Draft
wants to merge 23 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ jobs:
cache: maven
- name: Maven Build and Verify
run: mvn -B clean verify
- uses: codecov/codecov-action@v2
- uses: codecov/codecov-action@v4.0.0-beta.3
with:
token: ${{ secrets.CODECOV_TOKEN }}
name: Code coverage report
fail_ci_if_error: true
fail_ci_if_error: true
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ SOFTWARE.
<plugin>
<groupId>org.cqfn</groupId>
<artifactId>astranaut-maven-plugin</artifactId>
<version>0.1.15</version>
<version>0.2.21</version>
<configuration>
<dsl>${basedir}/src/main/dsl/uast.dsl</dsl>
<output>${basedir}/src/main/java</output>
Expand All @@ -171,7 +171,7 @@ SOFTWARE.
<dependency>
<groupId>org.cqfn</groupId>
<artifactId>astranaut-core</artifactId>
<version>1.0.6</version>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
699 changes: 54 additions & 645 deletions src/main/dsl/uast.dsl

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,42 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cqfn.uast.lang.java.gen;
package org.cqfn.uast;

import org.cqfn.uast.tree.green.Name;
import org.cqfn.astranaut.core.Node;
import org.cqfn.uast.codegen.Syntax;
import org.cqfn.uast.lang.SyntaxSelector;

/**
* Code generator for Java names.
* Generates source code from a unified tree.
*
* @since 0.1.2
* @since 0.1
*/
public final class Names {
public final class Generator {
/**
* The instance.
* Root node of the syntax tree.
*/
public static final Names INSTANCE = new Names();
private final Node root;

/**
* Constructor.
* @param root Root node of the syntax tree
*/
private Names() {
public Generator(final Node root) {
this.root = root;
}

/**
* Generates source code from Java name.
* @param name A name
* Generates source code from a unified tree.
* @param language The name of the programming language
* @return Source code
*/
public String generate(final Name name) {
final Name composition = name.getComposition();
String left = "";
if (composition != null) {
left = this.generate(composition);
public String generate(final String language) {
String code = "";
final Syntax syntax = SyntaxSelector.INSTANCE.select(language);
if (syntax != null) {
code = syntax.getCode(this.root);
}
final String right = name.getLast().getData();
final String result;
if (left.isEmpty()) {
result = right;
} else {
result = String.format("%s.%s", left, right);
}
return result;
return code;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cqfn.uast.lang;
package org.cqfn.uast;

import java.io.IOException;
import org.cqfn.astranaut.core.EmptyTree;
import org.cqfn.astranaut.core.Node;
import org.cqfn.astranaut.core.utils.FilesReader;
import org.cqfn.astranaut.core.utils.JsonDeserializer;
import org.cqfn.uast.lang.FactorySelector;
import org.cqfn.uast.lang.java.JavaParser;
import org.cqfn.uast.lang.javascript.JavaScriptParser;
import org.cqfn.uast.lang.python.PythonParser;
Expand All @@ -37,44 +38,49 @@
*
* @since 0.1
*/
public class SourceCodeParser {
public final class Parser {
/**
* The name of the file that contains source code.
* Flag indicating that the resulting tree should be unified.
*/
private final String filename;
private boolean unified;

/**
* Constructor.
* @param filename The name of the file that contains source code
*/
public SourceCodeParser(final String filename) {
this.filename = filename;
public Parser() {
this.unified = true;
}

/**
* Parses source code.
* Do not unify the resulting tree.
* Result will be a tree from the original parser.
*/
public void doNotUnifyResultingTree() {
this.unified = false;
}

/**
* Parses source code from string.
* @param code Source code
* @param language The name of the programming language
* @param unified Convert to unified syntax tree
* @param antlr Retain the original ANTLR syntax tree
* @return Root node of the [unified] syntax tree
* @throws IOException In case if impossible to read source code
*/
public Node parse(final String language, final boolean unified, final boolean antlr)
throws IOException {
public Node parseString(final String code, final String language) {
Node node = EmptyTree.INSTANCE;
final String code = new FilesReader(this.filename).readAsString();
switch (language) {
case "java":
final JavaParser jap = new JavaParser(code);
node = jap.parse(unified);
node = jap.parse(this.unified);
break;
case "python":
case "py":
final PythonParser pyp = new PythonParser(code, antlr);
node = pyp.parse(unified);
final PythonParser pyp = new PythonParser(code);
node = pyp.parse(this.unified);
break;
case "javascript":
case "js":
final JavaScriptParser jsp = new JavaScriptParser(code, antlr);
node = jsp.parse(unified);
final JavaScriptParser jsp = new JavaScriptParser(code);
node = jsp.parse(this.unified);
break;
case "json":
final JsonDeserializer deserializer =
Expand All @@ -86,4 +92,46 @@ public Node parse(final String language, final boolean unified, final boolean an
}
return node;
}

/**
* Parses source code from file.
* @param path Path to file
* @return Root node of the [unified] syntax tree
* @throws IOException In case if impossible to read source code
*/
public Node parseFile(final String path) throws IOException {
final String code = new FilesReader(path).readAsString();
return this.parseString(code, Parser.getFileExtension(path));
}

/**
* Parses source code from file.
* @param path Path to file
* @param language The name of the programming language
* @return Root node of the [unified] syntax tree
* @throws IOException In case if impossible to read source code
*/
public Node parseFile(final String path, final String language) throws IOException {
final String code = new FilesReader(path).readAsString();
return this.parseString(code, language);
}

/**
* Returns the extension (i.e., type) of a file by its full path.
* Of course, there is a similar function somewhere, but it is defined here
* so as not to have unnecessary dependencies.
* @param path Full file name
* @return File extension
*/
@SuppressWarnings("PMD.ProhibitPublicStaticMethods")
public static String getFileExtension(final String path) {
final int point = path.lastIndexOf('.');
final String ext;
if (point < 0) {
ext = "";
} else {
ext = path.substring(point + 1);
}
return ext;
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/cqfn/uast/algorithms/Greening.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.cqfn.astranaut.core.EmptyTree;
import org.cqfn.astranaut.core.Factory;
import org.cqfn.astranaut.core.Node;
import org.cqfn.astranaut.core.NodeReplacer;
import org.cqfn.astranaut.core.algorithms.ReplaceNode;
import org.cqfn.astranaut.core.utils.Pair;
import org.cqfn.uast.tree.green.GreenFactory;

Expand All @@ -58,7 +58,7 @@ public Node process(final Node tree) {
final Map<Node, Node> trees = Greening.findReplacementPairs(tree);
for (final Map.Entry<Node, Node> entry : trees.entrySet()) {
final Pair<Node, Integer> modification =
new NodeReplacer().replace(result, entry.getKey(), entry.getValue());
new ReplaceNode().replace(result, entry.getKey(), entry.getValue());
result = modification.getKey();
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cqfn/uast/cli/FileConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*
* @since 0.1
*/
public class FileConverter implements IStringConverter<File> {
public final class FileConverter implements IStringConverter<File> {
/**
* The option name.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cqfn/uast/cli/ImagePathValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*
* @since 0.1
*/
public class ImagePathValidator implements IParameterValidator {
public final class ImagePathValidator implements IParameterValidator {
@Override
/**
* Validates an input option parameter that should contain
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cqfn/uast/cli/JsonPathValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*
* @since 0.1
*/
public class JsonPathValidator implements IParameterValidator {
public final class JsonPathValidator implements IParameterValidator {
@Override
/**
* Validates an input option parameter that should contain
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cqfn/uast/cli/LanguageConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*
* @since 0.1
*/
public class LanguageConverter implements IStringConverter<String> {
public final class LanguageConverter implements IStringConverter<String> {
/**
* The option name.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* SOFTWARE.
*/

package org.cqfn.uast;
package org.cqfn.uast.cli;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
Expand All @@ -36,15 +36,11 @@
import org.cqfn.astranaut.core.exceptions.WrongFileExtension;
import org.cqfn.astranaut.core.utils.JsonSerializer;
import org.cqfn.astranaut.core.utils.TreeVisualizer;
import org.cqfn.uast.Parser;
import org.cqfn.uast.algorithms.Algorithm;
import org.cqfn.uast.cli.AlgorithmConverter;
import org.cqfn.uast.cli.ImagePathValidator;
import org.cqfn.uast.cli.JsonPathValidator;
import org.cqfn.uast.cli.LanguageConverter;
import org.cqfn.uast.lang.SourceCodeParser;

/**
* Main class.
* Starting point of command-line interface.
*
* @since 0.1
*/
Expand Down Expand Up @@ -114,15 +110,6 @@ public final class Main {
)
private boolean raw;

/**
* The ANTLR option.
*/
@Parameter(
names = "--antlr",
description = "Do not simplify ANTLR tree"
)
private boolean antlr;

/**
* The help option.
*/
Expand Down Expand Up @@ -165,8 +152,7 @@ public static void main(final String... args) throws IOException, WrongFileExten
* @throws WrongFileExtension If a file extension for visualization is invalid
*/
private void run() throws IOException, WrongFileExtension {
final String ext = this.source.getName()
.substring(this.source.getName().lastIndexOf('.') + 1);
final String ext = Parser.getFileExtension(this.source.getName());
String lang = ext;
if ("txt".equals(ext)) {
if (this.language.isEmpty()) {
Expand All @@ -177,8 +163,11 @@ private void run() throws IOException, WrongFileExtension {
lang = this.language;
}
}
final Node node = new SourceCodeParser(this.source.getPath())
.parse(lang, !this.raw, this.antlr);
final Parser parser = new Parser();
if (this.raw) {
parser.doNotUnifyResultingTree();
}
final Node node = parser.parseFile(this.source.getPath(), lang);
Node result = node;
if (!this.algorithms.isEmpty()) {
for (final Algorithm algorithm : this.algorithms) {
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/org/cqfn/uast/codegen/BaseBlockGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Ivan Kniazkov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cqfn.uast.codegen;

import org.cqfn.astranaut.core.Node;

/**
* A generator that generates a block of code for a single node.
*
* @since 0.1
*/
public interface BaseBlockGenerator {
/**
* Visits a node and generates some source code for it.
* @param node Node
* @param code Code block in which to add lines of code and nested blocks
* @param syntax Programming language syntax
*/
void exec(Node node, CodeBlock code, Syntax syntax);
}
Loading
Loading