Skip to content

Commit

Permalink
Add script to generate parser (#498)
Browse files Browse the repository at this point in the history
* Add script to automate generation of the parser

* Add license

* Improve error messages
  • Loading branch information
jcp19 authored Aug 5, 2022
1 parent 7294137 commit 36109bd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
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/

0 comments on commit 36109bd

Please sign in to comment.