Skip to content

Commit 4899d72

Browse files
authored
Generate parser (#848)
* integrates parser generation into build file and changes parser generation script to optionally download the necessary ANTLR JAR * fixes CI by providing access to genparser.sh * simplifies parser generator script as we no longer need to make the paths in the comments relative * cleanup
1 parent cd1dcfe commit 4899d72

File tree

8 files changed

+32
-19450
lines changed

8 files changed

+32
-19450
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,10 @@ project/
2828

2929
# genparser script
3030
genparser.config
31+
.genparser/
32+
33+
# generated parser files
34+
src/main/java/viper/gobra/frontend/GobraLexer.java
35+
src/main/java/viper/gobra/frontend/GobraParser.java
36+
src/main/java/viper/gobra/frontend/GobraParserBaseVisitor.java
37+
src/main/java/viper/gobra/frontend/GobraParserVisitor.java

build.sbt

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@
44
//
55
// Copyright (c) 2011-2020 ETH Zurich.
66

7-
import scala.sys.process.Process
7+
import scala.sys.process._
88
import scala.util.Try
99

1010
// Import general settings from viperserver and, transitively, from carbon, silicon and silver.
1111
// we assume that carbon/silver and silicon/silver point to the same version of the silver repo
1212
lazy val server = project in file("viperserver")
1313

14+
lazy val genParser = taskKey[Unit]("Generate Gobra's parser")
15+
genParser := {
16+
val res: Int = ("./genparser.sh --download" !) // parentheses are not optional despite what IntelliJ suggests
17+
if (res != 0) {
18+
sys.error(s"genparser.sh exited with the non-zero exit code $res")
19+
}
20+
}
21+
1422
// Gobra specific project settings
1523
lazy val gobra = (project in file("."))
1624
.dependsOn(server % "compile->compile;test->test")
@@ -44,6 +52,9 @@ lazy val gobra = (project in file("."))
4452

4553
javacOptions := Seq("-encoding", "UTF-8"),
4654

55+
// overwrite `compile` task to depend on the `genParser` task such that the parser is generated first:
56+
Compile / compile := (Compile / compile).dependsOn(genParser).value,
57+
4758
// Run settings
4859
run / javaOptions ++= Seq(
4960
"-Xss128m",

genparser.sh

+12-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
# This script generates a new parser from the antlr4 files stored in this repository.
99
# This script MUST NOT be run from a symlink.
10+
# '--download' can optionally be passed as a parameter to download ANTLR and automatically create the config file
11+
# if a config file does not exist yet.
1012

1113
##### Constants #####
1214
RED='\033[0;31m'
@@ -18,8 +20,17 @@ RESET='\033[0m'
1820
# https://stackoverflow.com/questions/24112727/relative-paths-based-on-file-location-instead-of-current-working-directory
1921
SCRIPT_DIR=$( cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1; pwd -P )
2022

21-
##### Configure if it is the first execution #####
2223
CONFIGFILE="$SCRIPT_DIR/genparser.config"
24+
25+
##### Download and configure ANTLR if config file does not exist yet #####
26+
if ! test -f "$CONFIGFILE" && [ "$1" = "--download" ]; then
27+
mkdir -p "$SCRIPT_DIR/.genparser"
28+
DESTINATION="$SCRIPT_DIR/.genparser/antlr-4.13.1-complete.jar"
29+
curl --fail --show-error -L "https://www.antlr.org/download/antlr-4.13.1-complete.jar" --output "$DESTINATION"
30+
echo "$DESTINATION" > "$CONFIGFILE"
31+
fi
32+
33+
##### Configure if it is the first execution #####
2334
if ! test -f "$CONFIGFILE"; then
2435
echo -e "What is the ${RED}ABSOLUTE${RESET} path to the antlr4 .jar file?"
2536
read -r ANSWER
@@ -39,11 +50,5 @@ java -jar "$ANTLR4_PATH" "$SCRIPT_DIR"/src/main/antlr4/GobraLexer.g4 -package vi
3950
echo -e "${GREEN}Generating the parser:${RESET}"
4051
java -jar "$ANTLR4_PATH" "$SCRIPT_DIR"/src/main/antlr4/GobraParser.g4 -package viper.gobra.frontend -visitor -no-listener || { echo -e "${RED}Error while generating the parser.${RESET}"; exit 3; }
4152

42-
echo -e "${GREEN}Making path in comments relative:${RESET}"
43-
sed -i '' 's/\/\/ Generated from .*src\/main\/antlr4\/GobraLexer\.g4/\/\/ Generated from src\/main\/antlr4\/GobraLexer\.g4/g' "$SCRIPT_DIR"/src/main/antlr4/GobraLexer.java
44-
sed -i '' 's/\/\/ Generated from .*src\/main\/antlr4\/GobraParser\.g4/\/\/ Generated from src\/main\/antlr4\/GobraParser\.g4/g' "$SCRIPT_DIR"/src/main/antlr4/GobraParser.java
45-
sed -i '' 's/\/\/ Generated from .*src\/main\/antlr4\/GobraParser\.g4/\/\/ Generated from src\/main\/antlr4\/GobraParser\.g4/g' "$SCRIPT_DIR"/src/main/antlr4/GobraParserBaseVisitor.java
46-
sed -i '' 's/\/\/ Generated from .*src\/main\/antlr4\/GobraParser\.g4/\/\/ Generated from src\/main\/antlr4\/GobraParser\.g4/g' "$SCRIPT_DIR"/src/main/antlr4/GobraParserVisitor.java
47-
4853
echo -e "${GREEN}Moving the generated files:${RESET}"
4954
mv -v "$SCRIPT_DIR"/src/main/antlr4/*.java "$SCRIPT_DIR"/src/main/java/viper/gobra/frontend/

src/main/java/viper/gobra/frontend/GobraLexer.java

-1,382
This file was deleted.

src/main/java/viper/gobra/frontend/GobraParser.java

-15,056
This file was deleted.

0 commit comments

Comments
 (0)