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

Add script to generate parser #498

Merged
merged 3 commits into from
Aug 5, 2022
Merged
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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ logger.log
.metals/
.vscode/
project/

# antlr4
*.interp
*.tokens

# genparser script
genparser.config
43 changes: 43 additions & 0 deletions genparser.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2011-2022 ETH Zurich.

# This script generates a new parser from the antlr4 files stored in this repository.
# This script MUST NOT be run from a symlink.

##### Constants #####
RED='\033[0;31m'
GREEN='\033[0;32m'
RESET='\033[0m'

# This path is taken to be able to call this script from any directory and have
# the same consistent behaviour. This was taken from
# https://stackoverflow.com/questions/24112727/relative-paths-based-on-file-location-instead-of-current-working-directory
SCRIPT_DIR=$( cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1; pwd -P )

##### Configure if it is the first execution #####
CONFIGFILE="$SCRIPT_DIR/genparser.config"
if ! test -f "$CONFIGFILE"; then
echo -e "What is the ${RED}ABSOLUTE${RESET} path to the antlr4 .jar file?"
read -r ANSWER
echo "$ANSWER" > "$CONFIGFILE"
fi

ANTLR4_PATH=$(cat "$CONFIGFILE")

if ! test -f "$ANTLR4_PATH"; then
echo "The antlr4 .jar file was not found ($ANTLR4_PATH). Delete the file $CONFIGFILE to reconfigure this script."
exit 2
fi

echo -e "${GREEN}Generating the lexer:${RESET}"
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; }

echo -e "${GREEN}Generating the parser:${RESET}"
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; }

echo -e "${GREEN}Moving the generated files:${RESET}"
mv -v "$SCRIPT_DIR"/src/main/antlr4/*.java "$SCRIPT_DIR"/src/main/java/viper/gobra/frontend/