Skip to content

Commit

Permalink
Add Solidity support.
Browse files Browse the repository at this point in the history
  • Loading branch information
gfour committed Feb 26, 2021
1 parent 0f14d56 commit f71bb7d
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "grammars-v4"]
path = grammars-v4
url = https://github.com/antlr/grammars-v4.git
[submodule "extra-grammars/solidity-parser"]
path = extra-grammars/solidity-parser
url = https://github.com/solidity-parser/antlr.git
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ task copyParsers(type: Copy) {
println "Copying parsers..."
String homeDir = System.getProperty("user.home")
from homeDir + "/.m2/repository/org/antlr/grammars/"
from 'extra-grammars'
into "src/main/resources/parsers"
['C', 'cobol85', 'CPP14', 'golang', 'kotlin-formal', 'Lua', 'php', 'python3', 'rust'].each { String lang ->
include "${lang}/**/*.jar"
}
include 'solidity-parser/solidity.jar'
}

task cleanResources() {
Expand Down
1 change: 1 addition & 0 deletions extra-grammars/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
antlr-4.*-complete.jar
2 changes: 2 additions & 0 deletions extra-grammars/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This directory contains ANTLR-based parsers that are not part of the
grammars-v4 repository.
1 change: 1 addition & 0 deletions extra-grammars/solidity-parser
Submodule solidity-parser added at a4d87b
17 changes: 17 additions & 0 deletions install-parsers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,20 @@ echo "* Building parser dependencies..."
cd grammars-v4
mvn install --pl c --pl cobol85 --pl cpp --pl golang --pl kotlin/kotlin-formal --pl lua --pl php --pl python/python3 --pl rust --am
cd ..

echo "* Building extra grammars..."
cd extra-grammars
ANTLR_JAR=antlr-4.9.1-complete.jar
if [ ! -f "${ANTLR_JAR}" ]; then
curl https://www.antlr.org/download/${ANTLR_JAR} -o ${ANTLR_JAR}
fi
echo "* Building Solidity parser..."
cd solidity-parser
java -jar ../antlr-4.9.1-complete.jar *.g4
javac -cp ../antlr-4.9.1-complete.jar *.java
rm -rf target
mkdir -p target
mv *.class target
jar -cf solidity.jar -C target .
cd ..
cd ..
2 changes: 2 additions & 0 deletions logic/solidity-logic.dl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "schema.dl"
#include "base-logic.dl"
10 changes: 8 additions & 2 deletions src/main/java/org/clyze/antlr2datalog/ParserConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.clyze.antlr2datalog;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -37,6 +38,8 @@ public enum ParserConfiguration {
PYTHON3("Python3", "Python3Lexer", "Python3Parser", "file_input", Collections.singletonList(".py"), "python3/1.0-SNAPSHOT/python3-1.0-SNAPSHOT.jar", true, false),
// Rust, MIT license
RUST("Rust", "RustLexer", "RustParser", "crate", Collections.singletonList(".rs"), "rust/1.0-SNAPSHOT/rust-1.0-SNAPSHOT.jar", true, false),
// Solidity, MIT license
SOLIDITY("Solidity", "SolidityLexer", "SolidityParser", "sourceUnit", Collections.singletonList(".sol"), "solidity-parser/solidity.jar", false, false),
;

/** The path in the local Maven repo for grammars-v4 grammars. */
Expand All @@ -54,7 +57,10 @@ public enum ParserConfiguration {
final Collection<String> extensions;
/** The parser JAR path in the local Maven repo (suffix). */
private final String jarPath;
/** If true, use special path prefix in local Maven repo. */
/**
* If true, use special grammars-v4 path prefix in local Maven repo.
* If false, use the extra-grammars directory.
*/
private final boolean isAntlrGrammars;
/** If true, use lowercase character stream (for case-insensitive languages such as PHP). */
public final boolean lowerCase;
Expand Down Expand Up @@ -122,7 +128,7 @@ private String getJarPath(boolean debug) {
System.out.println("No bundled parser, attempting resolution via file system...");
}
String homeDir = System.getProperty("user.home");
return (isAntlrGrammars ? homeDir + "/.m2/repository/" + ANTLR_GRAMMARS_PREFIX : "") + this.jarPath;
return (isAntlrGrammars ? homeDir + "/.m2/repository/" + ANTLR_GRAMMARS_PREFIX : (new File("extra-grammars").getAbsolutePath() + "/")) + this.jarPath;
}

public CharStream getCharStream(String path, InputStream inputStream) throws IOException, UnsupportedParserException {
Expand Down

0 comments on commit f71bb7d

Please sign in to comment.