Skip to content

Commit 36109bd

Browse files
authored
Add script to generate parser (#498)
* Add script to automate generation of the parser * Add license * Improve error messages
1 parent 7294137 commit 36109bd

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ logger.log
2424
.metals/
2525
.vscode/
2626
project/
27+
28+
# antlr4
29+
*.interp
30+
*.tokens
31+
32+
# genparser script
33+
genparser.config

genparser.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
# This Source Code Form is subject to the terms of the Mozilla Public
3+
# License, v. 2.0. If a copy of the MPL was not distributed with this
4+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
#
6+
# Copyright (c) 2011-2022 ETH Zurich.
7+
8+
# This script generates a new parser from the antlr4 files stored in this repository.
9+
# This script MUST NOT be run from a symlink.
10+
11+
##### Constants #####
12+
RED='\033[0;31m'
13+
GREEN='\033[0;32m'
14+
RESET='\033[0m'
15+
16+
# This path is taken to be able to call this script from any directory and have
17+
# the same consistent behaviour. This was taken from
18+
# https://stackoverflow.com/questions/24112727/relative-paths-based-on-file-location-instead-of-current-working-directory
19+
SCRIPT_DIR=$( cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1; pwd -P )
20+
21+
##### Configure if it is the first execution #####
22+
CONFIGFILE="$SCRIPT_DIR/genparser.config"
23+
if ! test -f "$CONFIGFILE"; then
24+
echo -e "What is the ${RED}ABSOLUTE${RESET} path to the antlr4 .jar file?"
25+
read -r ANSWER
26+
echo "$ANSWER" > "$CONFIGFILE"
27+
fi
28+
29+
ANTLR4_PATH=$(cat "$CONFIGFILE")
30+
31+
if ! test -f "$ANTLR4_PATH"; then
32+
echo "The antlr4 .jar file was not found ($ANTLR4_PATH). Delete the file $CONFIGFILE to reconfigure this script."
33+
exit 2
34+
fi
35+
36+
echo -e "${GREEN}Generating the lexer:${RESET}"
37+
java -jar "$ANTLR4_PATH" "$SCRIPT_DIR"/src/main/antlr4/GobraLexer.g4 -package viper.gobra.frontend || { echo -e "${RED}Error while generating the lexer.${RESET}"; exit 3; }
38+
39+
echo -e "${GREEN}Generating the parser:${RESET}"
40+
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; }
41+
42+
echo -e "${GREEN}Moving the generated files:${RESET}"
43+
mv -v "$SCRIPT_DIR"/src/main/antlr4/*.java "$SCRIPT_DIR"/src/main/java/viper/gobra/frontend/

0 commit comments

Comments
 (0)